-
Notifications
You must be signed in to change notification settings - Fork 149
/
StreamEx.cpp
566 lines (451 loc) · 10.7 KB
/
StreamEx.cpp
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#pragma once
#include "StreamEx.h"
#include "ComHelper.h"
#include "km.h"
#pragma warning(push)
#pragma warning(disable: 28251)
//内存流
class CMemStream : public IUnknownT<CMemStream, IReadStream>
{
public:
UINT64 Size;
const BYTE* pData;
UINT64 Point;
CMemStream(const void* _pData, UINT64 _Size)
: pData((byte*)_pData)
, Size(_Size)
, Point(0)
{
}
virtual /* [local] */ HRESULT STDMETHODCALLTYPE Read(
/* [annotation] */
_Out_writes_bytes_to_(cb, *pcbRead) void *pv,
/* [annotation][in] */
_In_ ULONG cb,
/* [annotation] */
_Out_opt_ ULONG *pcbRead,
UINT64* pByteOffset)
{
auto TempPoint = pByteOffset ? *pByteOffset : Point;
if(TempPoint>= Size)
return __HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
ULONG cbRead = min(cb, Size - TempPoint);
memcpy(pv, pData + TempPoint, cbRead);
if(!pByteOffset)
Point += cbRead;
if (pcbRead)
*pcbRead = cbRead;
return cbRead == cb ? S_OK : S_FALSE;
}
virtual /* [local] */ HRESULT STDMETHODCALLTYPE Seek(
/* [in] */ INT64 dlibMove,
/* [in] */ DWORD dwOrigin,
/* [annotation] */
_Out_opt_ UINT64 *plibNewPosition)
{
switch (dwOrigin)
{
case FILE_CURRENT:
dlibMove += Point;
break;
case FILE_BEGIN:
break;
case FILE_END:
dlibMove += Size;
break;
default:
return __HRESULT_FROM_WIN32(87);
break;
}
if(dlibMove<0)
return __HRESULT_FROM_WIN32(87);
if(dlibMove>Size)
return __HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
Point = dlibMove;
if (plibNewPosition)
*plibNewPosition = Point;
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE get_Size(_Out_ UINT64* pSize)
{
*pSize = Size;
return S_OK;
}
};
IReadStream * StreamCreate(const void * _pData, UINT64 _Size)
{
return (IReadStream*)new CMemStream(_pData, _Size);
}
//局部流
class CPartStream : public IUnknownT<CPartStream, IReadStream>
{
public:
UINT64 Size;
UINT64 Start;
IReadStream* pStream;
UINT64 Point;
CPartStream(IReadStream* _pStream, UINT64 _Start, UINT64 _Size)
: pStream(_pStream)
, Start(_Start)
, Size(_Size)
, Point(0)
{
pStream->AddRef();
}
~CPartStream()
{
pStream->Release();
}
virtual /* [local] */ HRESULT STDMETHODCALLTYPE Read(
/* [annotation] */
_Out_writes_bytes_to_(cb, *pcbRead) void *pv,
/* [annotation][in] */
_In_ ULONG cb,
/* [annotation] */
_Out_opt_ ULONG *pcbRead,
_In_ UINT64* pByteOffset)
{
UINT64 _Start = pByteOffset ? *pByteOffset : Point;
if (_Start >= Size)
return __HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
ULONG cbRead = min(cb, Size - _Start);
_Start += Start;
auto hr = pStream->Read(pv, cbRead, &cbRead, &_Start);
if (FAILED(hr))
return hr;
//更新文件指针
if (!pByteOffset)
Point += cbRead;
if (pcbRead)
*pcbRead = cbRead;
return cbRead == cb ? S_OK : S_FALSE;
}
virtual HRESULT STDMETHODCALLTYPE get_Size(_Out_ UINT64* pSize)
{
*pSize = Size;
return S_OK;
}
virtual /* [local] */ HRESULT STDMETHODCALLTYPE Seek(
/* [in] */ INT64 dlibMove,
/* [in] */ DWORD dwOrigin,
/* [annotation] */
_Out_opt_ UINT64 *plibNewPosition)
{
switch (dwOrigin)
{
case FILE_CURRENT:
dlibMove += Point;
break;
case FILE_BEGIN:
break;
case FILE_END:
dlibMove += Size;
break;
default:
return __HRESULT_FROM_WIN32(87);
break;
}
if (dlibMove<0)
return __HRESULT_FROM_WIN32(87);
if (dlibMove>Size)
return __HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
Point = dlibMove;
if (plibNewPosition)
{
*plibNewPosition = Point;
}
return S_OK;
}
};
IReadStream* StreamCreate(IReadStream* pStream, UINT64 Start, UINT64 Size)
{
if (pStream == NULL || Size == 0)
{
SetLastError(87);
return NULL;
}
UINT64 TSize;
auto hr = pStream->get_Size(&TSize);
if (hr!=S_OK)
{
SetLastError(hr);
return NULL;
}
//参数错误
if (TSize < Start + Size)
{
SetLastError(87);
return NULL;
}
return (IReadStream*)new CPartStream(pStream, Start, Size);
}
//文件流
class CFileStream : public IUnknownT<CFileStream, IReadWriteStream>
{
public:
HANDLE hFile;
CFileStream(HANDLE _hFile)
:hFile(_hFile)
{
}
~CFileStream()
{
CloseHandle(hFile);
}
virtual /* [local] */ HRESULT STDMETHODCALLTYPE Read(
/* [annotation] */
_Out_writes_bytes_to_(cb, *pcbRead) void *pv,
/* [annotation][in] */
_In_ ULONG cb,
/* [annotation] */
_Out_opt_ ULONG *pcbRead,
_Inout_opt_ UINT64* pByteOffset)
{
IO_STATUS_BLOCK IoStatusBlock;
auto hr=NtReadFile(hFile, NULL, NULL, NULL, &IoStatusBlock, pv, cb,(PLARGE_INTEGER)pByteOffset, NULL);
if (hr)
{
return HRESULT_FROM_WIN32(RtlNtStatusToDosError(hr));
}
if (pcbRead)
{
*pcbRead = IoStatusBlock.Information;
}
return cb == IoStatusBlock.Information ? S_OK : S_FALSE;
}
virtual /* [local] */ HRESULT STDMETHODCALLTYPE Seek(
/* [in] */ INT64 dlibMove,
/* [in] */ DWORD dwOrigin,
/* [annotation] */
_Out_opt_ UINT64 *plibNewPosition)
{
if (SetFilePointerEx(hFile, *(LARGE_INTEGER*)&dlibMove, (PLARGE_INTEGER)plibNewPosition, dwOrigin))
{
return S_OK;
}
else
{
return HresultFromBool();
}
}
virtual HRESULT STDMETHODCALLTYPE get_Size(_Out_ UINT64* pSize)
{
return GetFileSizeEx(hFile, (PLARGE_INTEGER)pSize) ? S_OK : HRESULT_FROM_WIN32(GetLastError());
}
virtual /* [local] */ HRESULT STDMETHODCALLTYPE Write(
/* [annotation] */
_In_reads_bytes_(cb) const void *pv,
/* [annotation][in] */
_In_ ULONG cb,
/* [annotation] */
_Out_opt_ ULONG *pcbWritten,
_Inout_opt_ UINT64* pByteOffset)
{
IO_STATUS_BLOCK IoStatusBlock;
auto Status =NtWriteFile(hFile, NULL, NULL, NULL, &IoStatusBlock, (void*)pv, cb, (PLARGE_INTEGER)pByteOffset, NULL);
if (Status<0)
{
return HRESULT_FROM_WIN32(RtlNtStatusToDosError(Status));
}
if (pcbWritten)
{
*pcbWritten = IoStatusBlock.Information;
}
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE put_Size(_In_ UINT64 Size)
{
IO_STATUS_BLOCK IoStatusBlock;
auto Status = NtSetInformationFile(hFile, &IoStatusBlock, &Size, sizeof(FILE_END_OF_FILE_INFORMATION), FileEndOfFileInformation);
if (Status < 0)
return HRESULT_FROM_WIN32(RtlNtStatusToDosError(Status));
else
return S_OK;
}
};
IReadWriteStream* StreamCreate(LPCWSTR FilePath,
DWORD dwDesiredAccess,
DWORD dwShareMode,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes )
{
auto hFile = CreateFile(FilePath, dwDesiredAccess, dwShareMode,NULL, dwCreationDisposition, dwFlagsAndAttributes| FILE_FLAG_BACKUP_SEMANTICS,0);
if (hFile == INVALID_HANDLE_VALUE)
return NULL;
return (IReadWriteStream*)new CFileStream(hFile);
}
struct VirtualWriteBuffer
{
BYTE Data[1024 * 1024];
};
class CVirtualWriteStream : public IUnknownT<CVirtualWriteStream, IReadWriteStream>
{
//HANDLE hFile;
std::map<UINT64, CStringA> Ceche;
UINT64 Size;
IReadStream* pStream;
UINT64 Point;
public:
CVirtualWriteStream(IReadStream* _pStream)
:pStream(_pStream)
, Point(0)
{
pStream->AddRef();
pStream->get_Size(&Size);
}
~CVirtualWriteStream()
{
pStream->Release();
}
virtual /* [local] */ HRESULT STDMETHODCALLTYPE Read(
/* [annotation] */
_Out_writes_bytes_to_(cb, *pcbRead) void *pv,
/* [annotation][in] */
_In_ ULONG cb,
/* [annotation] */
_Out_opt_ ULONG *pcbRead,
_In_opt_ UINT64* pByteOffset)
{
auto Offset = pByteOffset ? *pByteOffset : Point;
if (Offset >= Size)
return __HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
ULONG cbRead = min(cb, Size - Offset);
auto hr=pStream->Read(pv, cbRead, &cbRead,&Offset);
if (FAILED(hr))
return hr;
if (!pByteOffset)
{
Point += cbRead;
}
if (pcbRead)
*pcbRead = cbRead;
auto End = Offset + cbRead;
for (auto& Item : Ceche)
{
if (End <= Item.first)
break;
if (Offset >= Item.first + Item.second.GetLength())
continue;
INT64 NewOffect = Offset - Item.first;
if (NewOffect <= 0)
{
memcpy((byte*)pv - NewOffect, Item.second.GetString(), min(Item.second.GetLength(), cbRead + NewOffect));
}
else
{
memcpy(pv, Item.second.GetString() + NewOffect, min(Item.second.GetLength() - NewOffect, cbRead));
}
}
return S_OK;
}
//virtual /* [local] */ HRESULT STDMETHODCALLTYPE Read(
// /* [annotation] */
// _Out_writes_bytes_to_(cb, *pcbRead) void *pBuffer,
// /* [annotation][in] */
// _In_ ULONG nNumberOfBytesToRead,
// /* [annotation] */
// _Out_opt_ ULONG *pNumberOfBytesRead)
//{
// LARGE_INTEGER Offset = *(LARGE_INTEGER*)&Overlapped.Offset;
// //if (!SetFilePointerEx(hFile, LARGE_INTEGER{}, &Offset, FILE_CURRENT))
// //{
// // goto Error;
// //}
// auto Ret = ReadFile(hFile, pBuffer, nNumberOfBytesToRead, pNumberOfBytesRead, &Overlapped);
// Overlapped = OVERLAPPED{};
// *(LARGE_INTEGER*)&Overlapped.Offset = Offset;
// if (!Ret)
// goto Error;
// DWORD NumberOfBytesRead = *pNumberOfBytesRead;
// auto End = Offset.QuadPart + NumberOfBytesRead;
// for (auto& Item : Ceche)
// {
// if (End <= Item.first)
// break;
// if (Offset.QuadPart >= Item.first + Item.second.GetLength())
// continue;
// INT64 NewOffect = Offset.QuadPart - Item.first;
// if (NewOffect <= 0)
// {
// memcpy((byte*)pBuffer - NewOffect, Item.second.GetString(), min(Item.second.GetLength(), NumberOfBytesRead + NewOffect));
// }
// else
// {
// memcpy(pBuffer, Item.second.GetString() + NewOffect, min(Item.second.GetLength() - NewOffect, NumberOfBytesRead));
// }
// }
// return S_OK;
//Error:
// return HRESULT_FROM_WIN32(GetLastError());
//}
virtual /* [local] */ HRESULT STDMETHODCALLTYPE Write(
/* [annotation] */
_In_reads_bytes_(cb) const void *pv,
/* [annotation][in] */
_In_ ULONG cb,
/* [annotation] */
_Out_opt_ ULONG *pcbWritten,
_In_opt_ UINT64* pByteOffset)
{
auto TempPoint = pByteOffset ? *pByteOffset : Point;
Ceche[TempPoint].SetString((char*)pv, cb);
if (pcbWritten)
*pcbWritten = cb;
if (!pByteOffset)
Point += cb;
return S_OK;
}
virtual /* [local] */ HRESULT STDMETHODCALLTYPE Seek(
/* [in] */ INT64 dlibMove,
/* [in] */ DWORD dwOrigin,
/* [annotation] */
_Out_opt_ UINT64 *plibNewPosition)
{
switch (dwOrigin)
{
case FILE_BEGIN:
//*(UINT64*)&Overlapped.Offset = dlibMove.QuadPart;
break;
case FILE_END:
dlibMove += Size;
break;
case FILE_CURRENT:
dlibMove = Point + dlibMove;
break;
default:
return E_NOTIMPL;
break;
}
if (dlibMove <= Size)
{
Point = dlibMove;
if (plibNewPosition)
*plibNewPosition = Point;
return S_OK;
}
else
{
return E_NOTIMPL;
}
}
virtual HRESULT STDMETHODCALLTYPE put_Size(_In_ UINT64 NewSize)
{
if (NewSize <= Size)
{
Size = NewSize;
return S_OK;
}
else
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE get_Size(_Out_ UINT64* pSize)
{
*pSize = Size;
return S_OK;
}
};
IReadWriteStream* StreamCreateVirtualWrite(IReadStream* pStream)
{
return new CVirtualWriteStream(pStream);
}
#pragma warning(pop)