;/* ************************************************************************
; * fimp.dll v1.0.0.0
; * by Peace^Testaware / 01-Dec-2008
; * PureBasic example how to implode/explode Amiga FImp packed dataformat
; * ************************************************************************/


;/* FIMP lib error codes */
#ERR_FIMP_NONE           =  0
#ERR_FIMP_TOOSHORT       = -1
#ERR_FIMP_UNKNOWNFORMAT  = -2
#ERR_FIMP_COMPNOTEVEN    = -3
#ERR_FIMP_COMPTOOSHORT   = -4
#ERR_FIMP_NOTENOUGHDATA  = -5
#ERR_FIMP_OUTPUTLTINPUT  = -6
#ERR_FIMP_CANNOTALLOCMEM = -7
#ERR_FIMP_CANNOTDEPLODE  = -8


;/* FIMP lib functions */
Structure    FIMP
    *DLL
    *GetExplodeSize    ;(*Buffer)
    *Explode           ;(*In, In_Len.l, *Out, Out_Len.l)
    *Implode           ;(*In, In_Len.l, CMode.l)
EndStructure    :    Global    IMP.FIMP


;/* FIMP lib open library, must adapt path */
IMP\DLL    =    OpenLibrary(#PB_Any, "..\..\dll\fimp.dll")


;/* FIMP lib declare function addresses */
With    IMP
    If  \DLL
        \GetExplodeSize = GetFunction(\DLL, "impGetExplodeSize")
        \Implode        = GetFunction(\DLL, "impImplode")
        \Explode        = GetFunction(\DLL, "impExplode")
    Else
        Debug "Error: cannot initialize fimp.dll"
        End
    EndIf
EndWith


;/* 0 = implode memory / 1 = explode memory */
#FIMP = 0


CompilerIf    #FIMP    =    0


    Debug    "Implode..."

    ;/* Change filenames by your own ones */
    InFile.s  = "test\SOTB-TITLE.mod"
    OutFile.s = "C:\SOTB-TITLE.imp"

    ;/* Efficiency [0-11]: > 4 need a lot of time!! */
    CMode = 1

    hF  = ReadFile(#PB_Any, InFile)
    If  hF
        In_Len = Lof(hF)
        *In    = AllocateMemory(In_Len)
        ReadData(hF, *In, In_Len)
        CloseFile(hF)
    Else
        Debug "Error: cannot open - " + InFile
        End
    EndIf

    Out_Len = CallCFunctionFast(IMP\Implode, *In, In_Len, CMode)

    If  Out_Len > 0
        hF  =  CreateFile(#PB_Any, OutFile)
        If  hF
            WriteData(hF, *In, Out_Len)
            CloseFile(hF)
        Else
            Debug "Error: cannot create - " + OutFile
        EndIf
    Else
        Debug "Error: cannot implode file - " + Str(Out_Len)
    EndIf


CompilerElse


    Debug "Explode..."

    ;/* Change filenames by your own ones */
    InFile.s  = "test\SOTB-TITLE.imp"
    OutFile.s = "C:\SOTB-TITLE.mod"

    hF  = ReadFile(#PB_Any, InFile)
    If  hF
        In_Len = Lof(hF)
        *In    = AllocateMemory(In_Len)
        ReadData(hF, *In, In_Len)
        CloseFile(hF)
    Else
        Debug "Error: cannot open - " + InFile
        End
    EndIf

    Out_Len = CallCFunctionFast(IMP\GetExplodeSize, *In)

    If  Out_Len > 0

        *Out = AllocateMemory(Out_Len)
        
        n    = CallCFunctionFast(IMP\Explode, *In, In_Len, *Out, Out_Len)

        If  n  = #ERR_FIMP_NONE
            hF = CreateFile(#PB_Any, OutFile)
            If  hF
                WriteData(hF, *Out, Out_Len)
                CloseFile(hF)
            Else
                Debug "Error: cannot create - " + OutFile
            EndIf
        Else
            Debug "Error: cannot explode file - " + Str(n)
        EndIf

    EndIf


CompilerEndIf


CloseLibrary(IMP\DLL)


;/* Show infos in debugwindow /*
Debug "InFile:  " + InFile  + " - Size: " + Str(In_Len)
Debug "OutFile: " + OutFile + " - Size: " + Str(Out_Len)


If *In  : FreeMemory(*In)  : EndIf
If *Out : FreeMemory(*Out) : EndIf


End