예제 #1
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;
 }
예제 #2
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;
 }
 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;
 }
 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];
     }
       }
     }
 }
 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, 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;
 }
예제 #8
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;
 }
 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;
 }
        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;
        }
 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;
 }
 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;
 }
예제 #13
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;
     }
 }
예제 #14
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;
 }
예제 #15
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==0x7e){
            lead=0;
            if(b==0x7b){
              flag=true;
              continue;
            } else if(b==0x7d){
              flag=false;
              continue;
            } else if(b==0x7e){
              buffer[offset++]=0x7e;
              length--;
              count++;
              continue;
            } else if(b==0x0a){
              continue;
            } else {
              stream.reset();
              int o=error.emitDecoderError(buffer, offset, length);
              offset+=o;
              count+=o;
              length-=o;

              continue;
            }
              }
              if(lead!=0){
            int thislead=lead;
            int cp=-1;
            lead=0;
            if(b>=0x21 && b<=0x7e){
              cp=(thislead-1)*190+(b+0x3f);
              cp=GBK.indexToCodePoint(cp);
            }
            if(b==0x0a) {
              flag=false;
            }
            if(cp<0){
              int o=error.emitDecoderError(buffer, offset, length);
              offset+=o;
              count+=o;
              length-=o;

              continue;
            } else {
              buffer[offset++]=cp;
              length--;
              count++;
              continue;
            }
              }
              if(b==0x7e){
            lead=0x7e;
            stream.mark(2);
            continue;
              }
              if(flag){
            if(b>=0x20 && b<=0x7f){
              lead=b;
              continue;
            }
            if(b==0x0a){
              flag=false;
            }
            int o=error.emitDecoderError(buffer, offset, length);
            offset+=o;
            count+=o;
            length-=o;

            continue;
              }
              if(b<=0x7f){
            buffer[offset++]=b;
            length--;
            count++;
            continue;
              } else {
            int o=error.emitDecoderError(buffer, offset, length);
            offset+=o;
            count+=o;
            length-=o;

            continue;
              }
            }
            return (count==0) ? -1 : count;
        }
예제 #16
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 && (gbk1|gbk2|gbk3)==0)
     return count;
       else if(b<0){
     gbk1=gbk2=gbk3=0;
     int o=error.emitDecoderError(buffer, offset, length);
     offset+=o;
     count+=o;
     length-=o;
     break;
       }
       if(gbk3!=0){
     int cp=0;
     if(b>=0x30 && b<=0x39){
       int index=(((gbk1-0x81)*10+gbk2-0x30)*126+gbk3-0x81)*10+b-0x30;
       cp=GB18030CodePoint(index);
     }
     gbk1=gbk2=gbk3=0;
     if(cp==0){
       stream.reset(); // 'decrease the byte pointer by three'
       int o=error.emitDecoderError(buffer, offset, length);
       offset+=o;
       count+=o;
       length-=o;
       continue;
     } else {
       buffer[offset++]=(cp);
       count++;
       length--;
       continue;
     }
       }
       if(gbk2!=0){
     if(b>=0x80 && b<=0xFE){
       gbk3=b;
       continue;
     } else {
       gbk1=gbk2=0;
       stream.reset(); // 'decrease the byte pointer by two'
       int o=error.emitDecoderError(buffer, offset, length);
       offset+=o;
       count+=o;
       length-=o;
       continue;
     }
       }
       if(gbk1!=0){
     if(b>=0x30 && b<=0x39 && gb18030){
       gbk2=b;
       continue;
     } else {
       int pointer=-1;
       int lead=gbk1;
       gbk1=0;
       if((b>=0x40 && b<=0x7E) || (b>=0x80 && b<=0xFE)){
     int offset2=(b<0x7F) ? 0x40 : 0x41;
     pointer=(lead-0x81)*190+(b-offset2);
       }
       int cp=GBK.indexToCodePoint(pointer);
       if(pointer==-1){
     stream.reset(); // 'decrease byte pointer by one'
       }
       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==0x80){
     buffer[offset++]=(0x20AC);
     count++;
     length--;
     continue;
       } else if(b<=0xFE){
     stream.mark(4);
     gbk1=b;
       } else {
     int o=error.emitDecoderError(buffer, offset, length);
     offset+=o;
     count+=o;
     length-=o;
     continue;
       }
     }
     return (count==0) ? -1 : count;
 }