| 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\Git; |
| 39 | |
| 40 | /** |
| 41 | * パックの差分(デルタ) |
| 42 | */ |
| 43 | final class Delta { |
| 44 | /** 可変長の大きさ(7ビットずつ、小さい方から) */ |
| 45 | public static function size(string $delta, int &$pos): int { |
| 46 | $n = 0; |
| 47 | $shift = 0; |
| 48 | $len = strlen($delta); |
| 49 | do { |
| 50 | if ($pos >= $len) throw new GitException('壊れた差分'); |
| 51 | $b = ord($delta[$pos++]); |
| 52 | $n |= ($b & 0x7f) << $shift; |
| 53 | $shift += 7; |
| 54 | } while ($b & 0x80); |
| 55 | return $n; |
| 56 | } |
| 57 | |
| 58 | /** 差分を当てる */ |
| 59 | public static function apply(string $base, string $delta): string { |
| 60 | $pos = 0; |
| 61 | $srcSize = self::size($delta, $pos); |
| 62 | $dstSize = self::size($delta, $pos); |
| 63 | if ($srcSize !== strlen($base)) throw new GitException('差分の元の大きさが違います'); |
| 64 | $out = ''; |
| 65 | $len = strlen($delta); |
| 66 | while ($pos < $len) { |
| 67 | $op = ord($delta[$pos++]); |
| 68 | if ($op & 0x80) { |
| 69 | // 元からコピー |
| 70 | $off = 0; |
| 71 | $size = 0; |
| 72 | for ($i = 0; $i < 4; $i++) if ($op & (1 << $i)) $off |= ord($delta[$pos++]) << (8 * $i); |
| 73 | for ($i = 0; $i < 3; $i++) if ($op & (0x10 << $i)) $size |= ord($delta[$pos++]) << (8 * $i); |
| 74 | if ($size === 0) $size = 0x10000; |
| 75 | if ($off + $size > $srcSize) throw new GitException('壊れた差分(範囲外)'); |
| 76 | $out .= substr($base, $off, $size); |
| 77 | } else if ($op > 0) { |
| 78 | // そのまま挿入 |
| 79 | if ($pos + $op > $len) throw new GitException('壊れた差分(短い)'); |
| 80 | $out .= substr($delta, $pos, $op); |
| 81 | $pos += $op; |
| 82 | } else { |
| 83 | throw new GitException('壊れた差分(不明な命令)'); |
| 84 | } |
| 85 | } |
| 86 | if (strlen($out) !== $dstSize) throw new GitException('差分の結果の大きさが違います'); |
| 87 | return $out; |
| 88 | } |
| 89 | |
| 90 | /** 差分の結果の大きさ */ |
| 91 | public static function targetSize(string $deltaHead): int { |
| 92 | $pos = 0; |
| 93 | self::size($deltaHead, $pos); |
| 94 | return self::size($deltaHead, $pos); |
| 95 | } |
| 96 | } |