123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249# 1 "Camomile/public/stringPrep.ml"(* Copyright (C) 2010 Pierre Chambart *)(* This library is free software; you can redistribute it and/or *)(* modify it under the terms of the GNU Lesser General Public License *)(* as published by the Free Software Foundation; either version 2 of *)(* the License, or (at your option) any later version. *)(* As a special exception to the GNU Library General Public License, you *)(* may link, statically or dynamically, a "work that uses this library" *)(* with a publicly distributed version of this library to produce an *)(* executable file containing portions of this library, and distribute *)(* that executable file under terms of your choice, without any of the *)(* additional requirements listed in clause 6 of the GNU Library General *)(* Public License. By "a publicly distributed version of this library", *)(* we mean either the unmodified Library as distributed by the authors, *)(* or a modified version of this library that is distributed under the *)(* conditions defined in clause 3 of the GNU Library General Public *)(* License. This exception does not however invalidate any other reasons *)(* why the executable file might be covered by the GNU Library General *)(* Public License . *)(* This library is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *)(* Lesser General Public License for more details. *)(* You should have received a copy of the GNU Lesser General Public *)(* License along with this library; if not, write to the Free Software *)(* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *)(* USA *)(* You can contact the authour by sending email to *)(* yoriyuki.y@gmail.com *)moduletypeType=sigtypetextexceptionProhibitedofUChar.texceptionBad_biditypeprofile=[`Nameprep(** RFC 3491 *)|`Nodeprep(** RFC 3920, Appendix A *)|`Resourceprep(** RFC 3920, Appendix B *)|`Saslprep(** RFC 4013 *)|`Trace(** for SASL Anonymous, RFC 4505, Section 3 *)|`Iscsi(** RFC 3722 *)|`Mib(** RFC 4011 *)]valstringprep:profile->text->textendmoduleMake(Config:ConfigInt.Type)(Text:UnicodeString.Type):Typewithtypetext=Text.t=structmoduleUNF=UNF.Make(Config)(Text)moduleUCharInfo=UCharInfo.Make(Config)moduleStringPrep_data'=StringPrep_data.Make(Config)openStringPrep_data'typetext=Text.texceptionProhibitedofUChar.texceptionBad_biditypenormalisation=[`C|`KC|`D|`KD|`No]typeprofile=[`Nameprep(** RFC 3491 *)|`Nodeprep(** RFC 3920, Appendix A *)|`Resourceprep(** RFC 3920, Appendix B *)|`Saslprep(** RFC 4013 *)|`Trace(** for SASL Anonymous, RFC 4505, Section 3 *)|`Iscsi(** RFC 3722 *)|`Mib(** RFC 4011 *)]typeinternal_profile={map:UChar.t->UChar.tlist;normalize:normalisation;prohibited:UChar.t->bool;check_bidi:bool;unicode_version:UCharInfo.version_type;bidi_ral:UChar.t->bool;bidi_l:UChar.t->bool;}letmake_mapmap=letfx=letm=StringPrep_data.MappingMap.getmapxinStringPrep_data.mapping_to_listxminfletmake_setset=letfx=UCharTbl.Bool.getsetxinfletnodeprep()={map=make_map(map_b1b2());normalize=`KC;prohibited=make_set(nodeprep_prohibited());check_bidi=true;unicode_version=`v3_2;bidi_ral=make_set(d1());bidi_l=make_set(d2())}letresourceprep()={map=make_map(map_b1());normalize=`KC;prohibited=make_set(resourceprep_prohibited());check_bidi=true;unicode_version=`v3_2;bidi_ral=make_set(d1());bidi_l=make_set(d2())}letnameprep()={map=make_map(map_b1b2());normalize=`KC;prohibited=make_set(nameprep_prohibited());check_bidi=true;unicode_version=`v3_2;bidi_ral=make_set(d1());bidi_l=make_set(d2())}letsaslprep()={map=make_map(saslprep_map());normalize=`KC;prohibited=make_set(saslprep_prohibited());check_bidi=true;unicode_version=`v3_2;bidi_ral=make_set(d1());bidi_l=make_set(d2())}lettrace()={map=(funx->[x]);normalize=`No;prohibited=make_set(trace_prohibited());check_bidi=true;unicode_version=`v3_2;bidi_ral=make_set(d1());bidi_l=make_set(d2())}letiscsi()={map=make_map(map_b1b2());normalize=`KC;prohibited=make_set(iscsi_prohibited());check_bidi=true;unicode_version=`v3_2;bidi_ral=make_set(d1());bidi_l=make_set(d2())}(** rfc 4011 *)letmib()={map=make_map(map_b1());normalize=`KC;prohibited=make_set(mib_prohibited());check_bidi=false;unicode_version=`v3_2;bidi_ral=(fun_->false);bidi_l=(fun_->false)}letto_internal_profile:profile->unit->internal_profile=function|`Nameprep->nameprep|`Nodeprep->nodeprep|`Resourceprep->resourceprep|`Saslprep->saslprep|`Trace->trace|`Iscsi->iscsi|`Mib->mibletis_correct_bidiprofiletext=letis_rand_al_catindex=profile.bidi_ral(Text.looktextindex)inletis_lcatindex=profile.bidi_l(Text.looktextindex)inletreccheck_rand_al_catindex=letnext=Text.nexttextindexinifText.out_of_rangetextnextthenis_rand_al_catindexelseifis_lcatindexthenfalseelsecheck_rand_al_catnextinletreccheck_not_rand_al_catindex=ifis_rand_al_catindexthenfalseelseletnext=Text.nexttextindexinifText.out_of_rangetextnextthentrueelsecheck_not_rand_al_catnextinletfirst=Text.firsttextinifText.out_of_rangetextfirstthen(* empty text *)trueelseifis_rand_al_catfirstthencheck_rand_al_catfirstelsecheck_not_rand_al_catfirstletnormalisation:normalisation->text->text=function|`C->UNF.nfc|`KC->UNF.nfkc|`D->UNF.nfd|`KD->UNF.nfkd|`No->(funx->x)letstringprepprofiletext=letprofile=to_internal_profileprofile()inletbuffer=Text.Buf.create10inletadd_char=Text.Buf.add_charbufferinletmapc=List.iteradd_char(profile.mapc)inText.itermaptext;lettext=Text.Buf.contentsbufferinText.Buf.clearbuffer;lettext=normalisationprofile.normalizetextinletreccheck_prohibitedindex=ifText.out_of_rangetextindexthen()elsebeginletchar=(Text.looktextindex)inletprohibited=(not(UCharInfo.older(UCharInfo.agechar)profile.unicode_version))||(profile.prohibitedchar)inifprohibitedthenraise(Prohibited(Text.looktextindex))elsecheck_prohibited(Text.nexttextindex)endincheck_prohibited(Text.firsttext);ifprofile.check_bidithenbeginifis_correct_bidiprofiletextthentextelseraiseBad_bidiendelsetextend