12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2022 TriliTech <contact@trili.tech> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)moduleArray=Ctypes.CArrayexceptionOut_of_boundstypet={raw:Unsigned.uint8Array.t;min:Unsigned.uint32;max:Unsigned.uint32option;}letlengthmem=Array.lengthmem.rawletbad_boundsmemaddrlen=addr<0||len>lengthmem-addrletcheck_boundsmemaddrlen=ifbad_boundsmemaddrlenthenraiseOut_of_boundsletgetmemaddr=check_boundsmemaddr1;Array.unsafe_getmem.rawaddrletget_stringmem~address~length=check_boundsmemaddresslength;String.initlength@@funi->Array.unsafe_getmem.raw(i+address)|>Unsigned.UInt8.to_int|>Char.chrletsetmemaddrvalue=check_boundsmemaddr1;Array.unsafe_setmem.rawaddrvalueletset_stringmem~address~data=letlen=String.lengthdatainiflen>0then((* For compatibility we only check bounds when something shall be written. *)check_boundsmemaddresslen;foroffset=0tolen-1doString.get_uint8dataoffset|>Unsigned.UInt8.of_int|>Array.unsafe_setmem.raw(offset+address)done)moduleInternal_for_tests=structletof_list(content:Unsigned.uint8list)=letmem_length=List.lengthcontentinletpage_size=0x10000inletpages=mem_length/page_sizeinassert(Int.remmem_lengthpage_size=0);{raw=Array.of_listCtypes.uint8_tcontent;min=Unsigned.UInt32.of_intpages;max=None;}letto_list(mem:t)=Array.to_listmem.rawend