I hard wired the 28 "11110000" , and 28 "10111011" combos..
It's sometimes decompressing okay.. And sometimes not... Got to trouble shoot it some more..
Here's the "Test Bed" where i try to write the de-compressor..
Code: Select all
Declare Function compress_loop( chrs as string ) as string
Declare Function decompress_loop( chrs as string ) as string
screen 19
'=====================================================================
'=====================================================================
'start program
'=====================================================================
'=====================================================================
dim as double time1 , time2 , time3 , time4
do
randomize
dim as string s = ""
For n As Long = 1 To 8
s+=chr(Int(Rnd*256))
Next
time1=timer
'begin compress
dim as string comp = s
'do
' dim as longint chk = len(comp) - 1
' comp = compress_loop(comp)
' if len(comp) >= chk then exit do
'loop
for a as longint = 1 to 1 step 1
comp = compress_loop(comp)
next
'end compress
time2 = timer
time3=timer
'begin decompress
dim as string final_out = comp
for a as longint = 1 to 1 step 1
final_out = decompress_loop(final_out)
next
'end decompress
time4 = timer
'sleep
'cls
'draw string( 0,10) , left(s,100)
'draw string( 0,30) , left(final_out,100)
print string(99,"=")
print "inp = " ; (s)
print string(99,"=")
print "out = " ; (final_out)
print
print "compress time = "; time2-time1
print "decompress time = "; time4-time3
print
if s = final_out then print "Decompressed OK" else print "Decompression failed."
print string(99,"=")
sleep
loop until inkey = chr(27)
sleep
end
'===============================================================================
'===============================================================================
'compress
'===============================================================================
'===============================================================================
Function compress_loop( chrs as string ) as string
print "c inp = " ; len(chrs)
dim as ubyte in_count = 0
dim as string str1
dim as ubyte dec1
do
str1 = str( len( chrs ) / 8 )
dec1 = instr( 1 , str1 , "." )
if dec1 <> 0 then chrs+= chr(0) : in_count+=1
loop until dec1 = 0
dim as string bits = ""
dim as string zeros = string( 64 , "0" )
dim as string n1
dim as ulongint ptr usp = cptr( ulongint ptr , strptr( chrs ) )
for a as longint = 1 to len( chrs ) step 8
n1 = zeros + bin( *usp ) : usp+= 1
n1 = right( n1 , 64 )
bits+= n1
next
print "c bin = " ; len(bits) , bits
dim as ubyte bit_count = 0
do
str1 = str( len( bits ) / 6 )
dec1 = instr( 1 , str1 , "." )
if dec1 <> 0 then bits+= "0" : bit_count+=1
loop until dec1 = 0
dim as string outs1 = ""
dim as string * 8 vals
dim as ubyte v1 , v2
dim as ubyte ptr ubp
for a as longint = 1 to len( bits ) step 6
n1 = mid( bits , a , 6 )
v1 = val( "&B" + mid( n1 , 1 , 3 ) )
v2 = val( "&B" + mid( n1 , 4 , 3 ) )
'SET 2 BITS
if v1 < v2 then
vals = "11110000"
ubp = cptr( ubyte ptr , strptr( vals ) )
if *( ubp + v1 ) = 48 then *( ubp + v1 ) = 49 else *( ubp + v1 ) = 48
if *( ubp + v2 ) = 48 then *( ubp + v2 ) = 49 else *( ubp + v2 ) = 48
end if
'SET 1 BIT
if v1 = v2 then
vals = "00000000"
ubp = cptr( ubyte ptr , strptr( vals ) )
*( ubp + v1 ) = 49
end if
'SET 2 BITS
if v1 > v2 then
vals = "10111011"
ubp = cptr( ubyte ptr , strptr( vals ) )
if *( ubp + v1 ) = 48 then *( ubp + v1 ) = 49 else *( ubp + v1 ) = 48
if *( ubp + v2 ) = 48 then *( ubp + v2 ) = 49 else *( ubp + v2 ) = 48
end if
outs1+= vals
'print n1 , v1 , v2 , vals
'sleep
'if inkey = " " then end
next
print "c out = " ; len(outs1) , outs1
dim as ubyte out_count = 0
do
str1 = str( len( outs1 ) / 64 )
dec1 = instr( 1 , str1 , "." )
if dec1 <> 0 then outs1+= "0" : out_count+=1
loop until dec1 = 0
dim as string final = ""
for a as longint = 1 to len( outs1 ) step 64
final+= mklongint( valulng( "&B" + mid( outs1 , a , 64 ) ) )
next
final = chr( in_count ) + chr( bit_count) + chr( out_count ) + final
print "c fin = " ; len(final)
return final
end function
'===============================================================================
'============================================================================
Function decompress_loop( chrs as string ) as string
print "dc inp = " ; len(chrs)
dim as ubyte in_count = asc( left( chrs , 1 ) ) : chrs = mid( chrs , 2 )
dim as ubyte bit_count = asc( left( chrs , 1 ) ) : chrs = mid( chrs , 2 )
dim as ubyte out_count = asc( left( chrs , 1 ) ) : chrs = mid( chrs , 2 )
dim as string bits = ""
dim as string zeros = string( 64 , "0" )
dim as string n1
dim as ulongint ptr usp = cptr( ulongint ptr , strptr( chrs ) )
for a as longint = 1 to len( chrs ) step 8
n1 = zeros + bin( *usp ) : usp+= 1
n1 = right( n1 , 64)
bits+= n1
next
bits = left( bits , len( bits ) - out_count )
print "d bin = " ; len(bits) , bits
dim as string outs1 = ""
dim as string * 8 vals
dim as ubyte v1 , v2
for a as longint = 1 to len( bits ) step 8
vals = mid( bits , a , 8 )
'FOR "00000000" ( V1 = V2 )
if vals = "00000001" then v1 = 7 : v2 = 7
if vals = "00000010" then v1 = 6 : v2 = 6
if vals = "00000100" then v1 = 5 : v2 = 5
if vals = "00001000" then v1 = 4 : v2 = 4
if vals = "00010000" then v1 = 3 : v2 = 3
if vals = "00100000" then v1 = 2 : v2 = 2
if vals = "01000000" then v1 = 1 : v2 = 1
if vals = "10000000" then v1 = 0 : v2 = 0
'FOR "11110000" ( V1 < V2 )
if vals = "11110011" then v1 = 6 : v2 = 7
if vals = "11110101" then v1 = 5 : v2 = 7
if vals = "11110110" then v1 = 5 : v2 = 6
if vals = "11111001" then v1 = 4 : v2 = 7
if vals = "11111010" then v1 = 4 : v2 = 6
if vals = "11111100" then v1 = 4 : v2 = 5
if vals = "11100001" then v1 = 3 : v2 = 7
if vals = "11100010" then v1 = 3 : v2 = 6
if vals = "11100100" then v1 = 3 : v2 = 5
if vals = "11101000" then v1 = 3 : v2 = 4
if vals = "11010001" then v1 = 2 : v2 = 7
if vals = "11010010" then v1 = 2 : v2 = 6
if vals = "11010100" then v1 = 2 : v2 = 5
if vals = "11011000" then v1 = 2 : v2 = 4
if vals = "11000000" then v1 = 2 : v2 = 3
if vals = "10110001" then v1 = 1 : v2 = 7
if vals = "10110010" then v1 = 1 : v2 = 6
if vals = "10110100" then v1 = 1 : v2 = 5
if vals = "10111000" then v1 = 1 : v2 = 4
if vals = "10100000" then v1 = 1 : v2 = 3
if vals = "10010000" then v1 = 1 : v2 = 2
if vals = "01110001" then v1 = 0 : v2 = 7
if vals = "01110010" then v1 = 0 : v2 = 6
if vals = "01110100" then v1 = 0 : v2 = 5
if vals = "01111000" then v1 = 0 : v2 = 4
if vals = "01100000" then v1 = 0 : v2 = 3
if vals = "01010000" then v1 = 0 : v2 = 2
if vals = "00110000" then v1 = 0 : v2 = 1
'FOR "10111011" ( V1 < V2 )
if vals = "10111000" then v1 = 7 : v2 = 6
if vals = "10111110" then v1 = 7 : v2 = 5
if vals = "10111101" then v1 = 6 : v2 = 5
if vals = "10110010" then v1 = 7 : v2 = 4
if vals = "10110001" then v1 = 6 : v2 = 4
if vals = "10110111" then v1 = 5 : v2 = 4
if vals = "10101010" then v1 = 7 : v2 = 3
if vals = "10101001" then v1 = 6 : v2 = 3
if vals = "10101111" then v1 = 5 : v2 = 3
if vals = "10100011" then v1 = 4 : v2 = 3
if vals = "10011010" then v1 = 7 : v2 = 2
if vals = "10011001" then v1 = 6 : v2 = 2
if vals = "10011111" then v1 = 5 : v2 = 2
if vals = "10010011" then v1 = 4 : v2 = 2
if vals = "10001011" then v1 = 3 : v2 = 2
if vals = "11111010" then v1 = 7 : v2 = 1
if vals = "11111001" then v1 = 6 : v2 = 1
if vals = "11111111" then v1 = 5 : v2 = 1
if vals = "11110011" then v1 = 4 : v2 = 1
if vals = "11101011" then v1 = 3 : v2 = 1
if vals = "11011011" then v1 = 2 : v2 = 1
if vals = "00111010" then v1 = 7 : v2 = 0
if vals = "00111001" then v1 = 6 : v2 = 0
if vals = "00111111" then v1 = 5 : v2 = 0
if vals = "00110011" then v1 = 4 : v2 = 0
if vals = "00101011" then v1 = 3 : v2 = 0
if vals = "00011011" then v1 = 2 : v2 = 0
if vals = "01111011" then v1 = 1 : v2 = 0
outs1+= right("000" + bin( v1 ) , 3 ) + right( "000" + bin( v2 ) , 3 )
next
outs1 = left( outs1 , len( outs1 ) - bit_count )
print "d out = " ; len(outs1) , outs1
dim as string final = ""
for a as longint = 1 to len( outs1 ) step 64
final+= mklongint( valulng( "&B" + mid( outs1 , a , 64 ) ) )
next
final = left( final , len( final ) - in_count )
print "d fin = " ; len(final)
return final
end function