public int decode(PeterO.Support.InputStream stream, IEncodingError error)
 {
     int[] value=new int[1];
     int c=decode(stream,value,0,1, error);
     if(c<=0)return -1;
     return value[0];
 }
Пример #2
0
 public NTriplesParser(PeterO.Support.InputStream stream)
 {
     if((stream)==null)throw new ArgumentNullException("stream");
     this.input=new StackableCharacterInput(
     new AsciiCharacterInput(stream));
     bnodeLabels=new PeterO.Support.LenientDictionary<string,RDFTerm>();
 }
Пример #3
0
 public int decode(PeterO.Support.InputStream stream, int[] buffer, int offset, int length, IEncodingError error)
 {
     if(stream==null || buffer==null || offset<0 || length<0 ||
     offset+length>buffer.Length)
       throw new ArgumentException();
     int cp=0;
     int count=0;
     while(length>0){
       int b=stream.ReadByte();
       if(b<0 && (leadByte>=0 || leadSurrogate>=0)){
     leadByte=-1;
     leadSurrogate=-1;
     int o=error.emitDecoderError(buffer, offset, length);
     offset+=o;
     count+=o;
     length-=o;
     break;
       } else if(b<0){
     break;
       }
       if(leadByte==-1){
     leadByte=b;
     continue;
       }
       cp=(utf16be) ? (leadByte<<8)|b : (b<<8)|leadByte;
       leadByte=-1;
       if(leadSurrogate>=0){
     int lead=leadSurrogate;
     leadSurrogate=-1;
     if(cp>=0xDC00 && cp<=0xDFFF){
       int cp2=0x10000+(lead-0xD800)*0x400+(cp-0xDC00);
       buffer[offset++]=(cp2);
       count++;
       length--;
     } else {
       stream.reset();
       int o=error.emitDecoderError(buffer, offset, length);
       offset+=o;
       count+=o;
       length-=o;
     }
       } else if(cp>=0xD800 && cp<=0xDBFF){
     leadSurrogate=cp;
     stream.mark(4);
     continue;
       } else if(cp>=0xDC00 && cp<=0xDFFF){
     int o=error.emitDecoderError(buffer, offset, length);
     offset+=o;
     count+=o;
     length-=o;
       } else {
     buffer[offset++]=(cp);
     count++;
     length--;
       }
     }
     return (count<=0) ? -1 : count;
 }
 public int decode(PeterO.Support.InputStream stream, IEncodingError error)
 {
     if(stream==null)throw new ArgumentException();
     int c=stream.ReadByte();
     if(c<0)return -1;
     if(c<0x80)
       return c;
     else
       return 0xF780+c-0x80;
 }
Пример #5
0
 public int decode(PeterO.Support.InputStream stream, int[] buffer, int offset, int length, IEncodingError error)
 {
     if(stream==null || buffer==null || offset<0 || length<0 ||
     offset+length>buffer.Length)
       throw new ArgumentException();
     int cp=0;
     int count=0;
     while(length>0){
       int b=stream.ReadByte();
       if(b<0 && (b1>=0 || b2>=0 || b3>=0)){
     b1=b2=b3=-1;
     int o=error.emitDecoderError(buffer, offset, length);
     offset+=o;
     count+=o;
     length-=o;
     break;
       } else if(b<0){
     break;
       }
       if(b3>=0){
     if((utf32be && b1!=0) || (!utf32be && b!=0)){
       b1=b2=b3=-1;
       int o=error.emitDecoderError(buffer, offset, length);
       offset+=o;
       count+=o;
       length-=o;
     } else {
       cp=(utf32be) ? (b1<<24)|(b2<<16)|(b3<<8)|b : (b<<24)|(b3<<16)|(b2<<8)|b1;
       b1=b2=b3=-1;
       if((cp>=0xD800 && cp<=0xDFFF) || cp<0 || cp>=0x110000){
     // Surrogate and out-of-range code points are illegal
     int o=error.emitDecoderError(buffer, offset, length);
     offset+=o;
     count+=o;
     length-=o;
       } else {
     buffer[offset++]=(cp);
     count++;
     length--;
       }
     }
     continue;
       } else if(b2>=0){
     b3=b;
     continue;
       } else if(b1>=0){
     b2=b;
     continue;
       } else {
     b1=b;
     continue;
       }
     }
     return (count<=0) ? -1 : count;
 }
Пример #6
0
 public static CacheControl fromFile(PeterO.Support.File f)
 {
     PeterO.Support.WrappedInputStream fs=new PeterO.Support.WrappedInputStream(new FileStream(f.ToString(),FileMode.Open));
     try {
       return new CacheControlSerializer().readObjectFromStream(fs);
     } finally {
       if(fs!=null) {
     fs.Close();
       }
     }
 }
 public int decode(PeterO.Support.InputStream stream, int[] buffer, int offset, int length, IEncodingError error)
 {
     if(stream==null || buffer==null || offset<0 || length<0 ||
     offset+length>buffer.Length)
       throw new ArgumentException();
     if(length==0)return 0;
     if(!endofstream){
       endofstream=true;
       int o=error.emitDecoderError(buffer, offset, length);
       return o;
     }
     return -1;
 }
 public int decode(PeterO.Support.InputStream stream, IEncodingError error)
 {
     if(!endofstream){
       endofstream=true;
       if(error.Equals(TextEncoding.ENCODING_ERROR_REPLACE))
     return 0xFFFD;
       else {
     int[] data=new int[1];
     int o=error.emitDecoderError(data,0,1);
     return (o==0) ? -1 : data[0];
       }
     }
     return -1;
 }
Пример #9
0
 public int decode(PeterO.Support.InputStream stream, int[] buffer, int offset, int length, IEncodingError error)
 {
     if(stream==null || buffer==null || offset<0 || length<0 ||
     offset+length>buffer.Length)
       throw new ArgumentException();
     if(length==0)return 0;
     int count=0;
     while(length>0){
       int c=decoder.decode(stream, error);
       if(!havebom && !havecr && c>=0x20 && c<=0x7E){
     buffer[offset]=c;
     offset++;
     count++;
     length--;
     continue;
       }
       if(c<0) {
     break;
       }
       if(c==0x0D){
     // CR character
     havecr=true;
     c=0x0A;
       } else if(c==0x0A && havecr){
     havecr=false;
     continue;
       } else {
     havecr=false;
       }
       if(c==0xFEFF && !havebom){
     // leading BOM
     havebom=true;
     continue;
       } else if(c!=0xFEFF){
     havebom=false;
       }
       if(c<0x09 || (c>=0x0E && c<=0x1F) || (c>=0x7F && c<=0x9F) ||
       (c&0xFFFE)==0xFFFE || c>0x10FFFF || c==0x0B || (c>=0xFDD0 && c<=0xFDEF)){
     // control character or noncharacter
     iserror=true;
       }
       buffer[offset]=c;
       offset++;
       count++;
       length--;
     }
     return count==0 ? -1 : count;
 }
 public int decode(PeterO.Support.InputStream stream, IEncodingError error)
 {
     if(stream==null)throw new ArgumentException();
     while(true){
       int c=stream.ReadByte();
       if(c<0)return -1;
       if(c<0x80)
     return c;
       else {
     int cp=indexes[(c)&0x7F];
     if(cp!=0)return cp;
     if(error.Equals(TextEncoding.ENCODING_ERROR_REPLACE))
       return 0xFFFD;
     else {
       int[] data=new int[1];
       int o=error.emitDecoderError(data,0,1);
       if(o>0)return data[0];
     }
       }
     }
 }
Пример #11
0
 public XhtmlParser(PeterO.Support.InputStream source, string address, string charset, string lang)
 {
     if(source==null)throw new ArgumentException();
     if(address!=null && address.Length>0){
       URL url=URL.parse(address);
       if(url==null || url.getScheme().Length==0)
     throw new ArgumentException();
     }
     this.contentLang=HeaderParser.getLanguages(lang);
     this.address=address;
     try {
       this.reader=new PeterO.Support.SaxReader();
     } catch (SaxException e) {
       if(e.InnerException is IOException)
     throw (IOException)(e.InnerException);
       throw new IOException("",e);
     }
     handler=new XhtmlContentHandler(this);
     try {
       reader.SetFeature("http://xml.org/sax/features/namespaces",true);
       reader.SetFeature("http://xml.org/sax/features/use-entity-resolver2",true);
       reader.SetFeature("http://xml.org/sax/features/namespace-prefixes",true);
       reader.LexicalHandler=(handler);
     } catch (SaxException e) {
       throw new NotSupportedException("",e);
     }
     reader.ContentHandler=(handler);
     reader.EntityResolver=(handler);
     charset=TextEncoding.resolveEncoding(charset);
     if(charset==null){
       charset=sniffEncoding(source);
       if(charset==null) {
     charset="utf-8";
       }
     }
     this.isource=new InputSource<Stream>(source);
     this.isource.Encoding=(charset);
     this.encoding=charset;
 }
 public int decode(PeterO.Support.InputStream stream, int[] buffer, int offset, int length, IEncodingError error)
 {
     if((stream)==null)throw new ArgumentNullException("stream");
     if((error)==null)throw new ArgumentNullException("error");
     if((buffer)==null)throw new ArgumentNullException("buffer");
     if((offset)<0)throw new ArgumentOutOfRangeException("offset"+" not greater or equal to "+"0"+" ("+Convert.ToString(offset,CultureInfo.InvariantCulture)+")");
     if((length)<0)throw new ArgumentOutOfRangeException("length"+" not greater or equal to "+"0"+" ("+Convert.ToString(length,CultureInfo.InvariantCulture)+")");
     if((offset+length)>buffer.Length)throw new ArgumentOutOfRangeException("offset+length"+" not less or equal to "+Convert.ToString(buffer.Length,CultureInfo.InvariantCulture)+" ("+Convert.ToString(offset+length,CultureInfo.InvariantCulture)+")");
     if(length==0)return 0;
     int total=0;
     for(int i=0;i<length;i++){
       int c=stream.ReadByte();
       if(c<0){
     break;
       } else if(c<0x80){
     buffer[offset++]=c;
     total++;
       } else {
     buffer[offset++]=(0xF780+(c&0xFF)-0x80);
     total++;
       }
     }
     return (total==0) ? -1 : total;
 }
 public int decode(PeterO.Support.InputStream stream, int[] buffer, int offset, int length, IEncodingError error)
 {
     if(stream==null || buffer==null || offset<0 || length<0 ||
     offset+length>buffer.Length)
       throw new ArgumentException();
     int count=0;
     while(length>0){
       int b=stream.ReadByte();
       if(b<0 && (eucjp1|eucjp2)==0)
     return (count==0) ? -1 : count;
       else if(b<0){
     int o=error.emitDecoderError(buffer, offset, length);
     offset+=o;
     count+=o;
     length-=o;
     break;
       }
       if(eucjp2!=0){
     int lead=0;
     eucjp2=0;
     int cp=0;
     if((lead>=0xA1 && lead<=0xFE) && (b>=0xA1 && b<=0xFE)){
       int index=(lead-0xA1)*94+b-0xA1;
       cp=JIS0212.indexToCodePoint(index);
     }
     if(b<0xA1 || b==0xFF){
       stream.reset();
     }
     if(cp<=0){
       int o=error.emitDecoderError(buffer, offset, length);
       offset+=o;
       count+=o;
       length-=o;
       continue;
     } else {
       buffer[offset++]=(cp);
       count++;
       length--;
       continue;
     }
       }
       if(eucjp1==0x8E && b>=0xA1 && b<=0xDF){
     eucjp1=0;
     buffer[offset++]=(0xFF61+b-0xA1);
     count++;
     length--;
     //Console.WriteLine("return 0xFF61 cp: %04X",0xFF61+b-0xA1);
     continue;
       }
       if(eucjp1==0x8F && b>=0xA1 && b<=0xFE){
     eucjp1=0;
     eucjp2=b;
     stream.mark(4);
     continue;
       }
       if(eucjp1!=0){
     int lead=eucjp1;
     eucjp1=0;
     int cp=0;
     if((lead>=0xA1 && lead<=0xFE) && (b>=0xA1 && b<=0xFE)){
       int index=(lead-0xA1)*94+b-0xA1;
       cp=JIS0208.indexToCodePoint(index);
       //Console.WriteLine("return 0208 cp: %04X lead=%02X b=%02X index=%04X",cp,lead,b,index);
     }
     if(b<0xA1 || b==0xFF){
       stream.reset();
     }
     if(cp==0){
       int o=error.emitDecoderError(buffer, offset, length);
       offset+=o;
       count+=o;
       length-=o;
       continue;
     } else {
       buffer[offset++]=(cp);
       count++;
       length--;
       continue;
     }
       }
       if(b<0x80){
     buffer[offset++]=(b);
     count++;
     length--;
     continue;
       } else if(b==0x8E || b==0x8F || (b>=0xA1 && b<=0xFE)){
     eucjp1=b;
     stream.mark(4);
     continue;
       } else {
     int o=error.emitDecoderError(buffer, offset, length);
     offset+=o;
     count+=o;
     length-=o;
     continue;
       }
     }
     return (count==0) ? -1 : count;
 }
 public int decode(PeterO.Support.InputStream stream, int[] buffer, int offset, int length, IEncodingError error)
 {
     if((stream)==null)throw new ArgumentNullException("stream");
     if((error)==null)throw new ArgumentNullException("error");
     if((buffer)==null)throw new ArgumentNullException("buffer");
     if((offset)<0)throw new ArgumentOutOfRangeException("offset"+" not greater or equal to "+"0"+" ("+Convert.ToString(offset,CultureInfo.InvariantCulture)+")");
     if((length)<0)throw new ArgumentOutOfRangeException("length"+" not greater or equal to "+"0"+" ("+Convert.ToString(length,CultureInfo.InvariantCulture)+")");
     if((offset+length)>buffer.Length)throw new ArgumentOutOfRangeException("offset+length"+" not less or equal to "+Convert.ToString(buffer.Length,CultureInfo.InvariantCulture)+" ("+Convert.ToString(offset+length,CultureInfo.InvariantCulture)+")");
     if(length==0)return 0;
     int total=0;
     for(int i=0;i<length;i++){
       int c=stream.ReadByte();
       if(c<0){
     break;
       } else if(c<0x80){
     buffer[offset++]=c;
     total++;
       } else {
     int cp=indexes[(c)&0x7F];
     if(cp==0){
       if(error.Equals(TextEncoding.ENCODING_ERROR_REPLACE)) {
     cp=0xFFFD;
       } else {
     int[] data=new int[1];
     int o=error.emitDecoderError(data,0,1);
     if(o>0)return data[0];
       }
     }
     buffer[offset++]=cp;
     total++;
       }
     }
     return (total==0) ? -1 : total;
 }
 public int decode(PeterO.Support.InputStream stream)
 {
     return decode(stream, TextEncoding.ENCODING_ERROR_THROW);
 }
 public int decode(PeterO.Support.InputStream stream, int[] buffer, int offset, int length, IEncodingError error)
 {
     if(stream==null || buffer==null || offset<0 || length<0 ||
     offset+length>buffer.Length)
       throw new ArgumentException();
     int count=0;
     while(length>0){
       int b=stream.ReadByte();
       if(state==0){ // ASCII state
     if(b==0x1b){
       stream.mark(4);
       state=2; // escape start state
       continue;
     } else if(b<0){
       break;
     } else if(b<=0x7F){
       buffer[offset++]=(b);
       length--;
       count++;
       continue;
     } else {
       int o=error.emitDecoderError(buffer, offset, length);
       offset+=o;
       count+=o;
       length-=o;
       continue;
     }
       } else if(state==2){ // escape start state
     if(b==0x24 || b==0x28){
       lead=b;
       state=3; // escape middle state
       continue;
     } else {
       stream.reset(); // 'decrease by one'
       state=0;// ASCII state
       int o=error.emitDecoderError(buffer, offset, length);
       offset+=o;
       count+=o;
       length-=o;
       continue;
     }
       } else if(state==3){ // escape middle state
     if(lead==0x24 && (b==0x40 || b==0x42)){
       jis0212=false;
       state=5; // lead state
       continue;
     } else if(lead==0x24 && b==0x28){
       state=4; // escape  state
       continue;
     } else if(lead==0x28 && (b==0x42 || b==0x4a)){
       state=0; // ASCII state
       continue;
     } else if(lead==0x28 && (b==0x49)){
       state=7; // Katakana state
       continue;
     } else {
       stream.reset();
       state=0;// ASCII state
       int o=error.emitDecoderError(buffer, offset, length);
       offset+=o;
       count+=o;
       length-=o;
       continue;
     }
       } else if(state==4){ //  state
     if(b==0x44){
       jis0212=true;
       state=5;
       continue;
     } else {
       stream.reset();
       state=0;// ASCII state
       int o=error.emitDecoderError(buffer, offset, length);
       offset+=o;
       count+=o;
       length-=o;
       continue;
     }
       } else if(state==5){ // lead state
     if(b==0x0A){
       state=0;// ASCII state
       buffer[offset++]=0x0a;
       length--;
       count++;
       continue;
     } else if(b==0x1B){
       state=1; // escape start state
       continue;
     } else if(b<0){
       break;
     } else {
       lead=b;
       state=6;
       continue;
     }
       } else if(state==6){ // trail state
     state=5; // lead state
     if(b<0){
       int o=error.emitDecoderError(buffer, offset, length);
       offset+=o;
       count+=o;
       length-=o;
       continue;
     } else {
       int cp=-1;
       int index=(lead-0x21)*94+b-0x21;
       if((lead>=0x21 && lead<=0x7e) &&
       (b>=0x21 && b<=0x7e)){
     if(jis0212){
       cp=JIS0212.indexToCodePoint(index);
     } else {
       cp=JIS0208.indexToCodePoint(index);
     }
       }
       if(cp<=0){
     int o=error.emitDecoderError(buffer, offset, length);
     offset+=o;
     count+=o;
     length-=o;
     continue;
       } else {
     buffer[offset++]=(cp);
     length--;
     count++;
     continue;
       }
     }
       } else { // Katakana state
     if(b==0x1b){
       state=1; // escape start state
       continue;
     } else if((b>=0x21 && b<=0x5F)){
       buffer[offset++]=(0xFF61+b-0x21);
       length--;
       count++;
       continue;
     } else if(b<0){
       break;
     } else {
       int o=error.emitDecoderError(buffer, offset, length);
       offset+=o;
       count+=o;
       length-=o;
       continue;
     }
       }
     }
     return (count==0) ? -1 : count;
 }
Пример #17
0
   /**
      * Utility method to decode an input byte stream into a _string.
      *
      * @param input an input stream containing character data
      * encoded as bytes. If null,
      * @param decoder a text decoder.
      * @param error an _object that handles encoding errors.  You
      * can use TextEncoding.ENCODING_ERROR_THROW or
      * TextEncoding.ENCODING_ERROR_REPLACE for this.
      *
      * @ if the error handler  exception
      * or another I/O error occurs
      */
   public static string decodeString(
 PeterO.Support.InputStream input, ITextDecoder decoder, IEncodingError error)
   {
       if(decoder==null || input==null || error==null)
         throw new ArgumentException();
       int[] data=new int[64];
       StringBuilder builder=new StringBuilder();
       while(true){
         int count=decoder.decode(input,data,0,data.Length,error);
         if(count<0) {
       break;
         }
         for(int i=0;i<count;i++){
       if(data[i]<=0xFFFF){
         builder.Append((char)data[i]);
       } else {
         int ch=data[i]-0x10000;
         int lead=ch/0x400+0xd800;
         int trail=(ch&0x3FF)+0xdc00;
         builder.Append((char)lead);
         builder.Append((char)trail);
       }
         }
       }
       return builder.ToString();
   }
 public ConditionalBufferInputStream(PeterO.Support.InputStream input)
 {
     this.stream=input;
     this.buffer=new byte[1024];
 }
 public int decode(PeterO.Support.InputStream stream, int[] buffer, int offset, int length, IEncodingError error)
 {
     if(stream==null || buffer==null || offset<0 || length<0 ||
     offset+length>buffer.Length)
       throw new ArgumentException();
     if(length==0)return 0;
     int count=0;
     int o=0;
     while(length>0){
       int b=stream.ReadByte();
       if(b<0 && lead==0){
     break;
       } else if(b<0){
     lead=0;
     o=error.emitDecoderError(buffer, offset, length);
     offset+=o;
     count+=o;
     length-=o;
     break;
       }
       if(lead!=0){
     int thislead=lead;
     lead=0;
     int pointer=-1;
     if(thislead>=0x81 && thislead<=0xc6){
       pointer=(26+26+126)*(thislead-0x81);
       if(b>=0x41 && b<=0x5a) {
     pointer+=b-0x41;
       } else if(b>=0x61 && b<=0x7a) {
     pointer+=26+b-0x61;
       } else if(b>=0x81 && b<=0xfe) {
     pointer+=26+26+b-0x81;
       }
     }
     if(thislead>=0xc7 && thislead<=0xfe &&
     b>=0xa1 && b<=0xfe){
       pointer=(26+26+126)*(0xc7-0x81)+(thislead-0xC7)*94+(b-0xA1);
     }
     int cp=Korean.indexToCodePoint(pointer);
     if(pointer<0){
       stream.reset();
     }
     if(cp<=0){
       o=error.emitDecoderError(buffer, offset, length);
       offset+=o;
       count+=o;
       length-=o;
       continue;
     } else {
       buffer[offset++]=cp;
       count++;
       length--;
       continue;
     }
       }
       if(b<0x80){
     buffer[offset++]=b;
     count++;
     length--;
     continue;
       }
       if(b>=0x81 && b<=0xFE){
     lead=b;
     stream.mark(2);
     continue;
       }
       o=error.emitDecoderError(buffer, offset, length);
       offset+=o;
       count+=o;
       length-=o;
     }
     return (count==0) ? -1 : count;
 }
Пример #20
0
 public XhtmlParser(PeterO.Support.InputStream s, string _string, string charset)
     : this(s,_string,charset,null)
 {
 }
Пример #21
0
 public int decode(PeterO.Support.InputStream stream, int[] buffer, int offset, int length, IEncodingError error)
 {
     if(stream==null || buffer==null || offset<0 || length<0 ||
     offset+length>buffer.Length)
       throw new ArgumentException();
     int count=0;
     while(length>0){
       if(nextChar>=0){
     buffer[offset++]=nextChar;
     count++;
     length--;
     nextChar=-1;
     continue;
       }
       int b=stream.ReadByte();
       if(b<0 && lead==0){
     break;
       } else if(b<0){
     lead=0;
     int o=error.emitDecoderError(buffer, offset, length);
     offset+=o;
     count+=o;
     length-=o;
     continue;
       }
       if(lead!=0){
     int thislead=lead;
     int pointer=-1;
     lead=0;
     int thisoffset=(b<0x7F) ? 0x40 : 0x62;
     if((b>=0x40 && b<=0x7E) || (b>=0xA1 && b<=0xFE)){
       pointer=(thislead-0x81)*157+(b-thisoffset);
       if(pointer==1133 || pointer==1135){
     buffer[offset++]=(0xca);
     count++;
     length--;
     if(length<=0){
       nextChar=((pointer==1133) ? 0x304 : 0x30C);
       break;
     } else {
       buffer[offset++]=((pointer==1133) ? 0x304 : 0x30C);
       count++;
       length--;
       continue;
     }
       } else if(pointer==1164 || pointer==1166){
     buffer[offset++]=(0xea);
     count++;
     length--;
     if(length<=0){
       nextChar=((pointer==1164) ? 0x304 : 0x30C);
       break;
     } else {
       buffer[offset++]=((pointer==1164) ? 0x304 : 0x30C);
       count++;
       length--;
       continue;
     }
       }
     }
     int cp=Big5.indexToCodePoint(pointer);
     if(pointer<0){
       stream.reset();
     }
     if(cp<=0){
       int o=error.emitDecoderError(buffer, offset, length);
       offset+=o;
       count+=o;
       length-=o;
       continue;
     } else {
       buffer[offset++]=(cp);
       count++;
       length--;
       continue;
     }
       }
       if(b<0x7F){
     buffer[offset++]=(b);
     count++;
     length--;
     continue;
       } else if(b>=0x81 && b<=0xFE){
     stream.mark(2);
     lead=b;
     continue;
       } else {
     int o=error.emitDecoderError(buffer, offset, length);
     offset+=o;
     count+=o;
     length-=o;
     continue;
       }
     }
     return count>0 ? count : -1;
 }
Пример #22
0
 public static void toFile(CacheControl o, PeterO.Support.File file)
 {
     Stream fs=new FileStream((file).ToString(),FileMode.Create);
     try {
       new CacheControlSerializer().writeObjectToStream(o,fs);
     } finally {
       if(fs!=null) {
     fs.Close();
       }
     }
 }
Пример #23
0
 public CacheControl readObjectFromStream(PeterO.Support.InputStream stream)
 {
     try {
     JSONObject jsonobj=new JSONObject(StreamUtility.streamToString(stream));
     CacheControl cc=new CacheControl();
     cc.cacheability=jsonobj.getInt("cacheability");
     cc.noStore=jsonobj.getBoolean("noStore");
     cc.noTransform=jsonobj.getBoolean("noTransform");
     cc.mustRevalidate=jsonobj.getBoolean("mustRevalidate");
     cc.requestTime=Int64.Parse(jsonobj.getString("requestTime"),NumberStyles.AllowLeadingSign,CultureInfo.InvariantCulture);
     cc.responseTime=Int64.Parse(jsonobj.getString("responseTime"),NumberStyles.AllowLeadingSign,CultureInfo.InvariantCulture);
     cc.maxAge=Int64.Parse(jsonobj.getString("maxAge"),NumberStyles.AllowLeadingSign,CultureInfo.InvariantCulture);
     cc.date=Int64.Parse(jsonobj.getString("date"),NumberStyles.AllowLeadingSign,CultureInfo.InvariantCulture);
     cc.code=jsonobj.getInt("code");
     cc.age=Int64.Parse(jsonobj.getString("age"),NumberStyles.AllowLeadingSign,CultureInfo.InvariantCulture);
     cc.uri=jsonobj.getString("uri");
     cc.requestMethod=jsonobj.getString("requestMethod");
     if(cc.requestMethod!=null) {
       cc.requestMethod=StringUtility.toLowerCaseAscii(cc.requestMethod);
     }
     cc.headers=new List<string>();
     JSONArray jsonarr=jsonobj.getJSONArray("headers");
     for(int i=0;i<jsonarr.Length;i++){
       string str=jsonarr.getString(i);
       if(str!=null && (i%2)!=0){
     str=StringUtility.toLowerCaseAscii(str);
     if("age".Equals(str) ||
     "connection".Equals(str) ||
     "keep-alive".Equals(str) ||
     "proxy-authenticate".Equals(str) ||
     "proxy-authorization".Equals(str) ||
     "te".Equals(str) ||
     "trailers".Equals(str) ||
     "transfer-encoding".Equals(str) ||
     "upgrade".Equals(str)){
       // Skip "age" header field and
       // hop-by-hop header fields
       i++;
       continue;
     }
       }
       cc.headers.Add(str);
     }
     return cc;
       } catch(InvalidCastException e){
     Console.WriteLine(e.StackTrace);
     return null;
       } catch(FormatException e){
     Console.WriteLine(e.StackTrace);
     return null;
       } catch (Json.InvalidJsonException e) {
     Console.WriteLine(e.StackTrace);
     return null;
       }
 }
        public int decode(PeterO.Support.InputStream stream, int[] buffer, int offset, int length, IEncodingError error)
        {
            if(stream==null || buffer==null || offset<0 || length<0 ||
            offset+length>buffer.Length)
              throw new ArgumentException();
            if(length==0)return 0;
            int count=0;
            while(length>0){
              int b=stream.ReadByte();
              if(state==0){ // ASCII state
            if(b==0x0e){
              state=5; // lead state
            } else if(b==0x0f){
              continue;
            } else if(b==0x1b){
              stream.mark(4);
              state=2; // escape start state
              continue;
            } else if(b<0){
              break;
            } else if(b<=0x7F){
              buffer[offset++]=(b);
              length--;
              count++;
            } else {
              int o=error.emitDecoderError(buffer, offset, length);
              offset+=o;
              count+=o;
              length-=o;

            }
              } else if(state==2){ // escape start state
            if(b==0x24){
              state=3; // escape middle state
              continue;
            } else {
              stream.reset(); // 'decrease by one'
              state=0;// ASCII state
              int o=error.emitDecoderError(buffer, offset, length);
              offset+=o;
              count+=o;
              length-=o;

            }
              } else if(state==3){ // escape middle state
            if(b==0x29){
              state=4;
              continue;
            } else {
              stream.reset();
              state=0;// ASCII state
              int o=error.emitDecoderError(buffer, offset, length);
              offset+=o;
              count+=o;
              length-=o;

            }
              } else if(state==4){ //  state
            if(b==0x43){
              state=0;
              continue;
            } else {
              stream.reset();
              state=0;// ASCII state
              int o=error.emitDecoderError(buffer, offset, length);
              offset+=o;
              count+=o;
              length-=o;

            }
              } else if(state==5){ // lead state
            if(b==0x0A){
              state=0;// ASCII state
              buffer[offset++]=0x0a;
              length--;
              count++;
            } else if(b==0x0e){
              continue;
            } else if(b==0x0f){
              state=0;
              continue;
            } else if(b<0){
              break;
            } else {
              lead=b;
              state=6;
              continue;
            }
              } else if(state==6){ // trail state
            state=5; // lead state
            if(b<0){
              int o=error.emitDecoderError(buffer, offset, length);
              offset+=o;
              count+=o;
              length-=o;

            } else {
              int cp=-1;
              if((lead>=0x21 && lead<=0x46) &&
              (b>=0x21 && b<=0x7e)){
            cp=Korean.indexToCodePoint((26+26+126)*(lead-1)+26+26+b-1);
              } else if((lead>=0x47 && lead<=0x7E) &&
              (b>=0x21 && b<=0x7e)){
            cp=Korean.indexToCodePoint((26+26+126)*(0xc7-0x81)+(lead-0x47)*94+(b-0x21));
              }
              if(cp<=0){
            int o=error.emitDecoderError(buffer, offset, length);
            offset+=o;
            count+=o;
            length-=o;

              } else {
            buffer[offset++]=(cp);
            length--;
            count++;
              }
            }
              }
            }
            return (count==0) ? -1 : count;
        }
Пример #25
0
 public int decode(PeterO.Support.InputStream stream, IEncodingError error)
 {
     if(stream==null)
       throw new ArgumentException();
     while(true){
       int b=stream.ReadByte();
       if(b<0 && bytesNeeded!=0){
     bytesNeeded=0;
     if(error.Equals(TextEncoding.ENCODING_ERROR_REPLACE))
       return 0xFFFD;
     else {
       int[] data=new int[1];
       int o=error.emitDecoderError(data,0,1);
       if(o>0)return data[0];
       continue;
     }
       } else if(b<0)
     return -1;
       if(bytesNeeded==0){
     if(b<0x80)
       return b;
     else if(b>=0xc2 && b<=0xdf){
       stream.mark(4);
       bytesNeeded=1;
       cp=b-0xc0;
     } else if(b>=0xe0 && b<=0xef){
       stream.mark(4);
       lower=(b==0xe0) ? 0xa0 : 0x80;
       upper=(b==0xed) ? 0x9f : 0xbf;
       bytesNeeded=2;
       cp=b-0xe0;
     } else if(b>=0xf0 && b<=0xf4){
       stream.mark(4);
       lower=(b==0xf0) ? 0x90 : 0x80;
       upper=(b==0xf4) ? 0x8f : 0xbf;
       bytesNeeded=3;
       cp=b-0xf0;
     } else {
       if(error.Equals(TextEncoding.ENCODING_ERROR_REPLACE))
     return 0xFFFD;
       else {
     int[] data=new int[1];
     int o=error.emitDecoderError(data,0,1);
     if(o>0)return data[0];
     continue;
       }
     }
     cp<<=(6*bytesNeeded);
     continue;
       }
       if(b<lower || b>upper){
     cp=bytesNeeded=bytesSeen=0;
     lower=0x80;
     upper=0xbf;
     stream.reset(); // 'Decrease the byte pointer by one.'
     if(error.Equals(TextEncoding.ENCODING_ERROR_REPLACE))
       return 0xFFFD;
     else {
       int[] data=new int[1];
       int o=error.emitDecoderError(data,0,1);
       if(o>0)return data[0];
       continue;
     }
       }
       lower=0x80;
       upper=0xbf;
       bytesSeen++;
       cp+=(b-0x80)<<(6*(bytesNeeded-bytesSeen));
       stream.mark(4);
       if(bytesSeen!=bytesNeeded) {
     continue;
       }
       int ret=cp;
       cp=0;
       bytesSeen=0;
       bytesNeeded=0;
       return ret;
     }
 }
Пример #26
0
 public AsciiCharacterInput(PeterO.Support.InputStream stream)
 {
     this.stream=stream;
 }
 public int decode(PeterO.Support.InputStream stream, int[] buffer, int offset, int length)
 {
     return decode(stream, buffer, offset, length, TextEncoding.ENCODING_ERROR_THROW);
 }
Пример #28
0
 private static string sniffEncoding(PeterO.Support.InputStream s)
 {
     byte[] data=new byte[4];
     int count=0;
     s.mark(data.Length+2);
     try {
       count=s.Read(data,0,data.Length);
     } finally {
       s.reset();
     }
     if(count>=2 && (data[0]&0xFF)==0xfe && (data[1]&0xFF)==0xff)
       return "utf-16be";
     if(count>=2 && (data[0]&0xFF)==0xff && (data[1]&0xFF)==0xfe)
       return "utf-16le";
     if(count>=3 && (data[0]&0xFF)==0xef && (data[1]&0xFF)==0xbb &&
     (data[2]&0xFF)==0xbf)
       return "utf-8";
     if(count>=4 && (data[0]&0xFF)==0x00 && data[1]==0x3c &&
     data[2]==0x00 && data[3]==0x3f)
       return "utf-16be";
     if(count>=4 && data[0]==0x3c && data[1]==0x00 &&
     data[2]==0x3f && data[3]==0x00)
       return "utf-16le";
     if(count>=4 && data[0]==0x3c && data[1]==0x3f &&
     data[2]==0x78 && data[3]==0x6d){ // <?xm
       data=new byte[128];
       s.mark(data.Length+2);
       try {
     count=s.Read(data,0,data.Length);
       } finally {
     s.reset();
       }
       int i=4;
       if(i+1>count)return "utf-8";
       if(data[i++]!='l')return "utf-8"; // l in <?xml
       bool space=false;
       while(i<count){
     if(data[i]==0x09||data[i]==0x0a||data[i]==0x0d||data[i]==0x20)
     { space=true; i++; } else {
       break;
     }
       }
       if(!space || i+7>count)return "utf-8";
       if(!(data[i]=='v' && data[i+1]=='e' && data[i+2]=='r' &&
       data[i+3]=='s' && data[i+4]=='i' && data[i+5]=='o' &&
       data[i+6]=='n'))return "utf-8";
       i+=7;
       while(i<count){
     if(data[i]==0x09||data[i]==0x0a||data[i]==0x0d||data[i]==0x20) {
       i++;
     } else {
       break;
     }
       }
       if(i+1>count || data[i++]!='=')return "utf-8";
       while(i<count){
     if(data[i]==0x09||data[i]==0x0a||data[i]==0x0d||data[i]==0x20) {
       i++;
     } else {
       break;
     }
       }
       if(i+1>count)return "utf-8";
       int ch=data[i++];
       if(ch!='"' && ch!='\'')return "utf-8";
       while(i<count){
     if(data[i]==ch){ i++; break; }
     i++;
       }
       space=false;
       while(i<count){
     if(data[i]==0x09||data[i]==0x0a||data[i]==0x0d||data[i]==0x20)
     { space=true; i++; } else {
       break;
     }
       }
       if(i+8>count)return "utf-8";
       if(!(data[i]=='e' && data[i+1]=='n' && data[i+2]=='c' &&
       data[i+3]=='o' && data[i+4]=='d' && data[i+5]=='i' &&
       data[i+6]=='n' && data[i+7]=='g'))return "utf-8";
       i+=8;
       while(i<count){
     if(data[i]==0x09||data[i]==0x0a||data[i]==0x0d||data[i]==0x20) {
       i++;
     } else {
       break;
     }
       }
       if(i+1>count || data[i++]!='=')return "utf-8";
       while(i<count){
     if(data[i]==0x09||data[i]==0x0a||data[i]==0x0d||data[i]==0x20) {
       i++;
     } else {
       break;
     }
       }
       if(i+1>count)return "utf-8";
       ch=data[i++];
       if(ch!='"' && ch!='\'')return "utf-8";
       StringBuilder builder=new StringBuilder();
       while(i<count){
     if(data[i]==ch){
       string encoding=TextEncoding.resolveEncoding(builder.ToString());
       if(encoding==null)
     return null;
       if(encoding.Equals("utf-16le") || encoding.Equals("utf-16be"))
     return null;
       return builder.ToString();
     }
     builder.Append((char)data[i]);
     i++;
       }
       return "utf-8";
     }
     return "utf-8";
 }
Пример #29
0
 public int decode(PeterO.Support.InputStream stream, int[] buffer, int offset, int length, IEncodingError error)
 {
     if(stream==null || buffer==null || offset<0 || length<0 ||
     offset+length>buffer.Length)
       throw new ArgumentException();
     int count=0;
     while(length>0){
       int b=stream.ReadByte();
       if(b<0 && bytesNeeded!=0){
     bytesNeeded=0;
     int o=error.emitDecoderError(buffer,offset,length);
     offset+=o;
     count+=o;
     length-=o;
     break;
       } else if(b<0){
     break;
       }
       if(bytesNeeded==0){
     if(b<0x80){
       buffer[offset++]=(b);
       count++;
       length--;
       continue;
     } else if(b>=0xc2 && b<=0xdf){
       stream.mark(4);
       bytesNeeded=1;
       cp=b-0xc0;
     } else if(b>=0xe0 && b<=0xef){
       stream.mark(4);
       lower=(b==0xe0) ? 0xa0 : 0x80;
       upper=(b==0xed) ? 0x9f : 0xbf;
       bytesNeeded=2;
       cp=b-0xe0;
     } else if(b>=0xf0 && b<=0xf4){
       stream.mark(4);
       lower=(b==0xf0) ? 0x90 : 0x80;
       upper=(b==0xf4) ? 0x8f : 0xbf;
       bytesNeeded=3;
       cp=b-0xf0;
     } else {
       int o=error.emitDecoderError(buffer,offset,length);
       offset+=o;
       count+=o;
       length-=o;
       continue;
     }
     cp<<=(6*bytesNeeded);
     continue;
       }
       if(b<lower || b>upper){
     cp=bytesNeeded=bytesSeen=0;
     lower=0x80;
     upper=0xbf;
     stream.reset(); // 'Decrease the byte pointer by one.'
     int o=error.emitDecoderError(buffer,offset,length);
     offset+=o;
     count+=o;
     length-=o;
       }
       lower=0x80;
       upper=0xbf;
       bytesSeen++;
       cp+=(b-0x80)<<(6*(bytesNeeded-bytesSeen));
       stream.mark(4);
       if(bytesSeen!=bytesNeeded) {
     continue;
       }
       buffer[offset++]=(cp);
       count++;
       length--;
       cp=0;
       bytesSeen=0;
       bytesNeeded=0;
     }
     return (count<=0) ? -1 : count;
 }
Пример #30
0
 public int decode(PeterO.Support.InputStream stream, int[] buffer, int offset, int length, IEncodingError error)
 {
     if(stream==null || buffer==null || offset<0 || length<0 ||
     offset+length>buffer.Length)
       throw new ArgumentException();
     if(length==0)return 0;
     int count=0;
     while(length>0){
       int b=stream.ReadByte();
       if(b<0 && lead==0){
     break;
       } else if(b<0){
     lead=0;
     int o=error.emitDecoderError(buffer, offset, length);
     offset+=o;
     count+=o;
     length-=o;
     break;
       }
       if(lead!=0){
     int thislead=lead;
     int pointer=-1;
     lead=0;
     int thisoffset=(b<0x7F) ? 0x40 : 0x41;
     int leadOffset=(thislead<0xA0) ? 0x81 : 0xC1;
     if((b>=0x40 && b<=0xFC) && b!=0x7F){
       pointer=(thislead-leadOffset)*188+b-thisoffset;
     }
     int cp=-1;
     cp=JIS0208.indexToCodePoint(pointer);
     if(pointer<0){
       stream.reset();
     }
     if(cp<=0){
       int o=error.emitDecoderError(buffer, offset, length);
       offset+=o;
       count+=o;
       length-=o;
     } else {
       buffer[offset++]=cp;
       count++;
       length--;
     }
     continue;
       }
       if(b<=0x80){
     buffer[offset++]=b;
     count++;
     length--;
       } else if(b>=0xA1 && b<=0xDF){
     buffer[offset++]=0xFF61+b-0xA1;
     count++;
     length--;
       } else if((b>=0x81 && b<=0x9F) || (b>=0xE0 && b<=0xFC)){
     lead=b;
     stream.mark(2);
     continue;
       } else {
     int o=error.emitDecoderError(buffer, offset, length);
     offset+=o;
     count+=o;
     length-=o;
     continue;
       }
     }
     return (count==0) ? -1 : count;
 }