@@ -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 ) {
0 commit comments