; Copyright (C) 2004-2006 Jan Vorel
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
;
; This is a shared modul, class modul, usercontrol or form. It is also
; published under the GNU General Public License.
;
; If you have any questions, suggestions or bug reports about this product,
; contact me: info@ctuser.net; Do not contact me about general B.A.S.I.C.
; language or A.P.I. issues

EnableExplicit

#CodeName = "ctCompressA_Decompress.dasm"

Goto xSkip
#HeapPos_ptrDataIn        =  $0
#HeapPos_ptrDataOut       =  $4
#HeapPos_ProgressHandler  =  $8
#HeapPos_ProgressControl  =  $C
#HeapPos_ProgressStart    = $10
#HeapPos_ProgressTotal    = $14
#HeapPos_ProgressQueue    = $18

#HeapPos_X                = $1C
#HeapPos_Y                = $20
#HeapPos_OrgLen_ByteValue = $24
#HeapPos_NumberOfNodes    = $28
#HeapPos_NuNode           = $2C
#HeapPos_TotBits          = $30
#HeapPos_TelBits          = $34
#HeapPos_Nulen            = $38

#HeapPos_LenBits = #HeapPos_Nulen
#HeapPos_Bits = #HeapPos_LenBits +     4*256
#HeapPos_LeafA = #HeapPos_Bits +       32*256
#HeapPos_LeafB = #HeapPos_LeafA +      4*256

#HeapPos_TreeNodesA = #HeapPos_LeafB + 4*256
#HeapPos_TreeNodesB = #HeapPos_TreeNodesA + 4*512
#HeapPos_TreeNodesC = #HeapPos_TreeNodesB + 4*512
#HeapPos_TreeNodesE = #HeapPos_TreeNodesC + 4*512

#Size = #HeapPos_TreeNodesE + 4*512

ctCompressA_Decompress:
IncludeBinary("ctCompressA_Decompress.bin")


xSkip:
;#Size = 9*8 + 11*4 + 8 + 4*256 + 4*256
Procedure Prog(HWndProgress.l, ProgressPosition, ProgressTotal)
  ;End
  Static xLong1.l
  Define.l xLong2 = GetTickCount_()
  
  If xLong2 - xLong1 > 1000
    OutputDebugString_("Progress: " + Str(ProgressPosition) + " / " + Str(ProgressTotal))
    xLong1 = xLong2
    EndIf
  ProcedureReturn 0
  ;Debug ProgressPosition
  EndProcedure

Define.l MemOutSize = PeekL(?TestData + 9)
MOV eax, MemOutSize
BSWAP eax
MOV MemOutSize, eax
Debug MemOutSize
Define *Heap = AllocateMemory(#Size)
Define *Mem = AllocateMemory(MemOutSize)


If PeekB(?TestData)
  PokeL(*Heap + #HeapPos_ptrDataIn, ?TestData + 13)
  PokeL(*Heap + #HeapPos_ptrDataOut, *Mem)
  PokeL(*Heap + #HeapPos_ProgressHandler, @Prog())
  PokeL(*Heap + #HeapPos_ProgressControl, 1)
  PokeL(*Heap + #HeapPos_ProgressStart, 0)
  PokeL(*Heap + #HeapPos_ProgressTotal, MemOutSize)
  PokeL(*Heap + #HeapPos_ProgressQueue, MemOutSize)
  
  OutputDebugString_("Start...")
  Define.l xL = GetTickCount_()
  Define.l xLong1 = CallFunctionFast(?ctCompressA_Decompress, 0, 0, *Heap, 0)
  OutputDebugString_("Time: " + Str(GetTickCount_() - xL))
  OutputDebugString_("done: " + Hex(xLong1))
  
  ;Debug PeekS(*Mem, xLong1)
  ;Debug Hex(Len(PeekS(*Mem, xLong1)))
  ;Debug CallFunctionFast(?PowerTwo, 8)
  EndIf

CreateFile(1, "c:\c.new")
WriteData(1, *Mem, MemOutSize)
CloseFile(1)


DataSection
TestData:
  IncludeBinary "c:\c.bin"
TestDataD:
  EndDataSection
  
  
  
  

; IDE Options = PureBasic 4.51 (Windows - x86)
; CursorPosition = 72
; FirstLine = 47
; Folding = -
; EnableAsm
; Executable = d90.exe
; DisableDebugger