123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960(*
* Copyright (c) 2017 Christiano F. Haesbaert <haesbaert@haesbaert.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)typet={tlen:int;coff:int;cbuf:Cstruct.t;}letchunk_size=1024letcreate()={tlen=chunk_size;coff=0;cbuf=Cstruct.createchunk_size}letto_cstructt=Cstruct.subt.cbuf0t.coffletleftt=t.tlen-t.coffletusedt=t.coffletgrowlent=lettlen=t.tlen+leninletcbuf=Cstruct.appendt.cbuf(Cstruct.createlen)in{twithtlen;cbuf}letguard_spacelent=if(leftt)>=lenthentelsegrow(maxlenchunk_size)tletshiftnt={twithcoff=t.coff+n}letreservent=shiftntletput_uint8bt=lett=guard_space1tinCstruct.set_uint8t.cbuft.coffb;shift1tletput_uint32_bewt=lett=guard_space4tinCstruct.BE.set_uint32t.cbuft.coffw;shift4tletput_uint32_lewt=lett=guard_space4tinCstruct.LE.set_uint32t.cbuft.coffw;shift4t