2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
5506
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
*******************************************************************************
|
|
26 |
* Copyright (C) 2003-2004, International Business Machines Corporation and *
|
|
27 |
* others. All Rights Reserved. *
|
|
28 |
*******************************************************************************
|
|
29 |
*/
|
|
30 |
import java.io.IOException;
|
|
31 |
import java.io.InputStream;
|
|
32 |
import java.io.UnsupportedEncodingException;
|
|
33 |
import java.text.ParseException;
|
|
34 |
|
|
35 |
import sun.text.normalizer.ICUData;
|
|
36 |
import sun.net.idn.StringPrep;
|
|
37 |
import sun.text.normalizer.UCharacterIterator;
|
|
38 |
|
|
39 |
/**
|
|
40 |
* @author ram
|
|
41 |
*
|
|
42 |
* This is a dumb implementation of NFS4 profiles. It is a direct port of
|
|
43 |
* C code, does not use Object Oriented principles. Quick and Dirty implementation
|
|
44 |
* for testing.
|
|
45 |
*/
|
|
46 |
public final class NFS4StringPrep {
|
|
47 |
|
|
48 |
private StringPrep nfscss = null;
|
|
49 |
private StringPrep nfscsi = null;
|
|
50 |
private StringPrep nfscis = null;
|
|
51 |
private StringPrep nfsmxp = null;
|
|
52 |
private StringPrep nfsmxs = null;
|
|
53 |
//singleton instance
|
|
54 |
private static final NFS4StringPrep prep = new NFS4StringPrep();
|
|
55 |
|
|
56 |
|
|
57 |
private NFS4StringPrep (){
|
|
58 |
ClassLoader loader = NFS4StringPrep.class.getClassLoader();
|
|
59 |
try{
|
|
60 |
InputStream nfscsiFile = loader.getResourceAsStream("nfscsi.spp");
|
|
61 |
nfscsi = new StringPrep(nfscsiFile);
|
|
62 |
nfscsiFile.close();
|
|
63 |
|
|
64 |
InputStream nfscssFile = loader.getResourceAsStream("nfscss.spp");
|
|
65 |
nfscss = new StringPrep(nfscssFile);
|
|
66 |
nfscssFile.close();
|
|
67 |
|
|
68 |
InputStream nfscisFile = loader.getResourceAsStream("nfscis.spp");
|
|
69 |
nfscis = new StringPrep(nfscisFile);
|
|
70 |
nfscisFile.close();
|
|
71 |
|
|
72 |
InputStream nfsmxpFile = loader.getResourceAsStream("nfsmxp.spp");
|
|
73 |
nfsmxp = new StringPrep(nfsmxpFile);
|
|
74 |
nfsmxpFile.close();
|
|
75 |
|
|
76 |
InputStream nfsmxsFile = loader.getResourceAsStream("nfsmxs.spp");
|
|
77 |
nfsmxs = new StringPrep(nfsmxsFile);
|
|
78 |
nfsmxsFile.close();
|
|
79 |
}catch(IOException e){
|
|
80 |
throw new RuntimeException(e.toString());
|
|
81 |
}
|
|
82 |
}
|
|
83 |
|
|
84 |
private static byte[] prepare(byte[] src, StringPrep prep)
|
|
85 |
throws ParseException, UnsupportedEncodingException{
|
|
86 |
String s = new String(src, "UTF-8");
|
|
87 |
UCharacterIterator iter = UCharacterIterator.getInstance(s);
|
|
88 |
StringBuffer out = prep.prepare(iter,StringPrep.DEFAULT);
|
|
89 |
return out.toString().getBytes("UTF-8");
|
|
90 |
}
|
|
91 |
|
|
92 |
public static byte[] cs_prepare(byte[] src, boolean isCaseSensitive)
|
|
93 |
throws ParseException, UnsupportedEncodingException{
|
|
94 |
if(isCaseSensitive == true ){
|
|
95 |
return prepare(src, prep.nfscss);
|
|
96 |
}else{
|
|
97 |
return prepare(src, prep.nfscsi);
|
|
98 |
}
|
|
99 |
}
|
|
100 |
|
|
101 |
public static byte[] cis_prepare(byte[] src)
|
|
102 |
throws IOException, ParseException, UnsupportedEncodingException{
|
|
103 |
return prepare(src, prep.nfscis);
|
|
104 |
}
|
|
105 |
|
|
106 |
/* sorted array for binary search*/
|
|
107 |
private static final String[] special_prefixes={
|
|
108 |
"ANONYMOUS",
|
|
109 |
"AUTHENTICATED",
|
|
110 |
"BATCH",
|
|
111 |
"DIALUP",
|
|
112 |
"EVERYONE",
|
|
113 |
"GROUP",
|
|
114 |
"INTERACTIVE",
|
|
115 |
"NETWORK",
|
|
116 |
"OWNER",
|
|
117 |
};
|
|
118 |
|
|
119 |
|
|
120 |
/* binary search the sorted array */
|
|
121 |
private static final int findStringIndex(String[] sortedArr,String target){
|
|
122 |
|
|
123 |
int left, middle, right,rc;
|
|
124 |
|
|
125 |
left =0;
|
|
126 |
right= sortedArr.length-1;
|
|
127 |
|
|
128 |
while(left <= right){
|
|
129 |
middle = (left+right)/2;
|
|
130 |
rc= sortedArr[middle].compareTo(target);
|
|
131 |
|
|
132 |
if(rc<0){
|
|
133 |
left = middle+1;
|
|
134 |
}else if(rc >0){
|
|
135 |
right = middle -1;
|
|
136 |
}else{
|
|
137 |
return middle;
|
|
138 |
}
|
|
139 |
}
|
|
140 |
return -1;
|
|
141 |
}
|
|
142 |
private static final char AT_SIGN = '@';
|
|
143 |
|
|
144 |
public static byte[] mixed_prepare(byte[] src)
|
|
145 |
throws IOException, ParseException, UnsupportedEncodingException{
|
|
146 |
String s = new String(src, "UTF-8");
|
|
147 |
int index = s.indexOf(AT_SIGN);
|
|
148 |
StringBuffer out = new StringBuffer();
|
|
149 |
|
|
150 |
if(index > -1){
|
|
151 |
/* special prefixes must not be followed by suffixes! */
|
|
152 |
String prefixString = s.substring(0,index);
|
|
153 |
int i= findStringIndex(special_prefixes, prefixString);
|
|
154 |
String suffixString = s.substring(index+1, s.length());
|
|
155 |
if(i>-1 && !suffixString.equals("")){
|
|
156 |
throw new ParseException("Suffix following a special index", -1);
|
|
157 |
}
|
|
158 |
UCharacterIterator prefix = UCharacterIterator.getInstance(prefixString);
|
|
159 |
UCharacterIterator suffix = UCharacterIterator.getInstance(suffixString);
|
|
160 |
out.append(prep.nfsmxp.prepare(prefix,StringPrep.DEFAULT));
|
|
161 |
out.append(AT_SIGN); // add the delimiter
|
|
162 |
out.append(prep.nfsmxs.prepare(suffix, StringPrep.DEFAULT));
|
|
163 |
}else{
|
|
164 |
UCharacterIterator iter = UCharacterIterator.getInstance(s);
|
|
165 |
out.append(prep.nfsmxp.prepare(iter,StringPrep.DEFAULT));
|
|
166 |
|
|
167 |
}
|
|
168 |
return out.toString().getBytes("UTF-8");
|
|
169 |
}
|
|
170 |
|
|
171 |
}
|