From f228805a3e83bcfa698380a39266f7e0f2540e6e Mon Sep 17 00:00:00 2001 From: Olyutorskii Date: Thu, 1 Jun 2017 19:01:53 +0900 Subject: [PATCH] add irregular SJIS case test --- .../jindolf/parser/ContentBuilderSJ.java | 24 +++++++++++---- .../jp/sourceforge/jindolf/parser/SjisDecoder.java | 9 +++--- .../jindolf/parser/SjisDecoderTest.java | 35 ++++++++++++++++++++++ 3 files changed, 58 insertions(+), 10 deletions(-) diff --git a/src/main/java/jp/sourceforge/jindolf/parser/ContentBuilderSJ.java b/src/main/java/jp/sourceforge/jindolf/parser/ContentBuilderSJ.java index bcb14a8..50c3cc5 100644 --- a/src/main/java/jp/sourceforge/jindolf/parser/ContentBuilderSJ.java +++ b/src/main/java/jp/sourceforge/jindolf/parser/ContentBuilderSJ.java @@ -96,21 +96,34 @@ public class ContentBuilderSJ extends ContentBuilder{ @Override public void decodingError(byte[] errorArray, int offset, int length) throws DecodeException{ + DecodedContent text = getContent(); + + switch(length){ + case 1: + text.addDecodeError(errorArray[0]); + return; + case 2: + text.addDecodeError(errorArray[0], errorArray[1]); + return; + default: + break; + } + int limit = offset + length; for(int bpos = offset; bpos < limit; bpos++){ byte bval = errorArray[bpos]; if(this.hasByte1st){ if(ShiftJis.isShiftJIS2ndByte(bval)){ // 文字集合エラー - getContent().addDecodeError(this.byte1st, bval); + text.addDecodeError(this.byte1st, bval); this.hasByte1st = false; }else if(ShiftJis.isShiftJIS1stByte(bval)){ - getContent().addDecodeError(this.byte1st); + text.addDecodeError(this.byte1st); this.byte1st = bval; this.hasByte1st = true; }else{ - getContent().addDecodeError(this.byte1st); - getContent().addDecodeError(bval); + text.addDecodeError(this.byte1st); + text.addDecodeError(bval); this.hasByte1st = false; } }else{ @@ -118,10 +131,9 @@ public class ContentBuilderSJ extends ContentBuilder{ this.byte1st = bval; this.hasByte1st = true; }else{ - getContent().addDecodeError(bval); + text.addDecodeError(bval); } } - } return; diff --git a/src/main/java/jp/sourceforge/jindolf/parser/SjisDecoder.java b/src/main/java/jp/sourceforge/jindolf/parser/SjisDecoder.java index eb19be5..3353baa 100644 --- a/src/main/java/jp/sourceforge/jindolf/parser/SjisDecoder.java +++ b/src/main/java/jp/sourceforge/jindolf/parser/SjisDecoder.java @@ -47,6 +47,7 @@ public class SjisDecoder extends StreamDecoder{ return; } + /** * 1バイトのエラーをUnmap系2バイトエラーに統合できないか試す。 * @@ -57,7 +58,7 @@ public class SjisDecoder extends StreamDecoder{ * * @param result デコード異常系 * @return 修正されたデコード異常系。修正がなければ引数と同じものを返す。 - * @throws IOException 入力エラー + * @throws IOException 追加入力エラー */ private CoderResult modify1ByteError(CoderResult result) throws IOException { @@ -110,10 +111,10 @@ public class SjisDecoder extends StreamDecoder{ byte next = inbuffer.get(nextPos); CoderResult newResult; - if( ! ShiftJis.isShiftJIS(curr, next) ){ - newResult = CoderResult.malformedForLength(1); - }else{ + if( ShiftJis.isShiftJIS(curr, next) ){ newResult = result; + }else{ + newResult = CoderResult.malformedForLength(1); } return newResult; diff --git a/src/test/java/jp/sourceforge/jindolf/parser/SjisDecoderTest.java b/src/test/java/jp/sourceforge/jindolf/parser/SjisDecoderTest.java index 3074561..55271c8 100644 --- a/src/test/java/jp/sourceforge/jindolf/parser/SjisDecoderTest.java +++ b/src/test/java/jp/sourceforge/jindolf/parser/SjisDecoderTest.java @@ -146,6 +146,41 @@ public class SjisDecoderTest { // GOOD } + sjd = new SjisDecoder(4, 10); + sjd.setDecodeHandler(handler); + is = byteIs("41:8540:42"); + handler.clear(); + sjd.decode(is); + assertEquals("[ST][CH]A[ER]8540[CH]B[EN]", handler.toString()); + + sjd = new SjisDecoder(4, 10); + sjd.setDecodeHandler(handler); + is = byteIs("414243:8540:44"); + handler.clear(); + sjd.decode(is); + assertEquals("[ST][CH]ABC[ER]8540[CH]D[EN]", handler.toString()); + + sjd = new SjisDecoder(4, 10); + sjd.setDecodeHandler(handler); + is = byteIs("414243:85"); + handler.clear(); + sjd.decode(is); + assertEquals("[ST][CH]ABC[ER]85[EN]", handler.toString()); + + sjd = new SjisDecoder(4, 10); + sjd.setDecodeHandler(handler); + is = byteIs("41:ff32:42"); + handler.clear(); + sjd.decode(is); + assertEquals("[ST][CH]A[ER]ff[CH]2B[EN]", handler.toString()); + + sjd = new SjisDecoder(4, 10); + sjd.setDecodeHandler(handler); + is = byteIs("414243:ff32:44"); + handler.clear(); + sjd.decode(is); + assertEquals("[ST][CH]ABC[ER]ff[CH]2D[EN]", handler.toString()); + return; } -- 2.11.0