From 7d647c5d1cc90603aedde9a3d7f3377371c038fd Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Thu, 10 Nov 2022 13:43:39 -0600 Subject: [PATCH] Fixes for CESU-8 * Use sStart and oStart for transcoding in and out offsets. * Do not override state init. --- .../transcode/TranscodeFunctions.java | 24 +++++++++---------- .../specific/To_CESU_8_Transcoder.java | 11 --------- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/src/org/jcodings/transcode/TranscodeFunctions.java b/src/org/jcodings/transcode/TranscodeFunctions.java index ee790d4..a922bd4 100644 --- a/src/org/jcodings/transcode/TranscodeFunctions.java +++ b/src/org/jcodings/transcode/TranscodeFunctions.java @@ -15,23 +15,23 @@ public class TranscodeFunctions { public static final int LE = 2; public static int funSoToCESU8(byte[] statep, byte[] s, int sStart, int l, byte[] o, int oStart, int osize) { - long scalar = ((s[0] & 0x07) << 18) | ((s[1] & 0x3F) << 12) | ((s[2] & 0x3F) << 6) | (s[3] & 0x3F); + long scalar = ((toUnsignedInt(s[sStart+0]) & 0x07) << 18) | ((toUnsignedInt(s[sStart+1]) & 0x3F) << 12) | ((toUnsignedInt(s[sStart+2]) & 0x3F) << 6) | (toUnsignedInt(s[sStart+3]) & 0x3F); scalar -= 0x10000; - o[0] = (byte)0xED; - o[1] = (byte)(0xA0 | (scalar >> 16)); - o[2] = (byte)(0x80 | ((scalar >> 10) & 0x3F)); - o[3] = (byte)0xED; - o[4] = (byte)(0xB0 | ((scalar >> 6) & 0x0F)); - o[5] = (byte)(0x80 | (scalar & 0x3F)); + o[oStart+0] = (byte)0xED; + o[oStart+1] = (byte)(0xA0 | (scalar >> 16)); + o[oStart+2] = (byte)(0x80 | ((scalar >> 10) & 0x3F)); + o[oStart+3] = (byte)0xED; + o[oStart+4] = (byte)(0xB0 | ((scalar >> 6) & 0x0F)); + o[oStart+5] = (byte)(0x80 | (scalar & 0x3F)); return 6; } public static int funSoFromCESU8(byte[] statep, byte[] s, int sStart, int l, byte[] o, int oStart, int osize) { - long scalar = (((s[1] & 0x0F) << 16) | ((s[2] & 0x3F) << 10) | ((s[4] & 0x0F) << 6) | (s[5] & 0x3F)) + 0x10000; - o[0] = (byte)(0xF0 | (scalar >> 18)); - o[1] = (byte)(0x80 | ((scalar >> 12) & 0x3F)); - o[2] = (byte)(0x80 | ((scalar >> 6) & 0x3F)); - o[3] = (byte)(0x80 | (scalar & 0x3F)); + long scalar = (((toUnsignedInt(s[sStart+1]) & 0x0F) << 16) | ((toUnsignedInt(s[sStart+2]) & 0x3F) << 10) | ((toUnsignedInt(s[sStart+4]) & 0x0F) << 6) | (toUnsignedInt(s[sStart+5]) & 0x3F)) + 0x10000; + o[oStart+0] = (byte)(0xF0 | (scalar >> 18)); + o[oStart+1] = (byte)(0x80 | ((scalar >> 12) & 0x3F)); + o[oStart+2] = (byte)(0x80 | ((scalar >> 6) & 0x3F)); + o[oStart+3] = (byte)(0x80 | (scalar & 0x3F)); return 4; } diff --git a/src/org/jcodings/transcode/specific/To_CESU_8_Transcoder.java b/src/org/jcodings/transcode/specific/To_CESU_8_Transcoder.java index ce5824a..cb72c8d 100644 --- a/src/org/jcodings/transcode/specific/To_CESU_8_Transcoder.java +++ b/src/org/jcodings/transcode/specific/To_CESU_8_Transcoder.java @@ -30,17 +30,6 @@ protected To_CESU_8_Transcoder () { public static final Transcoder INSTANCE = new To_CESU_8_Transcoder(); - @Override - public boolean hasStateInit() { - return true; - } - - @Override - public int stateInit(byte[] statep) { - statep[0] = 0; - return 0; - } - @Override public int startToOutput(byte[] statep, byte[] s, int sStart, int l, byte[] o, int oStart, int oSize) { return TranscodeFunctions.funSoToCESU8(statep, s, sStart, l, o, oStart, oSize);