BriefLZ

Share your advanced PureBasic knowledge/code with the community.
javabean
User
User
Posts: 60
Joined: Sat Nov 08, 2003 10:29 am
Location: Austria

BriefLZ

Post by javabean »

I haven't found any PB code here in the PB Forum utilizing the static BriefLZ library (and not the user library).
So I coded a little example for this 'Tricks 'n' Tips' section - just to have it here for those who are looking for something like that. Suggestions for improvements are welcome...

Code: Select all

ImportC "brieflz.lib" ; static library taken from http://www.ibsensoftware.com/files/BriefLZ104.zip (..\lib\vc\brieflz.lib)
  blz_pack(*source, *destination, length.l, *workmem) As "_blz_pack"
  blz_depack(*source, *destination, depacked_size.l) As "_blz_depack"
  blz_workmem_size(input_size.l) As "_blz_workmem_size"
  blz_max_packed_size(input_size.l) As "_blz_max_packed_size"
  blz_crc32(*source, length.l, inital_crc32.l) As "_blz_crc32"
EndImport


Procedure.l Pack(sourcefile$, destfile$)
  If ReadFile(0, sourcefile$) 
    length = Lof(0)
    *source = AllocateMemory(length)
    *dest = AllocateMemory(blz_max_packed_size(length))
    *workmem = AllocateMemory(blz_workmem_size(length))
    If *source And *dest And *workmem
      If ReadData(0, *source, length)
        destlen = blz_pack(*source, *dest, length, *workmem)
      Else
        CloseFile(0)
        FreeMemory(*source)
        FreeMemory(*dest)
        FreeMemory(*workmem)
        ProcedureReturn -2
      EndIf
    Else
      CloseFile(0)
      ProcedureReturn -3
    EndIf
    CloseFile(0)
  Else
    ProcedureReturn -4
  EndIf
  
  If CreateFile(0, destfile$)
    If destlen
      WriteData(0, *dest, destlen)
      CloseFile(0)
    Else
      CloseFile(0)
      destlen=-5
    EndIf
  Else
    destlen=-6
  EndIf
  
  FreeMemory(*source)
  FreeMemory(*dest)
  FreeMemory(*workmem)
  ProcedureReturn destlen
EndProcedure


Procedure.l DePack(sourcefile$, destfile$, depacked_size)
  If ReadFile(0, sourcefile$) 
    length = Lof(0)
    *source = AllocateMemory(length)
    *dest = AllocateMemory(depacked_size)
    If *source And *dest
      If ReadData(0, *source, length)
        destlen = blz_depack(*source, *dest, depacked_size)
      Else
        CloseFile(0)
        FreeMemory(*source)
        FreeMemory(*dest)
        ProcedureReturn -2
      EndIf
    Else
      CloseFile(0)
      ProcedureReturn -3
    EndIf
    CloseFile(0)
  Else
    ProcedureReturn -4
  EndIf
  
  If CreateFile(0, destfile$)
    If destlen
      WriteData(0, *dest, destlen)
      CloseFile(0)
    Else
      CloseFile(0)
      destlen=-5
    EndIf
  Else
    destlen=-6
  EndIf
  
  FreeMemory(*source)
  FreeMemory(*dest)
  ProcedureReturn destlen
EndProcedure


Debug Pack("Test.mdb", "Test.mdb.blz"); adjust the filenames!
Debug DePack("Test.mdb.blz", "Test1.mdb",FileSize("Test.mdb")); adjust the filenames!