This code by it's self doesn't compress..
But when used in conjunction with Zlib it compresses..
See compress_loop()
it starts a the end of the hex input stream and squares each digit.
if puts the last hex digit into the output, and carry's the remainder to the next input digit.
I'm looking for someone who can decompress it's output...
[edit]
From OP, posted as a solution:
Quote:
The source is written in FreeBasic
[/edit]
What I have tried:
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 string bits = ""
dim as string zeros = string( 2 , "0" )
dim as string n1
dim as ubyte ptr usp = cptr( ubyte ptr , strptr( chrs ) )
for a as longint = 1 to len( chrs ) step 1
n1 = zeros + hex( *usp ) : usp+= 1
n1 = right( n1 , 2 )
bits+= n1
next
print "c bin = " ; len(bits) , bits
dim as string outs1 = string( len( bits ) , "0" )
dim as string s1 , s2 , s3
dim as longint v1 , v2 , carry
zeros = "000"
for a as longint = len( bits ) to 1 step - 1
's1 = str( ( val( "&H" + mid( bits , a , 1 ) ) ^ 2 ) + carry ) : carry = 0
's2 = right( "000" + hex( val( s1 ) ) , 3 )
v1 = bits[ a - 1 ]
if v1 = 48 then v2 = 000 + carry : goto done
if v1 = 49 then v2 = 001 + carry : goto done
if v1 = 50 then v2 = 004 + carry : goto done
if v1 = 51 then v2 = 009 + carry : goto done
if v1 = 52 then v2 = 016 + carry : goto done
if v1 = 53 then v2 = 025 + carry : goto done
if v1 = 54 then v2 = 036 + carry : goto done
if v1 = 55 then v2 = 049 + carry : goto done
if v1 = 56 then v2 = 064 + carry : goto done
if v1 = 57 then v2 = 081 + carry : goto done
if v1 = 65 then v2 = 100 + carry : goto done
if v1 = 66 then v2 = 121 + carry : goto done
if v1 = 67 then v2 = 144 + carry : goto done
if v1 = 68 then v2 = 169 + carry : goto done
if v1 = 69 then v2 = 196 + carry : goto done
if v1 = 70 then v2 = 225 + carry : goto done
done:
carry = 0
s2 = zeros + hex( v2 )
s2 = right( s2 , 3 )
carry = val( "&H" + left( s2 , 2 ) )
s3 = right( s2 , 1 )
outs1[ a - 1 ] = s3[ 0 ]
'print v1 , s2 , s3 ', outs1
'sleep
'if inkey = " " then end
next
if carry > 0 then outs1 = hex( carry ) + outs1
print "c out = " ; len( outs1 ) , outs1
dim as ubyte count = 0
if len( outs1 ) mod 2 = 1 then outs1+= "0" : count = 1
dim as string final = ""
for a as longint = 1 to len( outs1 ) step 2
final+= chr( val( "&H" + mid( outs1 , a , 2 ) ) )
next
final = chr( 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 count = asc( left( chrs , 1 ) )
chrs = mid( chrs , 2 )
dim as string bits = ""
dim as string zeros = string( 2 , "0" )
dim as string n1
dim as ubyte ptr usp = cptr( ubyte ptr , strptr( chrs ) )
for a as longint = 1 to len( chrs ) step 1
n1 = zeros + hex( *usp ) : usp+= 1
n1 = right( n1 , 2 )
bits+= n1
next
bits = left( bits , len( bits ) - count )
print "d bin = " ; len(bits) , bits
return chrs
end function