Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

JCALG (de)compression library

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • JCALG (de)compression library

    PB/CC20 test app for JCALG (de)compression library/
    Also for use with PB/DLL.
    Library can be dowloaded @ : http://www.collakesoftware.com

    Code:
     
    #include "Win32api.inc"
    #include "jcalg.inc"
     
    Function PbMain()
     
        Dim nLen        As Long         '// Multipurpose
        Dim Buffer      As String       '// Buffer with uncompressed data
        Dim BufferCmp   As String       '// Compressed data
        Dim BufferDeCmp As String       '// Decompressed data
        Dim JCALG1Info  As JCALG1_Info  '// Structure wich retrieves DLL version
     
        '// Load library.
        If JCALG_LoadLibrary( "" ) Then
     
            '// Retrieve info about JCALG DLL.
            JCALG_GetInfo JCALG1Info
            StdOut "Algorithm revision      : " & Format$( JCALG1Info.MajorRev ) & "." & Format$( JCALG1Info.MinorRev )
            StdOut "Small decompressor size : " & Format$( JCALG1Info.SmallDecompressorSize )
            StdOut "Fast decompressor size  : " & Format$( JCALG1Info.FastDecompressorSize )
     
            '===================================================================
            '// Compress
            '===================================================================
     
            '// Prepare buffer to be compressed.
            Buffer = Repeat$( 10000, "abcdefghijklmnopqrstuvwxyz0123456789" )
            StdOut "Buffer length:" & Str$( Len( Buffer ) )
     
            '// What's the required buffer length for compression?
            nLen = JCALG_GetNeededBufferSize( Len( Buffer ) )
            BufferCmp = String$( nLen, 0 )
     
            nLen = JCALG_Compress( _
                  StrPtr( Buffer ) _
                , Len( Buffer ) _
                , StrPtr( BufferCmp ) _
                , Min( Len( Buffer ), ( 4 * 1024 ) ) _
                , CodePtr( MYJCALG_CallbackFunc ) _
                )
     
            '// Be sure the string is the actual length.
            BufferCmp = Left$( BufferCmp, nLen )
            StdOut "Compressed len" & Str$( nLen )
            StdOut
     
            '===================================================================
            '// Decompress
            '===================================================================
     
            '// Prepare a buffer to decompress the compressed data
            nLen = JCALG_GetUncompressedSizeOfCompressedBlock( StrPtr( BufferCmp ) )
            BufferDeCmp = String$( nLen, 0 )
            StdOut "Uncompressed len" & Str$( nLen )
     
            '// Decompress..
            nLen = JCALG_Decompress_Fast( StrPtr( BufferCmp ), StrPtr( BufferDeCmp ) )
            StdOut "Decompressed len" & Str$( nLen ) & ", " & Left$( BufferDeCmp, 40 )
     
        End If
     
        '// Free library
        JCALG_FreeLibrary
     
        StdOut
        StdOut "Hit the 'any' key.."
     
        WaitKey$
     
    End Function
     
    '// Custom callback
    Function MYJCALG_CallbackFunc( ByVal pSourcePos As Dword, ByVal pDestinationPos As Dword ) As Dword
     
        StdOut "Callback: " & Format$( pSourcePos ) & ", " & Format$( pDestinationPos )
     
        Function = -1
     
    End Function
    "jcalg.inc"

    Code:
    Type JCALG1_Info
     
        MajorRev                As Dword
        MinorRev                As Dword
        FastDecompressorSize    As Dword
        SmallDecompressorSize   As Dword
     
    End Type
     
    '// Internal use only
    Declare Function JCALG1_Compress( _
          ByVal pSource         As Long _
        , ByVal nLength         As Dword _
        , ByVal pDestination    As Long _
        , ByVal nWindowSize     As Dword _
        , ByVal pAlloc          As Long _
        , ByVal pDealloc        As Long _
        , ByVal pCallback       As Long _
        ) As Dword
    Declare Function JCALG1_Decompress_Fast( ByVal pSource As Long, ByVal pDestination As Long ) As Dword
    Declare Function JCALG1_Decompress_Small( ByVal pSource As Long, ByVal pDestination As Long ) As Dword
    Declare Function JCALG1_GetUncompressedSizeOfCompressedBlock( ByVal pBlock As Dword ) As Dword
    Declare Function JCALG1_GetNeededBufferSize( ByVal nSize As Long ) As Dword
    Declare Sub JCALG1_GetInfo( JCALG1Info As JCALG1_Info )
     
    '// Internal use only
    Type JCALG_Library
     
        hLib                                    As Long
        pCompress                               As Dword
        pDecompress_Fast                        As Dword
        pDecompress_Small                       As Dword
        pGetUncompressedSizeOfCompressedBlock   As Dword
        pGetNeededBufferSize                    As Dword
        pGetInfo                                As Dword
     
    End Type
     
    '// Internal use only
    Global JCALGLibrary As JCALG_Library
     
    '// Loads the JCALG library, optionally you can set an alternative DLLfilename.
    Function JCALG_LoadLibrary( ByVal OptFileName As String ) As Long
     
        Dim hLib    As Long
        Dim JLib    As JCALG_Library
        Dim T       As String
     
        '// Library already loaded?
        If JCALGLibrary.hLib Then
     
            Function = 1
            Exit Function
     
        End If
     
        '// Load library with alternative filename?
        OptFileName = Trim$( OptFileName )
        If OptFileName > "" Then
            hLib = LoadLibrary( ByVal StrPtr( OptFileName ) )
        Else
            hLib = LoadLibrary( "JCALG1.DLL" )
        End If
     
        '// Succes?
        Select Case hLib
        Case 0 To 32: Exit Function
        End Select
     
        '// Retrieve all procedures available.
        JLib.hLib = hLib
        JLib.pCompress = GetProcAddress( hLib, "JCALG1_Compress" )
        JLib.pDecompress_Fast = GetProcAddress( hLib, "JCALG1_Decompress_Fast" )
        JLib.pDecompress_Small = GetProcAddress( hLib, "JCALG1_Decompress_Small" )
        JLib.pGetUncompressedSizeOfCompressedBlock = GetProcAddress( hLib, "JCALG1_GetUncompressedSizeOfCompressedBlock" )
        JLib.pGetNeededBufferSize = GetProcAddress( hLib, "JCALG1_GetNeededBufferSize" )
        JLib.pGetInfo = GetProcAddress( hLib, "JCALG1_GetInfo" )
     
        '// Test procedures
        T = Peek$( VarPtr( JLib ), Len( JLib ) )
        If Instr( T, MkDwd$( 0 ) ) Then
     
            Freelibrary hLib
            Exit Function
     
        End If
     
        '// Use it.
        JCALGLibrary = JLib
     
        Function = 1
     
    End Function
     
    '// Frees the JCALG library
    Function JCALG_FreeLibrary() As Long
     
        '// Was library loaded?
        If JCALGLibrary.hLib Then
            FreeLibrary JCALGLibrary.hLib
            Function = 1
        End If
     
        '// Make empty
        Poke$ VarPtr( JCALGLibrary ), String$( Len( JCALGLibrary ), 0 )
     
    End Function
     
    'Parameters
    'Source is a pointer to the uncompressed data.
    'Length is the size of the uncompressed data.
    'Destination is a pointer to the destination of the compressed data. This buffer should be at least 4 bytes larger than the uncompressed data size. One can retrieve the minimum needed buffer size by issuing a call to JCALG1_GetNeededBufferSize.
    'WindowSize is a nonzero value up to the size of the file. The larger, the better the compression ratio but the slower the compression.
    'pAlloc is a pointer to a memory allocation function. See prototype below.
    'pDealloc is a pointer to a memory deallocation function. See prototype below.
    'pCallback is a pointer to a callback function which is called in every iteration of the main compression loop. See protoype below.
    'Returns: Size of the compressed data, or NULL if the data could not be compressed.
     
    '// PowerBASIC:
    '// Some parameters are solved internally.
    '// The callback is optional.
     
    Function JCALG_Compress( _
          ByVal pSource         As Long _
        , ByVal nLength         As Dword _
        , ByVal pDestination    As Long _
        , ByVal nWindowSize     As Dword _
        , ByVal pCallback       As Long _
        ) As Dword
     
        Dim pAlloc          As Long
        Dim pDealloc        As Long
        Dim Result          As Dword
     
        If JCALGLibrary.hLib = 0 Then Exit Function
     
        pAlloc   = CodePtr( JCALG_AllocFunc )
        pDealloc = CodePtr( JCALG_DeallocFunc )
        If pCallback = 0 Then pCallback = CodePtr( JCALG_CallbackFunc )
     
        Call Dword JCALGLibrary.pCompress Using _
            JCALG1_Compress( pSource, nLength, pDestination, nWindowSize, pAlloc, pDealloc, pCallback ) To Result
     
        Function = Result
     
    End Function
     
    'Parameters
    'Source is a pointer to the source data.
    'Destination is a pointer to the destination buffer for the uncompressed data.
    'Returns: Size of the uncompressed data or 0 if the block of data was not compressed by JCALG1.
    Function JCALG_Decompress_Fast( ByVal pSource As Long, ByVal pDestination As Long ) As Dword
     
        Dim Result As Dword
     
        If JCALGLibrary.hLib = 0 Then Exit Function
     
        Call Dword JCALGLibrary.pDecompress_Fast Using JCALG1_Decompress_Fast( pSource, pDestination ) To Result
     
        Function = Result
     
    End Function
     
    'Parameters
    'Source is a pointer to the source data.
    'Destination is a pointer to the destination buffer for the uncompressed data.
    'Returns: Size of the uncompressed data or 0 if the block of data was not compressed by JCALG1.
    Function JCALG_Decompress_Small( ByVal pSource As Long, ByVal pDestination As Long ) As Dword
     
        Dim Result As Dword
     
        If JCALGLibrary.hLib = 0 Then Exit Function
     
        Call Dword JCALGLibrary.pDecompress_Small Using JCALG1_Decompress_Small( pSource, pDestination ) To Result
     
        Function = Result
     
    End Function
     
    'Parameters
    'pBlock is a pointer to a JCALG1 compressed block of memory.
    'Returns: Size of the uncompressed block, in bytes.
    Function JCALG_GetUncompressedSizeOfCompressedBlock( ByVal pBlock As Dword ) As Dword
     
        Dim Result As Dword
     
        If JCALGLibrary.hLib = 0 Then Exit Function
     
        Call Dword JCALGLibrary.pGetUncompressedSizeOfCompressedBlock _
            Using JCALG1_GetUncompressedSizeOfCompressedBlock( pBlock ) To Result
     
        Function = Result
     
    End Function
     
    'Parameters
    'nSize is size, in bytes, of a block of uncompressed data.
    'Returns: The minimum destination buffer size (currently nSize+4).
    Function JCALG_GetNeededBufferSize( ByVal nSize As Long ) As Dword
     
        Dim Result As Dword
     
        If JCALGLibrary.hLib = 0 Then Exit Function
     
        Call Dword JCALGLibrary.pGetNeededBufferSize Using JCALG1_GetNeededBufferSize( nSize ) To Result
     
        Function = Result
     
    End Function
     
    'Parameters :
    'JCALG1Info is a pointer to a JCALG1_Info structure.
    Sub JCALG_GetInfo( JCALG1Info As JCALG1_Info )
     
        If JCALGLibrary.hLib = 0 Then Exit Sub
     
        Call Dword JCALGLibrary.pGetInfo Using JCALG1_GetInfo( JCALG1Info )
     
    End Sub
     
    '// Internal use only
    Function JCALG_AllocFunc( ByVal nMemSize As Dword ) As Dword
        Function = GlobalAlloc( %GMEM_FIXED, nMemSize )
    End Function
     
    '// Internal use only
    Function JCALG_DeallocFunc( ByVal pBuffer As Long ) As Dword
        If pBuffer Then GlobalFree pBuffer
        Function = 1
    End Function
     
    '// Internal use only
    Function JCALG_CallbackFunc( ByVal pSourcePos As Dword, ByVal pDestinationPos As Dword ) As Dword
        Function = 1
    End Function

    ------------------
    hellobasic
Working...
X