123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638(*---------------------------------------------------------------------------
Copyright (c) 2016 Vincent Bernardoff. All rights reserved.
Distributed under the ISC license, see terms at the end of the file.
msgpck 1.7
---------------------------------------------------------------------------*)moduletypeSTRING=sigtypebuf_intypebuf_outvalget_uint8:buf_in->int->intvalget_int8:buf_in->int->intvalget_uint16:buf_in->int->intvalget_int16:buf_in->int->intvalget_int32:buf_in->int->int32valget_int64:buf_in->int->int64valget_float:buf_in->int->floatvalget_double:buf_in->int->floatvalset_int8:buf_out->int->int->unitvalset_int16:buf_out->int->int->unitvalset_int32:buf_out->int->int32->unitvalset_int64:buf_out->int->int64->unitvalset_double:buf_out->int->float->unitvallength:buf_in->intvalblit:string->int->buf_out->int->int->unitvalsub:buf_in->int->int->stringvalcreate_out:int->buf_outendmoduleSIBO=structtypebuf_in=stringtypebuf_out=Bytes.tincludeEndianString.BigEndianletlength=String.lengthletblit=Bytes.blit_stringletsub=String.subletcreate_out=Bytes.createendmoduleBIBO=structtypebuf_in=Bytes.ttypebuf_out=Bytes.tincludeEndianBytes.BigEndianletlength=Bytes.lengthletblit=Bytes.blit_stringletsub=Bytes.sub_stringletcreate_out=Bytes.createendmoduleSIBUFO=structtypebuf_in=stringtypebuf_out=Buffer.tincludeEndianString.BigEndianletset_int8buf_ii=Buffer.add_int8bufiletset_int16buf_ii=Buffer.add_int16_bebufiletset_int32buf_ii=Buffer.add_int32_bebufiletset_int64buf_ii=Buffer.add_int64_bebufiletset_doublebuf_if=Buffer.add_int64_bebuf(Int64.bits_of_floatf)letlength=String.lengthletblitii_poso_o_poslen=Buffer.add_substringoii_poslenletsub=String.subletcreate_out=Buffer.createendmoduleBIBUFO=structtypebuf_in=Bytes.ttypebuf_out=Buffer.tincludeEndianBytes.BigEndianletset_int8buf_ii=Buffer.add_int8bufiletset_int16buf_ii=Buffer.add_int16_bebufiletset_int32buf_ii=Buffer.add_int32_bebufiletset_int64buf_ii=Buffer.add_int64_bebufiletset_doublebuf_if=Buffer.add_int64_bebuf(Int64.bits_of_floatf)letlength=Bytes.lengthletblitii_poso_o_poslen=Buffer.add_substringoii_poslenletsub=Bytes.sub_stringletcreate_out=Buffer.createendtypet=|Nil|Boolofbool|Intofint|Uint32ofint32|Int32ofint32|Uint64ofint64|Int64ofint64|Float32ofint32|Floatoffloat|Stringofstring|Bytesofstring|Extofint*string|Listoftlist|Mapof(t*t)listletcompare=Stdlib.compareletequal=Stdlib.(=)letrecsize=function|Nil->1|Bool_->1|Inti->size_inti|Int32_|Uint32_|Float32_->5|Int64_|Uint64_|Float_->9|Strings->size_strings|Bytess->size_bytess|Ext(_typ,s)->size_exts|Listl->letnb_written=matchList.lengthlwith|lenwhenlen<=0xf->1|lenwhenlen<=0xffff->3|_->5inList.fold_left(funnbwe->nbw+sizee)nb_writtenl|Mapl->letnb_written=matchList.lengthlwith|lenwhenlen<=0xf->1|lenwhenlen<=0xffff->3|_->5inList.fold_left(funnbw(k,v)->letnbw=nbw+sizekinnbw+sizev)nb_writtenlandsize_inti=matchInt64.of_intiwith|iwheni>=0L&&i<=0x7fL->1|iwheni>=0L&&i<=0xffL->2|iwheni>=0L&&i<=0xffffL->3|iwheni>=0L&&i<=0xffff_ffffL->5|iwheni>=Int64.(sub(neg0x1fL)1L)->1|iwheni>=Int64.(sub(neg0x7fL)1L)->2|iwheni>=Int64.(sub(neg0x7fffL)1L)->3|iwheni>=Int64.(sub(neg0x7fff_ffffL)1L)->5|_->9andsize_stringstr=matchString.lengthstrwith|nwhenn<=0x1f->n+1|nwhenn<=0xff->n+2|nwhenn<=0xffff->n+3|n->n+5andsize_bytesstr=matchString.lengthstrwith|nwhenn<=0xff->n+2|nwhenn<=0xffff->n+3|n->n+5andsize_extstr=matchString.lengthstrwith|1->1+2|2->2+2|4->4+2|8->8+2|16->16+2|nwhenn<=0xff->n+3|nwhenn<=0xffff->n+4|n->n+6letrecppppft=letopenFormatinmatchtwith|Nil->pp_print_stringppf"()"|Boolb->pp_print_boolppfb|Inti->pp_print_intppfi|Uint32i->fprintfppf"%ldul"i|Int32i->fprintfppf"%ldl"i|Uint64i->fprintfppf"%LdUL"i|Int64i->fprintfppf"%LdL"i|Float32f->pp_print_floatppf(Int32.to_floatf)|Floatf->pp_print_floatppff|Strings->pp_print_stringppfs|Bytess->fprintfppf"%S"s|Ext(i,b)->fprintfppf"(%d %S)"ib|Listts->letpp_sepppf()=fprintfppf",@ "infprintfppf"[@[<hov 0>%a@]]"(pp_print_list~pp_seppp)ts|Mapts->letpp_sepppf()=fprintfppf",@ "inletpp_tupleppf(k,v)=fprintfppf"%a:@ %a"ppkppvinfprintfppf"{@[<hov 0>%a@]}"(pp_print_list~pp_seppp_tuple)tsletshowt=Format.asprintf"%a"pptletof_nil=Nilletof_boolb=Boolbletof_inti=Intiletof_uint32i=Uint32iletof_int32i=Int32iletof_uint64i=Uint64iletof_int64i=Int64iletof_float32i=Float32iletof_floatf=Floatfletof_strings=Stringsletof_bytess=Bytessletof_extts=Ext(t,s)letof_listl=Listlletof_mapl=Maplletraise_invalid_argtypv=invalid_arg(Format.asprintf"to_%s: got %a"typppv)letto_nil=functionNil->()|v->raise_invalid_arg"nil"vletto_bool=functionBoolb->b|v->raise_invalid_arg"bool"vletto_int=functionInti->i|v->raise_invalid_arg"int"vletto_uint32=functionUint32i->i|v->raise_invalid_arg"uint32"vletto_int32=functionInt32i->i|v->raise_invalid_arg"int32"vletto_uint64=functionUint64i->i|v->raise_invalid_arg"uint64"vletto_int64=functionInt64i->i|v->raise_invalid_arg"int64"vletto_float32=functionFloat32f->f|v->raise_invalid_arg"float32"vletto_float=functionFloatf->f|v->raise_invalid_arg"float"vletto_string=functionStrings->s|v->raise_invalid_arg"string"vletto_bytes=functionBytesb->b|v->raise_invalid_arg"bytes"vletto_ext=functionExt(t,s)->(t,s)|v->raise_invalid_arg"ext"vletto_list=functionListl->l|v->raise_invalid_arg"list"vletto_map=functionMapl->l|v->raise_invalid_arg"map"vmoduletypeS=sigtypebuf_intypebuf_outvalread:?pos:int->buf_in->int*tvalread_all:?pos:int->buf_in->int*tlistvalwrite:?pos:int->buf_out->t->intvalto_string:t->buf_outendmoduleMake(S:STRING)=structincludeSletwrite_nil?(pos=0)buf=set_int8bufpos0xc0;1letwrite_bool?(pos=0)bufb=set_int8bufpos(ifbthen0xc3else0xc2);1letwrite_float?(pos=0)bufi=set_int8bufpos0xca;set_int32buf(pos+1)i;5letwrite_double?(pos=0)buff=set_int8bufpos0xcb;set_doublebuf(pos+1)f;9letwrite_int?(pos=0)bufv=matchInt64.of_intvwith|iwheni>=0L&&i<=0x7fL->set_int8bufposv;1|iwheni>=0L&&i<=0xffL->set_int16bufpos((0xcclsl8)+v);2|iwheni>=0L&&i<=0xffffL->set_int8bufpos0xcd;set_int16buf(pos+1)v;3|iwheni>=0L&&i<=0xffff_ffffL->set_int8bufpos0xce;set_int32buf(pos+1)@@Int32.of_intv;5|iwheni>=0L->set_int8bufpos0xcf;set_int64buf(pos+1)i;9|iwheni>=Int64.(sub(neg0x1fL)1L)->set_int8bufposv;1|iwheni>=Int64.(sub(neg0x7fL)1L)->set_int8bufpos@@0xd0;set_int8buf(pos+1)v;2|iwheni>=Int64.(sub(neg0x7fffL)1L)->set_int8bufpos0xd1;set_int16buf(pos+1)v;3|iwheni>=Int64.(sub(neg0x7fff_ffffL)1L)->set_int8bufpos0xd2;set_int32buf(pos+1)@@Int32.of_intv;5|i->set_int8bufpos0xd3;set_int64buf(pos+1)i;9letwrite_uint32?(pos=0)bufi=set_int8bufpos0xce;set_int32buf(pos+1)i;5letwrite_uint64?(pos=0)bufi=set_int8bufpos0xcf;set_int64buf(pos+1)i;9letwrite_int32?(pos=0)bufi=set_int8bufpos0xd2;set_int32buf(pos+1)i;5letwrite_int64?(pos=0)bufi=set_int8bufpos0xd3;set_int64buf(pos+1)i;9letwrite_string~src?(src_pos=0)~dst?(dst_pos=0)?src_len()=letlen=matchsrc_lenwithSomel->l|None->String.lengthsrc-src_posinmatchlenwith|nwhenn<=0x1f->set_int8dstdst_pos@@(0xa0lorn);blitsrcsrc_posdst(dst_pos+1)len;len+1|nwhenn<=0xff->set_int16dstdst_pos@@((0xd9lsl8)+n);blitsrcsrc_posdst(dst_pos+2)len;len+2|nwhenn<=0xffff->set_int8dstdst_pos0xda;set_int16dst(dst_pos+1)len;blitsrcsrc_posdst(dst_pos+3)len;len+3|_->set_int8dstdst_pos0xdb;set_int32dst(dst_pos+1)(Int32.of_intlen);blitsrcsrc_posdst(dst_pos+5)len;len+5letwrite_bin~src?(src_pos=0)~dst?(dst_pos=0)?src_len()=letlen=matchsrc_lenwithSomel->l|None->String.lengthsrc-src_posinmatchlenwith|nwhenn<=0xff->set_int16dstdst_pos@@((0xc4lsl8)+n);blitsrcsrc_posdst(dst_pos+2)len;len+2|nwhenn<=0xffff->set_int8dstdst_pos0xc5;set_int16dst(dst_pos+1)len;blitsrcsrc_posdst(dst_pos+3)len;len+3|_->set_int8dstdst_pos0xc6;set_int32dst(dst_pos+1)(Int32.of_intlen);blitsrcsrc_posdst(dst_pos+5)len;len+5letwrite_ext~src?(src_pos=0)~dst?(dst_pos=0)?src_lentyp=letlen=matchsrc_lenwithSomel->l|None->String.lengthsrc-src_posinmatchlenwith|1->set_int16dstdst_pos@@((0xd4lsl8)+typ);blitsrcsrc_posdst(dst_pos+2)len;len+2|2->set_int16dstdst_pos@@((0xd5lsl8)+typ);blitsrcsrc_posdst(dst_pos+2)len;len+2|4->set_int16dstdst_pos@@((0xd6lsl8)+typ);blitsrcsrc_posdst(dst_pos+2)len;len+2|8->set_int16dstdst_pos@@((0xd7lsl8)+typ);blitsrcsrc_posdst(dst_pos+2)len;len+2|16->set_int16dstdst_pos@@((0xd8lsl8)+typ);blitsrcsrc_posdst(dst_pos+2)len;len+2|nwhenn<=0xff->set_int8dstdst_pos0xc7;set_int16dst(dst_pos+1)((nlsl8)+typ);blitsrcsrc_posdst(dst_pos+3)len;len+3|nwhenn<=0xffff->set_int32dstdst_pos((0xc8lsl24)+(lenlsl8)+typ|>Int32.of_int);blitsrcsrc_posdst(dst_pos+4)len;len+4|_->set_int8dstdst_pos0xc9;set_int32dst(dst_pos+1)(Int32.of_intlen);set_int8dst(dst_pos+5)typ;blitsrcsrc_posdst(dst_pos+6)len;len+6letrecwrite?(pos=0)buf=function|Nil->write_nil~posbuf|Boolb->write_bool~posbufb|Inti->write_int~posbufi|Int32i->write_int32~posbufi|Uint32i->write_uint32~posbufi|Int64i->write_int64~posbufi|Uint64i->write_uint64~posbufi|Float32i->write_float~posbufi|Floatf->write_double~posbuff|Strings->write_string~src:s~dst_pos:pos~dst:buf()|Bytess->write_bin~src:s~dst_pos:pos~dst:buf()|Ext(t,d)->write_ext~src:d~dst_pos:pos~dst:buft|Listl->letnb_written=matchList.lengthlwith|lenwhenlen<=0xf->set_int8bufpos@@(0x90lorlen);1|lenwhenlen<=0xffff->set_int8bufpos0xdc;set_int16buf(pos+1)len;3|len->set_int8bufpos0xdd;set_int32buf(pos+1)(Int32.of_intlen);5inList.fold_left(funnbwe->nbw+write~pos:(pos+nbw)bufe)nb_writtenl|Mapl->letnb_written=matchList.lengthlwith|lenwhenlen<=0xf->set_int8bufpos@@(0x80lorlen);1|lenwhenlen<=0xffff->set_int8bufpos0xde;set_int16buf(pos+1)len;3|len->set_int8bufpos0xdf;set_int32buf(pos+1)(Int32.of_intlen);5inList.fold_left(funnbw(k,v)->letnbw=nbw+write~pos:(pos+nbw)bufkinnbw+write~pos:(pos+nbw)bufv)nb_writtenlletto_stringmsg=letbuf=create_out@@sizemsginlet_nb_written:int=writebufmsginbufletmax_int31=Int32.(shift_leftone30|>pred)letmin_int31=Int32.(negmax_int31|>pred)letmax_int31_64=Int64.(shift_leftone30|>pred)letmin_int31_64=Int64.(negmax_int31_64|>pred)letmax_int63=Int64.(shift_leftone62|>pred)letmin_int63=Int64.(negmax_int63|>pred)letparse_int32i=matchSys.word_sizewith|32->ifi>=min_int31&&i<=max_int31thenInt(Int32.to_inti)elseInt32i|64->Int(Int32.to_inti)|_->invalid_arg"Sys.word_size"letparse_uint32i=matchSys.word_sizewith|32->ifi>=0l&&i<=max_int31thenInt(Int32.to_inti)elseUint32i|64->Int(ifi>=0lthenInt32.to_intielse(1lsl32)+Int32.to_inti)|_->invalid_arg"Sys.word_size"letparse_int64i=matchSys.word_sizewith|32->ifi>=min_int31_64&&i<=max_int31_64thenInt(Int64.to_inti)elseInt64i|64->ifi>=min_int63&&i<=max_int63thenInt(Int64.to_inti)elseInt64i|_->invalid_arg"Sys.word_size"letparse_uint64i=matchSys.word_sizewith|32->ifi>=0L&&i<=max_int31_64thenInt(Int64.to_inti)elseUint64i|64->ifi>=0L&&i<=max_int63thenInt(Int64.to_inti)elseUint64i|_->invalid_arg"Sys.word_size"letpairsl=List.fold_left(funacce->matchaccwith|None,acc->(Somee,acc)|Somev,acc->(None,(e,v)::acc))(None,[])l|>sndletget_uint32bufpos=matchget_int32bufposwith|iwheni>=0l->Int64.of_int32i|i->Int64.(add(add0xffff_ffffL1L)(of_int32i))letrecread_n?(pos=0)bufn=letrecinnernbreltsn=ifn>0Lthenletnbr',elt=read~pos:(pos+nbr)bufininner(nbr+nbr')(elt::elts)(Int64.predn)else(nbr,elts)ininner0[]nandread?(pos=0)buf=matchget_uint8bufposwith|iwhenilsr4=0x8->letn=iland0x0finread_n~pos:(pos+1)buf(Int64.of_int(2*n))|>fun(nb_read,elts)->(1+nb_read,Map(pairselts))|iwhenilsr4=0x9->letn=iland0x0finread_n~pos:(pos+1)buf(Int64.of_intn)|>fun(nb_read,elts)->(1+nb_read,List(List.revelts))|0xdc->letn=get_uint16buf(pos+1)inread_n~pos:(pos+3)buf(Int64.of_intn)|>fun(nb_read,elts)->(3+nb_read,List(List.revelts))|0xdd->letn=get_uint32buf(pos+1)inread_n~pos:(pos+5)bufn|>fun(nb_read,elts)->(5+nb_read,List(List.revelts))|0xde->letn=get_uint16buf(pos+1)inread_n~pos:(pos+3)buf(Int64.of_int(2*n))|>fun(nb_read,elts)->(3+nb_read,Map(pairselts))|0xdf->letn=get_uint32buf(pos+1)inread_n~pos:(pos+5)buf(Int64.mul2Ln)|>fun(nb_read,elts)->(5+nb_read,Map(pairselts))(* Atomic types (i.e. non-collection) *)|iwheni<0x80->(1,Int(iland0x7f))|iwhenilsr5=5->letlen=iland0x1fin(succlen,String(subbuf(pos+1)len))|0xc0->(1,Nil)|0xc2->(1,Boolfalse)|0xc3->(1,Booltrue)|0xc4->letlen=get_uint8buf(pos+1)in(len+2,Bytes(subbuf(pos+2)len))|0xc5->letlen=get_uint16buf(pos+1)in(len+3,Bytes(subbuf(pos+3)len))|0xc6->letlen=get_int32buf(pos+1)|>Int32.to_intin(len+5,Bytes(subbuf(pos+5)len))|0xc7->lethdr=get_uint16buf(pos+1)inletlen=hdrlsr8inlettyp=hdrland0xffin(len+3,Ext(typ,subbuf(pos+3)len))|0xc8->letlen=get_uint16buf(pos+1)inlettyp=get_int8buf(pos+3)in(len+4,Ext(typ,subbuf(pos+4)len))|0xc9->letlen=get_int32buf(pos+1)|>Int32.to_intinlettyp=get_int8buf(pos+5)in(len+6,Ext(typ,subbuf(pos+6)len))|0xca->(5,Float(get_floatbuf@@(pos+1)))|0xcb->(9,Float(get_doublebuf@@(pos+1)))|0xcc->(2,Int(get_uint8buf@@(pos+1)))|0xcd->(3,Int(get_uint16buf@@(pos+1)))|0xce->(5,parse_uint32(get_int32buf@@(pos+1)))|0xcf->(9,parse_uint64(get_int64buf@@(pos+1)))|0xd0->(2,Int(get_int8buf@@(pos+1)))|0xd1->(3,Int(get_int16buf@@(pos+1)))|0xd2->(5,parse_int32(get_int32buf@@(pos+1)))|0xd3->(9,parse_int64(get_int64buf@@(pos+1)))|0xd4->(3,lettyp=get_int8buf(pos+1)inExt(typ,subbuf(pos+2)1))|0xd5->(4,lettyp=get_int8buf(pos+1)inExt(typ,subbuf(pos+2)2))|0xd6->(6,lettyp=get_int8buf(pos+1)inExt(typ,subbuf(pos+2)4))|0xd7->(10,lettyp=get_int8buf(pos+1)inExt(typ,subbuf(pos+2)8))|0xd8->(18,lettyp=get_int8buf(pos+1)inExt(typ,subbuf(pos+2)16))|0xd9->letlen=get_uint8buf(pos+1)in(len+2,String(subbuf(pos+2)len))|0xda->letlen=get_uint16buf(pos+1)in(len+3,String(subbuf(pos+3)len))|0xdb->letlen=get_int32buf(pos+1)|>Int32.to_intin(len+5,String(subbuf(pos+5)len))|iwheni>=0xe0->(1,Int(get_int8bufpos))|i->invalid_arg(Printf.sprintf"read: unsupported tag 0x%x"i)letread_all?(pos=0)buf=letlen=lengthbufinletrecinneraccpos=ifpos>=lenthen(pos,List.revacc)elseletn_read,msg=read~posbufinletnew_pos=pos+n_readininner(msg::acc)new_posininner[]posendmoduleString=Make(SIBO)moduleBytes=Make(BIBO)moduleStringBuf=Make(SIBUFO)moduleBytesBuf=Make(BIBUFO)(*---------------------------------------------------------------------------
Copyright (c) 2016 Vincent Bernardoff
Permission to use, copy, modify, and/or 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.
---------------------------------------------------------------------------*)