| 1 | <?php |
| 2 | /************************************************************* |
| 3 | # 076 License |
| 4 | |
| 5 | Copyright (c) テクニカル諏訪子 |
| 6 | |
| 7 | Permission is hereby granted to any person obtaining a copy of the software |
| 8 | Little Beast (the "Software") to use, modify, merge, copy, publish, distribute, |
| 9 | sublicense, and/or sell copies of the Software, subject to the following conditions: |
| 10 | |
| 11 | 1. **Origin Attribution**: |
| 12 | - You must not misrepresent the origin of the Software; you must not claim |
| 13 | you created the original Software. |
| 14 | - If the Software is used in a product, you must either: |
| 15 | a. Provide clear attribution in the product's documentation, user interface, |
| 16 | or other visible areas, **OR** |
| 17 | b. Pay the original developers a fee they specify in writing. |
| 18 | 2. **Usage Restriction**: |
| 19 | - The Software, or any derivative works, dependencies, or libraries |
| 20 | incorporating it, must not be used for censorship or to suppress freedom of |
| 21 | speech, expression, or creativity. Prohibited uses include, but are not |
| 22 | limited to: |
| 23 | - Censorship of so-called "hate speech", visuals, non-mainstream opinions, |
| 24 | ideas, or objective reality. |
| 25 | - Tools or systems designed to restrict access to information or |
| 26 | artistic works. |
| 27 | 3. **Notice Preservation**: |
| 28 | - This license and the above copyright notice must remain intact in all copies |
| 29 | of the source code. |
| 30 | |
| 31 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 32 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 33 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 34 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 35 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE |
| 36 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 37 | */ |
| 38 | namespace Std\Lib; |
| 39 | |
| 40 | /** |
| 41 | * Base32とBase64(RFC 4648) |
| 42 | * |
| 43 | * Base32は二段階認証の秘密鍵(大文字・小文字、空白、ハイフン、パディングの有無は問わずに読む)、 |
| 44 | * Base64はURL用の変種(「-」と「_」)も使える。 |
| 45 | */ |
| 46 | final class BaseN { |
| 47 | private const B32 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'; |
| 48 | private const B64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; |
| 49 | private const B64URL = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'; |
| 50 | |
| 51 | public static function encode32(string $data, bool $pad = true): string { |
| 52 | return self::encode($data, self::B32, 5, $pad ? 8 : 1); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @throws \InvalidArgumentException Base32ではない文字、又は有り得ない長さ |
| 57 | */ |
| 58 | public static function decode32(string $text): string { |
| 59 | $text = rtrim(strtoupper(str_replace([' ', '-', "\t", "\r", "\n"], '', $text)), '='); |
| 60 | // 8文字(40ビット)の内、1・3・6文字で終わる事は無い |
| 61 | if (in_array(strlen($text) % 8, [1, 3, 6], true)) throw new \InvalidArgumentException('Base32の長さが不正です。'); |
| 62 | return self::decode($text, self::B32, 5); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @param bool $url URL用(「+/」の代わりに「-_」) |
| 67 | */ |
| 68 | public static function encode64(string $data, bool $url = false, bool $pad = true): string { |
| 69 | return self::encode($data, $url ? self::B64URL : self::B64, 6, $pad ? 4 : 1); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @throws \InvalidArgumentException Base64ではない文字、又は有り得ない長さ |
| 74 | */ |
| 75 | public static function decode64(string $text, bool $url = false): string { |
| 76 | $text = rtrim(str_replace([' ', "\t", "\r", "\n"], '', $text), '='); |
| 77 | if (strlen($text) % 4 === 1) throw new \InvalidArgumentException('Base64の長さが不正です。'); |
| 78 | return self::decode($text, $url ? self::B64URL : self::B64, 6); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @param int $bits 1文字のビット数 |
| 83 | * @param int $block パディングで揃える文字数(1なら付けない) |
| 84 | */ |
| 85 | private static function encode(string $data, string $alphabet, int $bits, int $block): string { |
| 86 | $out = ''; |
| 87 | $acc = 0; |
| 88 | $n = 0; |
| 89 | $mask = (1 << $bits) - 1; |
| 90 | for ($i = 0, $len = strlen($data); $i < $len; $i++) { |
| 91 | $acc = (($acc << 8) | ord($data[$i])) & 0xffff; |
| 92 | $n += 8; |
| 93 | while ($n >= $bits) { |
| 94 | $n -= $bits; |
| 95 | $out .= $alphabet[($acc >> $n) & $mask]; |
| 96 | } |
| 97 | } |
| 98 | if ($n > 0) $out .= $alphabet[($acc << ($bits - $n)) & $mask]; |
| 99 | $rest = strlen($out) % $block; |
| 100 | return $rest ? $out.str_repeat('=', $block - $rest) : $out; |
| 101 | } |
| 102 | |
| 103 | private static function decode(string $text, string $alphabet, int $bits): string { |
| 104 | $out = ''; |
| 105 | $acc = 0; |
| 106 | $n = 0; |
| 107 | for ($i = 0, $len = strlen($text); $i < $len; $i++) { |
| 108 | $v = strpos($alphabet, $text[$i]); |
| 109 | if ($v === false) throw new \InvalidArgumentException('不正な文字があります。'); |
| 110 | $acc = (($acc << $bits) | $v) & 0xffff; |
| 111 | $n += $bits; |
| 112 | if ($n >= 8) { |
| 113 | $n -= 8; |
| 114 | $out .= chr(($acc >> $n) & 0xff); |
| 115 | } |
| 116 | } |
| 117 | return $out; |
| 118 | } |
| 119 | } |