123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354(*
* Copyright (C) 2015 David Scott <dave.scott@unikernel.com>
* Copyright (C) 2016 Docker Inc
*
* 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=Cstruct.tlistletpp_tppft=List.iter(funt->Format.fprintfppf"[%d,%d](%d)"t.Cstruct.offt.Cstruct.len(Bigarray.Array1.dimt.Cstruct.buffer))tletlen=List.fold_left(funaccc->Cstruct.lengthc+acc)0leterrfmt=letb=Buffer.create20in(* for thread safety. *)letppf=Format.formatter_of_bufferbinletkppf=Format.pp_print_flushppf();invalid_arg(Buffer.contentsb)inFormat.kfprintfkppffmtletrecshifttx=ifx=0thentelsematchtwith|[]->err"Cstructs.shift %a %d"pp_ttx|y::ys->lety'=Cstruct.lengthyinify'>xthenCstruct.shiftyx::yselseshiftys(x-y')letsubtofflen=lett'=shifttoffin(* trim the length *)letrectrimacctsremaining=matchremaining,tswith|0,_->List.revacc|_,[]->err"invalid bounds in Cstructs.sub %a off=%d len=%d"pp_ttofflen|n,t::ts->letto_take=min(Cstruct.lengtht)nin(* either t is consumed and we only need ts, or t has data remaining in which
case we're finished *)trim(Cstruct.subt0to_take::acc)ts(remaining-to_take)intrim[]t'len