File size: 5,522 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
Imports System.Runtime.InteropServices.ComTypes
Imports System.IO
Imports System.Runtime.InteropServices

<System.Serializable()> Public NotInheritable Class ComIStreamWrapper

    Implements IStream

    Private Const STG_E_INVALIDFUNCTION As Integer = &H80030001

    Public ReadOnly baseStream As Stream
    Private position As Long = -1

    Sub New(ByVal stream As Stream)
        baseStream = stream
    End Sub

    Private Sub SetSizeToPosition()
        If position <> -1 Then
            If position > baseStream.Length Then
                baseStream.SetLength(position)
            End If
            baseStream.Position = position
            position = -1
        End If
    End Sub

    Public Sub Read(ByVal pv As Byte(), ByVal cb As Integer, ByVal pcbRead As IntPtr) Implements IStream.Read
        Dim read__1 As Integer = 0

        If cb <> 0 Then
            SetSizeToPosition()
            read__1 = baseStream.Read(pv, 0, cb)
        End If

        If pcbRead <> IntPtr.Zero Then
            Marshal.WriteInt32(pcbRead, read__1)
        End If
    End Sub

    Public Sub Write(ByVal pv As Byte(), ByVal cb As Integer, ByVal pcbWritten As IntPtr) Implements IStream.Write
        If cb <> 0 Then
            SetSizeToPosition()
            baseStream.Write(pv, 0, cb)
        End If

        If pcbWritten <> IntPtr.Zero Then
            Marshal.WriteInt32(pcbWritten, cb)
        End If
    End Sub

    Public Sub Seek(ByVal dlibMove As Long, ByVal dwOrigin As Integer, ByVal plibNewPosition As IntPtr) Implements IStream.Seek
        Dim length As Long = baseStream.Length
        Dim newPosition As Long

        Select Case CType(dwOrigin, SeekOrigin)
            Case SeekOrigin.Begin
                newPosition = dlibMove
                Exit Select
            Case SeekOrigin.Current
                If position = -1 Then
                    newPosition = baseStream.Position + dlibMove
                Else
                    newPosition = position + dlibMove
                End If
                Exit Select
            Case SeekOrigin.[End]
                newPosition = length + dlibMove
                Exit Select
            Case Else
                Throw New ExternalException(Nothing, STG_E_INVALIDFUNCTION)
        End Select

        If newPosition > length Then
            position = newPosition
        Else
            baseStream.Position = newPosition
            position = -1
        End If

        If plibNewPosition <> IntPtr.Zero Then
            Marshal.WriteInt64(plibNewPosition, newPosition)
        End If
    End Sub

    Public Sub SetSize(ByVal libNewSize As Long) Implements IStream.SetSize
        baseStream.SetLength(libNewSize)
    End Sub

    Public Sub CopyTo(ByVal pstm As IStream, ByVal cb As Long, ByVal pcbRead As IntPtr, ByVal pcbWritten As IntPtr) Implements IStream.CopyTo
        Dim buffer As Byte()
        Dim written As Long = 0
        Dim read As Integer
        Dim count As Integer

        If cb <> 0 Then
            If cb < 4096 Then
                count = Convert.ToInt32(cb)
            Else
                count = 4096
            End If
            buffer = New Byte(count - 1) {}
            SetSizeToPosition()
            While True
                If (InlineAssignHelper(read, baseStream.Read(buffer, 0, count))) = 0 Then
                    Exit While
                End If
                pstm.Write(buffer, read, IntPtr.Zero)
                written += read
                If written >= cb Then
                    Exit While
                End If
                If cb - written < 4096 Then
                    count = Convert.ToInt32(cb - written)
                End If
            End While
        End If

        If pcbRead <> IntPtr.Zero Then
            Marshal.WriteInt64(pcbRead, written)
        End If
        If pcbWritten <> IntPtr.Zero Then
            Marshal.WriteInt64(pcbWritten, written)
        End If
    End Sub

    Public Sub Commit(ByVal grfCommitFlags As Integer) Implements IStream.Commit
        baseStream.Flush()
        SetSizeToPosition()
    End Sub

    Public Sub Revert() Implements IStream.Revert
        Throw New Runtime.InteropServices.ExternalException(Nothing, STG_E_INVALIDFUNCTION)
    End Sub

    Public Sub LockRegion(ByVal libOffset As Long, ByVal cb As Long, ByVal dwLockType As Integer) Implements IStream.LockRegion
        Throw New Runtime.InteropServices.ExternalException(Nothing, STG_E_INVALIDFUNCTION)
    End Sub

    Public Sub UnlockRegion(ByVal libOffset As Long, ByVal cb As Long, ByVal dwLockType As Integer) Implements IStream.UnlockRegion
        Throw New Runtime.InteropServices.ExternalException(Nothing, STG_E_INVALIDFUNCTION)
    End Sub

    Public Sub Stat(ByRef pstatstg As System.Runtime.InteropServices.ComTypes.STATSTG, ByVal grfStatFlag As Integer) Implements IStream.Stat
        pstatstg = New System.Runtime.InteropServices.ComTypes.STATSTG()
        pstatstg.cbSize = baseStream.Length
    End Sub

    Public Sub Clone(ByRef ppstm As IStream) Implements IStream.Clone
        ppstm = Nothing
        Throw New Runtime.InteropServices.ExternalException(Nothing, STG_E_INVALIDFUNCTION)
    End Sub

    Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
        target = value
        Return value
    End Function

End Class