EnableExplicit

Structure BinaryBufferStruct
  hMem.i
  Blck.i
  Left.i
  RPos.i
  WPos.i
  EndStructure

Procedure.i BinaryBuffer_Create(BlockSize = $2000)
  Define.i xResult
  Define *xType1.BinaryBufferStruct

  *xType1 = LocalAlloc_(0, SizeOf(BinaryBufferStruct))
  If *xType1
    *xType1\hMem = LocalAlloc_(0, BlockSize + 4) ; 4 extrabytes for safety
    If *xType1\hMem
      With *xType1
        \Blck = BlockSize
        \Left = BlockSize
        \WPos = \hMem
        \RPos = \hMem
        EndWith
      xResult = *xType1
      EndIf
    If Not xResult
      LocalFree_(*xType1)
      EndIf
    EndIf
  ProcedureReturn xResult
  EndProcedure

Procedure.i BinaryBuffer_Discard(*hBinWrBuffer.BinaryBufferStruct)
  If *hBinWrBuffer
    If *hBinWrBuffer\hMem
      LocalFree_(*hBinWrBuffer\hMem)
      EndIf
    LocalFree_(*hBinWrBuffer)
    EndIf
  EndProcedure

; IDE Options = PureBasic 5.21 LTS (Windows - x86)
; CursorPosition = 33
; FirstLine = 16
; Folding = -
; EnableUnicode
; EnableXP