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
318
319
320
|
//+-------------------------------------------------------------------
//
// File: stream.cxx
//
// Contents: Stream interface on flat File.
//
// Classes: CStreamOnFile
//
// Macros: DEFINE_INTERFACE_XMIT_ROUTINES
//
// History: 08-08-95 Rickhi Created
//
//--------------------------------------------------------------------
#include <pch.cxx>
#pragma hdrstop
#include <stream.hxx>
CStreamOnFile::CStreamOnFile(const TCHAR *pwszFileName, SCODE &sc, BOOL fRead) :
_clRefs(1),
_hFile(NULL),
_lOffset(0),
_cSize(0),
_cbData(0),
_fRead(fRead)
{
_pbData = new BYTE[2048]; // should be big enough
if (!_pbData)
{
sc = E_OUTOFMEMORY;
return;
}
_cbData = 2048;
// open the file.
DWORD fdwCreate;
if (_fRead)
fdwCreate = OPEN_EXISTING; // open existing, fail if not found.
else
fdwCreate = CREATE_ALWAYS; // overwrite existing if present
_hFile = CreateFile(pwszFileName,
GENERIC_READ | GENERIC_WRITE, // fdwAccess
FILE_SHARE_READ | FILE_SHARE_WRITE, // fdwShareMode
NULL, // lpsaSecurity
fdwCreate, // creation options
FILE_ATTRIBUTE_NORMAL, // attributes & flags
NULL // hTemplateFile
);
if (_hFile == INVALID_HANDLE_VALUE)
{
sc = HRESULT_FROM_WIN32(GetLastError());
return;
}
// read the file into the memory block
DWORD cbRead = 0;
if (_fRead && ! ReadFile(_hFile,
_pbData,
_cbData,
&cbRead,
NULL))
{
sc = HRESULT_FROM_WIN32(GetLastError());
return;
}
if (_fRead)
{
_cSize = _cbData;
}
sc = S_OK;
}
CStreamOnFile::~CStreamOnFile(void)
{
if (_hFile)
{
if (!_fRead)
{
// write the data to the file
DWORD cbWritten = 0;
if (!WriteFile(_hFile,
_pbData,
_cbData,
&cbWritten,
NULL))
{
SCODE sc = HRESULT_FROM_WIN32(GetLastError());
}
}
CloseHandle(_hFile);
}
}
STDMETHODIMP CStreamOnFile::QueryInterface(
REFIID iidInterface,
void FAR* FAR* ppvObj)
{
HRESULT hresult = S_OK;
// We only support IUnknown and IStream
if (IsEqualIID(iidInterface, IID_IUnknown) ||
IsEqualIID(iidInterface, IID_IStream))
{
*ppvObj = this;
AddRef();
}
else
{
*ppvObj = NULL;
hresult = E_NOINTERFACE;
}
return hresult;
}
STDMETHODIMP_(ULONG) CStreamOnFile::AddRef(void)
{
InterlockedIncrement(&_clRefs);
return _clRefs;
}
STDMETHODIMP_(ULONG) CStreamOnFile::Release(void)
{
if (InterlockedDecrement(&_clRefs) == 0)
{
delete this;
return 0;
}
return _clRefs;
}
STDMETHODIMP CStreamOnFile::Read(
VOID HUGEP* pv,
ULONG cb,
ULONG FAR* pcbRead)
{
HRESULT hresult = S_OK;
if (pcbRead)
{
*pcbRead = 0L;
}
if (cb + _lOffset > _cSize)
{
cb = _cSize - _lOffset;
hresult = STG_E_READFAULT;
}
memcpy(pv, _pbData + _lOffset, (size_t) cb);
_lOffset += cb;
if (pcbRead != NULL)
{
*pcbRead = cb;
}
return hresult;
}
STDMETHODIMP CStreamOnFile::Write(
VOID const HUGEP* pv,
ULONG cbToWrite,
ULONG FAR* pcbWritten)
{
HRESULT hresult = S_OK;
if (pcbWritten)
{
*pcbWritten = 0L;
}
if (cbToWrite + _lOffset > _cbData)
{
return E_OUTOFMEMORY;
}
// copy in the new data
memcpy(_pbData + _lOffset, pv, (size_t) cbToWrite);
_lOffset += cbToWrite;
if (pcbWritten != NULL)
{
*pcbWritten = cbToWrite;
}
// We assume maxium size of buffer is the size to send on the network.
if (_cSize < _lOffset)
{
_cSize = _lOffset;
}
return hresult;
}
STDMETHODIMP CStreamOnFile::Seek(
LARGE_INTEGER dlibMoveIN,
DWORD dwOrigin,
ULARGE_INTEGER FAR* plibNewPosition)
{
HRESULT hresult = S_OK;
LONG dlibMove = dlibMoveIN.LowPart;
ULONG cbNewPos = dlibMove;
switch(dwOrigin)
{
case STREAM_SEEK_SET:
if (dlibMove >= 0)
{
_lOffset = dlibMove;
}
else
{
hresult = STG_E_SEEKERROR;
}
break;
case STREAM_SEEK_CUR:
if (!(dlibMove < 0 && ((ULONG) -dlibMove > _lOffset)))
{
_lOffset += (ULONG) dlibMove;
}
else
{
hresult = STG_E_SEEKERROR;
}
break;
case STREAM_SEEK_END:
if (!(dlibMove < 0 && ((ULONG) -dlibMove) > _cbData))
{
_lOffset = _cbData + dlibMove;
}
else
{
hresult = STG_E_SEEKERROR;
}
break;
default:
hresult = STG_E_SEEKERROR;
}
if (plibNewPosition != NULL)
{
ULISet32(*plibNewPosition, _lOffset);
}
return hresult;
}
STDMETHODIMP CStreamOnFile::SetSize(ULARGE_INTEGER cb)
{
return E_NOTIMPL;
}
STDMETHODIMP CStreamOnFile::CopyTo(
IStream FAR* pstm,
ULARGE_INTEGER cb,
ULARGE_INTEGER FAR* pcbRead,
ULARGE_INTEGER FAR* pcbWritten)
{
return E_NOTIMPL;
}
STDMETHODIMP CStreamOnFile::Commit(DWORD grfCommitFlags)
{
return NOERROR;
}
STDMETHODIMP CStreamOnFile::Revert(void)
{
return NOERROR;
}
STDMETHODIMP CStreamOnFile::LockRegion(
ULARGE_INTEGER libOffset,
ULARGE_INTEGER cb,
DWORD dwLockType)
{
return STG_E_INVALIDFUNCTION;
}
STDMETHODIMP CStreamOnFile::UnlockRegion(
ULARGE_INTEGER libOffset,
ULARGE_INTEGER cb,
DWORD dwLockType)
{
return STG_E_INVALIDFUNCTION;
}
STDMETHODIMP CStreamOnFile::Stat(
STATSTG FAR* pstatstg,
DWORD statflag)
{
memset(pstatstg, 0, sizeof(STATSTG));
return E_NOTIMPL;
}
STDMETHODIMP CStreamOnFile::Clone(IStream FAR * FAR *ppstm)
{
return E_NOTIMPL;
}
|