Private Function ReadBitsFromArray(FromArray() As Byte, FromPos As Long, ...)
The bug is located in this function declaration. Your loop can only exit when InpPos
increments beyond the upper bound of the array, setting the EOF_State
variable to True. The loop itself doesn't increment InpPos at all. Back in VB6, it got incremented by the ReadBitsFromArray() function, FromBit = FromBit + 1
statement. But that is no longer the case in VB.NET.
An important change in VB.NET is the way arguments are passed. Old Visual Basic versions defaulted to ByRef
. A somewhat strange choice whose reasons are lost in the mist of time. It is a very expensive way to pass arguments, passing ByRef requires passing a pointer to the value instead of the value itself. And every access to the variable requires a pointer dereference. It is at least 3 times slower than passing by value on modern machines, .NET is heavily optimized to pass argument values through CPU registers instead of the stack, that can't work if the argument is passed by reference.
So VB.NET made ByVal
the default. Since you didn't declare it explicitly, the default applies and InpPos
is passed by value. Never to increment, something that's easy to see in the debugger. Fix:
Private Function ReadBitsFromArray(ByVal FromArray() As Byte, _
ByRef FromPos As Integer, _
ByVal FromBit As Integer, _
ByVal NumBits As Integer) As Integer
Note that the choice between Long and Integer is something else you will have to fret about. A VB6 Long is a VB.NET Integer. I guessed that Integer was appropriate here. Do consider the .NET GZipStream class as an alternative, it doesn't sound like you remember enough about the way it works to still be able to maintain this code.