summaryrefslogtreecommitdiff
path: root/src/palrt/memorystream.cpp
blob: 61b0bb0180d20e7fdac6358d6c611409e633ec64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//

//
// ===========================================================================
// File: memorystream.cpp
// 
// ===========================================================================
/*++

Abstract:

    in memory stream


 

Revision History:

--*/

#include "common.h"

#include "objidl.h"

class MemoryStream : public IStream
{
    LONG m_cRef; // QI refcount
    ULONG m_nPos; // the current position in the stream
    ULONG m_nSize; // the current size of the stream
    ULONG m_nData; // the size of the allocated data storage, can be < m_nSize
    BYTE* m_pData; // the data storage

private:
    HRESULT Ensure(ULONG nNewData)
    {
        if (nNewData > m_nData)
        {
            // apply some heurestic for growing
            ULONG n = m_nData;

            // grow 2x for smaller sizes, 1.25x for bigger sizes
            n = min(2 * n, n + n / 4 + 0x100000);

            // don't allocate tiny chunks
            n = max(n, 0x100);

            // compare with the hard limit
            nNewData = max(n, nNewData);
        }
        else
        if (nNewData > m_nData / 4)
        {
            // shrinking but it is not worth it
            return S_OK;
        }

        BYTE * pNewData = (BYTE*)realloc(m_pData, nNewData);
        if (pNewData == NULL && nNewData != 0)
            return E_OUTOFMEMORY;

        m_nData = nNewData;
        m_pData = pNewData;
        return S_OK;
    }

public:
    MemoryStream()
    {
        m_cRef = 1;
        m_nPos = 0;
        m_nSize = 0;
        m_nData = 0;
        m_pData = NULL;
    }

#ifdef __GNUC__
    virtual
#endif
	~MemoryStream()
    {
        free(m_pData);
    }

    HRESULT STDMETHODCALLTYPE QueryInterface( 
        REFIID riid,
        void **ppvObject)
    {
        if (riid == IID_IStream || 
            riid == IID_ISequentialStream ||
            riid == IID_IUnknown)
        {
            InterlockedIncrement(&m_cRef);
            *ppvObject = this;
            return S_OK;
        }
        else
        {
            *ppvObject = NULL;
            return E_NOINTERFACE;
        }
    }
        
    ULONG STDMETHODCALLTYPE AddRef()
    {
        return InterlockedIncrement(&m_cRef);
    }
        
    ULONG STDMETHODCALLTYPE Release()
    {
        LONG cRef = InterlockedDecrement(&m_cRef);
        if (cRef == 0)
            delete this;
        return cRef;
    }

    HRESULT STDMETHODCALLTYPE Read(
        void *pv,
        ULONG cb,
        ULONG *pcbRead)
    {
        ULONG nData;
        ULONG nNewPos = m_nPos + cb;

        // check for overflow
        if (nNewPos < cb)
            return STG_E_INVALIDFUNCTION;

        // compare with the actual size
        nNewPos = min(nNewPos, m_nSize);

        // compare with the data available
        nData = min(nNewPos, m_nData);

        // copy the data over
        if (nData > m_nPos)
            memcpy(pv, m_pData + m_nPos, nData - m_nPos);

        // fill the rest with zeros
        if (nNewPos > nData)
            memset((BYTE*)pv + (nData - m_nPos), 0, nNewPos - nData);

        cb = nNewPos - m_nPos;
        m_nPos = nNewPos;

        if (pcbRead)
            *pcbRead = cb;

        return S_OK;
    }

    HRESULT STDMETHODCALLTYPE Write(
        const void *pv,
        ULONG cb,
        ULONG *pcbWritten)
    {
        ULONG nNewPos = m_nPos + cb;

        // check for overflow
        if (nNewPos < cb)
            return STG_E_INVALIDFUNCTION;

        // ensure the space
        if (nNewPos > m_nData)
        {
            HRESULT hr = Ensure(nNewPos);
            if (FAILED(hr)) return hr;
        }

        // copy the data over
        memcpy(m_pData + m_nPos, pv, cb);

        m_nPos = nNewPos;
        if (m_nPos > m_nSize)
            m_nSize = m_nPos;

        if (pcbWritten)
            *pcbWritten = cb;

        return S_OK;
    }

    HRESULT STDMETHODCALLTYPE Seek(
        LARGE_INTEGER dlibMove,
        DWORD dwOrigin,
        ULARGE_INTEGER *plibNewPosition)
    {       
        ULONG           lStartPos;
        LONGLONG        lNewPos;

        switch (dwOrigin)
        {
        case STREAM_SEEK_SET:
            lStartPos = 0;
            break;
        case STREAM_SEEK_CUR:
            lStartPos = m_nPos;
            break;
        case STREAM_SEEK_END:
            lStartPos = m_nSize;
            break;
        default:
            return STG_E_INVALIDFUNCTION;
        }

        lNewPos = lStartPos + dlibMove.QuadPart;

        // it is an error to seek before the beginning of the stream
        if (lNewPos < 0)
            return STG_E_INVALIDFUNCTION;

        // It is not, however, an error to seek past the end of the stream
        if (lNewPos > m_nSize)
        {
            ULARGE_INTEGER NewSize;
            NewSize.QuadPart = lNewPos;

            HRESULT hr = SetSize(NewSize);
            if (FAILED(hr)) return hr;
        }

        m_nPos = (ULONG)lNewPos;

        if (plibNewPosition != NULL)
            plibNewPosition->QuadPart = m_nPos;

        return S_OK;
    }

    HRESULT STDMETHODCALLTYPE SetSize(
        ULARGE_INTEGER libNewSize)
    {
        if (libNewSize.u.HighPart != 0)
            return STG_E_INVALIDFUNCTION;

        m_nSize = libNewSize.u.LowPart;

        // free the space if we are shrinking
        if (m_nSize < m_nData)
            Ensure(m_nSize);

        return S_OK;
    }

    HRESULT STDMETHODCALLTYPE CopyTo(
        IStream *pstm,
        ULARGE_INTEGER cb,
        ULARGE_INTEGER *pcbRead,
        ULARGE_INTEGER *pcbWritten)
    {
        _ASSERTE(false);
        return E_NOTIMPL;
    }

    HRESULT STDMETHODCALLTYPE Commit(
        DWORD grfCommitFlags)
    {
        _ASSERTE(false);
        return E_NOTIMPL;
    }

    HRESULT STDMETHODCALLTYPE Revert()
    {
        _ASSERTE(false);
        return E_NOTIMPL;
    }

    HRESULT STDMETHODCALLTYPE LockRegion(
        ULARGE_INTEGER libOffset,
        ULARGE_INTEGER cb,
        DWORD dwLockType)
    {
        _ASSERTE(false);
        return E_NOTIMPL;
    }

    HRESULT STDMETHODCALLTYPE UnlockRegion(
        ULARGE_INTEGER libOffset,
        ULARGE_INTEGER cb,
        DWORD dwLockType)
    {
        _ASSERTE(false);
        return E_NOTIMPL;
    }

    HRESULT STDMETHODCALLTYPE Stat(
        STATSTG *pstatstg,
        DWORD grfStatFlag)
    {
        memset(pstatstg, 0, sizeof(STATSTG));
        pstatstg->cbSize.QuadPart = m_nSize;
        return S_OK;
    }

    HRESULT STDMETHODCALLTYPE Clone(
        IStream **ppstm)
    {
        _ASSERTE(false);
        return E_NOTIMPL;
    }
};

STDAPI CreateStreamOnHGlobal(PVOID hGlobal, BOOL fDeleteOnRelease, IStream** ppstm)
{
    MemoryStream* pStream;

    if (hGlobal != NULL) return E_NOTIMPL;
    _ASSERTE(fDeleteOnRelease == TRUE);

    pStream = new MemoryStream;
    if (pStream == NULL) return E_OUTOFMEMORY;

    *ppstm = pStream;
    return S_OK;
}