self::MAX_LENGTH || \preg_match('/^b[a-z2-7]+$/', $value) !== 1) { return FALSE; } $bytes = $this->decodeBase32(\substr($value, 1)); if ($bytes === NULL) { return FALSE; } $components = $this->parseComponents($bytes); return ( $components !== NULL && \in_array($components['codec'], [0x55, 0x71], TRUE) && $components['hashCode'] === 0x12 && $components['digestLength'] === 32 ); } /** * Determines whether a value is any structurally valid CIDv0 or CIDv1. */ public function isValidGeneral(string $value): bool { if ($value === '' || \strlen($value) > self::MAX_LENGTH) { return FALSE; } if (\str_starts_with($value, 'Qm')) { $bytes = $this->decodeBase58($value); return $bytes !== NULL && \strlen($bytes) === 34 && \ord($bytes[0]) === 0x12 && \ord($bytes[1]) === 32; } if (\preg_match('/^b[a-z2-7]+$/', $value) !== 1) { return FALSE; } $bytes = $this->decodeBase32(\substr($value, 1)); return $bytes !== NULL && $this->parseComponents($bytes) !== NULL; } /** * Determines whether a CID is valid for an ATProto blob reference. */ public function isValidBlobCid(string $value): bool { if (!$this->isValid($value)) { return FALSE; } $bytes = $this->decodeBase32(\substr($value, 1)); if ($bytes === NULL) { return FALSE; } $components = $this->parseComponents($bytes); return $components !== NULL && $components['codec'] === 0x55; } /** * @return array{codec: int, hashCode: int, digestLength: int}|null */ private function parseComponents(string $bytes): ?array { $offset = 0; $version = $this->readVarint($bytes, $offset); $codec = $this->readVarint($bytes, $offset); $hashCode = $this->readVarint($bytes, $offset); $digestLength = $this->readVarint($bytes, $offset); if ( $version !== 1 || $codec === NULL || $hashCode === NULL || $digestLength === NULL || ($offset + $digestLength) !== \strlen($bytes) ) { return NULL; } return [ 'codec' => $codec, 'hashCode' => $hashCode, 'digestLength' => $digestLength, ]; } /** * Decodes unpadded lowercase RFC 4648 base32. */ private static function decodeBase32(string $encoded): ?string { $buffer = 0; $bits = 0; $decoded = ''; foreach (\str_split($encoded) as $character) { $value = \strpos(self::BASE32_ALPHABET, $character); if ($value === FALSE) { return NULL; } $buffer = ($buffer << 5) | $value; $bits += 5; while ($bits >= 8) { $bits -= 8; $decoded .= \chr(($buffer >> $bits) & 0xFF); $buffer &= (1 << $bits) - 1; } } return $bits === 0 || $buffer === 0 ? $decoded : NULL; } private static function decodeBase58(string $encoded): ?string { // cspell:disable-next-line $alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; $bytes = [0]; foreach (\str_split($encoded) as $character) { $digit = \strpos($alphabet, $character); if ($digit === FALSE) { return NULL; } $carry = $digit; for ($index = \count($bytes) - 1; $index >= 0; $index--) { $carry += $bytes[$index] * 58; $bytes[$index] = $carry & 0xFF; $carry >>= 8; } while ($carry > 0) { \array_unshift($bytes, $carry & 0xFF); $carry >>= 8; } } foreach (\str_split($encoded) as $character) { if ($character !== '1') { break; } \array_unshift($bytes, 0); } return \implode('', \array_map(static fn (int $byte): string => \chr($byte), $bytes)); } /** * Reads one canonical unsigned varint and advances the byte offset. */ private static function readVarint(string $bytes, int &$offset): ?int { $value = 0; $shift = 0; $length = \strlen($bytes); $consumed = 0; while ($offset < $length && $consumed < 9) { $byte = \ord($bytes[$offset++]); $payload = $byte & 0x7F; $value |= $payload << $shift; $consumed++; if (($byte & 0x80) === 0) { return $consumed > 1 && $payload === 0 ? NULL : $value; } $shift += 7; } return NULL; } }