private static string percentDecode(string str, string encoding) { int len=str.Length; bool percent=false; for(int i=0;i<len;i++){ char c=str[i]; if(c=='%') { percent=true; } else if(c>=0x80) // Non-ASCII characters not allowed return null; } if(!percent)return str; ITextDecoder decoder=TextEncoding.getDecoder(encoding); ByteList mos=new ByteList(); for(int i=0;i<len;i++){ int c=str[i]; if(c=='%'){ if(i+2<len){ int a=toHexNumber(str[i+1]); int b=toHexNumber(str[i+2]); if(a>=0 && b>=0){ mos.append((byte) (a*16+b)); i+=2; continue; } } } mos.append((byte) (c&0xFF)); } return TextEncoding.decodeString(mos.toInputStream(), decoder, TextEncoding.ENCODING_ERROR_REPLACE); }
private static byte[] getDataURLBytesInternal(string dataPath) { // assumes "data" consists of just the path extracted from a URL/URI int index=HeaderParser.skipDataUrlContentType(dataPath, 0,dataPath.Length,null); bool base64=false; if(com.upokecenter.util.StringUtility.startsWith(dataPath,";base64,",index)){ index+=7; base64=true; } if(index<dataPath.Length && dataPath[index]==','){ index++; ByteList mos=new ByteList(); int len=dataPath.Length; for(int j=index;j<len;j++){ int c=dataPath[j]; // matches productions "unreserved" and // "reserved" of RFC2396, including // '?' (even though it delimits // a query _string, which is allowed in all // URIs as of RFC3986) if(!((c&0x7F)==c && "-_.!~*'();/:@&=+$,?".IndexOf((char)c)>=0) && !(c>='A' && c<='Z') && !(c>='a' && c<='z') && !(c>='0' && c<='9')) return null; // matches percent-encoded characters // (production "escaped" of RFC2396) if(c=='%'){ if(index+2<len){ int a=HeaderParser.toHexNumber(dataPath[index+1]); int b=HeaderParser.toHexNumber(dataPath[index+2]); if(a>=0 && b>=0){ mos.append((byte) (a*16+b)); index+=2; continue; } } } mos.append((byte) (c&0xFF)); } byte[] retval=mos.toByteArray(); if(base64){ try { return Base64.decode(retval); } catch(IOException){ return null; } } return retval; } else return null; }