Post by VDC 8x2 on Aug 26, 2014 8:03:25 GMT
far 8 copy complete now.
now to clean up and work on user friendliness.
compressed the 37 block test file into a 15 block file
Lots of pictures to test too.
now to clean up and work on user friendliness.
compressed the 37 block test file into a 15 block file
Lots of pictures to test too.
Module Module1
Sub Main()
Dim outputString As String = ""
Dim starting() As Byte = {0}
Dim stringCount As UInteger
Dim historyPointer As Integer = 0
Dim processPointer As Integer = 1
Dim futurePointer As UInteger = 256
Dim endOfProcess As Integer
Dim farSize As Integer
Dim raw As String = ""
Dim compressTarget As String = ""
Dim anyCompression As Integer
Dim bytes = My.Computer.FileSystem.ReadAllBytes("D:/Emulation/drive10/testdata.prg")
'convert array into our workstring
For index = 0 To bytes.GetUpperBound(0)
compressTarget = compressTarget & Chr(bytes(index))
Next
endOfProcess = Len(compressTarget)
'start of the encoding loop
Do
stringCount = 1
anyCompression = 0
'big copy mirror
If historyPointer > 257 Then
If futurePointer = 256 Then
stringCount = futurePointer
If Mid(compressTarget, processPointer - 257, stringCount) = Mid(compressTarget, processPointer + 256, stringCount) Then
anyCompression = 2
End If
End If
End If
'64 mirror copy
If anyCompression = 0 Then
If historyPointer > 65 Then
If futurePointer > 63 Then
stringCount = 64
Else
stringCount = futurePointer
End If
Do
If Mid(compressTarget, processPointer - stringCount - 1, stringCount) = Mid(compressTarget, processPointer + stringCount, stringCount) Then
anyCompression = 2
Exit Do
Else
stringCount = stringCount - 1
If stringCount = 1 Then
Exit Do
End If
End If
Loop
End If
End If
'64byte far copy
If anyCompression = 0 Then
'has to be 64 bytes or more in the future and history buffer for this to work
If futurePointer > 63 And historyPointer > 65 Then
stringCount = historyPointer
farSize = 64
Do
If Mid(compressTarget, processPointer - stringCount, farSize) = Mid(compressTarget, processPointer, farSize) Then
anyCompression = 3
Exit Do
Else
stringCount = stringCount - 1
If stringCount < 65 Then 'nothing found so set stringCount for the next check
stringCount = 1
Exit Do
End If
End If
Loop
End If
End If
'rle copy for 256 or 64 bytes
If anyCompression = 0 Then
Do
If Mid(compressTarget, processPointer, 1) = Mid(compressTarget, processPointer + stringCount, 1) Then
stringCount = stringCount + 1
anyCompression = 1
If stringCount = futurePointer Then
Exit Do
End If
Else
Exit Do
End If
Loop
End If
If anyCompression = 0 Then 'far copy 8 byte
If futurePointer > 7 And historyPointer > 9 Then
farSize = 8
Do
stringCount = historyPointer
Do
If Mid(compressTarget, processPointer - stringCount, farSize) = Mid(compressTarget, processPointer, farSize) Then
anyCompression = 3
Exit Do
Else
stringCount = stringCount - 1
If stringCount < 9 Then 'nothing found so set stringCount for the next check
Exit Do
End If
End If
Loop
If anyCompression = 3 Then
Exit Do
End If
farSize = farSize - 1
If farSize = 1 Then
stringCount = 1
Exit Do
End If
Loop
End If
End If
'if any compression happened then flush raw buffer
If anyCompression > 0 Then
If Len(raw) <> 0 Then
outputString = outputString & Chr(Len(raw) * 4) & raw
raw = ""
End If
End If
'process 64 byte far copy
If anyCompression = 3 Then
If farSize = 64 Then 'hhhlllxx with xx =11 lllllsss with sss = farsize-1
outputString = outputString & Chr((stringCount And 2016) / 8 + 3) & Chr((stringCount And 31) * 8)
stringCount = farSize
Else
outputString = outputString & Chr((stringCount And 2016) / 8 + 3) & Chr((stringCount And 31) * 8 + (farSize - 1))
stringCount = farSize
End If
End If
'process mirror packet
If anyCompression = 2 Then
If stringCount = 256 Then
outputString = outputString & Chr(1)
Else
outputString = outputString & Chr((stringCount - 1) * 4 + 1)
End If
End If
'process rle packet
If anyCompression = 1 Then
If stringCount = 256 Then
outputString = outputString & Chr(2) & Mid(compressTarget, processPointer, 1)
Else
If stringCount > 64 Then
stringCount = 64
End If
outputString = outputString & Chr((stringCount - 1) * 4 + 2) & Mid(compressTarget, processPointer, 1)
End If
End If
If anyCompression = 0 Then
raw = raw & Mid(compressTarget, processPointer, 1)
'if raw has reached 63 bytes then flush the buffer to output and clear
If Len(raw) = 63 Then
outputString = outputString & Chr(63 * 4) & raw
raw = ""
End If
End If
historyPointer = historyPointer + stringCount
If historyPointer > 2048 Then
historyPointer = 2048
End If
processPointer = processPointer + stringCount
If processPointer + futurePointer > endOfProcess Then
futurePointer = futurePointer - (processPointer + futurePointer - endOfProcess)
End If
If futurePointer = 0 Then
'if any raw left in buffer then flush out before ending
If Len(raw) <> 0 Then
outputString = outputString & Chr(Len(raw) * 4) & raw
raw = ""
End If
outputString = outputString & Chr(0)
Exit Do
End If
Loop
Dim outputFile(Len(outputString)) As Byte
For index = 1 To Len(outputString)
outputFile(index) = Asc(Mid(outputString, index, 1))
Next
My.Computer.FileSystem.WriteAllBytes("D:/Emulation/drive10/outputData.prg", starting, False)
My.Computer.FileSystem.WriteAllBytes("D:/Emulation/drive10/outputData.prg", outputFile, True)
End Sub
End Module