Skip to content

Commit 735e04e

Browse files
authored
fix: correctly encode kanji bytes
1 parent 151a958 commit 735e04e

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/Encoder/Encoder.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ private static function isOnlyDoubleByteKanji(string $content) : bool
222222
}
223223

224224
for ($i = 0; $i < $length; $i += 2) {
225-
$byte = $bytes[$i] & 0xff;
225+
$byte = ord($bytes[$i]) & 0xff;
226226

227227
if (($byte < 0x81 || $byte > 0x9f) && $byte < 0xe0 || $byte > 0xeb) {
228228
return false;
@@ -634,17 +634,23 @@ private static function append8BitBytes(string $content, BitArray $bits, string
634634
*/
635635
private static function appendKanjiBytes(string $content, BitArray $bits) : void
636636
{
637-
if (strlen($content) % 2 > 0) {
637+
$bytes = @iconv('utf-8', 'SHIFT-JIS', $content);
638+
639+
if (false === $bytes) {
640+
throw new WriterException('Content could not be converted to SHIFT-JIS');
641+
}
642+
643+
if (strlen($bytes) % 2 > 0) {
638644
// We just do a simple length check here. The for loop will check
639645
// individual characters.
640646
throw new WriterException('Content does not seem to be encoded in SHIFT-JIS');
641647
}
642648

643-
$length = strlen($content);
649+
$length = strlen($bytes);
644650

645651
for ($i = 0; $i < $length; $i += 2) {
646-
$byte1 = ord($content[$i]) & 0xff;
647-
$byte2 = ord($content[$i + 1]) & 0xff;
652+
$byte1 = ord($bytes[$i]) & 0xff;
653+
$byte2 = ord($bytes[$i + 1]) & 0xff;
648654
$code = ($byte1 << 8) | $byte2;
649655

650656
if ($code >= 0x8140 && $code <= 0x9ffc) {

test/Encoder/EncoderTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,11 @@ public function testAppendBytes() : void
287287
$this->assertSame(' .XX....X .XX...X. .XX...XX', (string) $bits);
288288

289289
// Should use appendKanjiBytes.
290-
// 0x93, 0x5f
290+
// 0x93, 0x5f :点
291291
$bits = new BitArray();
292292
$this->methods['appendBytes']->invoke(
293293
null,
294-
"\x93\x5f",
294+
'',
295295
Mode::KANJI(),
296296
$bits,
297297
Encoder::DEFAULT_BYTE_MODE_ENCODING
@@ -477,12 +477,12 @@ public function testAppend8BitBytes() : void
477477

478478
public function testAppendKanjiBytes() : void
479479
{
480-
// Numbers are from page 21 of JISX0510:2004
480+
// Numbers are from page 21 of JISX0510:2004 点 and 茗
481481
$bits = new BitArray();
482-
$this->methods['appendKanjiBytes']->invoke(null, "\x93\x5f", $bits);
482+
$this->methods['appendKanjiBytes']->invoke(null, '', $bits);
483483
$this->assertSame(' .XX.XX.. XXXXX', (string) $bits);
484484

485-
$this->methods['appendKanjiBytes']->invoke(null, "\xe4\xaa", $bits);
485+
$this->methods['appendKanjiBytes']->invoke(null, '', $bits);
486486
$this->assertSame(' .XX.XX.. XXXXXXX. X.X.X.X. X.', (string) $bits);
487487
}
488488

0 commit comments

Comments
 (0)