#Include once "window9.bi"

'*******************************************************************************************
Function CompressMem(Byref BUF_DEST As Byte Ptr,byval SOURSEDATA As Byte ptr,Byval SIZEDATA As UInteger, ByVal level As Integer=5) As Integer Export
   Dim As Integer LEN_DEST=compressBound(SIZEDATA)
   BUF_DEST=Allocate(LEN_DEST)
   compress2(BUF_DEST,@LEN_DEST,SOURSEDATA, SIZEDATA,level)
   BUF_DEST=ReAllocate(BUF_DEST,LEN_DEST+4)
   RtlMoveMemory(BUF_DEST+LEN_DEST,@SIZEDATA,4)
   Function=LEN_DEST+4
End Function
'*******************************************************************************************
Function DeCompressMem(Byref BUF_COMPRESSED As Byte Ptr,byval SIZECOMPRESSED As integer,ByRef BUFDESTDATA As Byte Ptr) As Integer Export
   Dim LEN_SRC As Integer ptr=Cast(Integer Ptr,BUF_COMPRESSED+SIZECOMPRESSED-4)
   BUFDESTDATA=ReAllocate(BUFDESTDATA,*LEN_SRC )
   uncompress(BUFDESTDATA, Cast(uLongf Ptr,@LEN_SRC), BUF_COMPRESSED, SIZECOMPRESSED-4)
   Return Cast(Integer,LEN_SRC)
End Function

'*******************************************************************************************
Function CompressFile(ByVal filename As String, ByVal filenameDest As String,ByVal level As Integer=5) As Integer Export
  Dim As HANDLE handle,handle1
  Dim As Uinteger numcikl,ostatok,sizesourse,endsize,fullend,SizeReadWrite
  Dim As Byte Ptr bufsourse,bufdest
  handle=Read_File(FileName)
  If handle<>-1 Then
    sizesourse=Size_File(handle)
    If sizesourse>10000000 Then
      numcikl=sizesourse\10000000
      ostatok= sizesourse Mod 10000000
      Dim As Integer massvalue(numcikl)
      handle1=Create_File(filenameDest)
      If handle1<>-1 Then
        bufdest=Allocate(10000000)
        bufsourse=Allocate(10000000)
        For a As Integer =1 To numcikl
          ReadFile(Handle,bufsourse,10000000,Cast(LPDWORD,@SizeReadWrite),0)
          endsize=CompressMem(bufdest,bufsourse,10000000,level)
          fullend=fullend+endsize
          massvalue(a)=endsize
          Write_Data(handle1,Cast(Integer,bufdest),endsize)
        Next

        If ostatok>0 Then
          ReadFile(Handle,bufsourse,ostatok,Cast(LPDWORD,@SizeReadWrite),0)
          endsize=CompressMem(bufdest,bufsourse,ostatok,level)
          fullend=fullend+endsize
          Write_Data(handle1,Cast(Integer,bufdest),endsize)
        Else
          endsize=0
        EndIf
        For a As Integer =1 To numcikl
          Write_Integer(handle1,massvalue(a))
        Next
        Write_Integer(handle1,endsize)
        Write_Integer(handle1,numcikl)
        Close_File(handle1)
        Function=fullend+numcikl*4+8
      endif
    Else
      bufsourse=Allocate(sizesourse)
      ReadFile(Handle,bufsourse,sizesourse,Cast(LPDWORD,@SizeReadWrite),0)
      bufdest=Allocate(sizesourse)
      endsize=CompressMem(bufdest,bufsourse,sizesourse,level)
      handle1=Create_File(filenameDest)
      If handle1<>-1 Then
        Write_Data(handle1,Cast(Integer,bufdest),endsize)
        Write_Integer(handle1,0)
        Close_File(handle1)
        Function=endsize+4
      EndIf
    EndIf
    Close_file(handle)
  EndIf
  DeAllocate(bufsourse)
  DeAllocate(bufdest)
End Function
'*******************************************************************************************
Function DeCompressFile(ByVal filename As String, ByVal filenameDest As String) As Integer export
  Dim As HANDLE handle,handle1
  Dim As integer numcikl,ostatok,sizesourse,endsize,fullend,SizeReadWrite
  Dim As Byte Ptr bufsourse,bufdest
  handle=Read_File(FileName)
  If handle<>-1 Then
    Set_File_Pointer(handle,-8,FILE_END)
    ostatok=Read_Integer(handle)
    numcikl=Read_Integer(handle)
    If numcikl<>0 Then
      handle1=Create_File(filenameDest)
      If handle1<>-1 Then
        Dim As UInteger massvalue(numcikl+1)
        Set_File_Pointer(handle,-(numcikl*4+8),FILE_END)
        For a As Integer=1 To numcikl
          massvalue(a)=Read_Integer(handle)
        Next
        If ostatok<>0 Then
          massvalue(numcikl+1)=ostatok
          massvalue(0)=numcikl+1
        Else
          massvalue(0)=numcikl
        EndIf
        Set_File_Pointer(handle,0,FILE_Begin)
        bufsourse=Allocate(15000000)
        bufdest=Allocate(10000000)
        For a As Integer=1 To massvalue(0)
          ReadFile(Handle,bufsourse,massvalue(a),Cast(LPDWORD,@SizeReadWrite),0)
          endsize=DeCompressMem(bufsourse,massvalue(a),bufdest)
          Write_Data(handle1,Cast(Integer,bufdest),endsize)
          fullend+=endsize
        Next
        Close_File(handle1)
      EndIf
    Else
      handle1=Create_File(filenameDest)
      If handle1<>-1 Then
        sizesourse=Size_File(handle)-4 
        Set_File_Pointer(handle,0,FILE_BEGIN)
        bufsourse=Allocate(sizesourse)
        ReadFile(Handle,bufsourse,sizesourse,Cast(LPDWORD,@SizeReadWrite),0)
        bufdest=Allocate(sizesourse)
        endsize=DeCompressMem(bufsourse,sizesourse,bufdest)              
        Write_Data(handle1,Cast(Integer,bufdest),endsize)
        Close_File(handle1)
        fullend=endsize
      EndIf
    EndIf
    Close_File(handle)
  EndIf
  DeAllocate(bufdest)
  DeAllocate(bufsourse)
  Return fullend
End Function
'*******************************************************************************************




