|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-only |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OC\Snowflake; |
| 11 | + |
| 12 | +use OCP\Snowflake\IDecoder; |
| 13 | +use OCP\Snowflake\IGenerator; |
| 14 | +use Override; |
| 15 | + |
| 16 | +/** |
| 17 | + * Nextcloud Snowflake ID |
| 18 | + * |
| 19 | + * Get information about Snowflake Id |
| 20 | + * |
| 21 | + * @since 33.0.0 |
| 22 | + */ |
| 23 | +final class Decoder implements IDecoder { |
| 24 | + #[Override] |
| 25 | + public function decode(string $snowflakeId): array { |
| 26 | + if (!ctype_digit($snowflakeId)) { |
| 27 | + throw new \Exception('Invalid Snowflake ID: ' . $snowflakeId); |
| 28 | + } |
| 29 | + |
| 30 | + /** @var array{seconds: positive-int, milliseconds: int<0,999>, serverId: int<0, 1023>, sequenceId: int<0,4095>, isCli: bool} $data */ |
| 31 | + $data = PHP_INT_SIZE === 8 |
| 32 | + ? $this->decode64bits((int)$snowflakeId) |
| 33 | + : $this->decode32bits($snowflakeId); |
| 34 | + |
| 35 | + $data['createdAt'] = new \DateTimeImmutable( |
| 36 | + sprintf( |
| 37 | + '@%d.%03d', |
| 38 | + $data['seconds'] + IGenerator::TS_OFFSET + intdiv($data['milliseconds'], 1000), |
| 39 | + $data['milliseconds'] % 1000, |
| 40 | + ) |
| 41 | + ); |
| 42 | + |
| 43 | + return $data; |
| 44 | + } |
| 45 | + |
| 46 | + private function decode64bits(int $snowflakeId): array { |
| 47 | + $firstHalf = $snowflakeId >> 32; |
| 48 | + $secondHalf = $snowflakeId & 0xFFFFFFFF; |
| 49 | + |
| 50 | + $seconds = $firstHalf & 0x7FFFFFFF; |
| 51 | + $milliseconds = $secondHalf >> 22; |
| 52 | + |
| 53 | + return [ |
| 54 | + 'seconds' => $seconds, |
| 55 | + 'milliseconds' => $milliseconds, |
| 56 | + 'serverId' => ($secondHalf >> 13) & 0x1FF, |
| 57 | + 'sequenceId' => $secondHalf & 0xFFF, |
| 58 | + 'isCli' => (bool)(($secondHalf >> 12) & 0x1), |
| 59 | + ]; |
| 60 | + } |
| 61 | + |
| 62 | + private function decode32bits(string $snowflakeId): array { |
| 63 | + $id = $this->convertBase16($snowflakeId); |
| 64 | + |
| 65 | + $firstQuarter = (int)hexdec(substr($id, 0, 4)); |
| 66 | + $secondQuarter = (int)hexdec(substr($id, 4, 4)); |
| 67 | + $thirdQuarter = (int)hexdec(substr($id, 8, 4)); |
| 68 | + $fourthQuarter = (int)hexdec(substr($id, 12, 4)); |
| 69 | + |
| 70 | + $seconds = (($firstQuarter & 0x7FFF) << 16) | ($secondQuarter & 0xFFFF); |
| 71 | + $milliseconds = ($thirdQuarter >> 6) & 0x3FF; |
| 72 | + |
| 73 | + return [ |
| 74 | + 'seconds' => $seconds, |
| 75 | + 'milliseconds' => $milliseconds, |
| 76 | + 'serverId' => (($thirdQuarter & 0x3F) << 3) | (($fourthQuarter >> 13) & 0x7), |
| 77 | + 'sequenceId' => $fourthQuarter & 0xFFF, |
| 78 | + 'isCli' => (bool)(($fourthQuarter >> 12) & 0x1), |
| 79 | + ]; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Convert base 10 number to base 16, padded to 16 characters |
| 84 | + * |
| 85 | + * Required on 32 bits systems as base_convert will lose precision with large numbers |
| 86 | + */ |
| 87 | + private function convertBase16(string $decimal): string { |
| 88 | + $hex = ''; |
| 89 | + $digits = '0123456789ABCDEF'; |
| 90 | + |
| 91 | + while (strlen($decimal) > 0 && $decimal !== '0') { |
| 92 | + $remainder = 0; |
| 93 | + $newDecimal = ''; |
| 94 | + |
| 95 | + // Perform division by 16 manually for arbitrary precision |
| 96 | + for ($i = 0; $i < strlen($decimal); $i++) { |
| 97 | + $digit = (int)$decimal[$i]; |
| 98 | + $current = $remainder * 10 + $digit; |
| 99 | + |
| 100 | + if ($current >= 16) { |
| 101 | + $quotient = (int)($current / 16); |
| 102 | + $remainder = $current % 16; |
| 103 | + $newDecimal .= chr(ord('0') + $quotient); |
| 104 | + } else { |
| 105 | + $remainder = $current; |
| 106 | + // Only add quotient digit if we already have some digits in result |
| 107 | + if (strlen($newDecimal) > 0) { |
| 108 | + $newDecimal .= '0'; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Add the remainder (0-15) as hex digit |
| 114 | + $hex = $digits[$remainder] . $hex; |
| 115 | + |
| 116 | + // Update decimal for next iteration |
| 117 | + $decimal = ltrim($newDecimal, '0'); |
| 118 | + } |
| 119 | + |
| 120 | + return str_pad($hex, 16, '0', STR_PAD_LEFT); |
| 121 | + } |
| 122 | +} |
0 commit comments