iOS Question [Almost SOLVED] DEFLATE Compression

Star-Dust

Expert
Licensed User
Longtime User
I should compress strings specifically with the Deflate compression algorithm.
You know how it can be done on B4i or Obj-C

P.S: I anticipate the questions ... unfortunately I just need the deflate algorithm and nothing else
 

Star-Dust

Expert
Licensed User
Longtime User
B4X:
Dim Deflate As CompressedStreams
Dim CompressData() As Byte
CompressData=Deflate.CompressBytes(Data(),"zlib")
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
It seems that ZLib doesn't exactly match the deflate algorithm
Is there another solution?
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
This is the data compressed in bytes expressed in Hex
c1 45 aa 56 4a 49 2c 49 54 b2 8a ae 56 ca 4c 51 b2 52 4a 2a c9 2b 4e cd 4b 51 d2 51 4a 2d 4b cd 2b 01 8a 24 e7 64 26 67 2b d5 c6 02 45 4a 2a 0b 52 81 22 c5 a9 25 8e a5 25 f9 b9 89 25 99 c9 ae 20 55 c5 4a b5 00 00

So they are expressed in decimals
(170,86,74,73,44,73,84,178,138,174,86,202,76,81,178,82,74,42,201,43,78,205,75,81,210,81,74,45,75,205,43,1,138,36,231,100,38,103,43,213,198,2,69,74,42,11,82,129,34,197,169,37,142,165,37,249,185,137,37,153,201,174,32,85,197,74,181,0,0)

So is the uncompressed string
{"data":[{"id":"btnsend","event":"click"}],"etype":"setAutomaticEvents"}

Using this code:
B4X:
Dim DecData() As Byte=CompressedStreams1.DecompressBytes(DataDeflate,"zlib")
I get this error:
Errore nella linea: 114
java.util.zip.ZipException: incorrect header check
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:164)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at anywheresoftware.b4a.objects.streams.File.Copy2(File.java:349)
at anywheresoftware.b4a.randomaccessfile.CompressedStreams.DecompressBytes(CompressedStreams.java:152)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:64)
at b4j.example.main._elaboratewebsocket(main.java:136)
at b4j.example.main._buttondec_click(main.java:128)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA$1.run(BA.java:216)
at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
No dec: (Exception) java.lang.Exception: java.util.zip.ZipException: incorrect header check
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
If it helps, this site allows you to examine data packets expressed in either hexadecimal, decimal or otherwise and apply different compression algorithms, encoding, etc ...

 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Using this nice tool, I'm getting different results than you:

1616333900602.png


How did you compress the string?
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Using this nice tool, I'm getting different results than you:

View attachment 110164

How did you compress the string?
This string is the response from a browser compressed with Deflate.

I'm sure it's Deflate because that's how it is specified in the response handshake

Using a network sniffer I can unzip it and the resulting string is the one I entered.

But there seem to be different versions of Deflate, zlib, lz77, lzw etc ...
 
Last edited:
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
I found some more details in RFC 2616
deflate
The "zlib" format defined in RFC 1950 in combination with the "deflate" compression mechanism described in RFC 1951.

So CompressedStreams's zlib is not enough
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Studying the examples on GitHub in Java it would be the java.util.zip.Deflater class. Is there a correspondent in objective-c?
 
Upvote 0
D

Deleted member 103

Guest
Studying the examples on GitHub in Java it would be the java.util.zip.Deflater class. Is there a correspondent in objective-c?
Maybe this thread can help you.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Maybe this thread can help you.
I don't think I will be able to use it because I use the hosted builder
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Maybe Erel can help?
I have already asked Erel for help many times in relation to the work I am doing, I would not like to take on all the thorny problems of my library.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Studying the examples on GitHub in Java it would be the java.util.zip.Deflater class. Is there a correspondent in objective-c?
I'm unable to uncompress the input you posted with java.util.zip.Inflater (B4J):
B4X:
Dim h As String = "c1 45 aa 56 4a 49 2c 49 54 b2 8a ae 56 ca 4c 51 b2 52 4a 2a c9 2b 4e cd 4b 51 d2 51 4a 2d 4b cd 2b 01 8a 24 e7 64 26 67 2b d5 c6 02 45 4a 2a 0b 52 81 22 c5 a9 25 8e a5 25 f9 b9 89 25 99 c9 ae 20 55 c5 4a b5 00 00"
Dim bc As ByteConverter
Dim b() As Byte = bc.HexToBytes(h.Replace(" ", ""))
Dim Inflater As JavaObject
Inflater.InitializeNewInstance("java.util.zip.Inflater", Null)
Inflater.RunMethod("setInput", Array(b))
Dim raw(100) As Byte
Inflater.RunMethod("inflate", Array(raw))

Waiting for debugger to connect...
Program started.
Error occurred on line: 19 (Main)
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
at b4j.example.main._appstart(main.java:88)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
at b4j.example.main.main(main.java:29)
Caused by: java.util.zip.DataFormatException: incorrect header check
at java.base/java.util.zip.Inflater.inflateBytesBytes(Native Method)
at java.base/java.util.zip.Inflater.inflate(Inflater.java:378)
at java.base/java.util.zip.Inflater.inflate(Inflater.java:464)
... 21 more
Program terminated (StartMessageLoop was not called).
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
It's strange. that string is sniffed by a connection between jServer and the browser.

The sniffer manages to decompress and displays the original byte sequence that you decompressed.

jServer uses java.util.zip.Inflater and java.util.zip.Deflater for websocket communications.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Unfortunately, I am out of the office and cannot do any further tests. Perhaps the explanation lies in a suggestion I read on a site this morning. That is, delete the first 2 bytes from the sequence to be decompressed.

Yesterday all afternoon I researched the Huffman (Deflate) algorithm and it doesn't seem complex to implement. But from what we understand is that depending on the implementation and how the frequency tree is composed the resulting sequence of bytes can be different even if of the same length.
So comparing two compressed Arrays of the same string is not necessarily the same even if compressed with Deflate.

But the decompression on the other hand must obviously be identical
Huffman_huff_demo.gif
 
Last edited:
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Upvote 0
Top