zlib
 
Loss-less data compression library using the Deflate algorithm unencumbered by patents.

Website: http://www.zlib.net
Platforms supported: Windows (32-bit and 64-bit), Linux, DOS, FreeBSD, Haiku
Headers to include: zlib.bi
Header version: 1.2.8
Examples: in examples/compression/

Haiku R1 beta 6 amd64: `examples/compression/zlib.bas` compiled and ran with
FreeBASIC 1.20.4-3 against HaikuPorts zlib 1.3.2. Its compression, inflate,
dictionary and gzip checks passed; the example reported the expected runtime
version difference from the 1.2.8 declarations.

Windows qualification compares the stable stream layout with the installed zlib 1.3.2 SDK on both architectures, then exercises compression, small-buffer streams, synchronization, dictionaries and gzip file I/O. A newer compatible runtime may report a version warning against the 1.2.8 declarations.

The in-memory example checks the restored size and checksum, releases both buffers, and returns a failing process status on mismatch. Its final keypress wait remains part of the example. The longer examples/compression/zlib.bas test also returns its main routine's status. The bzip2 example accepts an optional input filename, checks complete file reads and output writes, and rejects file sizes above the signed 32-bit bound before allocating or narrowing the length.

Example

zlib-based PNG save & load code: http://www.freebasic.net/forum/viewtopic.php?t=3936

In-memory compression example:
'' examples/manual/libraries/zlib.bas
''
'' Example extracted from the FreeBASIC Manual
'' from topic 'zlib'
''
'' See Also: https://www.freebasic.net/wiki/wikka.php?wakka=ExtLibzlib
'' --------

'' Purpose: Demonstrate a checked in-memory zlib round trip.
'' Responsibilities: Bound output buffers, check the restored checksum, and release memory.
'' This file contains no file I/O or application compression format.

'' Ownership:
'' The example owns src and dest after allocation. Every codec or allocation
'' failure releases acquired buffers before terminating the demonstration.

'' Zlib compress/decompress example, by yetifoot

#include Once "zlib.bi"

Dim As Integer errlev

'' This is the size of our test data in bytes.
Dim As uLong_ src_len = 100000

Print "ZLib test - Version " & *zlibVersion()
Print
Print "Test data size      : " & src_len & " bytes."

'' The size of the destination buffer for the compressed data is calculated by
'' the compressBound function.
Dim As uLong_ dest_len = compressBound(src_len)

'' Allocate our needed memory.
Dim As UByte Ptr src = Allocate(src_len)
If src = 0 Then
    Print "**** Unable to allocate source buffer ****"
    End 1
End If

Dim As UByte Ptr dest = Allocate(dest_len)
If dest = 0 Then
    Deallocate(src)
    src = 0
    Print "**** Unable to allocate compression buffer ****"
    End 1
End If

'' Fill the src buffer with random, yet still compressable data.
Randomize Timer
For i As Integer = 0 To src_len - 1
    src[i] = Rnd * 4
Next

'' Store the crc32 checksum of the input data, so we can check if the
'' uncompression has worked.
Dim As UInteger crc = crc32(0, src, src_len)

'' Perform the compression.  dest_len is passed as its address, because when
'' the function returns it will contain the size of the compressed data.
errlev = compress(dest, @dest_len, src, src_len)
If errlev <> 0 Then
    '' If the function returns a value other than 0 then an error occured.
    Print "**** Error during compress - code " & errlev & " ****"
    Deallocate(src)
    src = 0
    Deallocate(dest)
    dest = 0
    End 1
End If
Print "Compressed to       : " & dest_len & " bytes."

'' NOTE: in normal use in a program, you would store the src_len, in order to
'' be able to tell uncompress the output size.  However in this example we can
'' just leave it in src_len.  The same goes for dest_len, which is the compressed
'' datas size.

'' Wipe the src buffer before we uncompress to it, so that we can check if the
'' decompression has worked.
For i As Integer = 0 To src_len - 1
    src[i] = 0
Next

'' Perform a decompression.  This time we uncompress the data back to src.
'' src_len is passed as its address, because when
'' the function returns it will contain the size of the uncompressed data.
errlev = uncompress(src, @src_len, dest, dest_len)
If errlev <> 0 Then
    '' If the function returns a value other than 0 then an error occured.
    Print "**** Error during uncompress - code " & errlev & " ****"
    Deallocate(src)
    src = 0
    Deallocate(dest)
    dest = 0
    End 1
End If
Print "Uncompressed to     : " & src_len & " bytes."

'' Make sure the checksum of the uncompressed data matches our original data.
Dim checksum_valid As Boolean = (src_len = 100000 AndAlso crc = crc32(0, src, src_len))
If Not checksum_valid Then
    Print "crc32 checksum      : FAILED"
Else
    Print "crc32 checksum      : PASSED"
End If

'' Free the buffers used in the test.
Deallocate(src)
src = 0
Deallocate(dest)
dest = 0

Print
If Not checksum_valid Then End 1
Print "Press any key to end . . . "
Sleep

'' end of zlib.bas