Skip to content

Commit

Permalink
Fixes for CESU-8
Browse files Browse the repository at this point in the history
* Use sStart and oStart for transcoding in and out offsets.
* Do not override state init.
  • Loading branch information
headius committed Nov 10, 2022
1 parent c1e3097 commit 7d647c5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
24 changes: 12 additions & 12 deletions src/org/jcodings/transcode/TranscodeFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
11 changes: 0 additions & 11 deletions src/org/jcodings/transcode/specific/To_CESU_8_Transcoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 7d647c5

Please sign in to comment.