1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2022 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Encodings; 21 22use function chr; 23use function intdiv; 24use function ord; 25 26/** 27 * Convert between UTF-16LE and UTF-8. 28 */ 29class UTF16LE extends AbstractUTF16Encoding 30{ 31 public const NAME = 'UTF-16LE'; 32 33 public const BYTE_ORDER_MARK = "\xFF\xFE"; 34 public const REPLACEMENT_CHARACTER = "\xFD\xFF"; 35 36 /** 37 * Convert two bytes to a code-point, taking care of byte-order. 38 * 39 * @param string $character 40 * 41 * @return int 42 */ 43 protected function characterToCodePoint(string $character): int 44 { 45 return ord($character[0]) + 256 * ord($character[1]); 46 } 47 48 /** 49 * Convert a code-point to two bytes, taking care of byte-order. 50 * 51 * @param int $code_point 52 * 53 * @return string 54 */ 55 protected function codePointToCharacter(int $code_point): string 56 { 57 if ($code_point >= 0xD800 && $code_point <= 0xDFFF) { 58 return self::REPLACEMENT_CHARACTER; 59 } 60 61 return chr($code_point % 256) . chr(intdiv($code_point, 256)); 62 } 63} 64