; _______________________________________________________________________________________
;|                                                                                       |
;| ..:: Fresh IDE ::..  template project.                                                |
;|_______________________________________________________________________________________|
;
;  Description: FreshLib portable console application.
;
;  Target OS: Any, supported by FreshLib
;
;  Dependencies: FreshLib
;
;  Notes:
;_________________________________________________________________________________________

include "%lib%/freshlib.inc"

@BinaryType console, compact

options.DebugMode = 0
options.ShowSizes = 1
options.SkipZeroSizes = 1


include "%lib%/freshlib.asm"


struc htlen [length] {
common
  local ..start
  label ..start byte
forward
   local flag
   flag equ FALSE
   match count =* len, length \{
     rept count \\{
       db len
     \\}
     flag equ TRUE
   \}

   match =FALSE, flag \{
     db length
   \}

common
  .count = $ - ..start
}


iglobal
; The example from RFC-1951
;  TestArray htlen 5*3, 2, 2*4

;  TestArray htlen 5*2, 3, 2*4  ; invalid array!
;  TestArray htlen 5*3, 3, 2*4  ; invalid array!
;   TestArray htlen 27*0, 1*1, 2*2;, 2*2

  Sum dd ?

endg


start:
        InitializeAll

        stdcall LoadBinaryFile, 'test_inflate.gz'
        jc      .error

        mov     esi, eax
        lea     eax, [8*ecx]

        stdcall FileCreate, 'test_inflate.txt'
        mov     edi, eax

        stdcall GetTimestamp
        mov     ebx, eax

        stdcall Inflate, FileWrite, edi, esi, 80, ecx
        jc      .error

        stdcall GetTimestamp
        sub     eax, ebx

        stdcall NumToStr, eax, ntsUnsigned or ntsDec
        mov     ebx, eax

        stdcall FileWriteString, [STDOUT], 'Inflate processing time: '
        stdcall FileWriteString, [STDOUT], ebx
        stdcall FileWriteString, [STDOUT], cCRLF
        stdcall StrDel, ebx

        jmp     .finish

.error:
        stdcall FileWrite, [STDOUT], cError, cError.length


.finish:
        stdcall FileClose, edi

        FinalizeAll
        stdcall TerminateAll, 0

proc DummyFileWrite, .file, .ptr, .count
begin
        return
endp

cError text "Error inflate file.", 13, 10
cCRLF  text 13, 10