Path: blob/master/test/jdk/sun/net/idn/NFS4StringPrep.java
41152 views
/*1* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24*******************************************************************************25* Copyright (C) 2003-2004, International Business Machines Corporation and *26* others. All Rights Reserved. *27*******************************************************************************28*/29import java.io.IOException;30import java.io.InputStream;31import java.io.UnsupportedEncodingException;32import java.text.ParseException;3334import jdk.internal.icu.text.StringPrep;35import jdk.internal.icu.text.UCharacterIterator;3637/**38* @author ram39*40* This is a dumb implementation of NFS4 profiles. It is a direct port of41* C code, does not use Object Oriented principles. Quick and Dirty implementation42* for testing.43*/44public final class NFS4StringPrep {4546private StringPrep nfscss = null;47private StringPrep nfscsi = null;48private StringPrep nfscis = null;49private StringPrep nfsmxp = null;50private StringPrep nfsmxs = null;51//singleton instance52private static final NFS4StringPrep prep = new NFS4StringPrep();535455private NFS4StringPrep (){56ClassLoader loader = NFS4StringPrep.class.getClassLoader();57try{58InputStream nfscsiFile = loader.getResourceAsStream("nfscsi.spp");59nfscsi = new StringPrep(nfscsiFile);60nfscsiFile.close();6162InputStream nfscssFile = loader.getResourceAsStream("nfscss.spp");63nfscss = new StringPrep(nfscssFile);64nfscssFile.close();6566InputStream nfscisFile = loader.getResourceAsStream("nfscis.spp");67nfscis = new StringPrep(nfscisFile);68nfscisFile.close();6970InputStream nfsmxpFile = loader.getResourceAsStream("nfsmxp.spp");71nfsmxp = new StringPrep(nfsmxpFile);72nfsmxpFile.close();7374InputStream nfsmxsFile = loader.getResourceAsStream("nfsmxs.spp");75nfsmxs = new StringPrep(nfsmxsFile);76nfsmxsFile.close();77}catch(IOException e){78throw new RuntimeException(e.toString());79}80}8182private static byte[] prepare(byte[] src, StringPrep prep)83throws ParseException, UnsupportedEncodingException{84String s = new String(src, "UTF-8");85UCharacterIterator iter = UCharacterIterator.getInstance(s);86StringBuffer out = prep.prepare(iter,StringPrep.DEFAULT);87return out.toString().getBytes("UTF-8");88}8990public static byte[] cs_prepare(byte[] src, boolean isCaseSensitive)91throws ParseException, UnsupportedEncodingException{92if(isCaseSensitive == true ){93return prepare(src, prep.nfscss);94}else{95return prepare(src, prep.nfscsi);96}97}9899public static byte[] cis_prepare(byte[] src)100throws IOException, ParseException, UnsupportedEncodingException{101return prepare(src, prep.nfscis);102}103104/* sorted array for binary search*/105private static final String[] special_prefixes={106"ANONYMOUS",107"AUTHENTICATED",108"BATCH",109"DIALUP",110"EVERYONE",111"GROUP",112"INTERACTIVE",113"NETWORK",114"OWNER",115};116117118/* binary search the sorted array */119private static final int findStringIndex(String[] sortedArr,String target){120121int left, middle, right,rc;122123left =0;124right= sortedArr.length-1;125126while(left <= right){127middle = (left+right)/2;128rc= sortedArr[middle].compareTo(target);129130if(rc<0){131left = middle+1;132}else if(rc >0){133right = middle -1;134}else{135return middle;136}137}138return -1;139}140private static final char AT_SIGN = '@';141142public static byte[] mixed_prepare(byte[] src)143throws IOException, ParseException, UnsupportedEncodingException{144String s = new String(src, "UTF-8");145int index = s.indexOf(AT_SIGN);146StringBuffer out = new StringBuffer();147148if(index > -1){149/* special prefixes must not be followed by suffixes! */150String prefixString = s.substring(0,index);151int i= findStringIndex(special_prefixes, prefixString);152String suffixString = s.substring(index+1, s.length());153if(i>-1 && !suffixString.equals("")){154throw new ParseException("Suffix following a special index", -1);155}156UCharacterIterator prefix = UCharacterIterator.getInstance(prefixString);157UCharacterIterator suffix = UCharacterIterator.getInstance(suffixString);158out.append(prep.nfsmxp.prepare(prefix,StringPrep.DEFAULT));159out.append(AT_SIGN); // add the delimiter160out.append(prep.nfsmxs.prepare(suffix, StringPrep.DEFAULT));161}else{162UCharacterIterator iter = UCharacterIterator.getInstance(s);163out.append(prep.nfsmxp.prepare(iter,StringPrep.DEFAULT));164165}166return out.toString().getBytes("UTF-8");167}168169}170171172