00001 /******************************************************************************* 00002 00003 @file UStringPrep.d 00004 00005 Copyright (c) 2004 Kris Bell 00006 00007 This software is provided 'as-is', without any express or implied 00008 warranty. In no event will the authors be held liable for damages 00009 of any kind arising from the use of this software. 00010 00011 Permission is hereby granted to anyone to use this software for any 00012 purpose, including commercial applications, and to alter it and/or 00013 redistribute it freely, subject to the following restrictions: 00014 00015 1. The origin of this software must not be misrepresented; you must 00016 not claim that you wrote the original software. If you use this 00017 software in a product, an acknowledgment within documentation of 00018 said product would be appreciated but is not required. 00019 00020 2. Altered source versions must be plainly marked as such, and must 00021 not be misrepresented as being the original software. 00022 00023 3. This notice may not be removed or altered from any distribution 00024 of the source. 00025 00026 4. Derivative works are permitted, but they must carry this notice 00027 in full and credit the original source. 00028 00029 00030 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00031 00032 00033 @version Initial version, November 2004 00034 @author Kris 00035 00036 Note that this package and documentation is built around the ICU 00037 project (http://oss.software.ibm.com/icu/). Below is the license 00038 statement as specified by that software: 00039 00040 00041 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00042 00043 00044 ICU License - ICU 1.8.1 and later 00045 00046 COPYRIGHT AND PERMISSION NOTICE 00047 00048 Copyright (c) 1995-2003 International Business Machines Corporation and 00049 others. 00050 00051 All rights reserved. 00052 00053 Permission is hereby granted, free of charge, to any person obtaining a 00054 copy of this software and associated documentation files (the 00055 "Software"), to deal in the Software without restriction, including 00056 without limitation the rights to use, copy, modify, merge, publish, 00057 distribute, and/or sell copies of the Software, and to permit persons 00058 to whom the Software is furnished to do so, provided that the above 00059 copyright notice(s) and this permission notice appear in all copies of 00060 the Software and that both the above copyright notice(s) and this 00061 permission notice appear in supporting documentation. 00062 00063 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00064 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00065 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT 00066 OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 00067 HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL 00068 INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING 00069 FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 00070 NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 00071 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00072 00073 Except as contained in this notice, the name of a copyright holder 00074 shall not be used in advertising or otherwise to promote the sale, use 00075 or other dealings in this Software without prior written authorization 00076 of the copyright holder. 00077 00078 ---------------------------------------------------------------------- 00079 00080 All trademarks and registered trademarks mentioned herein are the 00081 property of their respective owners. 00082 00083 *******************************************************************************/ 00084 00085 module mango.icu.UStringPrep; 00086 00087 private import mango.icu.ICU, 00088 mango.icu.UString; 00089 00090 /******************************************************************************* 00091 00092 StringPrep API implements the StingPrep framework as described 00093 by RFC 3454. 00094 00095 StringPrep prepares Unicode strings for use in network protocols. 00096 Profiles of StingPrep are set of rules and data according to with 00097 the Unicode Strings are prepared. Each profiles contains tables 00098 which describe how a code point should be treated. The tables are 00099 broadly classied into 00100 00101 - Unassinged Table: Contains code points that are unassigned 00102 in the Unicode Version supported by StringPrep. Currently 00103 RFC 3454 supports Unicode 3.2. 00104 00105 - Prohibited Table: Contains code points that are prohibted 00106 from the output of the StringPrep processing function. 00107 00108 - Mapping Table: Contains code ponts that are deleted from the 00109 output or case mapped. 00110 00111 The procedure for preparing Unicode strings: 00112 00113 1. Map: For each character in the input, check if it has a mapping 00114 and, if so, replace it with its mapping. 00115 00116 2. Normalize: Possibly normalize the result of step 1 using Unicode 00117 normalization. 00118 00119 3. Prohibit: Check for any characters that are not allowed in the 00120 output. If any are found, return an error. 00121 00122 4. Check bidi: Possibly check for right-to-left characters, and if 00123 any are found, make sure that the whole string satisfies the 00124 requirements for bidirectional strings. If the string does not 00125 satisfy the requirements for bidirectional strings, return an 00126 error. 00127 00128 See <A HREF="http://oss.software.ibm.com/icu/apiref/usprep_8h.html"> 00129 this page</A> for full details. 00130 00131 *******************************************************************************/ 00132 00133 class UStringPrep : ICU 00134 { 00135 private Handle handle; 00136 00137 enum Options 00138 { 00139 Strict, 00140 Lenient 00141 } 00142 00143 00144 /*********************************************************************** 00145 00146 Creates a StringPrep profile from the data file. 00147 00148 path string containing the full path pointing 00149 to the directory where the profile reside 00150 followed by the package name e.g. 00151 "/usr/resource/my_app/profiles/mydata" on 00152 a Unix system. if NULL, ICU default data 00153 files will be used. 00154 00155 fileName name of the profile file to be opened 00156 00157 ***********************************************************************/ 00158 00159 this (char[] path, char[] filename) 00160 { 00161 Error e; 00162 00163 handle = usprep_open (toString(path), toString(filename), e); 00164 testError (e, "failed to open string-prep"); 00165 } 00166 00167 /*********************************************************************** 00168 00169 Close this profile 00170 00171 ***********************************************************************/ 00172 00173 ~this () 00174 { 00175 usprep_close (handle); 00176 } 00177 00178 /*********************************************************************** 00179 00180 Prepare the input buffer 00181 00182 This operation maps, normalizes(NFKC), checks for prohited 00183 and BiDi characters in the order defined by RFC 3454 depending 00184 on the options specified in the profile 00185 00186 ***********************************************************************/ 00187 00188 void prepare (UText src, UString dst, Options o = Options.Strict) 00189 { 00190 uint fmt (wchar* p, uint len, inout Error e) 00191 { 00192 return usprep_prepare (handle, src.get, src.len, p, len, o, null, e); 00193 } 00194 00195 dst.format (&fmt, "failed to prepare text"); 00196 } 00197 00198 00199 /*********************************************************************** 00200 00201 Bind the ICU functions from a shared library. This is 00202 complicated by the issues regarding D and DLLs on the 00203 Windows platform 00204 00205 ***********************************************************************/ 00206 00207 private static void* library; 00208 00209 /*********************************************************************** 00210 00211 ***********************************************************************/ 00212 00213 private static extern (C) 00214 { 00215 Handle function (char*, char*, inout Error) usprep_open; 00216 void function (Handle) usprep_close; 00217 uint function (Handle, wchar*, uint, wchar*, uint, uint, void*, inout Error) usprep_prepare; 00218 } 00219 00220 /*********************************************************************** 00221 00222 ***********************************************************************/ 00223 00224 static FunctionLoader.Bind[] targets = 00225 [ 00226 {cast(void**) &usprep_open, "usprep_open"}, 00227 {cast(void**) &usprep_close, "usprep_close"}, 00228 {cast(void**) &usprep_prepare, "usprep_prepare"}, 00229 ]; 00230 00231 /*********************************************************************** 00232 00233 ***********************************************************************/ 00234 00235 static this () 00236 { 00237 library = FunctionLoader.bind (icuuc, targets); 00238 } 00239 00240 /*********************************************************************** 00241 00242 ***********************************************************************/ 00243 00244 static ~this () 00245 { 00246 FunctionLoader.unbind (library); 00247 } 00248 } 00249