Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function DecompressLZW(Data As IO.Stream, MaxBits As Byte) As IO.MemoryStream
- Dim out As New IO.MemoryStream
- Dim dict As New List(Of String)
- For x = 0 To 255
- dict.Add(Chr(x))
- Next
- dict.Add("")
- dict.Add(Nothing)
- Dim usebits As Byte = 9
- Dim bpos As Long
- Dim bits As New List(Of Boolean)
- Do Until Data.Position = Data.Length
- Dim b, ub As Byte
- b = Data.ReadByte
- ub = b
- For x = 7 To 0 Step -1
- If ub - (2 ^ x) >= 0 Then
- ub -= (2 ^ x)
- bits.Add(True)
- Else
- bits.Add(False)
- End If
- Next
- Loop
- Dim GetCode = Function() As UInteger
- Dim u As UInteger
- For x = usebits To 1 Step -1
- If bits(bpos) Then u += (2 ^ (x - 1))
- bpos += 1
- Next
- Return u
- End Function
- Dim Output = Sub(DecompData As String)
- Dim n As UInteger = DecompData.Length
- out.Write(DecompData.Select(Function(h) CByte(Asc(h))).ToArray, 0, n)
- End Sub
- Dim AddToDict = Sub(Entry As String)
- If dict.Count < (2 ^ MaxBits) Then
- dict.Add(Entry)
- If dict.Count = (2 ^ usebits) - 1 Then usebits = Math.Min(usebits + 1, MaxBits)
- End If
- End Sub
- Dim fcode As UInteger = GetCode()
- Dim match As String = dict(fcode)
- Output(match)
- Do
- Dim ncode As UInteger = GetCode()
- If ncode = 257 Then Exit Do
- If ncode = 256 Then Throw New Exception
- Dim nmatch As String
- If ncode < dict.Count Then
- nmatch = dict(ncode)
- Else
- nmatch = match & match(0)
- End If
- Output(nmatch)
- AddToDict(match & nmatch(0))
- match = nmatch
- Loop
- out.Position = 0
- Return out
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement