]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/mstream.cpp
fixing overrelease and out-of-bounds write, fixes #13725
[wxWidgets.git] / src / common / mstream.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/common/mstream.cpp
3// Purpose: "Memory stream" classes
4// Author: Guilhem Lavaux
5// Modified by: VZ (23.11.00): general code review
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Guilhem Lavaux
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27#if wxUSE_STREAMS
28
29#include "wx/mstream.h"
30
31#ifndef WX_PRECOMP
32 #include "wx/stream.h"
33#endif //WX_PRECOMP
34
35#include <stdlib.h>
36
37// ============================================================================
38// implementation
39// ============================================================================
40
41// ----------------------------------------------------------------------------
42// wxMemoryInputStream
43// ----------------------------------------------------------------------------
44
45IMPLEMENT_ABSTRACT_CLASS(wxMemoryInputStream, wxInputStream)
46
47wxMemoryInputStream::wxMemoryInputStream(const void *data, size_t len)
48{
49 m_i_streambuf = new wxStreamBuffer(wxStreamBuffer::read);
50 m_i_streambuf->SetBufferIO(const_cast<void *>(data), len);
51 m_i_streambuf->SetIntPosition(0); // seek to start pos
52 m_i_streambuf->Fixed(true);
53
54 m_length = len;
55}
56
57wxMemoryInputStream::wxMemoryInputStream(const wxMemoryOutputStream& stream)
58{
59 const wxFileOffset lenFile = stream.GetLength();
60 if ( lenFile == wxInvalidOffset )
61 {
62 m_i_streambuf = NULL;
63 m_lasterror = wxSTREAM_EOF;
64 return;
65 }
66
67 const size_t len = wx_truncate_cast(size_t, lenFile);
68 wxASSERT_MSG( len == lenFile + size_t(0), wxT("huge files not supported") );
69
70 m_i_streambuf = new wxStreamBuffer(wxStreamBuffer::read);
71 m_i_streambuf->SetBufferIO(len); // create buffer
72 stream.CopyTo(m_i_streambuf->GetBufferStart(), len);
73 m_i_streambuf->SetIntPosition(0); // seek to start pos
74 m_i_streambuf->Fixed(true);
75 m_length = len;
76}
77
78void
79wxMemoryInputStream::InitFromStream(wxInputStream& stream, wxFileOffset lenFile)
80{
81 if ( lenFile == wxInvalidOffset )
82 lenFile = stream.GetLength();
83
84 if ( lenFile == wxInvalidOffset )
85 {
86 m_i_streambuf = NULL;
87 m_lasterror = wxSTREAM_EOF;
88 return;
89 }
90
91 const size_t len = wx_truncate_cast(size_t, lenFile);
92 wxASSERT_MSG( (wxFileOffset)len == lenFile, wxT("huge files not supported") );
93
94 m_i_streambuf = new wxStreamBuffer(wxStreamBuffer::read);
95 m_i_streambuf->SetBufferIO(len); // create buffer
96 stream.Read(m_i_streambuf->GetBufferStart(), len);
97 m_i_streambuf->SetIntPosition(0); // seek to start pos
98 m_i_streambuf->Fixed(true);
99 m_length = stream.LastRead();
100}
101
102bool wxMemoryInputStream::CanRead() const
103{
104 return m_i_streambuf->GetIntPosition() != m_length;
105}
106
107wxMemoryInputStream::~wxMemoryInputStream()
108{
109 delete m_i_streambuf;
110}
111
112char wxMemoryInputStream::Peek()
113{
114 char *buf = (char *)m_i_streambuf->GetBufferStart();
115 size_t pos = m_i_streambuf->GetIntPosition();
116 if ( pos == m_length )
117 {
118 m_lasterror = wxSTREAM_READ_ERROR;
119
120 return 0;
121 }
122
123 return buf[pos];
124}
125
126size_t wxMemoryInputStream::OnSysRead(void *buffer, size_t nbytes)
127{
128 size_t pos = m_i_streambuf->GetIntPosition();
129 if ( pos == m_length )
130 {
131 m_lasterror = wxSTREAM_EOF;
132
133 return 0;
134 }
135
136 m_i_streambuf->Read(buffer, nbytes);
137 m_lasterror = wxSTREAM_NO_ERROR;
138
139 return m_i_streambuf->GetIntPosition() - pos;
140}
141
142wxFileOffset wxMemoryInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
143{
144 return m_i_streambuf->Seek(pos, mode);
145}
146
147wxFileOffset wxMemoryInputStream::OnSysTell() const
148{
149 return m_i_streambuf->Tell();
150}
151
152// ----------------------------------------------------------------------------
153// wxMemoryOutputStream
154// ----------------------------------------------------------------------------
155
156IMPLEMENT_DYNAMIC_CLASS(wxMemoryOutputStream, wxOutputStream)
157
158wxMemoryOutputStream::wxMemoryOutputStream(void *data, size_t len)
159{
160 m_o_streambuf = new wxStreamBuffer(wxStreamBuffer::write);
161 if ( data )
162 m_o_streambuf->SetBufferIO(data, len);
163 m_o_streambuf->Fixed(false);
164 m_o_streambuf->Flushable(false);
165}
166
167wxMemoryOutputStream::~wxMemoryOutputStream()
168{
169 delete m_o_streambuf;
170}
171
172size_t wxMemoryOutputStream::OnSysWrite(const void *buffer, size_t nbytes)
173{
174 size_t oldpos = m_o_streambuf->GetIntPosition();
175 m_o_streambuf->Write(buffer, nbytes);
176 size_t newpos = m_o_streambuf->GetIntPosition();
177
178 // FIXME can someone please explain what this does? (VZ)
179 if ( !newpos )
180 newpos = m_o_streambuf->GetBufferSize();
181
182 return newpos - oldpos;
183}
184
185wxFileOffset wxMemoryOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
186{
187 return m_o_streambuf->Seek(pos, mode);
188}
189
190wxFileOffset wxMemoryOutputStream::OnSysTell() const
191{
192 return m_o_streambuf->Tell();
193}
194
195size_t wxMemoryOutputStream::CopyTo(void *buffer, size_t len) const
196{
197 wxCHECK_MSG( buffer, 0, wxT("must have buffer to CopyTo") );
198
199 if ( len > GetSize() )
200 len = GetSize();
201
202 memcpy(buffer, m_o_streambuf->GetBufferStart(), len);
203
204 return len;
205}
206
207#endif // wxUSE_STREAMS