Phase 0: Fundament für Inhaus
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m14s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m12s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 1m7s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 1m19s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 1m32s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 1m22s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 1m32s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 1m35s
CI / Build-Artefakte aktuell (pull_request) Failing after 1m7s
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m14s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m12s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 1m7s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 1m19s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 1m32s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 1m22s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 1m32s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 1m35s
CI / Build-Artefakte aktuell (pull_request) Failing after 1m7s
- Laravel 13 mit Filament 5 (Admin), Inertia 3 + Vue 3 + Vuetify 4 (Material 3) - Anmeldung über Fortify (Login, Passwort vergessen/ändern), keine Selbstregistrierung - Profil mit Profilbild (WebP in festen Größen), Personenseiten, Header mit Avatar-Menü - Material-3-Farbschema aus der Primärfarbe, hell/dunkel/System pro Person - Basis-Branding (Portalname, Logo, Primärfarbe) im Admin unter „Erscheinungsbild“ - Benutzerverwaltung im Admin, Zugang vorläufig über is_admin - vendor/ (ohne Dev-Pakete) und Frontend-Build mitversioniert, Dev-Pakete in vendor-dev/ - PHPUnit-Tests gegen MySQL/MariaDB, Forgejo-CI-Matrix, Docker-Entwicklungsumgebung - VERSION, CHANGELOG.md und CONTRIBUTING.md mit Release-Ablauf Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
7197eaebf2
commit
9c0f8de791
15771 changed files with 1564103 additions and 5 deletions
44
vendor/spomky-labs/cbor-php/.github/CONTRIBUTING.md
vendored
Normal file
44
vendor/spomky-labs/cbor-php/.github/CONTRIBUTING.md
vendored
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
# Contributing
|
||||
|
||||
First of all, **thank you** for contributing.
|
||||
|
||||
Bugs or feature requests can be posted online on the GitHub issues section of the project.
|
||||
|
||||
Few rules to ease code reviews and merges:
|
||||
|
||||
- You MUST follow the [PSR-12](http://www.php-fig.org/psr/psr-12/) coding standards.
|
||||
- You MUST run the test suite.
|
||||
- You MUST write (or update) unit tests when bugs are fixed or features are added.
|
||||
- You SHOULD write documentation.
|
||||
|
||||
We use [Git-Flow](http://jeffkreeftmeijer.com/2010/why-arent-you-using-git-flow/) to automate our git branching
|
||||
workflow.
|
||||
|
||||
To contribute use [Pull Requests](https://help.github.com/articles/using-pull-requests), please, write commit messages
|
||||
that make sense, and rebase your branch before submitting your PR.
|
||||
|
||||
May be asked to squash your commits too. This is used to "clean" your Pull Request before merging it, avoiding commits
|
||||
such as fix tests, fix 2, fix 3, etc.
|
||||
|
||||
Run test suite
|
||||
------------
|
||||
|
||||
* install Composer by following the [official installation instructions](https://getcomposer.org/download/), which
|
||||
verify the installer signature before running it. Never pipe the installer straight into `php`.
|
||||
* install dependencies: `composer install`
|
||||
* run tests: `vendor/bin/phpunit --configuration .ci-tools/phpunit.xml.dist`
|
||||
|
||||
Run the full CI suite locally
|
||||
------------
|
||||
|
||||
The quality checks that gate a Pull Request all run through [Castor](https://castor.jolicode.com/), inside the same
|
||||
`ghcr.io/spomky-labs/phpqa` image the CI uses, so the tool versions match:
|
||||
|
||||
```sh
|
||||
castor ecs # coding standards (castor ecs_fix to apply)
|
||||
castor rector # automated refactoring, dry-run (castor rector_fix to apply)
|
||||
castor phpstan # static analysis
|
||||
castor deptrac # architecture layers
|
||||
castor lint # syntax check
|
||||
castor phpunit # tests, with coverage
|
||||
```
|
||||
2
vendor/spomky-labs/cbor-php/.github/FUNDING.yml
vendored
Normal file
2
vendor/spomky-labs/cbor-php/.github/FUNDING.yml
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
github: Spomky
|
||||
patreon: FlorentMorselli
|
||||
42
vendor/spomky-labs/cbor-php/.github/ISSUE_TEMPLATE/1_Bug_report.yaml
vendored
Normal file
42
vendor/spomky-labs/cbor-php/.github/ISSUE_TEMPLATE/1_Bug_report.yaml
vendored
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
name: 🐛 Bug Report
|
||||
description: ⚠️ NEVER report security issues, email security AT spomky-labs.com instead
|
||||
labels: Bug
|
||||
body:
|
||||
- type: input
|
||||
id: affected-versions
|
||||
attributes:
|
||||
label: Version(s) affected
|
||||
placeholder: x.y.z
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: description
|
||||
attributes:
|
||||
label: Description
|
||||
description: A clear and concise description of the problem
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: how-to-reproduce
|
||||
attributes:
|
||||
label: How to reproduce
|
||||
description: |
|
||||
⚠️ This is the most important part of the report ⚠️
|
||||
Without a way to easily reproduce your issue, there is little chance we will be able to help you and work on a fix.
|
||||
Please, take the time to show us some code and/or config that is needed for others to reproduce the problem easily.
|
||||
Most of the time, creating a "bug reproducer" is the best way to help us and increases the chances someone
|
||||
will have a look at it.
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: possible-solution
|
||||
attributes:
|
||||
label: Possible Solution
|
||||
description: |
|
||||
Optional: only if you have suggestions on a fix/reason for the bug
|
||||
Don't hesitate to create a pull request with your solution, it helps get faster feedback.
|
||||
- type: textarea
|
||||
id: additional-context
|
||||
attributes:
|
||||
label: Additional Context
|
||||
description: "Optional: any other context about the problem: log messages, screenshots, etc."
|
||||
17
vendor/spomky-labs/cbor-php/.github/ISSUE_TEMPLATE/2_Feature_request.yaml
vendored
Normal file
17
vendor/spomky-labs/cbor-php/.github/ISSUE_TEMPLATE/2_Feature_request.yaml
vendored
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
name: 🚀 Feature Request
|
||||
description: RFC and ideas for new features and improvements
|
||||
body:
|
||||
- type: textarea
|
||||
id: description
|
||||
attributes:
|
||||
label: Description
|
||||
description: A clear and concise description of the new feature
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: example
|
||||
attributes:
|
||||
label: Example
|
||||
description: |
|
||||
A simple example of the new feature in action (include PHP code, YAML config, etc.)
|
||||
If the new feature changes an existing feature, include a simple before/after comparison.
|
||||
10
vendor/spomky-labs/cbor-php/.github/ISSUE_TEMPLATE/3_Documentation.yaml
vendored
Normal file
10
vendor/spomky-labs/cbor-php/.github/ISSUE_TEMPLATE/3_Documentation.yaml
vendored
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
name: 📖 Documentation Issue
|
||||
description: To report typo or obsolete section in the documentation
|
||||
body:
|
||||
- type: textarea
|
||||
id: description
|
||||
attributes:
|
||||
label: Description
|
||||
description: A clear and concise description of the error you found in the documentation
|
||||
validations:
|
||||
required: true
|
||||
8
vendor/spomky-labs/cbor-php/.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
8
vendor/spomky-labs/cbor-php/.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
blank_issues_enabled: false
|
||||
contact_links:
|
||||
- name: Support Question
|
||||
url: https://spomky-labs.com/contact/
|
||||
about: |
|
||||
We use GitHub issues only to discuss about bugs and new features.
|
||||
For this kind of questions about using the framework or third-party bundles,
|
||||
please email us contact AT spomky-labs.com for quoting
|
||||
17
vendor/spomky-labs/cbor-php/.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
17
vendor/spomky-labs/cbor-php/.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
Target branch:
|
||||
Resolves issue # <!-- #-prefixed issue number(s), if any -->
|
||||
|
||||
<!-- replace space with "x" in square brackets: [x] -->
|
||||
- [ ] It is a Bug fix
|
||||
- [ ] It is a New feature
|
||||
- [ ] Breaks BC
|
||||
- [ ] Includes Deprecations
|
||||
|
||||
<!--
|
||||
Fill in this template according to the PR you're about to submit.
|
||||
Replace this comment by a description of what your PR is solving.
|
||||
|
||||
Please consider the following requirement:
|
||||
* Modification of existing tests should be avoided unless deemed necessary.
|
||||
* You MUST never open a PR related to a security issue. Contact Spomky in private at https://gitter.im/Spomky/
|
||||
-->
|
||||
37
vendor/spomky-labs/cbor-php/.github/actions/automatic-releases/action.yml
vendored
Normal file
37
vendor/spomky-labs/cbor-php/.github/actions/automatic-releases/action.yml
vendored
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
# Vendored copy of https://github.com/laminas/automatic-releases/blob/1.26.2/action.yml
|
||||
#
|
||||
# The upstream action pins its own Docker image to the FLOATING major tag
|
||||
# `ghcr.io/laminas/automatic-releases:1`. That tag is not a semver version: it
|
||||
# moves with every 1.x release, and GHCR publishes no `:1.26.2` manifest to
|
||||
# pin instead. So `uses: laminas/automatic-releases@1.26.2` says nothing about
|
||||
# what actually executes — whatever was last pushed to `:1` runs, with this
|
||||
# workflow's token and the release signing key in its environment.
|
||||
#
|
||||
# Vendoring the definition is the only way to name a fixed image. To refresh
|
||||
# it after reviewing an upstream release:
|
||||
#
|
||||
# docker buildx imagetools inspect ghcr.io/laminas/automatic-releases:1 \
|
||||
# --format '{{ .Manifest.Digest }}'
|
||||
#
|
||||
# and update both the digest below and the version recorded in the comment.
|
||||
|
||||
name: 'laminas/automatic-releases (digest-pinned)'
|
||||
description: 'Automates automatic releases for semver-compliant repositories'
|
||||
|
||||
inputs:
|
||||
command-name:
|
||||
description: |
|
||||
Command to execute: one of
|
||||
* `laminas:automatic-releases:release`
|
||||
* `laminas:automatic-releases:create-merge-up-pull-request`
|
||||
* `laminas:automatic-releases:switch-default-branch-to-next-minor`
|
||||
* `laminas:automatic-releases:bump-changelog`
|
||||
* `laminas:automatic-releases:create-milestones`
|
||||
required: true
|
||||
|
||||
runs:
|
||||
using: 'docker'
|
||||
# Digest of ghcr.io/laminas/automatic-releases:1 as of 2026-09-10.
|
||||
image: 'docker://ghcr.io/laminas/automatic-releases@sha256:c86f165782c462d031f7f8a5932cad237c4a7d16a57b1b0ab0e76e68e08ffba1'
|
||||
args:
|
||||
- ${{ inputs.command-name }}
|
||||
19
vendor/spomky-labs/cbor-php/.github/dependabot.yml
vendored
Normal file
19
vendor/spomky-labs/cbor-php/.github/dependabot.yml
vendored
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: "composer"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "weekly"
|
||||
day: "friday"
|
||||
versioning-strategy: "widen"
|
||||
open-pull-requests-limit: 20
|
||||
allow:
|
||||
- dependency-type: all
|
||||
labels: [ "dependencies" ]
|
||||
|
||||
- package-ecosystem: "github-actions"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "monthly"
|
||||
open-pull-requests-limit: 20
|
||||
labels: [ "dependencies" ]
|
||||
8
vendor/spomky-labs/cbor-php/.github/stale.yml
vendored
Normal file
8
vendor/spomky-labs/cbor-php/.github/stale.yml
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
daysUntilStale: 30
|
||||
daysUntilClose: 7
|
||||
staleLabel: wontfix
|
||||
markComment: >
|
||||
This issue has been automatically marked as stale because it has not had
|
||||
recent activity. It will be closed if no further activity occurs. Thank you
|
||||
for your contributions.
|
||||
closeComment: false
|
||||
266
vendor/spomky-labs/cbor-php/.github/workflows/ci.yml
vendored
Normal file
266
vendor/spomky-labs/cbor-php/.github/workflows/ci.yml
vendored
Normal file
|
|
@ -0,0 +1,266 @@
|
|||
name: 📁 PHP CI & Docker Build
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- '*.*.x'
|
||||
tags: ['*']
|
||||
pull_request: ~
|
||||
workflow_dispatch: ~
|
||||
|
||||
# Every job below only ever reads the repository. Third-party actions and the
|
||||
# `phpqa` container therefore never receive a token able to write to contents,
|
||||
# packages, issues or pull requests.
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
pre_checks:
|
||||
name: "0️⃣ Pre-checks"
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v7.0.1
|
||||
|
||||
- name: "Check file permissions"
|
||||
run: |
|
||||
NON_EXECUTABLE=$(find . -type f -not -path './.git/*' -not -path './bin/console' -not -path './frankenphp/docker-entrypoint.sh' -not -path './link' -executable)
|
||||
if [[ -n "$NON_EXECUTABLE" ]]; then
|
||||
echo "Found non-executable files with executable permission:"
|
||||
echo "$NON_EXECUTABLE"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
- name: "Find non-printable ASCII characters"
|
||||
run: |
|
||||
if LC_ALL=C.UTF-8 find . -type f -name "*.php" -print0 | xargs -0 grep -PHn "[[:cntrl:]]"; then
|
||||
echo "Non-printable ASCII characters found"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
prepare_dependencies:
|
||||
name: "📦 Prepare Composer Dependencies"
|
||||
needs: [pre_checks]
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ghcr.io/spomky-labs/phpqa:8.4
|
||||
outputs:
|
||||
cache-key: ${{ steps.cache-key-generator.outputs.key }}
|
||||
steps:
|
||||
- uses: actions/checkout@v7.0.1
|
||||
|
||||
# `composer.lock` is gitignored (this is a library), so hashing it would
|
||||
# always yield the empty hash and freeze the cache key forever. The
|
||||
# manifest is the only committed input to dependency resolution.
|
||||
- id: cache-key-generator
|
||||
run: echo "key=composer-${{ runner.os }}-${{ hashFiles('composer.json') }}" >> "$GITHUB_OUTPUT"
|
||||
|
||||
- uses: actions/cache@v6.1.0
|
||||
with:
|
||||
path: |
|
||||
vendor
|
||||
~/.composer/cache
|
||||
key: ${{ steps.cache-key-generator.outputs.key }}
|
||||
restore-keys: |
|
||||
composer-${{ runner.os }}-
|
||||
|
||||
- uses: ramsey/composer-install@4.0.0
|
||||
with:
|
||||
dependency-versions: highest
|
||||
composer-options: --optimize-autoloader
|
||||
|
||||
phpstan:
|
||||
name: "1️⃣ Static Analysis (PHPStan)"
|
||||
needs: [prepare_dependencies]
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ghcr.io/spomky-labs/phpqa:8.4
|
||||
steps:
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: actions/cache@v6.1.0
|
||||
with:
|
||||
path: |
|
||||
vendor
|
||||
~/.composer/cache
|
||||
key: ${{ needs.prepare_dependencies.outputs.cache-key }}
|
||||
- run: castor phpstan
|
||||
|
||||
ecs:
|
||||
name: "2️⃣ Coding Standards (ECS)"
|
||||
needs: [prepare_dependencies]
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ghcr.io/spomky-labs/phpqa:8.4
|
||||
steps:
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: actions/cache@v6.1.0
|
||||
with:
|
||||
path: |
|
||||
vendor
|
||||
~/.composer/cache
|
||||
key: ${{ needs.prepare_dependencies.outputs.cache-key }}
|
||||
- run: castor ecs
|
||||
|
||||
rector:
|
||||
name: "3️⃣ Refactoring (Rector)"
|
||||
needs: [prepare_dependencies]
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ghcr.io/spomky-labs/phpqa:8.4
|
||||
steps:
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: actions/cache@v6.1.0
|
||||
with:
|
||||
path: |
|
||||
vendor
|
||||
~/.composer/cache
|
||||
key: ${{ needs.prepare_dependencies.outputs.cache-key }}
|
||||
- run: castor rector
|
||||
|
||||
validate:
|
||||
name: "4️⃣ Validate composer.json"
|
||||
needs: [prepare_dependencies]
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ghcr.io/spomky-labs/phpqa:8.4
|
||||
steps:
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: actions/cache@v6.1.0
|
||||
with:
|
||||
path: |
|
||||
vendor
|
||||
~/.composer/cache
|
||||
key: ${{ needs.prepare_dependencies.outputs.cache-key }}
|
||||
- run: |
|
||||
composer dump-autoload --optimize --strict-psr
|
||||
composer validate --strict
|
||||
composer normalize --dry-run --diff || true
|
||||
|
||||
lint:
|
||||
name: "5️⃣ Syntax Check"
|
||||
needs: [prepare_dependencies]
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ghcr.io/spomky-labs/phpqa:8.4
|
||||
steps:
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- run: composer exec -- parallel-lint src tests
|
||||
|
||||
check_licenses:
|
||||
name: "6️⃣ License Check"
|
||||
needs: [prepare_dependencies]
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ghcr.io/spomky-labs/phpqa:8.4
|
||||
steps:
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: actions/cache@v6.1.0
|
||||
with:
|
||||
path: |
|
||||
vendor
|
||||
~/.composer/cache
|
||||
key: ${{ needs.prepare_dependencies.outputs.cache-key }}
|
||||
# `composer licenses` reads the lock file, which is gitignored and not
|
||||
# part of the cache. Without this install the command reports zero
|
||||
# packages and the job passes without checking anything.
|
||||
- name: Install dependencies
|
||||
run: composer install --no-interaction --no-progress
|
||||
- run: castor check-licenses
|
||||
|
||||
deptrac:
|
||||
name: "7️⃣ Architecture Layer Check (Deptrac)"
|
||||
needs: [prepare_dependencies]
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ghcr.io/spomky-labs/phpqa:8.4
|
||||
steps:
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: actions/cache@v6.1.0
|
||||
with:
|
||||
path: |
|
||||
vendor
|
||||
~/.composer/cache
|
||||
key: ${{ needs.prepare_dependencies.outputs.cache-key }}
|
||||
- run: castor deptrac
|
||||
|
||||
tests:
|
||||
name: "🧪 Unit & Functional Tests (PHP ${{ matrix.php-version }}${{ matrix.lowest-deps && ' - Lowest Deps' || '' }})"
|
||||
needs:
|
||||
- prepare_dependencies
|
||||
- phpstan
|
||||
- ecs
|
||||
- rector
|
||||
- lint
|
||||
- validate
|
||||
- check_licenses
|
||||
- deptrac
|
||||
runs-on: ubuntu-latest
|
||||
continue-on-error: ${{ matrix.experimental || false }}
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- php-version: '8.2'
|
||||
lowest-deps: true
|
||||
- php-version: '8.2'
|
||||
- php-version: '8.3'
|
||||
- php-version: '8.4'
|
||||
- php-version: '8.5'
|
||||
experimental: true
|
||||
container:
|
||||
image: ghcr.io/spomky-labs/phpqa:${{ matrix.php-version }}
|
||||
env:
|
||||
XDEBUG_MODE: 'off'
|
||||
PHP_VERSION: ${{ matrix.php-version }}
|
||||
steps:
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
if [ "${{ matrix.lowest-deps || 'false' }}" = "true" ]; then
|
||||
composer update --prefer-lowest --no-interaction --no-progress
|
||||
else
|
||||
composer install --no-interaction --no-progress
|
||||
fi
|
||||
|
||||
# PHPUnit is invoked directly rather than through `castor phpunit`: castor itself requires PHP >= 8.4 and
|
||||
# this job also runs on the older versions of the matrix. Coverage is produced by the mutation testing job.
|
||||
- name: Run PHPUnit
|
||||
run: composer exec -- phpunit-11 --configuration .ci-tools/phpunit.xml.dist
|
||||
|
||||
infection:
|
||||
name: "🧬 Mutation Testing"
|
||||
needs: [tests]
|
||||
if: ${{ endsWith(github.ref_name, '.x') }}
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: ghcr.io/spomky-labs/phpqa:8.4
|
||||
env:
|
||||
XDEBUG_MODE: coverage
|
||||
steps:
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- name: Install dependencies
|
||||
run: composer install --no-interaction --no-progress
|
||||
- name: Run PHPUnit
|
||||
run: castor phpunit
|
||||
- name: Execute Infection
|
||||
run: castor infect
|
||||
|
||||
exported_files:
|
||||
name: "8️⃣ Exported Files Check"
|
||||
needs: [prepare_dependencies]
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- name: Check exported files
|
||||
run: |
|
||||
EXPECTED="CODE_OF_CONDUCT.md,LICENSE,README.md,RELEASES.md,SECURITY.md,composer.json"
|
||||
CURRENT=$(git archive HEAD | tar --list | grep -v '/$' | grep -Ev '^(\.github|assets|bin|config|fixtures|frankenphp|migrations|public|src|templates|translations)/' | sort | paste -sd "," -)
|
||||
echo "CURRENT = ${CURRENT}"
|
||||
echo "EXPECTED = ${EXPECTED}"
|
||||
if [[ "$CURRENT" != "$EXPECTED" ]]; then
|
||||
echo "❌ Exported files do not match expected list"
|
||||
exit 1
|
||||
fi
|
||||
14
vendor/spomky-labs/cbor-php/.github/workflows/dependency-review.yml
vendored
Normal file
14
vendor/spomky-labs/cbor-php/.github/workflows/dependency-review.yml
vendored
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
name: 'Dependency Review'
|
||||
on: [pull_request]
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
dependency-review:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: 'Checkout Repository'
|
||||
uses: actions/checkout@v7.0.1
|
||||
- name: 'Dependency Review'
|
||||
uses: actions/dependency-review-action@v5.0.0
|
||||
29
vendor/spomky-labs/cbor-php/.github/workflows/lock-closed-issues.yml
vendored
Normal file
29
vendor/spomky-labs/cbor-php/.github/workflows/lock-closed-issues.yml
vendored
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
name: 'Lock Issues'
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: '12 6 * * *'
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
lock:
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
# Required to comment on and lock closed issues.
|
||||
issues: write
|
||||
steps:
|
||||
- uses: dessant/lock-threads@v6.0.2
|
||||
with:
|
||||
github-token: ${{ github.token }}
|
||||
issue-inactive-days: '31'
|
||||
exclude-issue-created-before: ''
|
||||
exclude-any-issue-labels: ''
|
||||
add-issue-labels: ''
|
||||
issue-comment: >
|
||||
This thread has been automatically locked since there has not been
|
||||
any recent activity after it was closed. Please open a new issue for
|
||||
related bugs.
|
||||
issue-lock-reason: 'resolved'
|
||||
process-only: 'issues'
|
||||
105
vendor/spomky-labs/cbor-php/.github/workflows/release-on-milestone-closed.yml
vendored
Normal file
105
vendor/spomky-labs/cbor-php/.github/workflows/release-on-milestone-closed.yml
vendored
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
name: "Automatic Releases"
|
||||
|
||||
# The five commands must stay steps of a single job: "release" runs
|
||||
# `git checkout <release branch>`, which is what makes that branch available
|
||||
# locally, and the following commands require it. Splitting them into separate
|
||||
# jobs gives each one a fresh checkout of the default branch, so every release
|
||||
# made from an older branch fails with "not a valid object name".
|
||||
#
|
||||
# The checkout must be unshallow for the same reason: "switch-default-branch-
|
||||
# to-next-minor" resolves the newest release branch locally, and the default
|
||||
# shallow clone only contains the ref that triggered the workflow.
|
||||
#
|
||||
# Every step uses the vendored ./.github/actions/automatic-releases, which is
|
||||
# laminas/automatic-releases 1.26.2 with its Docker image referenced by digest
|
||||
# instead of the floating `:1` tag — GHCR publishes no versioned manifest to
|
||||
# pin instead. See that file for the refresh procedure.
|
||||
#
|
||||
# TODO(#154): ORGANIZATION_ADMIN_TOKEN is an org-wide classic PAT with admin
|
||||
# rights over every Spomky-Labs repository. It should be replaced by a
|
||||
# fine-grained PAT scoped to this repository alone, with Contents, Pull
|
||||
# requests and Issues set to write. Only the two steps below actually need it
|
||||
# (they push branches and change the default branch); the remaining three run
|
||||
# on the workflow's own GITHUB_TOKEN.
|
||||
|
||||
on:
|
||||
milestone:
|
||||
types:
|
||||
- "closed"
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
release:
|
||||
name: "Release"
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
permissions:
|
||||
# `bump-changelog` pushes a commit to the originating release branch.
|
||||
contents: write
|
||||
# `create-milestones` opens the milestones for the next versions.
|
||||
issues: write
|
||||
# `create-merge-up-pull-request` opens the merge-up PR.
|
||||
pull-requests: write
|
||||
|
||||
steps:
|
||||
- name: "Checkout"
|
||||
uses: "actions/checkout@v7.0.1"
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: "Release"
|
||||
uses: "./.github/actions/automatic-releases"
|
||||
with:
|
||||
command-name: "laminas:automatic-releases:release"
|
||||
env:
|
||||
"SHELL_VERBOSITY": "3"
|
||||
"GITHUB_TOKEN": ${{ secrets.ORGANIZATION_ADMIN_TOKEN }}
|
||||
"SIGNING_SECRET_KEY": ${{ secrets.SIGNING_SECRET_KEY }}
|
||||
"GIT_AUTHOR_NAME": ${{ secrets.GIT_AUTHOR_NAME }}
|
||||
"GIT_AUTHOR_EMAIL": ${{ secrets.GIT_AUTHOR_EMAIL }}
|
||||
|
||||
- name: "Create Merge-Up Pull Request"
|
||||
uses: "./.github/actions/automatic-releases"
|
||||
with:
|
||||
command-name: "laminas:automatic-releases:create-merge-up-pull-request"
|
||||
env:
|
||||
"SHELL_VERBOSITY": "3"
|
||||
"GITHUB_TOKEN": ${{ secrets.GITHUB_TOKEN }}
|
||||
"SIGNING_SECRET_KEY": ${{ secrets.SIGNING_SECRET_KEY }}
|
||||
"GIT_AUTHOR_NAME": ${{ secrets.GIT_AUTHOR_NAME }}
|
||||
"GIT_AUTHOR_EMAIL": ${{ secrets.GIT_AUTHOR_EMAIL }}
|
||||
|
||||
- name: "Create and/or Switch to new Release Branch"
|
||||
uses: "./.github/actions/automatic-releases"
|
||||
with:
|
||||
command-name: "laminas:automatic-releases:switch-default-branch-to-next-minor"
|
||||
env:
|
||||
"SHELL_VERBOSITY": "3"
|
||||
"GITHUB_TOKEN": ${{ secrets.ORGANIZATION_ADMIN_TOKEN }}
|
||||
"SIGNING_SECRET_KEY": ${{ secrets.SIGNING_SECRET_KEY }}
|
||||
"GIT_AUTHOR_NAME": ${{ secrets.GIT_AUTHOR_NAME }}
|
||||
"GIT_AUTHOR_EMAIL": ${{ secrets.GIT_AUTHOR_EMAIL }}
|
||||
|
||||
- name: "Bump Changelog Version On Originating Release Branch"
|
||||
uses: "./.github/actions/automatic-releases"
|
||||
with:
|
||||
command-name: "laminas:automatic-releases:bump-changelog"
|
||||
env:
|
||||
"SHELL_VERBOSITY": "3"
|
||||
"GITHUB_TOKEN": ${{ secrets.GITHUB_TOKEN }}
|
||||
"SIGNING_SECRET_KEY": ${{ secrets.SIGNING_SECRET_KEY }}
|
||||
"GIT_AUTHOR_NAME": ${{ secrets.GIT_AUTHOR_NAME }}
|
||||
"GIT_AUTHOR_EMAIL": ${{ secrets.GIT_AUTHOR_EMAIL }}
|
||||
|
||||
- name: "Create new milestones"
|
||||
uses: "./.github/actions/automatic-releases"
|
||||
with:
|
||||
command-name: "laminas:automatic-releases:create-milestones"
|
||||
env:
|
||||
"SHELL_VERBOSITY": "3"
|
||||
"GITHUB_TOKEN": ${{ secrets.GITHUB_TOKEN }}
|
||||
"SIGNING_SECRET_KEY": ${{ secrets.SIGNING_SECRET_KEY }}
|
||||
"GIT_AUTHOR_NAME": ${{ secrets.GIT_AUTHOR_NAME }}
|
||||
"GIT_AUTHOR_EMAIL": ${{ secrets.GIT_AUTHOR_EMAIL }}
|
||||
62
vendor/spomky-labs/cbor-php/.github/workflows/scorecards.yml
vendored
Normal file
62
vendor/spomky-labs/cbor-php/.github/workflows/scorecards.yml
vendored
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
name: Scorecards supply-chain security
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: '34 4 * * 6'
|
||||
push:
|
||||
branches:
|
||||
- "*.*.x"
|
||||
|
||||
# Declare default permissions as read only.
|
||||
permissions: read-all
|
||||
|
||||
jobs:
|
||||
analysis:
|
||||
name: Scorecards analysis
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
# Needed to upload the results to code-scanning dashboard.
|
||||
security-events: write
|
||||
# Used to receive a badge. (Upcoming feature)
|
||||
id-token: write
|
||||
# Needs for private repositories.
|
||||
contents: read
|
||||
actions: read
|
||||
|
||||
steps:
|
||||
- name: "Checkout code"
|
||||
uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
|
||||
- name: "Run analysis"
|
||||
uses: ossf/scorecard-action@v2.4.4
|
||||
with:
|
||||
results_file: results.sarif
|
||||
results_format: sarif
|
||||
# (Optional) Read-only PAT token. Uncomment the `repo_token` line below if:
|
||||
# - you want to enable the Branch-Protection check on a *public* repository, or
|
||||
# - you are installing Scorecards on a *private* repository
|
||||
# To create the PAT, follow the steps in https://github.com/ossf/scorecard-action#authentication-with-pat.
|
||||
# repo_token: ${{ secrets.SCORECARD_READ_TOKEN }}
|
||||
|
||||
# Publish the results for public repositories to enable scorecard badges. For more details, see
|
||||
# https://github.com/ossf/scorecard-action#publishing-results.
|
||||
# For private repositories, `publish_results` will automatically be set to `false`, regardless
|
||||
# of the value entered here.
|
||||
publish_results: true
|
||||
|
||||
# Upload the results as artifacts (optional). Commenting out will disable uploads of run results in SARIF
|
||||
# format to the repository Actions tab.
|
||||
- name: "Upload artifact"
|
||||
uses: actions/upload-artifact@v7.0.1
|
||||
with:
|
||||
name: SARIF file
|
||||
path: results.sarif
|
||||
retention-days: 5
|
||||
|
||||
# Upload the results to GitHub's code scanning dashboard.
|
||||
- name: "Upload to code-scanning"
|
||||
uses: github/codeql-action/upload-sarif@v4.37.9
|
||||
with:
|
||||
sarif_file: results.sarif
|
||||
46
vendor/spomky-labs/cbor-php/CODE_OF_CONDUCT.md
vendored
Normal file
46
vendor/spomky-labs/cbor-php/CODE_OF_CONDUCT.md
vendored
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
# Contributor Covenant Code of Conduct
|
||||
|
||||
## Our Pledge
|
||||
|
||||
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
||||
|
||||
## Our Standards
|
||||
|
||||
Examples of behavior that contributes to creating a positive environment include:
|
||||
|
||||
* Using welcoming and inclusive language
|
||||
* Being respectful of differing viewpoints and experiences
|
||||
* Gracefully accepting constructive criticism
|
||||
* Focusing on what is best for the community
|
||||
* Showing empathy towards other community members
|
||||
|
||||
Examples of unacceptable behavior by participants include:
|
||||
|
||||
* The use of sexualized language or imagery and unwelcome sexual attention or advances
|
||||
* Trolling, insulting/derogatory comments, and personal or political attacks
|
||||
* Public or private harassment
|
||||
* Publishing others' private information, such as a physical or electronic address, without explicit permission
|
||||
* Other conduct which could reasonably be considered inappropriate in a professional setting
|
||||
|
||||
## Our Responsibilities
|
||||
|
||||
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
|
||||
|
||||
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
|
||||
|
||||
## Scope
|
||||
|
||||
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
|
||||
|
||||
## Enforcement
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at contact@spomky-labs.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
|
||||
|
||||
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
|
||||
|
||||
## Attribution
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
|
||||
|
||||
[homepage]: http://contributor-covenant.org
|
||||
[version]: http://contributor-covenant.org/version/1/4/
|
||||
21
vendor/spomky-labs/cbor-php/LICENSE
vendored
Normal file
21
vendor/spomky-labs/cbor-php/LICENSE
vendored
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2018 Spomky-Labs
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
220
vendor/spomky-labs/cbor-php/README.md
vendored
Normal file
220
vendor/spomky-labs/cbor-php/README.md
vendored
Normal file
|
|
@ -0,0 +1,220 @@
|
|||
# CBOR for PHP
|
||||
|
||||
[](https://github.com/Spomky-Labs/cbor-php/actions/workflows/ci.yml)
|
||||
[](https://packagist.org/packages/spomky-labs/cbor-php)
|
||||
[](https://packagist.org/packages/spomky-labs/cbor-php)
|
||||
[](https://packagist.org/packages/spomky-labs/cbor-php)
|
||||
|
||||
A comprehensive PHP library for encoding and decoding **CBOR** (Concise Binary Object Representation) data according to [RFC 8949](https://tools.ietf.org/html/rfc8949).
|
||||
|
||||
## Features
|
||||
|
||||
- ✅ Full support for all CBOR major types (0-7)
|
||||
- ✅ Extensible tag system with built-in support for common tags
|
||||
- ✅ Streaming decoder for efficient memory usage
|
||||
- ✅ Type-safe API with modern PHP 8.0+ features
|
||||
- ✅ Comprehensive support for indefinite-length objects
|
||||
- ✅ Built-in normalization to PHP native types
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
composer require spomky-labs/cbor-php
|
||||
```
|
||||
|
||||
**Requirements:**
|
||||
- PHP 8.0 or higher
|
||||
- ext-mbstring
|
||||
- brick/math
|
||||
|
||||
**Optional but recommended:**
|
||||
- ext-gmp or ext-bcmath for improved performance with large integers
|
||||
- ext-bcmath for Big Float and Decimal Fraction support
|
||||
|
||||
This project follows [semantic versioning](http://semver.org/) strictly.
|
||||
|
||||
## Quick Start
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
use CBOR\Decoder;
|
||||
use CBOR\MapObject;
|
||||
use CBOR\TextStringObject;
|
||||
use CBOR\UnsignedIntegerObject;
|
||||
|
||||
// Encoding: Create a CBOR object
|
||||
$map = MapObject::create()
|
||||
->add(TextStringObject::create('name'), TextStringObject::create('John Doe'))
|
||||
->add(TextStringObject::create('age'), UnsignedIntegerObject::create(30));
|
||||
|
||||
$encoded = (string) $map;
|
||||
|
||||
// Decoding: Parse CBOR data
|
||||
$decoder = Decoder::create();
|
||||
$decoded = $decoder->decode(StringStream::create($encoded));
|
||||
|
||||
// Normalize to native PHP types
|
||||
$data = $decoded->normalize();
|
||||
// ['name' => 'John Doe', 'age' => '30']
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
📖 **[Complete Documentation](doc/index.md)** - Full API reference and guides
|
||||
|
||||
### Quick Links
|
||||
|
||||
- **[Tags Reference](doc/tags.md)** - Complete guide to the 70+ supported CBOR tags
|
||||
- **[Creating Custom Tags](doc/custom-tags.md)** - Implement your own tags for domain-specific needs
|
||||
- **[API Reference](doc/index.md#api-reference)** - Encoding and decoding API
|
||||
- **[Examples](doc/index.md#integration-examples)** - WebAuthn, COSE, IoT, and more
|
||||
|
||||
### Major Types Overview
|
||||
|
||||
This library supports all CBOR major types defined in RFC 8949:
|
||||
|
||||
| Major Type | Description | Classes |
|
||||
|------------|-------------|---------|
|
||||
| 0 | Unsigned Integer | `UnsignedIntegerObject` |
|
||||
| 1 | Negative Integer | `NegativeIntegerObject` |
|
||||
| 2 | Byte String | `ByteStringObject`, `IndefiniteLengthByteStringObject` |
|
||||
| 3 | Text String | `TextStringObject`, `IndefiniteLengthTextStringObject` |
|
||||
| 4 | Array | `ListObject`, `IndefiniteLengthListObject` |
|
||||
| 5 | Map | `MapObject`, `IndefiniteLengthMapObject` |
|
||||
| 6 | Tag | `Tag` and subclasses - [See Tags Reference](doc/tags.md) |
|
||||
| 7 | Other | `TrueObject`, `FalseObject`, `NullObject`, etc. |
|
||||
|
||||
**Common API:**
|
||||
- All objects have a static `create()` method for instantiation
|
||||
- All objects can be converted to binary: `(string) $object`
|
||||
- Many objects implement `Normalizable` to convert to native PHP types
|
||||
|
||||
### Basic Usage Examples
|
||||
|
||||
For complete documentation with all examples, see the [**Documentation**](doc/index.md).
|
||||
|
||||
#### Working with Basic Types
|
||||
|
||||
```php
|
||||
use CBOR\UnsignedIntegerObject;
|
||||
use CBOR\TextStringObject;
|
||||
use CBOR\ListObject;
|
||||
use CBOR\MapObject;
|
||||
|
||||
// Integers
|
||||
$number = UnsignedIntegerObject::create(42);
|
||||
|
||||
// Strings
|
||||
$text = TextStringObject::create('Hello World');
|
||||
|
||||
// Arrays
|
||||
$list = ListObject::create([
|
||||
UnsignedIntegerObject::create(1),
|
||||
TextStringObject::create('two'),
|
||||
]);
|
||||
|
||||
// Maps/Objects
|
||||
$map = MapObject::create()
|
||||
->add(TextStringObject::create('key'), TextStringObject::create('value'));
|
||||
```
|
||||
|
||||
#### Working with Tags
|
||||
|
||||
**📖 For complete tags documentation, see [Tags Reference](doc/tags.md)**
|
||||
|
||||
```php
|
||||
use CBOR\Tag\TimestampTag;
|
||||
use CBOR\Tag\DecimalFractionTag;
|
||||
use CBOR\UnsignedIntegerObject;
|
||||
|
||||
// Timestamps
|
||||
$timestamp = TimestampTag::create(UnsignedIntegerObject::create(time()));
|
||||
$dateTime = $timestamp->normalize(); // DateTimeImmutable
|
||||
|
||||
// Decimal fractions
|
||||
$decimal = DecimalFractionTag::createFromFloat(3.14159);
|
||||
echo $decimal->normalize(); // "3.14159"
|
||||
```
|
||||
|
||||
**Supported Tags:**
|
||||
- Date/Time (Tags 0, 1) and dates without a time (Tags 100, 1004)
|
||||
- Big Numbers (Tags 2, 3), Decimal/Binary Fractions (Tags 4, 5) and Rationals (Tag 30)
|
||||
- Encoding hints (Tags 21, 22, 23), embedded CBOR (Tags 24, 63)
|
||||
- URIs, MIME and UUIDs (Tags 32, 36, 37, 257)
|
||||
- COSE structures and CBOR Web Tokens (Tags 16, 17, 18, 61, 96, 97, 98)
|
||||
- Typed and multi-dimensional arrays (Tags 40, 41, 64-87, 1040)
|
||||
- IP and network addresses (Tags 52, 54, 260, 261)
|
||||
- [And more...](doc/tags.md)
|
||||
|
||||
**Create your own:** See [Creating Custom Tags](doc/custom-tags.md) guide.
|
||||
|
||||
### Complete Example
|
||||
|
||||
```php
|
||||
use CBOR\Decoder;
|
||||
use CBOR\MapObject;
|
||||
use CBOR\ListObject;
|
||||
use CBOR\TextStringObject;
|
||||
use CBOR\UnsignedIntegerObject;
|
||||
use CBOR\StringStream;
|
||||
use CBOR\Tag\TimestampTag;
|
||||
|
||||
// Build a complex nested structure
|
||||
$data = MapObject::create()
|
||||
->add(
|
||||
TextStringObject::create('user'),
|
||||
MapObject::create()
|
||||
->add(TextStringObject::create('name'), TextStringObject::create('Alice'))
|
||||
->add(TextStringObject::create('age'), UnsignedIntegerObject::create(30))
|
||||
)
|
||||
->add(
|
||||
TextStringObject::create('scores'),
|
||||
ListObject::create([
|
||||
UnsignedIntegerObject::create(95),
|
||||
UnsignedIntegerObject::create(87),
|
||||
])
|
||||
)
|
||||
->add(
|
||||
TextStringObject::create('timestamp'),
|
||||
TimestampTag::create(UnsignedIntegerObject::create(time()))
|
||||
);
|
||||
|
||||
// Encode to binary
|
||||
$encoded = (string) $data;
|
||||
|
||||
// Decode back
|
||||
$decoder = Decoder::create();
|
||||
$decoded = $decoder->decode(StringStream::create($encoded));
|
||||
|
||||
// Convert to native PHP types
|
||||
$phpData = $decoded->normalize();
|
||||
```
|
||||
|
||||
For more examples including WebAuthn, COSE, IoT scenarios, and advanced usage, see the [complete documentation](doc/index.md#integration-examples).
|
||||
|
||||
## Contributing
|
||||
|
||||
Contributions are welcome! Here's how you can help:
|
||||
|
||||
- Report bugs and request features via [GitHub Issues](https://github.com/Spomky-Labs/cbor-php/issues)
|
||||
- Fix [issues labeled "help wanted"](https://github.com/Spomky-Labs/cbor-php/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22)
|
||||
- Improve documentation
|
||||
- Submit pull requests
|
||||
|
||||
Please read our [contribution guidelines](.github/CONTRIBUTING.md) before submitting.
|
||||
|
||||
## Support
|
||||
|
||||
If you find this project valuable, consider supporting its development:
|
||||
|
||||
- [Become a GitHub Sponsor](https://github.com/sponsors/Spomky)
|
||||
- [Support via Patreon](https://www.patreon.com/FlorentMorselli)
|
||||
|
||||
## License
|
||||
|
||||
This project is released under the [MIT License](LICENSE).
|
||||
|
||||
---
|
||||
|
||||
**Maintained by:** [Florent Morselli](https://github.com/Spomky) and [contributors](https://github.com/Spomky-Labs/cbor-php/contributors)
|
||||
19
vendor/spomky-labs/cbor-php/RELEASES.md
vendored
Normal file
19
vendor/spomky-labs/cbor-php/RELEASES.md
vendored
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
# Versioning and Release
|
||||
|
||||
This document describes the versioning and release process of CBOR PHP.
|
||||
This document is a living document, contents will be updated according to each release.
|
||||
|
||||
## Releases
|
||||
|
||||
CBOR PHP releases will be versioned using dotted triples, similar to [Semantic Version](http://semver.org/).
|
||||
For this specific document, we will refer to the respective components of this triple as `<major>.<minor>.<patch>`.
|
||||
The version number may have additional information, such as "-rc1,-rc2,-rc3" to mark release candidate builds for earlier access.
|
||||
Such releases will be considered as "pre-releases".
|
||||
|
||||
## Minor Release Support Matrix
|
||||
|
||||
| Version | Supported |
|
||||
|---------|--------------------|
|
||||
| 3.3.x | :white_check_mark: |
|
||||
| 3.2.x | :white_check_mark: |
|
||||
| < 3.2.x | :x: |
|
||||
74
vendor/spomky-labs/cbor-php/SECURITY.md
vendored
Normal file
74
vendor/spomky-labs/cbor-php/SECURITY.md
vendored
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
# Security Release Process
|
||||
|
||||
CBOR PHP is devoted in providing the best experience for all developers who want to encode and decode CBOR data structures in their applications.
|
||||
Spomky-Labs has adopted this security disclosure and response policy to ensure we responsibly handle critical issues.
|
||||
|
||||
## Supported Versions
|
||||
|
||||
The CBOR PHP project maintains release branches for the three most recent minor releases.
|
||||
Applicable fixes, including security fixes, may be backported to those three release branches, depending on severity and feasibility. Please refer to [RELEASES.md](RELEASES.md) for details.
|
||||
|
||||
## Reporting a Vulnerability - Private Disclosure Process
|
||||
|
||||
Security is of the highest importance and all security vulnerabilities or suspected security vulnerabilities should be reported to CBOR PHP privately, to minimize attacks against current users of CBOR PHP before they are fixed.
|
||||
Vulnerabilities will be investigated and patched on the next patch (or minor) release as soon as possible.
|
||||
This information could be kept entirely internal to the project.
|
||||
|
||||
If you know of a publicly disclosed security vulnerability for CBOR PHP, please **IMMEDIATELY** contact security@spomky-labs.com to inform the CBOR PHP Security Team.
|
||||
|
||||
**IMPORTANT: Do not file public issues on GitHub for security vulnerabilities**
|
||||
|
||||
To report a vulnerability or a security-related issue, please use the [private vulnerability reporting form](https://github.com/Spomky-Labs/cbor-php/security/advisories/new), or email the private address security@spomky-labs.com with the details of the vulnerability.
|
||||
The report will be fielded by the CBOR PHP Security Team, which is made up of the CBOR PHP maintainers who have committer and release permissions.
|
||||
Do not report non-security-impacting bugs through this channel. Use [GitHub issues](https://github.com/Spomky-Labs/cbor-php/issues/new/choose) instead.
|
||||
|
||||
Emails can be encrypted if you wish to share the vulnerability details securely.
|
||||
The CBOR PHP Security Team's PGP key is available on the [PGP keyservers](https://keys.openpgp.org/search?q=security%40spomky-labs.com).
|
||||
|
||||
### Proposed Email Content
|
||||
|
||||
Provide a descriptive subject line and in the body of the email include the following information:
|
||||
|
||||
- Basic identity information, such as your name and your affiliation or company.
|
||||
- Detailed steps to reproduce the vulnerability (POC scripts, screenshots, and CBOR test vectors are all helpful to us).
|
||||
- Description of the effects of the vulnerability on CBOR PHP and the related hardware and software configurations, so that the CBOR PHP Security Team can reproduce it.
|
||||
- How the vulnerability affects CBOR PHP usage and an estimation of the attack surface, if there is one.
|
||||
- List other projects or dependencies that were used in conjunction with CBOR PHP to produce the vulnerability.
|
||||
|
||||
## When to report a vulnerability
|
||||
|
||||
- When you think CBOR PHP has a potential security vulnerability.
|
||||
- When you suspect a potential vulnerability, but you are unsure that it impacts CBOR PHP.
|
||||
- When you know of or suspect a potential vulnerability on another project that is used by CBOR PHP. For example CBOR PHP has a dependency on `brick/math` and on the `mbstring`, `gmp` and `bcmath` PHP extensions.
|
||||
|
||||
## Patch, Release, and Disclosure
|
||||
|
||||
The CBOR PHP Security Team will respond to vulnerability reports as follows:
|
||||
|
||||
1. The Security Team will investigate the vulnerability and determine its effects and criticality.
|
||||
2. If the issue is not deemed to be a vulnerability, the Security Team will follow up with a detailed reason for rejection.
|
||||
3. The Security Team will initiate a conversation with the reporter as soon as possible.
|
||||
4. If a vulnerability is acknowledged and the timeline for a fix is determined, the Security Team will work on a plan to communicate with the appropriate community, including identifying mitigating steps that affected users can take to protect themselves until the fix is rolled out.
|
||||
5. The Security Team will work on fixing the vulnerability and perform internal testing before preparing to roll out the fix.
|
||||
6. A public disclosure date is negotiated by the CBOR PHP Security Team and the bug submitter. We prefer to fully disclose the bug as soon as possible once a user mitigation or patch is available. It is reasonable to delay disclosure when the bug or the fix is not yet fully understood, the solution is not well-tested, or for distributor coordination. The timeframe for disclosure is from immediate (especially if it’s already publicly known) to a few weeks. For a critical vulnerability with a straightforward mitigation, we expect report date to public disclosure date to be on the order of 14 business days. The CBOR PHP Security Team holds the final say when setting a public disclosure date.
|
||||
7. Once the fix is confirmed, the Security Team will patch the vulnerability in the next patch or minor release, and backport a patch release into all earlier supported releases. Upon release of the patched version of CBOR PHP, we will follow the **Public Disclosure Process**.
|
||||
|
||||
### Public Disclosure Process
|
||||
|
||||
The Security Team publishes a public [advisory](https://github.com/Spomky-Labs/cbor-php/security/advisories) to the CBOR PHP community via GitHub. In most cases, additional communication via blog and other channels will assist in educating CBOR PHP users and rolling out the patched release to affected users.
|
||||
|
||||
The Security Team will also publish any mitigating steps users can take until the fix can be applied to their CBOR PHP installations. CBOR PHP distributors will handle creating and publishing their own security advisories.
|
||||
|
||||
## Mailing lists
|
||||
|
||||
- Use security@spomky-labs.com to report security concerns to the CBOR PHP Security Team, who uses the list to privately discuss security issues and fixes prior to disclosure.
|
||||
|
||||
## Early Disclosure to CBOR PHP Distributors List
|
||||
|
||||
This private list is intended to be used primarily to provide actionable information to multiple distributor projects at once. This list is not intended to inform individuals about security issues.
|
||||
|
||||
## Confidentiality, integrity and availability
|
||||
|
||||
We consider vulnerabilities leading to the compromise of data confidentiality, elevation of privilege, or integrity to be our highest priority concerns.
|
||||
Availability, in particular in areas relating to DoS and resource exhaustion, is also a serious security concern.
|
||||
The CBOR PHP Security Team takes all vulnerabilities, potential vulnerabilities, and suspected vulnerabilities seriously and will investigate them in an urgent and expeditious manner.
|
||||
49
vendor/spomky-labs/cbor-php/composer.json
vendored
Normal file
49
vendor/spomky-labs/cbor-php/composer.json
vendored
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
{
|
||||
"name": "spomky-labs/cbor-php",
|
||||
"type": "library",
|
||||
"license": "MIT",
|
||||
"keywords": ["CBOR", "Concise Binary Object Representation", "RFC7049"],
|
||||
"description": "CBOR Encoder/Decoder for PHP",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Florent Morselli",
|
||||
"homepage": "https://github.com/Spomky"
|
||||
},{
|
||||
"name": "All contributors",
|
||||
"homepage": "https://github.com/Spomky-Labs/cbor-php/contributors"
|
||||
}
|
||||
],
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"CBOR\\": "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"CBOR\\Test\\": "tests/"
|
||||
}
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.0",
|
||||
"ext-mbstring": "*",
|
||||
"brick/math": "^0.9|^0.10|^0.11|^0.12|^0.13|^0.14|^0.15|^0.16|^0.17|^0.18|^0.19|^0.20|^1.0",
|
||||
"symfony/polyfill-php81": "^1.32"
|
||||
},
|
||||
"require-dev": {
|
||||
"ext-json": "*",
|
||||
"roave/security-advisories": "dev-latest",
|
||||
"symfony/error-handler": "^6.4|^7.1|^8.0",
|
||||
"symfony/var-dumper": "^6.4|^7.1|^8.0"
|
||||
},
|
||||
"config": {
|
||||
"sort-packages": true,
|
||||
"allow-plugins": {
|
||||
"phpstan/extension-installer": true,
|
||||
"infection/extension-installer": true
|
||||
}
|
||||
},
|
||||
"suggest": {
|
||||
"ext-gmp": "Strongly recommended when decoding untrusted input: without it, converting the byte string of a Big Number Tag (2 and 3) is quadratic in its length. Also improves the library performance overall",
|
||||
"ext-bcmath": "Improves the library performance when ext-gmp is missing, and is required to handle the Big Float and Decimal Fraction Tags (4 and 5)"
|
||||
}
|
||||
}
|
||||
33
vendor/spomky-labs/cbor-php/src/AbstractCBORObject.php
vendored
Normal file
33
vendor/spomky-labs/cbor-php/src/AbstractCBORObject.php
vendored
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR;
|
||||
|
||||
use function chr;
|
||||
|
||||
abstract class AbstractCBORObject implements CBORObject
|
||||
{
|
||||
public function __construct(
|
||||
private int $majorType,
|
||||
protected int $additionalInformation
|
||||
) {
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
// A CBOR head is a single byte: three bits of major type followed by five of additional information. The
|
||||
// mask is what chr() already applies to an out-of-range codepoint, and it lets the byte be typed as one.
|
||||
return chr(($this->majorType << 5 | $this->additionalInformation) & 0xFF);
|
||||
}
|
||||
|
||||
public function getMajorType(): int
|
||||
{
|
||||
return $this->majorType;
|
||||
}
|
||||
|
||||
public function getAdditionalInformation(): int
|
||||
{
|
||||
return $this->additionalInformation;
|
||||
}
|
||||
}
|
||||
76
vendor/spomky-labs/cbor-php/src/ByteStringObject.php
vendored
Normal file
76
vendor/spomky-labs/cbor-php/src/ByteStringObject.php
vendored
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR;
|
||||
|
||||
use function strlen;
|
||||
|
||||
/**
|
||||
* @see \CBOR\Test\ByteStringObjectTest
|
||||
*/
|
||||
final class ByteStringObject extends AbstractCBORObject implements Normalizable
|
||||
{
|
||||
private const MAJOR_TYPE = self::MAJOR_TYPE_BYTE_STRING;
|
||||
|
||||
private ?string $length = null;
|
||||
|
||||
private bool $lengthComputed = false;
|
||||
|
||||
public function __construct(
|
||||
private string $value
|
||||
) {
|
||||
parent::__construct(self::MAJOR_TYPE, 0);
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
$this->computeLength();
|
||||
$result = parent::__toString();
|
||||
$result .= $this->length ?? '';
|
||||
|
||||
return $result . $this->value;
|
||||
}
|
||||
|
||||
public function getAdditionalInformation(): int
|
||||
{
|
||||
$this->computeLength();
|
||||
|
||||
return parent::getAdditionalInformation();
|
||||
}
|
||||
|
||||
public static function create(string $data): self
|
||||
{
|
||||
return new self($data);
|
||||
}
|
||||
|
||||
public function getValue(): string
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
public function getLength(): int
|
||||
{
|
||||
return strlen($this->value);
|
||||
}
|
||||
|
||||
public function normalize(): string
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* A string object is immutable, so its head never changes -- but decoding never asks for it: the object is built
|
||||
* from the payload and read back through normalize(), and only re-encoding calls __toString(). Computing it up
|
||||
* front made every decoded string pay for a head nobody reads.
|
||||
*/
|
||||
private function computeLength(): void
|
||||
{
|
||||
if ($this->lengthComputed) {
|
||||
return;
|
||||
}
|
||||
|
||||
[$this->additionalInformation, $this->length] = LengthCalculator::getLengthOfString($this->value);
|
||||
$this->lengthComputed = true;
|
||||
}
|
||||
}
|
||||
210
vendor/spomky-labs/cbor-php/src/CBORObject.php
vendored
Normal file
210
vendor/spomky-labs/cbor-php/src/CBORObject.php
vendored
Normal file
|
|
@ -0,0 +1,210 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR;
|
||||
|
||||
use Stringable;
|
||||
|
||||
interface CBORObject extends Stringable
|
||||
{
|
||||
public const MAJOR_TYPE_UNSIGNED_INTEGER = 0b000;
|
||||
|
||||
public const MAJOR_TYPE_NEGATIVE_INTEGER = 0b001;
|
||||
|
||||
public const MAJOR_TYPE_BYTE_STRING = 0b010;
|
||||
|
||||
public const MAJOR_TYPE_TEXT_STRING = 0b011;
|
||||
|
||||
public const MAJOR_TYPE_LIST = 0b100;
|
||||
|
||||
public const MAJOR_TYPE_MAP = 0b101;
|
||||
|
||||
public const MAJOR_TYPE_TAG = 0b110;
|
||||
|
||||
public const MAJOR_TYPE_OTHER_TYPE = 0b111;
|
||||
|
||||
public const LENGTH_1_BYTE = 0b00011000;
|
||||
|
||||
public const LENGTH_2_BYTES = 0b00011001;
|
||||
|
||||
public const LENGTH_4_BYTES = 0b00011010;
|
||||
|
||||
public const LENGTH_8_BYTES = 0b00011011;
|
||||
|
||||
public const LENGTH_INDEFINITE = 0b00011111;
|
||||
|
||||
public const FUTURE_USE_1 = 0b00011100;
|
||||
|
||||
public const FUTURE_USE_2 = 0b00011101;
|
||||
|
||||
public const FUTURE_USE_3 = 0b00011110;
|
||||
|
||||
public const OBJECT_FALSE = 20;
|
||||
|
||||
public const OBJECT_TRUE = 21;
|
||||
|
||||
public const OBJECT_NULL = 22;
|
||||
|
||||
public const OBJECT_UNDEFINED = 23;
|
||||
|
||||
public const OBJECT_SIMPLE_VALUE = 24;
|
||||
|
||||
public const OBJECT_HALF_PRECISION_FLOAT = 25;
|
||||
|
||||
public const OBJECT_SINGLE_PRECISION_FLOAT = 26;
|
||||
|
||||
public const OBJECT_DOUBLE_PRECISION_FLOAT = 27;
|
||||
|
||||
public const OBJECT_BREAK = 0b00011111;
|
||||
|
||||
public const TAG_STANDARD_DATETIME = 0;
|
||||
|
||||
public const TAG_EPOCH_DATETIME = 1;
|
||||
|
||||
public const TAG_UNSIGNED_BIG_NUM = 2;
|
||||
|
||||
public const TAG_NEGATIVE_BIG_NUM = 3;
|
||||
|
||||
public const TAG_DECIMAL_FRACTION = 4;
|
||||
|
||||
public const TAG_BIG_FLOAT = 5;
|
||||
|
||||
public const TAG_COSE_ENCRYPT0 = 16;
|
||||
|
||||
public const TAG_COSE_MAC0 = 17;
|
||||
|
||||
public const TAG_COSE_SIGN1 = 18;
|
||||
|
||||
public const TAG_ENCODED_BASE64_URL = 21;
|
||||
|
||||
public const TAG_ENCODED_BASE64 = 22;
|
||||
|
||||
public const TAG_ENCODED_BASE16 = 23;
|
||||
|
||||
public const TAG_ENCODED_CBOR = 24;
|
||||
|
||||
public const TAG_STRING_REFERENCE = 25;
|
||||
|
||||
public const TAG_PERL_OBJECT = 26;
|
||||
|
||||
public const TAG_LANGUAGE_INDEPENDENT_OBJECT = 27;
|
||||
|
||||
public const TAG_SHAREABLE = 28;
|
||||
|
||||
public const TAG_SHARED_REFERENCE = 29;
|
||||
|
||||
public const TAG_RATIONAL_NUMBER = 30;
|
||||
|
||||
public const TAG_URI = 32;
|
||||
|
||||
public const TAG_BASE64_URL = 33;
|
||||
|
||||
public const TAG_BASE64 = 34;
|
||||
|
||||
public const TAG_REGULAR_EXPRESSION = 35;
|
||||
|
||||
public const TAG_MIME = 36;
|
||||
|
||||
public const TAG_UUID = 37;
|
||||
|
||||
public const TAG_LANGUAGE_TAGGED_STRING = 38;
|
||||
|
||||
public const TAG_IDENTIFIER = 39;
|
||||
|
||||
public const TAG_ROW_MAJOR_MULTI_DIMENSIONAL_ARRAY = 40;
|
||||
|
||||
public const TAG_HOMOGENEOUS_ARRAY = 41;
|
||||
|
||||
public const TAG_IPLD_CONTENT_IDENTIFIER = 42;
|
||||
|
||||
public const TAG_IPV4 = 52;
|
||||
|
||||
public const TAG_IPV6 = 54;
|
||||
|
||||
public const TAG_CWT = 61;
|
||||
|
||||
public const TAG_ENCODED_CBOR_SEQUENCE = 63;
|
||||
|
||||
public const TAG_TYPED_ARRAY_UINT8 = 64;
|
||||
|
||||
public const TAG_TYPED_ARRAY_UINT16_BE = 65;
|
||||
|
||||
public const TAG_TYPED_ARRAY_UINT32_BE = 66;
|
||||
|
||||
public const TAG_TYPED_ARRAY_UINT64_BE = 67;
|
||||
|
||||
public const TAG_TYPED_ARRAY_UINT8_CLAMPED = 68;
|
||||
|
||||
public const TAG_TYPED_ARRAY_UINT16_LE = 69;
|
||||
|
||||
public const TAG_TYPED_ARRAY_UINT32_LE = 70;
|
||||
|
||||
public const TAG_TYPED_ARRAY_UINT64_LE = 71;
|
||||
|
||||
public const TAG_TYPED_ARRAY_SINT8 = 72;
|
||||
|
||||
public const TAG_TYPED_ARRAY_SINT16_BE = 73;
|
||||
|
||||
public const TAG_TYPED_ARRAY_SINT32_BE = 74;
|
||||
|
||||
public const TAG_TYPED_ARRAY_SINT64_BE = 75;
|
||||
|
||||
public const TAG_TYPED_ARRAY_SINT16_LE = 77;
|
||||
|
||||
public const TAG_TYPED_ARRAY_SINT32_LE = 78;
|
||||
|
||||
public const TAG_TYPED_ARRAY_SINT64_LE = 79;
|
||||
|
||||
public const TAG_TYPED_ARRAY_FLOAT16_BE = 80;
|
||||
|
||||
public const TAG_TYPED_ARRAY_FLOAT32_BE = 81;
|
||||
|
||||
public const TAG_TYPED_ARRAY_FLOAT64_BE = 82;
|
||||
|
||||
public const TAG_TYPED_ARRAY_FLOAT128_BE = 83;
|
||||
|
||||
public const TAG_TYPED_ARRAY_FLOAT16_LE = 84;
|
||||
|
||||
public const TAG_TYPED_ARRAY_FLOAT32_LE = 85;
|
||||
|
||||
public const TAG_TYPED_ARRAY_FLOAT64_LE = 86;
|
||||
|
||||
public const TAG_TYPED_ARRAY_FLOAT128_LE = 87;
|
||||
|
||||
public const TAG_COSE_ENCRYPT = 96;
|
||||
|
||||
public const TAG_COSE_MAC = 97;
|
||||
|
||||
public const TAG_COSE_SIGN = 98;
|
||||
|
||||
public const TAG_DATE = 100;
|
||||
|
||||
public const TAG_STRING_REFERENCE_NAMESPACE = 256;
|
||||
|
||||
public const TAG_BINARY_MIME = 257;
|
||||
|
||||
public const TAG_SET = 258;
|
||||
|
||||
public const TAG_EXPLICIT_MAP = 259;
|
||||
|
||||
public const TAG_NETWORK_ADDRESS = 260;
|
||||
|
||||
public const TAG_NETWORK_ADDRESS_PREFIX = 261;
|
||||
|
||||
public const TAG_EXTENDED_TIME = 1001;
|
||||
|
||||
public const TAG_DURATION = 1002;
|
||||
|
||||
public const TAG_PERIOD = 1003;
|
||||
|
||||
public const TAG_DATE_STRING = 1004;
|
||||
|
||||
public const TAG_COLUMN_MAJOR_MULTI_DIMENSIONAL_ARRAY = 1040;
|
||||
|
||||
public const TAG_CBOR = 55799;
|
||||
|
||||
public function getMajorType(): int;
|
||||
|
||||
public function getAdditionalInformation(): int;
|
||||
}
|
||||
404
vendor/spomky-labs/cbor-php/src/Decoder.php
vendored
Normal file
404
vendor/spomky-labs/cbor-php/src/Decoder.php
vendored
Normal file
|
|
@ -0,0 +1,404 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR;
|
||||
|
||||
use CBOR\OtherObject\BreakObject;
|
||||
use CBOR\OtherObject\DoublePrecisionFloatObject;
|
||||
use CBOR\OtherObject\FalseObject;
|
||||
use CBOR\OtherObject\HalfPrecisionFloatObject;
|
||||
use CBOR\OtherObject\NullObject;
|
||||
use CBOR\OtherObject\OtherObjectManager;
|
||||
use CBOR\OtherObject\OtherObjectManagerInterface;
|
||||
use CBOR\OtherObject\SimpleObject;
|
||||
use CBOR\OtherObject\SinglePrecisionFloatObject;
|
||||
use CBOR\OtherObject\TrueObject;
|
||||
use CBOR\OtherObject\UndefinedObject;
|
||||
use CBOR\Tag\Base16EncodingTag;
|
||||
use CBOR\Tag\Base64EncodingTag;
|
||||
use CBOR\Tag\Base64Tag;
|
||||
use CBOR\Tag\Base64UrlEncodingTag;
|
||||
use CBOR\Tag\Base64UrlTag;
|
||||
use CBOR\Tag\BigFloatTag;
|
||||
use CBOR\Tag\BinaryMimeTag;
|
||||
use CBOR\Tag\CBOREncodingTag;
|
||||
use CBOR\Tag\CBORSequenceTag;
|
||||
use CBOR\Tag\CBORTag;
|
||||
use CBOR\Tag\ColumnMajorMultiDimensionalArrayTag;
|
||||
use CBOR\Tag\CoseEncrypt0Tag;
|
||||
use CBOR\Tag\CoseEncryptTag;
|
||||
use CBOR\Tag\CoseMac0Tag;
|
||||
use CBOR\Tag\CoseMacTag;
|
||||
use CBOR\Tag\CoseSign1Tag;
|
||||
use CBOR\Tag\CoseSignTag;
|
||||
use CBOR\Tag\CwtTag;
|
||||
use CBOR\Tag\DateStringTag;
|
||||
use CBOR\Tag\DateTag;
|
||||
use CBOR\Tag\DatetimeTag;
|
||||
use CBOR\Tag\DecimalFractionTag;
|
||||
use CBOR\Tag\DurationTag;
|
||||
use CBOR\Tag\ExplicitMapTag;
|
||||
use CBOR\Tag\ExtendedTimeTag;
|
||||
use CBOR\Tag\HomogeneousArrayTag;
|
||||
use CBOR\Tag\IdentifierTag;
|
||||
use CBOR\Tag\IpldContentIdentifierTag;
|
||||
use CBOR\Tag\Ipv4Tag;
|
||||
use CBOR\Tag\Ipv6Tag;
|
||||
use CBOR\Tag\LanguageIndependentObjectTag;
|
||||
use CBOR\Tag\LanguageTaggedStringTag;
|
||||
use CBOR\Tag\MimeTag;
|
||||
use CBOR\Tag\NegativeBigIntegerTag;
|
||||
use CBOR\Tag\NetworkAddressPrefixTag;
|
||||
use CBOR\Tag\NetworkAddressTag;
|
||||
use CBOR\Tag\PeriodTag;
|
||||
use CBOR\Tag\PerlObjectTag;
|
||||
use CBOR\Tag\RationalNumberTag;
|
||||
use CBOR\Tag\RegexpTag;
|
||||
use CBOR\Tag\RowMajorMultiDimensionalArrayTag;
|
||||
use CBOR\Tag\SetTag;
|
||||
use CBOR\Tag\ShareableTag;
|
||||
use CBOR\Tag\SharedReferenceTag;
|
||||
use CBOR\Tag\StringReferenceNamespaceTag;
|
||||
use CBOR\Tag\StringReferenceTag;
|
||||
use CBOR\Tag\TagInterface;
|
||||
use CBOR\Tag\TagManager;
|
||||
use CBOR\Tag\TagManagerInterface;
|
||||
use CBOR\Tag\TimestampTag;
|
||||
use CBOR\Tag\TypedArray\Float128BigEndianArrayTag;
|
||||
use CBOR\Tag\TypedArray\Float128LittleEndianArrayTag;
|
||||
use CBOR\Tag\TypedArray\Float16BigEndianArrayTag;
|
||||
use CBOR\Tag\TypedArray\Float16LittleEndianArrayTag;
|
||||
use CBOR\Tag\TypedArray\Float32BigEndianArrayTag;
|
||||
use CBOR\Tag\TypedArray\Float32LittleEndianArrayTag;
|
||||
use CBOR\Tag\TypedArray\Float64BigEndianArrayTag;
|
||||
use CBOR\Tag\TypedArray\Float64LittleEndianArrayTag;
|
||||
use CBOR\Tag\TypedArray\Sint16BigEndianArrayTag;
|
||||
use CBOR\Tag\TypedArray\Sint16LittleEndianArrayTag;
|
||||
use CBOR\Tag\TypedArray\Sint32BigEndianArrayTag;
|
||||
use CBOR\Tag\TypedArray\Sint32LittleEndianArrayTag;
|
||||
use CBOR\Tag\TypedArray\Sint64BigEndianArrayTag;
|
||||
use CBOR\Tag\TypedArray\Sint64LittleEndianArrayTag;
|
||||
use CBOR\Tag\TypedArray\Sint8ArrayTag;
|
||||
use CBOR\Tag\TypedArray\Uint16BigEndianArrayTag;
|
||||
use CBOR\Tag\TypedArray\Uint16LittleEndianArrayTag;
|
||||
use CBOR\Tag\TypedArray\Uint32BigEndianArrayTag;
|
||||
use CBOR\Tag\TypedArray\Uint32LittleEndianArrayTag;
|
||||
use CBOR\Tag\TypedArray\Uint64BigEndianArrayTag;
|
||||
use CBOR\Tag\TypedArray\Uint64LittleEndianArrayTag;
|
||||
use CBOR\Tag\TypedArray\Uint8ArrayTag;
|
||||
use CBOR\Tag\TypedArray\Uint8ClampedArrayTag;
|
||||
use CBOR\Tag\UnsignedBigIntegerTag;
|
||||
use CBOR\Tag\UriTag;
|
||||
use CBOR\Tag\UuidTag;
|
||||
use InvalidArgumentException;
|
||||
use function ord;
|
||||
use RuntimeException;
|
||||
use function sprintf;
|
||||
use const STR_PAD_LEFT;
|
||||
|
||||
final class Decoder implements DecoderInterface
|
||||
{
|
||||
/**
|
||||
* The tag classes the decoder knows about, by tag number.
|
||||
*
|
||||
* Every entry is a class this library implements from the IANA CBOR tags registry. A tag that is not listed
|
||||
* here -- unassigned, or specific to an application -- is decoded as a GenericTag, which keeps the number and
|
||||
* the item without claiming to understand them.
|
||||
*
|
||||
* @var array<int, class-string<TagInterface>>
|
||||
*/
|
||||
private const DEFAULT_TAGS = [
|
||||
CBORObject::TAG_STANDARD_DATETIME => DatetimeTag::class,
|
||||
CBORObject::TAG_EPOCH_DATETIME => TimestampTag::class,
|
||||
CBORObject::TAG_UNSIGNED_BIG_NUM => UnsignedBigIntegerTag::class,
|
||||
CBORObject::TAG_NEGATIVE_BIG_NUM => NegativeBigIntegerTag::class,
|
||||
CBORObject::TAG_DECIMAL_FRACTION => DecimalFractionTag::class,
|
||||
CBORObject::TAG_BIG_FLOAT => BigFloatTag::class,
|
||||
CBORObject::TAG_COSE_ENCRYPT0 => CoseEncrypt0Tag::class,
|
||||
CBORObject::TAG_COSE_MAC0 => CoseMac0Tag::class,
|
||||
CBORObject::TAG_COSE_SIGN1 => CoseSign1Tag::class,
|
||||
CBORObject::TAG_ENCODED_BASE64_URL => Base64UrlEncodingTag::class,
|
||||
CBORObject::TAG_ENCODED_BASE64 => Base64EncodingTag::class,
|
||||
CBORObject::TAG_ENCODED_BASE16 => Base16EncodingTag::class,
|
||||
CBORObject::TAG_ENCODED_CBOR => CBOREncodingTag::class,
|
||||
CBORObject::TAG_STRING_REFERENCE => StringReferenceTag::class,
|
||||
CBORObject::TAG_PERL_OBJECT => PerlObjectTag::class,
|
||||
CBORObject::TAG_LANGUAGE_INDEPENDENT_OBJECT => LanguageIndependentObjectTag::class,
|
||||
CBORObject::TAG_SHAREABLE => ShareableTag::class,
|
||||
CBORObject::TAG_SHARED_REFERENCE => SharedReferenceTag::class,
|
||||
CBORObject::TAG_RATIONAL_NUMBER => RationalNumberTag::class,
|
||||
CBORObject::TAG_URI => UriTag::class,
|
||||
CBORObject::TAG_BASE64_URL => Base64UrlTag::class,
|
||||
CBORObject::TAG_BASE64 => Base64Tag::class,
|
||||
CBORObject::TAG_REGULAR_EXPRESSION => RegexpTag::class,
|
||||
CBORObject::TAG_MIME => MimeTag::class,
|
||||
CBORObject::TAG_UUID => UuidTag::class,
|
||||
CBORObject::TAG_LANGUAGE_TAGGED_STRING => LanguageTaggedStringTag::class,
|
||||
CBORObject::TAG_IDENTIFIER => IdentifierTag::class,
|
||||
CBORObject::TAG_ROW_MAJOR_MULTI_DIMENSIONAL_ARRAY => RowMajorMultiDimensionalArrayTag::class,
|
||||
CBORObject::TAG_HOMOGENEOUS_ARRAY => HomogeneousArrayTag::class,
|
||||
CBORObject::TAG_IPLD_CONTENT_IDENTIFIER => IpldContentIdentifierTag::class,
|
||||
CBORObject::TAG_IPV4 => Ipv4Tag::class,
|
||||
CBORObject::TAG_IPV6 => Ipv6Tag::class,
|
||||
CBORObject::TAG_CWT => CwtTag::class,
|
||||
CBORObject::TAG_ENCODED_CBOR_SEQUENCE => CBORSequenceTag::class,
|
||||
CBORObject::TAG_TYPED_ARRAY_UINT8 => Uint8ArrayTag::class,
|
||||
CBORObject::TAG_TYPED_ARRAY_UINT16_BE => Uint16BigEndianArrayTag::class,
|
||||
CBORObject::TAG_TYPED_ARRAY_UINT32_BE => Uint32BigEndianArrayTag::class,
|
||||
CBORObject::TAG_TYPED_ARRAY_UINT64_BE => Uint64BigEndianArrayTag::class,
|
||||
CBORObject::TAG_TYPED_ARRAY_UINT8_CLAMPED => Uint8ClampedArrayTag::class,
|
||||
CBORObject::TAG_TYPED_ARRAY_UINT16_LE => Uint16LittleEndianArrayTag::class,
|
||||
CBORObject::TAG_TYPED_ARRAY_UINT32_LE => Uint32LittleEndianArrayTag::class,
|
||||
CBORObject::TAG_TYPED_ARRAY_UINT64_LE => Uint64LittleEndianArrayTag::class,
|
||||
CBORObject::TAG_TYPED_ARRAY_SINT8 => Sint8ArrayTag::class,
|
||||
CBORObject::TAG_TYPED_ARRAY_SINT16_BE => Sint16BigEndianArrayTag::class,
|
||||
CBORObject::TAG_TYPED_ARRAY_SINT32_BE => Sint32BigEndianArrayTag::class,
|
||||
CBORObject::TAG_TYPED_ARRAY_SINT64_BE => Sint64BigEndianArrayTag::class,
|
||||
CBORObject::TAG_TYPED_ARRAY_SINT16_LE => Sint16LittleEndianArrayTag::class,
|
||||
CBORObject::TAG_TYPED_ARRAY_SINT32_LE => Sint32LittleEndianArrayTag::class,
|
||||
CBORObject::TAG_TYPED_ARRAY_SINT64_LE => Sint64LittleEndianArrayTag::class,
|
||||
CBORObject::TAG_TYPED_ARRAY_FLOAT16_BE => Float16BigEndianArrayTag::class,
|
||||
CBORObject::TAG_TYPED_ARRAY_FLOAT32_BE => Float32BigEndianArrayTag::class,
|
||||
CBORObject::TAG_TYPED_ARRAY_FLOAT64_BE => Float64BigEndianArrayTag::class,
|
||||
CBORObject::TAG_TYPED_ARRAY_FLOAT128_BE => Float128BigEndianArrayTag::class,
|
||||
CBORObject::TAG_TYPED_ARRAY_FLOAT16_LE => Float16LittleEndianArrayTag::class,
|
||||
CBORObject::TAG_TYPED_ARRAY_FLOAT32_LE => Float32LittleEndianArrayTag::class,
|
||||
CBORObject::TAG_TYPED_ARRAY_FLOAT64_LE => Float64LittleEndianArrayTag::class,
|
||||
CBORObject::TAG_TYPED_ARRAY_FLOAT128_LE => Float128LittleEndianArrayTag::class,
|
||||
CBORObject::TAG_COSE_ENCRYPT => CoseEncryptTag::class,
|
||||
CBORObject::TAG_COSE_MAC => CoseMacTag::class,
|
||||
CBORObject::TAG_COSE_SIGN => CoseSignTag::class,
|
||||
CBORObject::TAG_DATE => DateTag::class,
|
||||
CBORObject::TAG_STRING_REFERENCE_NAMESPACE => StringReferenceNamespaceTag::class,
|
||||
CBORObject::TAG_BINARY_MIME => BinaryMimeTag::class,
|
||||
CBORObject::TAG_SET => SetTag::class,
|
||||
CBORObject::TAG_EXPLICIT_MAP => ExplicitMapTag::class,
|
||||
CBORObject::TAG_NETWORK_ADDRESS => NetworkAddressTag::class,
|
||||
CBORObject::TAG_NETWORK_ADDRESS_PREFIX => NetworkAddressPrefixTag::class,
|
||||
CBORObject::TAG_EXTENDED_TIME => ExtendedTimeTag::class,
|
||||
CBORObject::TAG_DURATION => DurationTag::class,
|
||||
CBORObject::TAG_PERIOD => PeriodTag::class,
|
||||
CBORObject::TAG_DATE_STRING => DateStringTag::class,
|
||||
CBORObject::TAG_COLUMN_MAJOR_MULTI_DIMENSIONAL_ARRAY => ColumnMajorMultiDimensionalArrayTag::class,
|
||||
CBORObject::TAG_CBOR => CBORTag::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* The maximum nesting depth allowed by default. Deeper structures are rejected as the recursive processing of the
|
||||
* data may exhaust the call stack.
|
||||
*/
|
||||
public const DEFAULT_MAX_DEPTH = 1000;
|
||||
|
||||
private TagManagerInterface $tagObjectManager;
|
||||
|
||||
private OtherObjectManagerInterface $otherTypeManager;
|
||||
|
||||
private int $maxDepth;
|
||||
|
||||
public function __construct(
|
||||
?TagManagerInterface $tagObjectManager = null,
|
||||
?OtherObjectManagerInterface $otherTypeManager = null,
|
||||
int $maxDepth = self::DEFAULT_MAX_DEPTH
|
||||
) {
|
||||
if ($maxDepth < 1) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'The maximum nesting depth shall be at least 1. Got %d.',
|
||||
$maxDepth
|
||||
));
|
||||
}
|
||||
$this->tagObjectManager = $tagObjectManager ?? $this->generateTagManager();
|
||||
$this->otherTypeManager = $otherTypeManager ?? $this->generateOtherObjectManager();
|
||||
$this->maxDepth = $maxDepth;
|
||||
}
|
||||
|
||||
public static function create(
|
||||
?TagManagerInterface $tagObjectManager = null,
|
||||
?OtherObjectManagerInterface $otherTypeManager = null,
|
||||
int $maxDepth = self::DEFAULT_MAX_DEPTH
|
||||
): self {
|
||||
return new self($tagObjectManager, $otherTypeManager, $maxDepth);
|
||||
}
|
||||
|
||||
public function decode(Stream $stream): CBORObject
|
||||
{
|
||||
return $this->process($stream, false, 0);
|
||||
}
|
||||
|
||||
private function process(Stream $stream, bool $breakable, int $depth): CBORObject
|
||||
{
|
||||
if ($depth > $this->maxDepth) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Cannot parse the data. Maximum nesting depth of %d exceeded.',
|
||||
$this->maxDepth
|
||||
));
|
||||
}
|
||||
$ib = ord($stream->read(1));
|
||||
$mt = $ib >> 5;
|
||||
$ai = $ib & 0b00011111;
|
||||
$val = null;
|
||||
switch ($ai) {
|
||||
case CBORObject::LENGTH_1_BYTE: // 24
|
||||
case CBORObject::LENGTH_2_BYTES: // 25
|
||||
case CBORObject::LENGTH_4_BYTES: // 26
|
||||
case CBORObject::LENGTH_8_BYTES: // 27
|
||||
// 24..27 carry 1, 2, 4 and 8 bytes of argument; a table beats a float exponentiation per head.
|
||||
$val = $stream->read(match ($ai) {
|
||||
CBORObject::LENGTH_1_BYTE => 1,
|
||||
CBORObject::LENGTH_2_BYTES => 2,
|
||||
CBORObject::LENGTH_4_BYTES => 4,
|
||||
default => 8,
|
||||
});
|
||||
break;
|
||||
case CBORObject::FUTURE_USE_1: // 28
|
||||
case CBORObject::FUTURE_USE_2: // 29
|
||||
case CBORObject::FUTURE_USE_3: // 30
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Cannot parse the data. Found invalid Additional Information "%s" (%d).',
|
||||
str_pad(decbin($ai), 8, '0', STR_PAD_LEFT),
|
||||
$ai
|
||||
));
|
||||
case CBORObject::LENGTH_INDEFINITE: // 31
|
||||
return $this->processInfinite($stream, $mt, $breakable, $depth);
|
||||
}
|
||||
|
||||
return $this->processFinite($stream, $mt, $ai, $val, $depth);
|
||||
}
|
||||
|
||||
private function processFinite(Stream $stream, int $mt, int $ai, ?string $val, int $depth): CBORObject
|
||||
{
|
||||
switch ($mt) {
|
||||
case CBORObject::MAJOR_TYPE_UNSIGNED_INTEGER: // 0
|
||||
return UnsignedIntegerObject::createObjectForValue($ai, $val);
|
||||
case CBORObject::MAJOR_TYPE_NEGATIVE_INTEGER: // 1
|
||||
return NegativeIntegerObject::createObjectForValue($ai, $val);
|
||||
case CBORObject::MAJOR_TYPE_BYTE_STRING: // 2
|
||||
$length = $val === null ? $ai : Utils::binToInt($val);
|
||||
|
||||
return ByteStringObject::create($stream->read($length));
|
||||
case CBORObject::MAJOR_TYPE_TEXT_STRING: // 3
|
||||
$length = $val === null ? $ai : Utils::binToInt($val);
|
||||
|
||||
return TextStringObject::create($stream->read($length));
|
||||
case CBORObject::MAJOR_TYPE_LIST: // 4
|
||||
$object = ListObject::create();
|
||||
$nbItems = $val === null ? $ai : Utils::binToInt($val);
|
||||
for ($i = 0; $i < $nbItems; ++$i) {
|
||||
$object->add($this->process($stream, false, $depth + 1));
|
||||
}
|
||||
|
||||
return $object;
|
||||
case CBORObject::MAJOR_TYPE_MAP: // 5
|
||||
$object = MapObject::create();
|
||||
$nbItems = $val === null ? $ai : Utils::binToInt($val);
|
||||
for ($i = 0; $i < $nbItems; ++$i) {
|
||||
$object->add(
|
||||
$this->process($stream, false, $depth + 1),
|
||||
$this->process($stream, false, $depth + 1)
|
||||
);
|
||||
}
|
||||
|
||||
return $object;
|
||||
case CBORObject::MAJOR_TYPE_TAG: // 6
|
||||
return $this->tagObjectManager->createObjectForValue(
|
||||
$ai,
|
||||
$val,
|
||||
$this->process($stream, false, $depth + 1)
|
||||
);
|
||||
case CBORObject::MAJOR_TYPE_OTHER_TYPE: // 7
|
||||
return $this->otherTypeManager->createObjectForValue($ai, $val);
|
||||
default:
|
||||
throw new RuntimeException(sprintf(
|
||||
'Unsupported major type "%s" (%d).',
|
||||
str_pad(decbin($mt), 5, '0', STR_PAD_LEFT),
|
||||
$mt
|
||||
)); // Should never append
|
||||
}
|
||||
}
|
||||
|
||||
private function processInfinite(Stream $stream, int $mt, bool $breakable, int $depth): CBORObject
|
||||
{
|
||||
switch ($mt) {
|
||||
case CBORObject::MAJOR_TYPE_BYTE_STRING: // 2
|
||||
$object = IndefiniteLengthByteStringObject::create();
|
||||
while (! ($it = $this->process($stream, true, $depth + 1)) instanceof BreakObject) {
|
||||
if (! $it instanceof ByteStringObject) {
|
||||
throw new InvalidArgumentException(
|
||||
'Unable to parse the data. Infinite Byte String object can only get Byte String objects.'
|
||||
);
|
||||
}
|
||||
$object->add($it);
|
||||
}
|
||||
|
||||
return $object;
|
||||
case CBORObject::MAJOR_TYPE_TEXT_STRING: // 3
|
||||
$object = IndefiniteLengthTextStringObject::create();
|
||||
while (! ($it = $this->process($stream, true, $depth + 1)) instanceof BreakObject) {
|
||||
if (! $it instanceof TextStringObject) {
|
||||
throw new InvalidArgumentException(
|
||||
'Unable to parse the data. Infinite Text String object can only get Text String objects.'
|
||||
);
|
||||
}
|
||||
$object->add($it);
|
||||
}
|
||||
|
||||
return $object;
|
||||
case CBORObject::MAJOR_TYPE_LIST: // 4
|
||||
$object = IndefiniteLengthListObject::create();
|
||||
$it = $this->process($stream, true, $depth + 1);
|
||||
while (! $it instanceof BreakObject) {
|
||||
$object->add($it);
|
||||
$it = $this->process($stream, true, $depth + 1);
|
||||
}
|
||||
|
||||
return $object;
|
||||
case CBORObject::MAJOR_TYPE_MAP: // 5
|
||||
$object = IndefiniteLengthMapObject::create();
|
||||
while (! ($it = $this->process($stream, true, $depth + 1)) instanceof BreakObject) {
|
||||
$object->add($it, $this->process($stream, false, $depth + 1));
|
||||
}
|
||||
|
||||
return $object;
|
||||
case CBORObject::MAJOR_TYPE_OTHER_TYPE: // 7
|
||||
if (! $breakable) {
|
||||
throw new InvalidArgumentException('Cannot parse the data. No enclosing indefinite.');
|
||||
}
|
||||
|
||||
return BreakObject::create();
|
||||
case CBORObject::MAJOR_TYPE_UNSIGNED_INTEGER: // 0
|
||||
case CBORObject::MAJOR_TYPE_NEGATIVE_INTEGER: // 1
|
||||
case CBORObject::MAJOR_TYPE_TAG: // 6
|
||||
default:
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Cannot parse the data. Found infinite length for Major Type "%s" (%d).',
|
||||
str_pad(decbin($mt), 5, '0', STR_PAD_LEFT),
|
||||
$mt
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
private function generateTagManager(): TagManagerInterface
|
||||
{
|
||||
$manager = TagManager::create();
|
||||
foreach (self::DEFAULT_TAGS as $tagId => $class) {
|
||||
$manager->register($tagId, $class);
|
||||
}
|
||||
|
||||
return $manager;
|
||||
}
|
||||
|
||||
private function generateOtherObjectManager(): OtherObjectManagerInterface
|
||||
{
|
||||
return OtherObjectManager::create()
|
||||
->add(BreakObject::class)
|
||||
->add(SimpleObject::class)
|
||||
->add(FalseObject::class)
|
||||
->add(TrueObject::class)
|
||||
->add(NullObject::class)
|
||||
->add(UndefinedObject::class)
|
||||
->add(HalfPrecisionFloatObject::class)
|
||||
->add(SinglePrecisionFloatObject::class)
|
||||
->add(DoublePrecisionFloatObject::class)
|
||||
;
|
||||
}
|
||||
}
|
||||
10
vendor/spomky-labs/cbor-php/src/DecoderInterface.php
vendored
Normal file
10
vendor/spomky-labs/cbor-php/src/DecoderInterface.php
vendored
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR;
|
||||
|
||||
interface DecoderInterface
|
||||
{
|
||||
public function decode(Stream $stream): CBORObject;
|
||||
}
|
||||
118
vendor/spomky-labs/cbor-php/src/Encoder.php
vendored
Normal file
118
vendor/spomky-labs/cbor-php/src/Encoder.php
vendored
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR;
|
||||
|
||||
use function array_is_list;
|
||||
use CBOR\OtherObject\DoublePrecisionFloatObject;
|
||||
use CBOR\OtherObject\FalseObject;
|
||||
use CBOR\OtherObject\NullObject;
|
||||
use CBOR\OtherObject\SinglePrecisionFloatObject;
|
||||
use CBOR\OtherObject\TrueObject;
|
||||
use InvalidArgumentException;
|
||||
use function is_array;
|
||||
use function is_float;
|
||||
use function is_int;
|
||||
use function is_string;
|
||||
|
||||
final class Encoder implements EncoderInterface
|
||||
{
|
||||
public const FLOAT_FORMAT_SINGLE_PRECISION = 0b0000001;
|
||||
|
||||
public const INDEFINITE_BYTE_STRING_LENGTH = 0b00000010;
|
||||
|
||||
public const INDEFINITE_TEXT_STRING_LENGTH = 0b00000100;
|
||||
|
||||
public const INDEFINITE_LIST_LENGTH = 0b00001000;
|
||||
|
||||
public const INDEFINITE_MAP_LENGTH = 0b00010000;
|
||||
|
||||
public function encode(mixed $data, int $options = 0): string
|
||||
{
|
||||
return (string) $this->processData($data, $options);
|
||||
}
|
||||
|
||||
private function processData(mixed $data, int $option): CBORObject
|
||||
{
|
||||
return match (true) {
|
||||
$data instanceof CBORObject => $data,
|
||||
is_string($data) => preg_match('//u', $data) === 1 ? $this->processTextString(
|
||||
$data,
|
||||
$option
|
||||
) : $this->processByteString($data, $option),
|
||||
is_array($data) => array_is_list($data) ? $this->processList($data, $option) : $this->processMap(
|
||||
$data,
|
||||
$option
|
||||
),
|
||||
is_int($data) => $data < 0 ? NegativeIntegerObject::create($data) : UnsignedIntegerObject::create($data),
|
||||
is_float($data) => $this->processFloat($data, $option),
|
||||
$data === null => NullObject::create(),
|
||||
$data === false => FalseObject::create(),
|
||||
$data === true => TrueObject::create(),
|
||||
default => throw new InvalidArgumentException('Unsupported data type'),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<array-key, mixed> $data
|
||||
*/
|
||||
private function processList(array $data, int $option): ListObject|IndefiniteLengthListObject
|
||||
{
|
||||
$isIndefinite = 0 !== ($option & self::INDEFINITE_LIST_LENGTH);
|
||||
$list = $isIndefinite ? IndefiniteLengthListObject::create() : ListObject::create();
|
||||
foreach ($data as $item) {
|
||||
$list->add($this->processData($item, $option));
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<array-key, mixed> $data
|
||||
*/
|
||||
private function processMap(array $data, int $option): MapObject|IndefiniteLengthMapObject
|
||||
{
|
||||
$isIndefinite = 0 !== ($option & self::INDEFINITE_MAP_LENGTH);
|
||||
$map = $isIndefinite ? IndefiniteLengthMapObject::create() : MapObject::create();
|
||||
foreach ($data as $key => $value) {
|
||||
$map->add($this->processData($key, $option), $this->processData($value, $option));
|
||||
}
|
||||
|
||||
return $map;
|
||||
}
|
||||
|
||||
private function processFloat(float $data, int $option): SinglePrecisionFloatObject|DoublePrecisionFloatObject
|
||||
{
|
||||
$isSinglePrecisionFloat = 0 !== ($option & self::FLOAT_FORMAT_SINGLE_PRECISION);
|
||||
|
||||
return match (true) {
|
||||
$isSinglePrecisionFloat => SinglePrecisionFloatObject::createFromFloat($data),
|
||||
default => DoublePrecisionFloatObject::createFromFloat($data),
|
||||
};
|
||||
}
|
||||
|
||||
private function processTextString(string $data, int $option): TextStringObject|IndefiniteLengthTextStringObject
|
||||
{
|
||||
$isIndefinite = 0 !== ($option & self::INDEFINITE_TEXT_STRING_LENGTH);
|
||||
$cbor = TextStringObject::create($data);
|
||||
|
||||
if (! $isIndefinite) {
|
||||
return $cbor;
|
||||
}
|
||||
|
||||
return IndefiniteLengthTextStringObject::create()->add($cbor);
|
||||
}
|
||||
|
||||
private function processByteString(string $data, int $option): ByteStringObject|IndefiniteLengthByteStringObject
|
||||
{
|
||||
$isIndefinite = 0 !== ($option & self::INDEFINITE_BYTE_STRING_LENGTH);
|
||||
$cbor = ByteStringObject::create($data);
|
||||
|
||||
if (! $isIndefinite) {
|
||||
return $cbor;
|
||||
}
|
||||
|
||||
return IndefiniteLengthByteStringObject::create()->add($cbor);
|
||||
}
|
||||
}
|
||||
10
vendor/spomky-labs/cbor-php/src/EncoderInterface.php
vendored
Normal file
10
vendor/spomky-labs/cbor-php/src/EncoderInterface.php
vendored
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR;
|
||||
|
||||
interface EncoderInterface
|
||||
{
|
||||
public function encode(mixed $data, int $options = 0): string;
|
||||
}
|
||||
89
vendor/spomky-labs/cbor-php/src/IndefiniteLengthByteStringObject.php
vendored
Normal file
89
vendor/spomky-labs/cbor-php/src/IndefiniteLengthByteStringObject.php
vendored
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR;
|
||||
|
||||
/**
|
||||
* @see \CBOR\Test\IndefiniteLengthByteStringObjectTest
|
||||
*/
|
||||
final class IndefiniteLengthByteStringObject extends AbstractCBORObject implements Normalizable
|
||||
{
|
||||
private const MAJOR_TYPE = self::MAJOR_TYPE_BYTE_STRING;
|
||||
|
||||
private const ADDITIONAL_INFORMATION = self::LENGTH_INDEFINITE;
|
||||
|
||||
/**
|
||||
* @var ByteStringObject[]
|
||||
*/
|
||||
private array $chunks = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(self::MAJOR_TYPE, self::ADDITIONAL_INFORMATION);
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
$result = parent::__toString();
|
||||
foreach ($this->chunks as $chunk) {
|
||||
$result .= (string) $chunk;
|
||||
}
|
||||
|
||||
return $result . "\xFF";
|
||||
}
|
||||
|
||||
public static function create(string ...$chunks): self
|
||||
{
|
||||
$object = new self();
|
||||
foreach ($chunks as $chunk) {
|
||||
$object->append($chunk);
|
||||
}
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
public function add(ByteStringObject $chunk): self
|
||||
{
|
||||
$this->chunks[] = $chunk;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function append(string $chunk): self
|
||||
{
|
||||
$this->add(ByteStringObject::create($chunk));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getValue(): string
|
||||
{
|
||||
$result = '';
|
||||
foreach ($this->chunks as $chunk) {
|
||||
$result .= $chunk->getValue();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getLength(): int
|
||||
{
|
||||
$length = 0;
|
||||
foreach ($this->chunks as $chunk) {
|
||||
$length += $chunk->getLength();
|
||||
}
|
||||
|
||||
return $length;
|
||||
}
|
||||
|
||||
public function normalize(): string
|
||||
{
|
||||
$result = '';
|
||||
foreach ($this->chunks as $chunk) {
|
||||
$result .= $chunk->normalize();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
152
vendor/spomky-labs/cbor-php/src/IndefiniteLengthListObject.php
vendored
Normal file
152
vendor/spomky-labs/cbor-php/src/IndefiniteLengthListObject.php
vendored
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR;
|
||||
|
||||
use function array_key_exists;
|
||||
use ArrayAccess;
|
||||
use ArrayIterator;
|
||||
use function count;
|
||||
use Countable;
|
||||
use InvalidArgumentException;
|
||||
use Iterator;
|
||||
use IteratorAggregate;
|
||||
|
||||
/**
|
||||
* @phpstan-implements ArrayAccess<int, CBORObject>
|
||||
* @phpstan-implements IteratorAggregate<int, CBORObject>
|
||||
* @final
|
||||
*/
|
||||
class IndefiniteLengthListObject extends AbstractCBORObject implements Countable, IteratorAggregate, Normalizable, ArrayAccess
|
||||
{
|
||||
private const MAJOR_TYPE = self::MAJOR_TYPE_LIST;
|
||||
|
||||
private const ADDITIONAL_INFORMATION = self::LENGTH_INDEFINITE;
|
||||
|
||||
/**
|
||||
* @var array<int, CBORObject>
|
||||
*/
|
||||
private array $data = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(self::MAJOR_TYPE, self::ADDITIONAL_INFORMATION);
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
$result = parent::__toString();
|
||||
foreach ($this->data as $object) {
|
||||
$result .= (string) $object;
|
||||
}
|
||||
|
||||
return $result . "\xFF";
|
||||
}
|
||||
|
||||
public static function create(CBORObject ...$items): self
|
||||
{
|
||||
$object = new self();
|
||||
foreach ($items as $item) {
|
||||
$object->add($item);
|
||||
}
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Items that do not implement Normalizable -- the encoding tags or the "break" simple value, for instance -- have
|
||||
* no native counterpart and are returned as the CBORObject they are.
|
||||
*
|
||||
* @return array<int, mixed>
|
||||
*/
|
||||
public function normalize(): array
|
||||
{
|
||||
return array_map(
|
||||
static fn (CBORObject $object) => $object instanceof Normalizable ? $object->normalize() : $object,
|
||||
$this->data
|
||||
);
|
||||
}
|
||||
|
||||
public function add(CBORObject $item): self
|
||||
{
|
||||
$this->data[] = $item;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function has(int $index): bool
|
||||
{
|
||||
return array_key_exists($index, $this->data);
|
||||
}
|
||||
|
||||
public function remove(int $index): self
|
||||
{
|
||||
if (! $this->has($index)) {
|
||||
return $this;
|
||||
}
|
||||
unset($this->data[$index]);
|
||||
$this->data = array_values($this->data);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function get(int $index): CBORObject
|
||||
{
|
||||
if (! $this->has($index)) {
|
||||
throw new InvalidArgumentException('Index not found.');
|
||||
}
|
||||
|
||||
return $this->data[$index];
|
||||
}
|
||||
|
||||
public function set(int $index, CBORObject $object): self
|
||||
{
|
||||
if (! $this->has($index)) {
|
||||
throw new InvalidArgumentException('Index not found.');
|
||||
}
|
||||
|
||||
$this->data[$index] = $object;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
return count($this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<int, CBORObject>
|
||||
*/
|
||||
public function getIterator(): Iterator
|
||||
{
|
||||
return new ArrayIterator($this->data);
|
||||
}
|
||||
|
||||
public function offsetExists($offset): bool
|
||||
{
|
||||
return $this->has($offset);
|
||||
}
|
||||
|
||||
public function offsetGet($offset): CBORObject
|
||||
{
|
||||
return $this->get($offset);
|
||||
}
|
||||
|
||||
public function offsetSet($offset, $value): void
|
||||
{
|
||||
if ($offset === null) {
|
||||
$this->add($value);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->set($offset, $value);
|
||||
}
|
||||
|
||||
public function offsetUnset($offset): void
|
||||
{
|
||||
$this->remove($offset);
|
||||
}
|
||||
}
|
||||
151
vendor/spomky-labs/cbor-php/src/IndefiniteLengthMapObject.php
vendored
Normal file
151
vendor/spomky-labs/cbor-php/src/IndefiniteLengthMapObject.php
vendored
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR;
|
||||
|
||||
use ArrayAccess;
|
||||
use ArrayIterator;
|
||||
use function count;
|
||||
use Countable;
|
||||
use InvalidArgumentException;
|
||||
use Iterator;
|
||||
use IteratorAggregate;
|
||||
|
||||
/**
|
||||
* @phpstan-implements ArrayAccess<int, CBORObject>
|
||||
* @phpstan-implements IteratorAggregate<int, MapItem>
|
||||
* @final
|
||||
*/
|
||||
class IndefiniteLengthMapObject extends AbstractCBORObject implements Countable, IteratorAggregate, Normalizable, ArrayAccess
|
||||
{
|
||||
use MapKeyRegistryTrait;
|
||||
|
||||
private const MAJOR_TYPE = self::MAJOR_TYPE_MAP;
|
||||
|
||||
private const ADDITIONAL_INFORMATION = self::LENGTH_INDEFINITE;
|
||||
|
||||
/**
|
||||
* @var MapItem[]
|
||||
*/
|
||||
private array $data = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(self::MAJOR_TYPE, self::ADDITIONAL_INFORMATION);
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
$result = parent::__toString();
|
||||
foreach ($this->data as $object) {
|
||||
$result .= (string) $object->getKey();
|
||||
$result .= (string) $object->getValue();
|
||||
}
|
||||
|
||||
return $result . "\xFF";
|
||||
}
|
||||
|
||||
public static function create(): self
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
public function add(CBORObject $key, CBORObject $value): self
|
||||
{
|
||||
$this->data[$this->registerKey($key, false)] = MapItem::create($key, $value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether an entry is reachable at that offset: a key that is opaque, or that shares its offset with a key of
|
||||
* another major type, is only reached by iterating the map.
|
||||
*/
|
||||
public function has(int|string $key): bool
|
||||
{
|
||||
return $this->isAddressable($this->data, $key);
|
||||
}
|
||||
|
||||
public function remove(int|string $index): self
|
||||
{
|
||||
if (! $this->has($index)) {
|
||||
return $this;
|
||||
}
|
||||
unset($this->data[$index]);
|
||||
$this->unregisterKey($index);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function get(int|string $index): CBORObject
|
||||
{
|
||||
if (! $this->has($index)) {
|
||||
throw new InvalidArgumentException('Index not found.');
|
||||
}
|
||||
|
||||
return $this->data[$index]->getValue();
|
||||
}
|
||||
|
||||
public function set(MapItem $object): self
|
||||
{
|
||||
$this->data[$this->registerKey($object->getKey(), true)] = $object;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
return count($this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<int, MapItem>
|
||||
*/
|
||||
public function getIterator(): Iterator
|
||||
{
|
||||
return new ArrayIterator($this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Items that do not implement Normalizable -- the encoding tags or the "break" simple value, for instance -- have
|
||||
* no native counterpart and are returned as the CBORObject they are.
|
||||
*
|
||||
* A key has to become a PHP array offset, which only an integer or a string can. A map holding any other kind of
|
||||
* key -- a float, a boolean, null, a list, a map -- or two keys of different major types that resolve to the
|
||||
* same offset, decodes and iterates, but cannot be normalized.
|
||||
*
|
||||
* @return array<int|string, mixed>
|
||||
*/
|
||||
public function normalize(): array
|
||||
{
|
||||
return $this->normalizeEntries($this->data);
|
||||
}
|
||||
|
||||
public function offsetExists($offset): bool
|
||||
{
|
||||
return $this->has($offset);
|
||||
}
|
||||
|
||||
public function offsetGet($offset): CBORObject
|
||||
{
|
||||
return $this->get($offset);
|
||||
}
|
||||
|
||||
public function offsetSet($offset, $value): void
|
||||
{
|
||||
if (! $offset instanceof CBORObject) {
|
||||
throw new InvalidArgumentException('Invalid key');
|
||||
}
|
||||
if (! $value instanceof CBORObject) {
|
||||
throw new InvalidArgumentException('Invalid value');
|
||||
}
|
||||
|
||||
$this->set(MapItem::create($offset, $value));
|
||||
}
|
||||
|
||||
public function offsetUnset($offset): void
|
||||
{
|
||||
$this->remove($offset);
|
||||
}
|
||||
}
|
||||
89
vendor/spomky-labs/cbor-php/src/IndefiniteLengthTextStringObject.php
vendored
Normal file
89
vendor/spomky-labs/cbor-php/src/IndefiniteLengthTextStringObject.php
vendored
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR;
|
||||
|
||||
/**
|
||||
* @see \CBOR\Test\IndefiniteLengthTextStringObjectTest
|
||||
*/
|
||||
final class IndefiniteLengthTextStringObject extends AbstractCBORObject implements Normalizable
|
||||
{
|
||||
private const MAJOR_TYPE = self::MAJOR_TYPE_TEXT_STRING;
|
||||
|
||||
private const ADDITIONAL_INFORMATION = self::LENGTH_INDEFINITE;
|
||||
|
||||
/**
|
||||
* @var TextStringObject[]
|
||||
*/
|
||||
private array $data = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(self::MAJOR_TYPE, self::ADDITIONAL_INFORMATION);
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
$result = parent::__toString();
|
||||
foreach ($this->data as $object) {
|
||||
$result .= (string) $object;
|
||||
}
|
||||
|
||||
return $result . "\xFF";
|
||||
}
|
||||
|
||||
public static function create(string ...$chunks): self
|
||||
{
|
||||
$object = new self();
|
||||
foreach ($chunks as $chunk) {
|
||||
$object->append($chunk);
|
||||
}
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
public function add(TextStringObject $chunk): self
|
||||
{
|
||||
$this->data[] = $chunk;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function append(string $chunk): self
|
||||
{
|
||||
$this->add(TextStringObject::create($chunk));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getValue(): string
|
||||
{
|
||||
$result = '';
|
||||
foreach ($this->data as $object) {
|
||||
$result .= $object->getValue();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getLength(): int
|
||||
{
|
||||
$length = 0;
|
||||
foreach ($this->data as $object) {
|
||||
$length += $object->getLength();
|
||||
}
|
||||
|
||||
return $length;
|
||||
}
|
||||
|
||||
public function normalize(): string
|
||||
{
|
||||
$result = '';
|
||||
foreach ($this->data as $object) {
|
||||
$result .= $object->normalize();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
96
vendor/spomky-labs/cbor-php/src/IntegerPayloadTrait.php
vendored
Normal file
96
vendor/spomky-labs/cbor-php/src/IntegerPayloadTrait.php
vendored
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR;
|
||||
|
||||
use Brick\Math\BigInteger;
|
||||
use function chr;
|
||||
use InvalidArgumentException;
|
||||
use function ord;
|
||||
use const PHP_INT_MAX;
|
||||
use const STR_PAD_LEFT;
|
||||
use function strlen;
|
||||
|
||||
/**
|
||||
* Shared head/payload conversion for the two integer objects.
|
||||
*
|
||||
* Both major type 0 and major type 1 encode a single unsigned argument: the value itself for the former, -1 - value
|
||||
* for the latter. RFC 8949 section 3 gives that argument five encodings and selects the shortest one that holds it,
|
||||
* which section 4.2 then requires. The bounds below are therefore inclusive: 255 is the largest one-byte argument,
|
||||
* not the first two-byte one.
|
||||
*
|
||||
* Arguments up to PHP_INT_MAX are packed with native integer arithmetic. brick/math is reached for only in the
|
||||
* 8-byte range above it, which no PHP integer can hold and which callers supply through createFromString() or
|
||||
* createFromHex().
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
trait IntegerPayloadTrait
|
||||
{
|
||||
/**
|
||||
* Largest argument a CBOR head can carry, 2^64 - 1. Beyond it a bignum tag is the only representation.
|
||||
*/
|
||||
private static function maximumArgument(): BigInteger
|
||||
{
|
||||
return BigInteger::fromBase('FFFFFFFFFFFFFFFF', 16);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{int, null|string} the additional information and its payload
|
||||
*/
|
||||
private static function payloadForInt(int $argument): array
|
||||
{
|
||||
return match (true) {
|
||||
$argument <= 23 => [$argument, null],
|
||||
$argument <= 0xFF => [CBORObject::LENGTH_1_BYTE, chr($argument)],
|
||||
$argument <= 0xFFFF => [CBORObject::LENGTH_2_BYTES, pack('n', $argument)],
|
||||
$argument <= 0xFFFFFFFF => [CBORObject::LENGTH_4_BYTES, pack('N', $argument)],
|
||||
default => [CBORObject::LENGTH_8_BYTES, pack('J', $argument)],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{int, null|string} the additional information and its payload
|
||||
*/
|
||||
private static function payloadForBigInteger(BigInteger $argument): array
|
||||
{
|
||||
if ($argument->isLessThanOrEqualTo(BigInteger::of(PHP_INT_MAX))) {
|
||||
return self::payloadForInt($argument->toInt());
|
||||
}
|
||||
|
||||
return [CBORObject::LENGTH_8_BYTES, self::hex2bin(str_pad($argument->toBase(16), 16, '0', STR_PAD_LEFT))];
|
||||
}
|
||||
|
||||
/**
|
||||
* The argument carried by a decoded head.
|
||||
*
|
||||
* A CBOR argument is unsigned, so a negative result cannot be one: it reports that the argument sits above
|
||||
* PHP_INT_MAX and has no PHP integer representation, and the caller shall fall back to brick/math.
|
||||
*/
|
||||
private static function argumentFromPayload(string $data): int
|
||||
{
|
||||
$length = strlen($data);
|
||||
if ($length > 8) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
$argument = 0;
|
||||
for ($i = 0; $i < $length; ++$i) {
|
||||
$argument = ($argument << 8) | ord($data[$i]);
|
||||
}
|
||||
|
||||
// Only the eighth byte can carry the argument past PHP_INT_MAX, where the shift wraps it into the sign bit.
|
||||
return $argument;
|
||||
}
|
||||
|
||||
private static function hex2bin(string $data): string
|
||||
{
|
||||
$result = hex2bin($data);
|
||||
if ($result === false) {
|
||||
throw new InvalidArgumentException('Unable to convert the data');
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
48
vendor/spomky-labs/cbor-php/src/LengthCalculator.php
vendored
Normal file
48
vendor/spomky-labs/cbor-php/src/LengthCalculator.php
vendored
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR;
|
||||
|
||||
use function chr;
|
||||
use function count;
|
||||
use function strlen;
|
||||
|
||||
final class LengthCalculator
|
||||
{
|
||||
/**
|
||||
* @return array{int, null|string}
|
||||
*/
|
||||
public static function getLengthOfString(string $data): array
|
||||
{
|
||||
$length = strlen($data);
|
||||
|
||||
return self::computeLength($length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int|string, mixed> $data
|
||||
*
|
||||
* @return array{int, null|string}
|
||||
*/
|
||||
public static function getLengthOfArray(array $data): array
|
||||
{
|
||||
$length = count($data);
|
||||
|
||||
return self::computeLength($length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{int, null|string}
|
||||
*/
|
||||
private static function computeLength(int $length): array
|
||||
{
|
||||
return match (true) {
|
||||
$length <= 23 => [$length, null],
|
||||
$length <= 0xFF => [24, chr($length)],
|
||||
$length <= 0xFFFF => [25, pack('n', $length)],
|
||||
$length <= 0xFFFFFFFF => [26, pack('N', $length)],
|
||||
default => [27, pack('J', $length)],
|
||||
};
|
||||
}
|
||||
}
|
||||
196
vendor/spomky-labs/cbor-php/src/ListObject.php
vendored
Normal file
196
vendor/spomky-labs/cbor-php/src/ListObject.php
vendored
Normal file
|
|
@ -0,0 +1,196 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR;
|
||||
|
||||
use function array_key_exists;
|
||||
use ArrayAccess;
|
||||
use ArrayIterator;
|
||||
use function count;
|
||||
use Countable;
|
||||
use function get_debug_type;
|
||||
use InvalidArgumentException;
|
||||
use Iterator;
|
||||
use IteratorAggregate;
|
||||
use function sprintf;
|
||||
|
||||
/**
|
||||
* @phpstan-implements ArrayAccess<int, CBORObject>
|
||||
* @phpstan-implements IteratorAggregate<int, CBORObject>
|
||||
* @see \CBOR\Test\ListObjectTest
|
||||
*/
|
||||
class ListObject extends AbstractCBORObject implements Countable, IteratorAggregate, Normalizable, ArrayAccess
|
||||
{
|
||||
private const MAJOR_TYPE = self::MAJOR_TYPE_LIST;
|
||||
|
||||
/**
|
||||
* @var array<int, CBORObject>
|
||||
*/
|
||||
private array $data;
|
||||
|
||||
private ?string $length;
|
||||
|
||||
private bool $lengthStale = false;
|
||||
|
||||
/**
|
||||
* @param CBORObject[] $data
|
||||
*/
|
||||
public function __construct(array $data = [])
|
||||
{
|
||||
[$additionalInformation, $length] = LengthCalculator::getLengthOfArray($data);
|
||||
foreach ($data as $index => $item) {
|
||||
if (! $item instanceof CBORObject) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Invalid item at index "%s". Expected a CBORObject, got "%s".',
|
||||
$index,
|
||||
get_debug_type($item)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
parent::__construct(self::MAJOR_TYPE, $additionalInformation);
|
||||
$this->data = array_values($data);
|
||||
$this->length = $length;
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
$this->refreshLength();
|
||||
$result = parent::__toString();
|
||||
$result .= $this->length ?? '';
|
||||
foreach ($this->data as $object) {
|
||||
$result .= (string) $object;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getAdditionalInformation(): int
|
||||
{
|
||||
$this->refreshLength();
|
||||
|
||||
return parent::getAdditionalInformation();
|
||||
}
|
||||
|
||||
/**
|
||||
* The head carries the item count, so every insertion or removal invalidates it. Recomputing it there made the
|
||||
* cost of building a container quadratic in call count; it is only ever observed when the object is written out.
|
||||
*/
|
||||
private function refreshLength(): void
|
||||
{
|
||||
if (! $this->lengthStale) {
|
||||
return;
|
||||
}
|
||||
|
||||
[$this->additionalInformation, $this->length] = LengthCalculator::getLengthOfArray($this->data);
|
||||
$this->lengthStale = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CBORObject[] $data
|
||||
*/
|
||||
public static function create(array $data = []): self
|
||||
{
|
||||
return new self($data);
|
||||
}
|
||||
|
||||
public function add(CBORObject $object): self
|
||||
{
|
||||
$this->data[] = $object;
|
||||
$this->lengthStale = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function has(int $index): bool
|
||||
{
|
||||
return array_key_exists($index, $this->data);
|
||||
}
|
||||
|
||||
public function remove(int $index): self
|
||||
{
|
||||
if (! $this->has($index)) {
|
||||
return $this;
|
||||
}
|
||||
unset($this->data[$index]);
|
||||
$this->data = array_values($this->data);
|
||||
$this->lengthStale = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function get(int $index): CBORObject
|
||||
{
|
||||
if (! $this->has($index)) {
|
||||
throw new InvalidArgumentException('Index not found.');
|
||||
}
|
||||
|
||||
return $this->data[$index];
|
||||
}
|
||||
|
||||
public function set(int $index, CBORObject $object): self
|
||||
{
|
||||
if (! $this->has($index)) {
|
||||
throw new InvalidArgumentException('Index not found.');
|
||||
}
|
||||
|
||||
$this->data[$index] = $object;
|
||||
$this->lengthStale = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Items that do not implement Normalizable -- the encoding tags or the "break" simple value, for instance -- have
|
||||
* no native counterpart and are returned as the CBORObject they are.
|
||||
*
|
||||
* @return array<int, mixed>
|
||||
*/
|
||||
public function normalize(): array
|
||||
{
|
||||
return array_map(
|
||||
static fn (CBORObject $object) => $object instanceof Normalizable ? $object->normalize() : $object,
|
||||
$this->data
|
||||
);
|
||||
}
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
return count($this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<int, CBORObject>
|
||||
*/
|
||||
public function getIterator(): Iterator
|
||||
{
|
||||
return new ArrayIterator($this->data);
|
||||
}
|
||||
|
||||
public function offsetExists($offset): bool
|
||||
{
|
||||
return $this->has($offset);
|
||||
}
|
||||
|
||||
public function offsetGet($offset): CBORObject
|
||||
{
|
||||
return $this->get($offset);
|
||||
}
|
||||
|
||||
public function offsetSet($offset, $value): void
|
||||
{
|
||||
if ($offset === null) {
|
||||
$this->add($value);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->set($offset, $value);
|
||||
}
|
||||
|
||||
public function offsetUnset($offset): void
|
||||
{
|
||||
$this->remove($offset);
|
||||
}
|
||||
}
|
||||
29
vendor/spomky-labs/cbor-php/src/MapItem.php
vendored
Normal file
29
vendor/spomky-labs/cbor-php/src/MapItem.php
vendored
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR;
|
||||
|
||||
class MapItem
|
||||
{
|
||||
public function __construct(
|
||||
private CBORObject $key,
|
||||
private CBORObject $value
|
||||
) {
|
||||
}
|
||||
|
||||
public static function create(CBORObject $key, CBORObject $value): self
|
||||
{
|
||||
return new self($key, $value);
|
||||
}
|
||||
|
||||
public function getKey(): CBORObject
|
||||
{
|
||||
return $this->key;
|
||||
}
|
||||
|
||||
public function getValue(): CBORObject
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
}
|
||||
217
vendor/spomky-labs/cbor-php/src/MapKeyRegistryTrait.php
vendored
Normal file
217
vendor/spomky-labs/cbor-php/src/MapKeyRegistryTrait.php
vendored
Normal file
|
|
@ -0,0 +1,217 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR;
|
||||
|
||||
use function array_key_exists;
|
||||
use function get_debug_type;
|
||||
use InvalidArgumentException;
|
||||
use function is_int;
|
||||
use function is_string;
|
||||
use function sprintf;
|
||||
|
||||
/**
|
||||
* Shared key bookkeeping for the two map objects.
|
||||
*
|
||||
* Map entries are stored in a native PHP array keyed by the normalized key. PHP casts numeric strings to integers
|
||||
* when they are used as an array offset, so several structurally distinct CBOR keys used to land on the same slot
|
||||
* and silently overwrite one another: the integer 1, the text string "1" and the byte string h'31' all normalize to
|
||||
* the string '1'. The registry records, for every occupied offset, the major type of the key that owns it, which is
|
||||
* enough to tell a genuine duplicate -- rejected -- from two distinct keys meeting on one offset.
|
||||
*
|
||||
* RFC 8949 section 3.1 allows any data item as a key, and a PHP array offset can only stand for some of them. A key
|
||||
* that does not normalize to an integer or a string -- a float, a boolean, null, a list, a map, a tag that expands
|
||||
* to an object -- or that resolves to an offset a key of another major type already owns is kept all the same, in
|
||||
* order, under an offset derived from its encoded bytes. Such a key is "opaque": the map decodes, iterates and
|
||||
* writes back exactly as it was read, and a repeated key is still caught. What cannot be done is to normalize the
|
||||
* map to a PHP array, or to reach the entry through get(), has() and remove(), whose offsets are the integers and
|
||||
* strings the other keys normalize to. An offset that keys of two major types resolve to is ambiguous and is not
|
||||
* reachable that way either.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
trait MapKeyRegistryTrait
|
||||
{
|
||||
/**
|
||||
* Occupied offset => "<major type>:<offset>" of the key that owns it.
|
||||
*
|
||||
* @var array<int|string, string>
|
||||
*/
|
||||
private array $keyIdentities = [];
|
||||
|
||||
/**
|
||||
* The offsets of the opaque keys.
|
||||
*
|
||||
* @var array<int|string, true>
|
||||
*/
|
||||
private array $opaqueOffsets = [];
|
||||
|
||||
/**
|
||||
* Scalar offset => major type of the second kind of key that resolved to it.
|
||||
*
|
||||
* @var array<int|string, int>
|
||||
*/
|
||||
private array $ambiguousOffsets = [];
|
||||
|
||||
/**
|
||||
* @return int|string the offset the entry shall be stored at
|
||||
*/
|
||||
private function registerKey(CBORObject $key, bool $allowReplace): int|string
|
||||
{
|
||||
$majorType = $key->getMajorType();
|
||||
[$offset, $opaque] = self::offsetOf($key);
|
||||
$existing = $this->keyIdentities[$offset] ?? null;
|
||||
|
||||
if (! $opaque && $existing !== null && $existing !== $majorType . ':' . $offset) {
|
||||
// A key of another major type owns the offset. Neither can be told from the other through a PHP
|
||||
// offset, so this one is stored opaque and the offset is marked as ambiguous.
|
||||
$this->ambiguousOffsets[$offset] = $majorType;
|
||||
$offset = self::opaqueOffsetOf($key);
|
||||
$opaque = true;
|
||||
$existing = $this->keyIdentities[$offset] ?? null;
|
||||
}
|
||||
|
||||
$identity = $majorType . ':' . $offset;
|
||||
if ($existing !== null && $existing !== $identity) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Invalid key. A key of major type %s and a key of major type %s both resolve to the offset "%s".',
|
||||
explode(':', $existing)[0],
|
||||
$majorType,
|
||||
(string) $offset
|
||||
));
|
||||
}
|
||||
|
||||
if ($existing !== null && ! $allowReplace) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Invalid key. The key "%s" is defined more than once in the map.',
|
||||
$opaque ? bin2hex(substr((string) $offset, 1)) : (string) $offset
|
||||
));
|
||||
}
|
||||
|
||||
$this->keyIdentities[$offset] = $identity;
|
||||
if ($opaque) {
|
||||
$this->opaqueOffsets[$offset] = true;
|
||||
}
|
||||
|
||||
return $offset;
|
||||
}
|
||||
|
||||
private function unregisterKey(int|string $offset): void
|
||||
{
|
||||
unset($this->keyIdentities[$offset], $this->opaqueOffsets[$offset], $this->ambiguousOffsets[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the offset addresses exactly one entry.
|
||||
*
|
||||
* @param MapItem[] $data
|
||||
*/
|
||||
private function isAddressable(array $data, int|string $offset): bool
|
||||
{
|
||||
return array_key_exists($offset, $data) && ! isset($this->ambiguousOffsets[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MapItem[] $data
|
||||
*
|
||||
* @return MapItem[] the entries, re-keyed by their normalized key
|
||||
*/
|
||||
private function registerKeys(array $data): array
|
||||
{
|
||||
$entries = [];
|
||||
foreach ($data as $item) {
|
||||
if (! $item instanceof MapItem) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Invalid item. A map shall only contain "%s" objects, got "%s".',
|
||||
MapItem::class,
|
||||
get_debug_type($item)
|
||||
));
|
||||
}
|
||||
$entries[$this->registerKey($item->getKey(), false)] = $item;
|
||||
}
|
||||
|
||||
return $entries;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns the entries into a PHP array, or explains which key stands in the way.
|
||||
*
|
||||
* @param MapItem[] $data
|
||||
*
|
||||
* @return array<int|string, mixed>
|
||||
*/
|
||||
private function normalizeEntries(array $data): array
|
||||
{
|
||||
$normalized = [];
|
||||
foreach ($data as $offset => $item) {
|
||||
if (isset($this->ambiguousOffsets[$offset])) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Invalid key. A key of major type %s and a key of major type %s both resolve to the offset "%s".',
|
||||
explode(':', $this->keyIdentities[$offset])[0],
|
||||
$this->ambiguousOffsets[$offset],
|
||||
(string) $offset
|
||||
));
|
||||
}
|
||||
if (isset($this->opaqueOffsets[$offset])) {
|
||||
throw self::opaqueKeyException($item->getKey());
|
||||
}
|
||||
$valueObject = $item->getValue();
|
||||
$normalized[$offset] = $valueObject instanceof Normalizable ? $valueObject->normalize() : $valueObject;
|
||||
}
|
||||
|
||||
return $normalized;
|
||||
}
|
||||
|
||||
/**
|
||||
* The PHP array offset of the key.
|
||||
*
|
||||
* A list or a map never normalizes to a scalar, so the answer is already known from the head of the item.
|
||||
* Reading it first matters: normalizing a container walks the whole sub-structure, and a hostile document can
|
||||
* make that arbitrarily expensive for a key whose value nobody can address anyway.
|
||||
*
|
||||
* @return array{int|string, bool} the offset, and whether the key is opaque
|
||||
*/
|
||||
private static function offsetOf(CBORObject $key): array
|
||||
{
|
||||
$majorType = $key->getMajorType();
|
||||
if ($majorType !== CBORObject::MAJOR_TYPE_LIST && $majorType !== CBORObject::MAJOR_TYPE_MAP && $key instanceof Normalizable) {
|
||||
$normalized = $key->normalize();
|
||||
if (is_int($normalized) || is_string($normalized)) {
|
||||
return [$normalized, false];
|
||||
}
|
||||
}
|
||||
|
||||
return [self::opaqueOffsetOf($key), true];
|
||||
}
|
||||
|
||||
/**
|
||||
* The encoded bytes of the key behind a NUL byte: an offset no normalized key produces, since an integer never
|
||||
* starts with NUL and a text string never contains it. A byte string that happens to hold the same bytes would
|
||||
* meet the opaque key on that offset, and the registry then reports the two as it does any other collision.
|
||||
*/
|
||||
private static function opaqueOffsetOf(CBORObject $key): string
|
||||
{
|
||||
return "\0" . $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* The exception an opaque key raises when the map is normalized, naming what the key would have become.
|
||||
*/
|
||||
private static function opaqueKeyException(CBORObject $key): InvalidArgumentException
|
||||
{
|
||||
$majorType = $key->getMajorType();
|
||||
if ($majorType === CBORObject::MAJOR_TYPE_LIST || $majorType === CBORObject::MAJOR_TYPE_MAP) {
|
||||
$type = 'array';
|
||||
} elseif ($key instanceof Normalizable) {
|
||||
$type = get_debug_type($key->normalize());
|
||||
} else {
|
||||
$type = get_debug_type($key);
|
||||
}
|
||||
|
||||
return new InvalidArgumentException(sprintf(
|
||||
'Invalid key. A map key shall normalize to an integer or a string, got "%s".',
|
||||
$type
|
||||
));
|
||||
}
|
||||
}
|
||||
188
vendor/spomky-labs/cbor-php/src/MapObject.php
vendored
Normal file
188
vendor/spomky-labs/cbor-php/src/MapObject.php
vendored
Normal file
|
|
@ -0,0 +1,188 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR;
|
||||
|
||||
use ArrayAccess;
|
||||
use ArrayIterator;
|
||||
use function count;
|
||||
use Countable;
|
||||
use InvalidArgumentException;
|
||||
use Iterator;
|
||||
use IteratorAggregate;
|
||||
|
||||
/**
|
||||
* @phpstan-implements ArrayAccess<int, CBORObject>
|
||||
* @phpstan-implements IteratorAggregate<int, MapItem>
|
||||
*/
|
||||
final class MapObject extends AbstractCBORObject implements Countable, IteratorAggregate, Normalizable, ArrayAccess
|
||||
{
|
||||
use MapKeyRegistryTrait;
|
||||
|
||||
private const MAJOR_TYPE = self::MAJOR_TYPE_MAP;
|
||||
|
||||
/**
|
||||
* @var MapItem[]
|
||||
*/
|
||||
private array $data;
|
||||
|
||||
private ?string $length;
|
||||
|
||||
private bool $lengthStale = false;
|
||||
|
||||
/**
|
||||
* @param MapItem[] $data
|
||||
*/
|
||||
public function __construct(array $data = [])
|
||||
{
|
||||
$entries = $this->registerKeys($data);
|
||||
[$additionalInformation, $length] = LengthCalculator::getLengthOfArray($entries);
|
||||
parent::__construct(self::MAJOR_TYPE, $additionalInformation);
|
||||
$this->data = $entries;
|
||||
$this->length = $length;
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
$this->refreshLength();
|
||||
$result = parent::__toString();
|
||||
$result .= $this->length ?? '';
|
||||
foreach ($this->data as $object) {
|
||||
$result .= (string) $object->getKey();
|
||||
$result .= (string) $object->getValue();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getAdditionalInformation(): int
|
||||
{
|
||||
$this->refreshLength();
|
||||
|
||||
return parent::getAdditionalInformation();
|
||||
}
|
||||
|
||||
/**
|
||||
* The head carries the item count, so every insertion or removal invalidates it. Recomputing it there made the
|
||||
* cost of building a container quadratic in call count; it is only ever observed when the object is written out.
|
||||
*/
|
||||
private function refreshLength(): void
|
||||
{
|
||||
if (! $this->lengthStale) {
|
||||
return;
|
||||
}
|
||||
|
||||
[$this->additionalInformation, $this->length] = LengthCalculator::getLengthOfArray($this->data);
|
||||
$this->lengthStale = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MapItem[] $data
|
||||
*/
|
||||
public static function create(array $data = []): self
|
||||
{
|
||||
return new self($data);
|
||||
}
|
||||
|
||||
public function add(CBORObject $key, CBORObject $value): self
|
||||
{
|
||||
$this->data[$this->registerKey($key, false)] = MapItem::create($key, $value);
|
||||
$this->lengthStale = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether an entry is reachable at that offset: a key that is opaque, or that shares its offset with a key of
|
||||
* another major type, is only reached by iterating the map.
|
||||
*/
|
||||
public function has(int|string $key): bool
|
||||
{
|
||||
return $this->isAddressable($this->data, $key);
|
||||
}
|
||||
|
||||
public function remove(int|string $index): self
|
||||
{
|
||||
if (! $this->has($index)) {
|
||||
return $this;
|
||||
}
|
||||
unset($this->data[$index]);
|
||||
$this->unregisterKey($index);
|
||||
$this->lengthStale = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function get(int|string $index): CBORObject
|
||||
{
|
||||
if (! $this->has($index)) {
|
||||
throw new InvalidArgumentException('Index not found.');
|
||||
}
|
||||
|
||||
return $this->data[$index]->getValue();
|
||||
}
|
||||
|
||||
public function set(MapItem $object): self
|
||||
{
|
||||
$this->data[$this->registerKey($object->getKey(), true)] = $object;
|
||||
$this->lengthStale = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
return count($this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<int, MapItem>
|
||||
*/
|
||||
public function getIterator(): Iterator
|
||||
{
|
||||
return new ArrayIterator($this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Items that do not implement Normalizable -- the encoding tags or the "break" simple value, for instance -- have
|
||||
* no native counterpart and are returned as the CBORObject they are.
|
||||
*
|
||||
* A key has to become a PHP array offset, which only an integer or a string can. A map holding any other kind of
|
||||
* key -- a float, a boolean, null, a list, a map -- or two keys of different major types that resolve to the
|
||||
* same offset, decodes and iterates, but cannot be normalized.
|
||||
*
|
||||
* @return array<int|string, mixed>
|
||||
*/
|
||||
public function normalize(): array
|
||||
{
|
||||
return $this->normalizeEntries($this->data);
|
||||
}
|
||||
|
||||
public function offsetExists($offset): bool
|
||||
{
|
||||
return $this->has($offset);
|
||||
}
|
||||
|
||||
public function offsetGet($offset): CBORObject
|
||||
{
|
||||
return $this->get($offset);
|
||||
}
|
||||
|
||||
public function offsetSet($offset, $value): void
|
||||
{
|
||||
if (! $offset instanceof CBORObject) {
|
||||
throw new InvalidArgumentException('Invalid key');
|
||||
}
|
||||
if (! $value instanceof CBORObject) {
|
||||
throw new InvalidArgumentException('Invalid value');
|
||||
}
|
||||
|
||||
$this->set(MapItem::create($offset, $value));
|
||||
}
|
||||
|
||||
public function offsetUnset($offset): void
|
||||
{
|
||||
$this->remove($offset);
|
||||
}
|
||||
}
|
||||
101
vendor/spomky-labs/cbor-php/src/NegativeIntegerObject.php
vendored
Normal file
101
vendor/spomky-labs/cbor-php/src/NegativeIntegerObject.php
vendored
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR;
|
||||
|
||||
use Brick\Math\BigInteger;
|
||||
use InvalidArgumentException;
|
||||
|
||||
final class NegativeIntegerObject extends AbstractCBORObject implements Normalizable
|
||||
{
|
||||
use IntegerPayloadTrait;
|
||||
|
||||
private const MAJOR_TYPE = self::MAJOR_TYPE_NEGATIVE_INTEGER;
|
||||
|
||||
public function __construct(
|
||||
int $additionalInformation,
|
||||
private ?string $data
|
||||
) {
|
||||
parent::__construct(self::MAJOR_TYPE, $additionalInformation);
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
$result = parent::__toString();
|
||||
if ($this->data !== null) {
|
||||
$result .= $this->data;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function createObjectForValue(int $additionalInformation, ?string $data): self
|
||||
{
|
||||
return new self($additionalInformation, $data);
|
||||
}
|
||||
|
||||
public static function create(int $value): self
|
||||
{
|
||||
if ($value >= 0) {
|
||||
throw new InvalidArgumentException('The value must be a negative integer.');
|
||||
}
|
||||
|
||||
// Exact for every negative PHP integer: -1 - PHP_INT_MIN is PHP_INT_MAX, so the argument never overflows.
|
||||
[$additionalInformation, $data] = self::payloadForInt(-1 - $value);
|
||||
|
||||
return new self($additionalInformation, $data);
|
||||
}
|
||||
|
||||
public static function createFromString(string $value): self
|
||||
{
|
||||
$integer = BigInteger::of($value);
|
||||
|
||||
return self::createBigInteger($integer);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return numeric-string
|
||||
*/
|
||||
public function getValue(): string
|
||||
{
|
||||
if ($this->data === null) {
|
||||
return (string) (-1 - $this->additionalInformation);
|
||||
}
|
||||
|
||||
$argument = self::argumentFromPayload($this->data);
|
||||
if ($argument >= 0) {
|
||||
return (string) (-1 - $argument);
|
||||
}
|
||||
|
||||
return BigInteger::of(-1)->minus(Utils::binToBigInteger($this->data))
|
||||
->toBase(10)
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return numeric-string
|
||||
*/
|
||||
public function normalize(): string
|
||||
{
|
||||
return $this->getValue();
|
||||
}
|
||||
|
||||
private static function createBigInteger(BigInteger $integer): self
|
||||
{
|
||||
if ($integer->isGreaterThanOrEqualTo(BigInteger::zero())) {
|
||||
throw new InvalidArgumentException('The value must be a negative integer.');
|
||||
}
|
||||
|
||||
$argument = BigInteger::of(-1)->minus($integer);
|
||||
if ($argument->isGreaterThan(self::maximumArgument())) {
|
||||
throw new InvalidArgumentException(
|
||||
'Out of range. Please use NegativeBigIntegerTag tag with ByteStringObject object instead.'
|
||||
);
|
||||
}
|
||||
|
||||
[$additionalInformation, $data] = self::payloadForBigInteger($argument);
|
||||
|
||||
return new self($additionalInformation, $data);
|
||||
}
|
||||
}
|
||||
13
vendor/spomky-labs/cbor-php/src/Normalizable.php
vendored
Normal file
13
vendor/spomky-labs/cbor-php/src/Normalizable.php
vendored
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR;
|
||||
|
||||
interface Normalizable
|
||||
{
|
||||
/**
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function normalize();
|
||||
}
|
||||
34
vendor/spomky-labs/cbor-php/src/OtherObject.php
vendored
Normal file
34
vendor/spomky-labs/cbor-php/src/OtherObject.php
vendored
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR;
|
||||
|
||||
use CBOR\OtherObject\OtherObjectInterface;
|
||||
|
||||
abstract class OtherObject extends AbstractCBORObject implements OtherObjectInterface
|
||||
{
|
||||
private const MAJOR_TYPE = self::MAJOR_TYPE_OTHER_TYPE;
|
||||
|
||||
public function __construct(
|
||||
int $additionalInformation,
|
||||
protected ?string $data
|
||||
) {
|
||||
parent::__construct(self::MAJOR_TYPE, $additionalInformation);
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
$result = parent::__toString();
|
||||
if ($this->data !== null) {
|
||||
$result .= $this->data;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getContent(): ?string
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
}
|
||||
30
vendor/spomky-labs/cbor-php/src/OtherObject/BreakObject.php
vendored
Normal file
30
vendor/spomky-labs/cbor-php/src/OtherObject/BreakObject.php
vendored
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\OtherObject;
|
||||
|
||||
use CBOR\OtherObject as Base;
|
||||
|
||||
final class BreakObject extends Base
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(self::OBJECT_BREAK, null);
|
||||
}
|
||||
|
||||
public static function create(): self
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
public static function supportedAdditionalInformation(): array
|
||||
{
|
||||
return [self::OBJECT_BREAK];
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data): Base
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
}
|
||||
79
vendor/spomky-labs/cbor-php/src/OtherObject/DoublePrecisionFloatObject.php
vendored
Normal file
79
vendor/spomky-labs/cbor-php/src/OtherObject/DoublePrecisionFloatObject.php
vendored
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\OtherObject;
|
||||
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\OtherObject as Base;
|
||||
use InvalidArgumentException;
|
||||
use function strlen;
|
||||
|
||||
final class DoublePrecisionFloatObject extends Base implements Normalizable
|
||||
{
|
||||
use FloatBitsTrait;
|
||||
|
||||
public static function supportedAdditionalInformation(): array
|
||||
{
|
||||
return [self::OBJECT_DOUBLE_PRECISION_FLOAT];
|
||||
}
|
||||
|
||||
public static function createFromFloat(float $number): self
|
||||
{
|
||||
$value = match (true) {
|
||||
is_nan($number) => self::hex2binSafe('7FF8000000000000'),
|
||||
is_infinite($number) && $number > 0 => self::hex2binSafe('7FF0000000000000'),
|
||||
is_infinite($number) && $number < 0 => self::hex2binSafe('FFF0000000000000'),
|
||||
default => (static fn (): string => unpack('S', "\x01\x00")[1] === 1 ? strrev(pack('d', $number)) : pack(
|
||||
'd',
|
||||
$number
|
||||
))(),
|
||||
};
|
||||
|
||||
return new self(self::OBJECT_DOUBLE_PRECISION_FLOAT, $value);
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data): Base
|
||||
{
|
||||
return new self($additionalInformation, $data);
|
||||
}
|
||||
|
||||
public static function create(string $value): self
|
||||
{
|
||||
if (strlen($value) !== 8) {
|
||||
throw new InvalidArgumentException('The value is not a valid double precision floating point');
|
||||
}
|
||||
|
||||
return new self(self::OBJECT_DOUBLE_PRECISION_FLOAT, $value);
|
||||
}
|
||||
|
||||
public function normalize(): float
|
||||
{
|
||||
return $this->value('E');
|
||||
}
|
||||
|
||||
public function getExponent(): int
|
||||
{
|
||||
return $this->bits('J') >> 52 & 0b11111111111;
|
||||
}
|
||||
|
||||
public function getMantissa(): int
|
||||
{
|
||||
return $this->bits('J') & 0xFFFFFFFFFFFFF;
|
||||
}
|
||||
|
||||
public function getSign(): int
|
||||
{
|
||||
// "J" is unpacked signed, so the sign bit of the binary64 payload is the sign of the PHP integer.
|
||||
return $this->bits('J') < 0 ? -1 : 1;
|
||||
}
|
||||
|
||||
private static function hex2binSafe(string $hex): string
|
||||
{
|
||||
$result = hex2bin($hex);
|
||||
if ($result === false) {
|
||||
throw new InvalidArgumentException('Invalid hex string');
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
36
vendor/spomky-labs/cbor-php/src/OtherObject/FalseObject.php
vendored
Normal file
36
vendor/spomky-labs/cbor-php/src/OtherObject/FalseObject.php
vendored
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\OtherObject;
|
||||
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\OtherObject as Base;
|
||||
|
||||
final class FalseObject extends Base implements Normalizable
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(self::OBJECT_FALSE, null);
|
||||
}
|
||||
|
||||
public static function create(): self
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
public static function supportedAdditionalInformation(): array
|
||||
{
|
||||
return [self::OBJECT_FALSE];
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data): Base
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
public function normalize(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
57
vendor/spomky-labs/cbor-php/src/OtherObject/FloatBitsTrait.php
vendored
Normal file
57
vendor/spomky-labs/cbor-php/src/OtherObject/FloatBitsTrait.php
vendored
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\OtherObject;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use function is_float;
|
||||
use function is_int;
|
||||
|
||||
/**
|
||||
* Reads the IEEE 754 payload of a float object as native machine values.
|
||||
*
|
||||
* CBOR stores floats big-endian, which is exactly what unpack() consumes: "E" and "G" hand back the binary64 and
|
||||
* binary32 values themselves, and "J", "N" and "n" the raw bit pattern the sign, exponent and mantissa accessors sit
|
||||
* on. Both used to go through brick/math, which cost six BigInteger allocations for every float normalized.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
trait FloatBitsTrait
|
||||
{
|
||||
/**
|
||||
* The payload read as the floating point value it encodes.
|
||||
*/
|
||||
private function value(string $format): float
|
||||
{
|
||||
return (float) $this->unpackPayload($format);
|
||||
}
|
||||
|
||||
/**
|
||||
* The payload read as its raw bit pattern, big-endian.
|
||||
*/
|
||||
private function bits(string $format): int
|
||||
{
|
||||
return (int) $this->unpackPayload($format);
|
||||
}
|
||||
|
||||
private function unpackPayload(string $format): int|float
|
||||
{
|
||||
$data = $this->data;
|
||||
if ($data === null) {
|
||||
throw new InvalidArgumentException('Invalid data');
|
||||
}
|
||||
|
||||
$unpacked = unpack($format, $data);
|
||||
if ($unpacked === false) {
|
||||
throw new InvalidArgumentException('Invalid data');
|
||||
}
|
||||
|
||||
$value = $unpacked[1];
|
||||
if (! is_int($value) && ! is_float($value)) {
|
||||
throw new InvalidArgumentException('Invalid data');
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
26
vendor/spomky-labs/cbor-php/src/OtherObject/GenericObject.php
vendored
Normal file
26
vendor/spomky-labs/cbor-php/src/OtherObject/GenericObject.php
vendored
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\OtherObject;
|
||||
|
||||
use CBOR\OtherObject as Base;
|
||||
use InvalidArgumentException;
|
||||
use function ord;
|
||||
|
||||
final class GenericObject extends Base
|
||||
{
|
||||
public static function supportedAdditionalInformation(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data): Base
|
||||
{
|
||||
if ($data !== null && ord($data) < 32) {
|
||||
throw new InvalidArgumentException('Invalid simple value. Content data should not be present.');
|
||||
}
|
||||
|
||||
return new self($additionalInformation, $data);
|
||||
}
|
||||
}
|
||||
165
vendor/spomky-labs/cbor-php/src/OtherObject/HalfPrecisionFloatObject.php
vendored
Normal file
165
vendor/spomky-labs/cbor-php/src/OtherObject/HalfPrecisionFloatObject.php
vendored
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\OtherObject;
|
||||
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\OtherObject as Base;
|
||||
use const INF;
|
||||
use InvalidArgumentException;
|
||||
use const NAN;
|
||||
use function ord;
|
||||
use function strlen;
|
||||
|
||||
final class HalfPrecisionFloatObject extends Base implements Normalizable
|
||||
{
|
||||
use FloatBitsTrait;
|
||||
|
||||
public static function supportedAdditionalInformation(): array
|
||||
{
|
||||
return [self::OBJECT_HALF_PRECISION_FLOAT];
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode a PHP float as an IEEE 754 binary16 value: 1 sign bit, 5 exponent bits (bias 15) and 10 mantissa bits.
|
||||
*
|
||||
* The conversion reads the binary64 representation of the argument directly and rounds to nearest, ties to even,
|
||||
* as IEEE 754 mandates. Going through binary32 first would round twice and would therefore be off by one unit in
|
||||
* the last place for the values that sit exactly halfway between two binary16 neighbours.
|
||||
*/
|
||||
public static function createFromFloat(float $number): self
|
||||
{
|
||||
if (is_nan($number)) {
|
||||
// RFC 8949: canonical NaN is 0xf97e00 (quiet NaN with zero payload)
|
||||
return self::createFromBits(0x7E00);
|
||||
}
|
||||
|
||||
// abs() and comparisons cannot tell -0.0 from 0.0, so every bit is read from the packed representation.
|
||||
$packed = pack('E', $number);
|
||||
$signBit = (ord($packed[0]) & 0x80) === 0 ? 0 : 1 << 15;
|
||||
|
||||
if (is_infinite($number)) {
|
||||
return self::createFromBits($signBit | 0x7C00);
|
||||
}
|
||||
|
||||
// Magnitude of the binary64 value: 11 exponent bits (bias 1023) followed by 52 mantissa bits.
|
||||
$bits = ord($packed[0]) & 0x7F;
|
||||
for ($index = 1; $index < 8; $index++) {
|
||||
$bits = ($bits << 8) | ord($packed[$index]);
|
||||
}
|
||||
$doubleExponent = $bits >> 52;
|
||||
$doubleMantissa = $bits & 0x000FFFFFFFFFFFFF;
|
||||
|
||||
// Zero, or a subnormal binary64 below 2^-1022, which is far under half of the smallest binary16 subnormal.
|
||||
if ($doubleExponent === 0) {
|
||||
return self::createFromBits($signBit);
|
||||
}
|
||||
|
||||
// Significand with its implicit leading bit: abs($number) === $significand * 2 ** ($doubleExponent - 1075).
|
||||
$significand = $doubleMantissa | (1 << 52);
|
||||
|
||||
// Exponent field the value would carry as a normal binary16 number.
|
||||
$halfExponent = $doubleExponent - 1023 + 15;
|
||||
|
||||
// Too large for binary16, before rounding is even considered.
|
||||
if ($halfExponent >= 0x1F) {
|
||||
return self::createFromBits($signBit | 0x7C00);
|
||||
}
|
||||
|
||||
if ($halfExponent > 0) {
|
||||
// Normal: keep 11 significant bits (the implicit one plus 10) out of the 53 the significand holds.
|
||||
$halfSignificand = self::roundToNearestEven($significand, 42);
|
||||
if ($halfSignificand === 0x800) {
|
||||
// Rounding carried out of the mantissa and into the exponent.
|
||||
$halfSignificand >>= 1;
|
||||
$halfExponent++;
|
||||
if ($halfExponent >= 0x1F) {
|
||||
return self::createFromBits($signBit | 0x7C00);
|
||||
}
|
||||
}
|
||||
|
||||
return self::createFromBits($signBit | ($halfExponent << 10) | ($halfSignificand & 0x3FF));
|
||||
}
|
||||
|
||||
// Subnormal: express the value as a multiple of 2^-24, the smallest binary16 step.
|
||||
$shift = 43 - $halfExponent;
|
||||
if ($shift > 53) {
|
||||
// Strictly below half of the smallest subnormal, so it rounds to zero.
|
||||
return self::createFromBits($signBit);
|
||||
}
|
||||
|
||||
// A carry out of the 10 mantissa bits spills into the exponent field, which yields the smallest normal.
|
||||
return self::createFromBits($signBit | self::roundToNearestEven($significand, $shift));
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data): Base
|
||||
{
|
||||
return new self($additionalInformation, $data);
|
||||
}
|
||||
|
||||
public static function create(string $value): self
|
||||
{
|
||||
if (strlen($value) !== 2) {
|
||||
throw new InvalidArgumentException('The value is not a valid half precision floating point');
|
||||
}
|
||||
|
||||
return new self(self::OBJECT_HALF_PRECISION_FLOAT, $value);
|
||||
}
|
||||
|
||||
public function normalize(): float
|
||||
{
|
||||
// PHP has no native binary16, so the value is still assembled by hand -- but from the raw bits rather than
|
||||
// from three brick/math round trips.
|
||||
$bits = $this->bits('n');
|
||||
$exponent = $bits >> 10 & 0b11111;
|
||||
$mantissa = $bits & 0b1111111111;
|
||||
$sign = ($bits >> 15 & 1) === 1 ? -1 : 1;
|
||||
|
||||
if ($exponent === 0) {
|
||||
$val = $mantissa * 2 ** (-24);
|
||||
} elseif ($exponent !== 0b11111) {
|
||||
$val = ($mantissa + (1 << 10)) * 2 ** ($exponent - 25);
|
||||
} else {
|
||||
$val = $mantissa === 0 ? INF : NAN;
|
||||
}
|
||||
|
||||
return (float) ($sign * $val);
|
||||
}
|
||||
|
||||
public function getExponent(): int
|
||||
{
|
||||
return $this->bits('n') >> 10 & 0b11111;
|
||||
}
|
||||
|
||||
public function getMantissa(): int
|
||||
{
|
||||
return $this->bits('n') & 0b1111111111;
|
||||
}
|
||||
|
||||
public function getSign(): int
|
||||
{
|
||||
return ($this->bits('n') >> 15 & 1) === 1 ? -1 : 1;
|
||||
}
|
||||
|
||||
private static function createFromBits(int $bits): self
|
||||
{
|
||||
return new self(self::OBJECT_HALF_PRECISION_FLOAT, pack('n', $bits));
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop the $shift lowest bits of $significand, rounding to nearest and breaking ties towards the even value.
|
||||
*/
|
||||
private static function roundToNearestEven(int $significand, int $shift): int
|
||||
{
|
||||
$kept = $significand >> $shift;
|
||||
$dropped = $significand & ((1 << $shift) - 1);
|
||||
$halfway = 1 << ($shift - 1);
|
||||
|
||||
if ($dropped > $halfway || ($dropped === $halfway && ($kept & 1) === 1)) {
|
||||
$kept++;
|
||||
}
|
||||
|
||||
return $kept;
|
||||
}
|
||||
}
|
||||
36
vendor/spomky-labs/cbor-php/src/OtherObject/NullObject.php
vendored
Normal file
36
vendor/spomky-labs/cbor-php/src/OtherObject/NullObject.php
vendored
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\OtherObject;
|
||||
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\OtherObject as Base;
|
||||
|
||||
final class NullObject extends Base implements Normalizable
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(self::OBJECT_NULL, null);
|
||||
}
|
||||
|
||||
public static function create(): self
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
public static function supportedAdditionalInformation(): array
|
||||
{
|
||||
return [self::OBJECT_NULL];
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data): Base
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
public function normalize(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
17
vendor/spomky-labs/cbor-php/src/OtherObject/OtherObjectInterface.php
vendored
Normal file
17
vendor/spomky-labs/cbor-php/src/OtherObject/OtherObjectInterface.php
vendored
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\OtherObject;
|
||||
|
||||
use CBOR\CBORObject;
|
||||
|
||||
interface OtherObjectInterface extends CBORObject
|
||||
{
|
||||
/**
|
||||
* @return int[]
|
||||
*/
|
||||
public static function supportedAdditionalInformation(): array;
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data): self;
|
||||
}
|
||||
64
vendor/spomky-labs/cbor-php/src/OtherObject/OtherObjectManager.php
vendored
Normal file
64
vendor/spomky-labs/cbor-php/src/OtherObject/OtherObjectManager.php
vendored
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\OtherObject;
|
||||
|
||||
use function array_key_exists;
|
||||
use InvalidArgumentException;
|
||||
|
||||
final class OtherObjectManager implements OtherObjectManagerInterface
|
||||
{
|
||||
/**
|
||||
* @var array<int, class-string<OtherObjectInterface>>
|
||||
*/
|
||||
private array $classes = [];
|
||||
|
||||
/**
|
||||
* @param class-string<OtherObjectInterface>[] $classes
|
||||
*/
|
||||
public function __construct(array $classes = [])
|
||||
{
|
||||
foreach ($classes as $class) {
|
||||
$this->add($class);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param class-string<OtherObjectInterface>[] $classes
|
||||
*/
|
||||
public static function create(array $classes = []): self
|
||||
{
|
||||
return new self($classes);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param class-string<OtherObjectInterface> $class
|
||||
*/
|
||||
public function add(string $class): self
|
||||
{
|
||||
foreach ($class::supportedAdditionalInformation() as $ai) {
|
||||
if ($ai < 0) {
|
||||
throw new InvalidArgumentException('Invalid additional information.');
|
||||
}
|
||||
$this->classes[$ai] = $class;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return class-string<OtherObjectInterface>
|
||||
*/
|
||||
public function getClassForValue(int $value): string
|
||||
{
|
||||
return array_key_exists($value, $this->classes) ? $this->classes[$value] : GenericObject::class;
|
||||
}
|
||||
|
||||
public function createObjectForValue(int $value, ?string $data): OtherObjectInterface
|
||||
{
|
||||
$class = $this->getClassForValue($value);
|
||||
|
||||
return $class::createFromLoadedData($value, $data);
|
||||
}
|
||||
}
|
||||
10
vendor/spomky-labs/cbor-php/src/OtherObject/OtherObjectManagerInterface.php
vendored
Normal file
10
vendor/spomky-labs/cbor-php/src/OtherObject/OtherObjectManagerInterface.php
vendored
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\OtherObject;
|
||||
|
||||
interface OtherObjectManagerInterface
|
||||
{
|
||||
public function createObjectForValue(int $value, ?string $data): OtherObjectInterface;
|
||||
}
|
||||
73
vendor/spomky-labs/cbor-php/src/OtherObject/SimpleObject.php
vendored
Normal file
73
vendor/spomky-labs/cbor-php/src/OtherObject/SimpleObject.php
vendored
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\OtherObject;
|
||||
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\OtherObject as Base;
|
||||
use CBOR\Utils;
|
||||
use function chr;
|
||||
use InvalidArgumentException;
|
||||
use function ord;
|
||||
use function strlen;
|
||||
|
||||
final class SimpleObject extends Base implements Normalizable
|
||||
{
|
||||
public static function supportedAdditionalInformation(): array
|
||||
{
|
||||
return [...range(0, 19), 24];
|
||||
}
|
||||
|
||||
public static function create(int $value): self|FalseObject|TrueObject|NullObject|UndefinedObject
|
||||
{
|
||||
switch (true) {
|
||||
case $value >= 0 && $value <= 19:
|
||||
return new self($value, null);
|
||||
case $value === 20:
|
||||
return FalseObject::create();
|
||||
case $value === 21:
|
||||
return TrueObject::create();
|
||||
case $value === 22:
|
||||
return NullObject::create();
|
||||
case $value === 23:
|
||||
return UndefinedObject::create();
|
||||
case $value <= 31:
|
||||
throw new InvalidArgumentException('Invalid simple value. Shall be between 32 and 255.');
|
||||
case $value <= 255:
|
||||
return new self(24, chr($value));
|
||||
default:
|
||||
throw new InvalidArgumentException('The value is not a valid simple value.');
|
||||
}
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data): Base
|
||||
{
|
||||
if ($additionalInformation === 24) {
|
||||
if ($data === null) {
|
||||
throw new InvalidArgumentException('Invalid simple value. Content data is missing.');
|
||||
}
|
||||
if (strlen($data) !== 1) {
|
||||
throw new InvalidArgumentException('Invalid simple value. Content data is too long.');
|
||||
}
|
||||
if (ord($data) < 32) {
|
||||
throw new InvalidArgumentException('Invalid simple value. Content data must be between 32 and 255.');
|
||||
}
|
||||
} elseif ($additionalInformation < 20) {
|
||||
if ($data !== null) {
|
||||
throw new InvalidArgumentException('Invalid simple value. Content data should not be present.');
|
||||
}
|
||||
}
|
||||
|
||||
return new self($additionalInformation, $data);
|
||||
}
|
||||
|
||||
public function normalize(): int
|
||||
{
|
||||
if ($this->data === null) {
|
||||
return $this->getAdditionalInformation();
|
||||
}
|
||||
|
||||
return Utils::binToInt($this->data);
|
||||
}
|
||||
}
|
||||
78
vendor/spomky-labs/cbor-php/src/OtherObject/SinglePrecisionFloatObject.php
vendored
Normal file
78
vendor/spomky-labs/cbor-php/src/OtherObject/SinglePrecisionFloatObject.php
vendored
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\OtherObject;
|
||||
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\OtherObject as Base;
|
||||
use InvalidArgumentException;
|
||||
use function strlen;
|
||||
|
||||
final class SinglePrecisionFloatObject extends Base implements Normalizable
|
||||
{
|
||||
use FloatBitsTrait;
|
||||
|
||||
public static function supportedAdditionalInformation(): array
|
||||
{
|
||||
return [self::OBJECT_SINGLE_PRECISION_FLOAT];
|
||||
}
|
||||
|
||||
public static function createFromFloat(float $number): self
|
||||
{
|
||||
$value = match (true) {
|
||||
is_nan($number) => self::hex2binSafe('7FC00000'),
|
||||
is_infinite($number) && $number > 0 => self::hex2binSafe('7F800000'),
|
||||
is_infinite($number) && $number < 0 => self::hex2binSafe('FF800000'),
|
||||
default => (static fn (): string => unpack('S', "\x01\x00")[1] === 1 ? strrev(pack('f', $number)) : pack(
|
||||
'f',
|
||||
$number
|
||||
))(),
|
||||
};
|
||||
|
||||
return new self(self::OBJECT_SINGLE_PRECISION_FLOAT, $value);
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data): Base
|
||||
{
|
||||
return new self($additionalInformation, $data);
|
||||
}
|
||||
|
||||
public static function create(string $value): self
|
||||
{
|
||||
if (strlen($value) !== 4) {
|
||||
throw new InvalidArgumentException('The value is not a valid single precision floating point');
|
||||
}
|
||||
|
||||
return new self(self::OBJECT_SINGLE_PRECISION_FLOAT, $value);
|
||||
}
|
||||
|
||||
public function normalize(): float
|
||||
{
|
||||
return $this->value('G');
|
||||
}
|
||||
|
||||
public function getExponent(): int
|
||||
{
|
||||
return $this->bits('N') >> 23 & 0b11111111;
|
||||
}
|
||||
|
||||
public function getMantissa(): int
|
||||
{
|
||||
return $this->bits('N') & 0x7FFFFF;
|
||||
}
|
||||
|
||||
public function getSign(): int
|
||||
{
|
||||
return ($this->bits('N') >> 31 & 1) === 1 ? -1 : 1;
|
||||
}
|
||||
|
||||
private static function hex2binSafe(string $hex): string
|
||||
{
|
||||
$result = hex2bin($hex);
|
||||
if ($result === false) {
|
||||
throw new InvalidArgumentException('Invalid hex string');
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
36
vendor/spomky-labs/cbor-php/src/OtherObject/TrueObject.php
vendored
Normal file
36
vendor/spomky-labs/cbor-php/src/OtherObject/TrueObject.php
vendored
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\OtherObject;
|
||||
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\OtherObject as Base;
|
||||
|
||||
final class TrueObject extends Base implements Normalizable
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(self::OBJECT_TRUE, null);
|
||||
}
|
||||
|
||||
public static function create(): self
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
public static function supportedAdditionalInformation(): array
|
||||
{
|
||||
return [self::OBJECT_TRUE];
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data): Base
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
public function normalize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
40
vendor/spomky-labs/cbor-php/src/OtherObject/UndefinedObject.php
vendored
Normal file
40
vendor/spomky-labs/cbor-php/src/OtherObject/UndefinedObject.php
vendored
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\OtherObject;
|
||||
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\OtherObject as Base;
|
||||
|
||||
final class UndefinedObject extends Base implements Normalizable
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(self::OBJECT_UNDEFINED, null);
|
||||
}
|
||||
|
||||
public static function create(): self
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
public static function supportedAdditionalInformation(): array
|
||||
{
|
||||
return [self::OBJECT_UNDEFINED];
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data): Base
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
/**
|
||||
* PHP has no counterpart for the CBOR "undefined" simple value, so it normalizes to null. Use the object itself
|
||||
* when the distinction with an explicit CBOR "null" matters.
|
||||
*/
|
||||
public function normalize(): mixed
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
10
vendor/spomky-labs/cbor-php/src/Stream.php
vendored
Normal file
10
vendor/spomky-labs/cbor-php/src/Stream.php
vendored
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR;
|
||||
|
||||
interface Stream
|
||||
{
|
||||
public function read(int $length): string;
|
||||
}
|
||||
48
vendor/spomky-labs/cbor-php/src/StringStream.php
vendored
Normal file
48
vendor/spomky-labs/cbor-php/src/StringStream.php
vendored
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use function sprintf;
|
||||
use function strlen;
|
||||
|
||||
final class StringStream implements Stream
|
||||
{
|
||||
private int $offset = 0;
|
||||
|
||||
private readonly int $length;
|
||||
|
||||
public function __construct(
|
||||
private readonly string $data
|
||||
) {
|
||||
$this->length = strlen($data);
|
||||
}
|
||||
|
||||
public static function create(string $data): self
|
||||
{
|
||||
return new self($data);
|
||||
}
|
||||
|
||||
public function read(int $length): string
|
||||
{
|
||||
if ($length === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$available = $this->length - $this->offset;
|
||||
if ($available < $length) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Out of range. Expected: %d, read: %d.',
|
||||
$length,
|
||||
$available < 0 ? 0 : $available
|
||||
));
|
||||
}
|
||||
|
||||
$data = substr($this->data, $this->offset, $length);
|
||||
$this->offset += $length;
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
68
vendor/spomky-labs/cbor-php/src/Tag.php
vendored
Normal file
68
vendor/spomky-labs/cbor-php/src/Tag.php
vendored
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR;
|
||||
|
||||
use CBOR\Tag\TagInterface;
|
||||
use function chr;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* @phpstan-consistent-constructor
|
||||
*/
|
||||
abstract class Tag extends AbstractCBORObject implements TagInterface
|
||||
{
|
||||
private const MAJOR_TYPE = self::MAJOR_TYPE_TAG;
|
||||
|
||||
public function __construct(
|
||||
int $additionalInformation,
|
||||
protected ?string $data,
|
||||
protected CBORObject $object
|
||||
) {
|
||||
parent::__construct(self::MAJOR_TYPE, $additionalInformation);
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
$result = parent::__toString();
|
||||
if ($this->data !== null) {
|
||||
$result .= $this->data;
|
||||
}
|
||||
|
||||
return $result . $this->object;
|
||||
}
|
||||
|
||||
public function getData(): ?string
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
public function getValue(): CBORObject
|
||||
{
|
||||
return $this->object;
|
||||
}
|
||||
|
||||
/**
|
||||
* A tag number is the argument of a major type 6 head, so RFC 8949 section 3 gives it the same five encodings as
|
||||
* any other argument and section 4.2 requires the shortest one that holds it. The bounds are therefore inclusive
|
||||
* and each payload is packed to the exact width its additional information announces: 255 is the largest
|
||||
* one-byte tag number, not the first two-byte one, and tag 256 is "d9 0100", never a single byte.
|
||||
*
|
||||
* @return array{int, null|string}
|
||||
*/
|
||||
protected static function determineComponents(int $tag): array
|
||||
{
|
||||
if ($tag < 0) {
|
||||
throw new InvalidArgumentException('The value must be a positive integer.');
|
||||
}
|
||||
|
||||
return match (true) {
|
||||
$tag <= 23 => [$tag, null],
|
||||
$tag <= 0xFF => [self::LENGTH_1_BYTE, chr($tag)],
|
||||
$tag <= 0xFFFF => [self::LENGTH_2_BYTES, pack('n', $tag)],
|
||||
$tag <= 0xFFFFFFFF => [self::LENGTH_4_BYTES, pack('N', $tag)],
|
||||
default => [self::LENGTH_8_BYTES, pack('J', $tag)],
|
||||
};
|
||||
}
|
||||
}
|
||||
176
vendor/spomky-labs/cbor-php/src/Tag/AbstractCoseTag.php
vendored
Normal file
176
vendor/spomky-labs/cbor-php/src/Tag/AbstractCoseTag.php
vendored
Normal file
|
|
@ -0,0 +1,176 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\ByteStringObject;
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\Decoder;
|
||||
use CBOR\DecoderInterface;
|
||||
use CBOR\IndefiniteLengthByteStringObject;
|
||||
use CBOR\IndefiniteLengthListObject;
|
||||
use CBOR\IndefiniteLengthMapObject;
|
||||
use CBOR\ListObject;
|
||||
use CBOR\MapObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\OtherObject\NullObject;
|
||||
use CBOR\StringStream;
|
||||
use CBOR\Tag;
|
||||
use function count;
|
||||
use InvalidArgumentException;
|
||||
use function sprintf;
|
||||
|
||||
/**
|
||||
* Common behaviour of the six COSE structures of RFC 9052: tags 16, 17, 18, 96, 97 and 98.
|
||||
*
|
||||
* Every one of them is an array that opens with the same two items -- the protected header, wrapped in a byte
|
||||
* string so that it is signed exactly as it was written, and the unprotected header as a plain map -- and then
|
||||
* differs in what follows. The tag only describes that shape: verifying a signature or a MAC needs keys and
|
||||
* algorithms this library knows nothing about, and belongs to a COSE implementation such as web-auth/cose-lib.
|
||||
*
|
||||
* @see \CBOR\Test\Tag\CoseTagTest
|
||||
* @see https://datatracker.ietf.org/doc/html/rfc9052
|
||||
*/
|
||||
abstract class AbstractCoseTag extends Tag implements Normalizable
|
||||
{
|
||||
/**
|
||||
* The protected header as it is carried: a byte string whose content is the encoded header map.
|
||||
*/
|
||||
public function getProtectedHeader(): ByteStringObject|IndefiniteLengthByteStringObject
|
||||
{
|
||||
/** @var ByteStringObject|IndefiniteLengthByteStringObject $item */
|
||||
$item = $this->itemAt(0);
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* The protected header, decoded.
|
||||
*
|
||||
* An empty byte string is the COSE way of saying "no protected header", so it decodes to an empty map rather
|
||||
* than to a parse error.
|
||||
*/
|
||||
public function getProtectedHeaderAsMap(
|
||||
?DecoderInterface $decoder = null
|
||||
): MapObject|IndefiniteLengthMapObject {
|
||||
$value = $this->getProtectedHeader()
|
||||
->getValue();
|
||||
if ($value === '') {
|
||||
return MapObject::create();
|
||||
}
|
||||
|
||||
$decoder ??= Decoder::create();
|
||||
$decoded = $decoder->decode(StringStream::create($value));
|
||||
if (! $decoded instanceof MapObject && ! $decoded instanceof IndefiniteLengthMapObject) {
|
||||
throw new InvalidArgumentException('Protected header is not a valid Map object.');
|
||||
}
|
||||
|
||||
return $decoded;
|
||||
}
|
||||
|
||||
public function getUnprotectedHeader(): MapObject|IndefiniteLengthMapObject
|
||||
{
|
||||
/** @var IndefiniteLengthMapObject|MapObject $item */
|
||||
$item = $this->itemAt(1);
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, mixed> the structure as the array it is
|
||||
*/
|
||||
public function normalize(): array
|
||||
{
|
||||
/** @var IndefiniteLengthListObject|ListObject $object */
|
||||
$object = $this->object;
|
||||
|
||||
return $object->normalize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the array and the two items every COSE structure opens with.
|
||||
*
|
||||
* @return IndefiniteLengthListObject|ListObject the same object, once it is known to be a list
|
||||
*/
|
||||
protected static function assertStructure(
|
||||
CBORObject $object,
|
||||
int $count,
|
||||
string $name
|
||||
): ListObject|IndefiniteLengthListObject {
|
||||
if (! $object instanceof ListObject && ! $object instanceof IndefiniteLengthListObject) {
|
||||
throw new InvalidArgumentException(sprintf('Not a valid %s object. Expected a List object.', $name));
|
||||
}
|
||||
if (count($object) !== $count) {
|
||||
throw new InvalidArgumentException(
|
||||
sprintf('Not a valid %s object. The list shall have %d items.', $name, $count)
|
||||
);
|
||||
}
|
||||
|
||||
$protectedHeader = $object->get(0);
|
||||
if (! $protectedHeader instanceof ByteStringObject && ! $protectedHeader instanceof IndefiniteLengthByteStringObject) {
|
||||
throw new InvalidArgumentException(
|
||||
sprintf('Not a valid %s object. The item 1 shall be a Byte String object.', $name)
|
||||
);
|
||||
}
|
||||
$unprotectedHeader = $object->get(1);
|
||||
if (! $unprotectedHeader instanceof MapObject && ! $unprotectedHeader instanceof IndefiniteLengthMapObject) {
|
||||
throw new InvalidArgumentException(
|
||||
sprintf('Not a valid %s object. The item 2 shall be a Map object.', $name)
|
||||
);
|
||||
}
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
protected static function assertByteStringAt(
|
||||
ListObject|IndefiniteLengthListObject $object,
|
||||
int $index,
|
||||
string $name
|
||||
): void {
|
||||
$item = $object->get($index);
|
||||
if (! $item instanceof ByteStringObject && ! $item instanceof IndefiniteLengthByteStringObject) {
|
||||
throw new InvalidArgumentException(
|
||||
sprintf('Not a valid %s object. The item %d shall be a Byte String object.', $name, $index + 1)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A payload or a ciphertext may be detached, which COSE spells as null: the content is carried out of band
|
||||
* and only the signature or the tag travels with the structure.
|
||||
*/
|
||||
protected static function assertPayloadAt(
|
||||
ListObject|IndefiniteLengthListObject $object,
|
||||
int $index,
|
||||
string $name
|
||||
): void {
|
||||
$item = $object->get($index);
|
||||
if (! $item instanceof ByteStringObject && ! $item instanceof IndefiniteLengthByteStringObject && ! $item instanceof NullObject) {
|
||||
throw new InvalidArgumentException(
|
||||
sprintf('Not a valid %s object. The item %d shall be a Byte String object or null.', $name, $index + 1)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
protected static function assertListAt(
|
||||
ListObject|IndefiniteLengthListObject $object,
|
||||
int $index,
|
||||
string $name
|
||||
): void {
|
||||
$item = $object->get($index);
|
||||
if (! $item instanceof ListObject && ! $item instanceof IndefiniteLengthListObject) {
|
||||
throw new InvalidArgumentException(
|
||||
sprintf('Not a valid %s object. The item %d shall be a List object.', $name, $index + 1)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
protected function itemAt(int $index): CBORObject
|
||||
{
|
||||
/** @var IndefiniteLengthListObject|ListObject $object */
|
||||
$object = $this->object;
|
||||
|
||||
return $object->get($index);
|
||||
}
|
||||
}
|
||||
142
vendor/spomky-labs/cbor-php/src/Tag/AbstractIpAddressTag.php
vendored
Normal file
142
vendor/spomky-labs/cbor-php/src/Tag/AbstractIpAddressTag.php
vendored
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\ByteStringObject;
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthByteStringObject;
|
||||
use CBOR\IndefiniteLengthListObject;
|
||||
use CBOR\IndefiniteLengthTextStringObject;
|
||||
use CBOR\ListObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
use CBOR\TextStringObject;
|
||||
use CBOR\UnsignedIntegerObject;
|
||||
use function count;
|
||||
use function inet_ntop;
|
||||
use InvalidArgumentException;
|
||||
use function sprintf;
|
||||
use function str_pad;
|
||||
use const STR_PAD_RIGHT;
|
||||
use function strlen;
|
||||
|
||||
/**
|
||||
* Common behaviour of the two IP address tags of RFC 9164: 52 (IPv4) and 54 (IPv6).
|
||||
*
|
||||
* Each tag carries one of three shapes: a byte string, which is a plain address; the array [prefix length,
|
||||
* bytes], which is a prefix whose trailing zero bytes may be left out; or the array [bytes, zone], which is an
|
||||
* address together with the zone identifier that scopes it. The first item tells the last two apart -- an
|
||||
* integer opens a prefix, a byte string an address -- which is why the shapes can share one tag number.
|
||||
*/
|
||||
abstract class AbstractIpAddressTag extends Tag implements Normalizable
|
||||
{
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
$length = static::getAddressLength();
|
||||
if ($object instanceof ByteStringObject || $object instanceof IndefiniteLengthByteStringObject) {
|
||||
if ($object->getLength() !== $length) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'This tag only accepts a %d byte Byte String object as an address.',
|
||||
$length
|
||||
));
|
||||
}
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $object instanceof ListObject && ! $object instanceof IndefiniteLengthListObject) {
|
||||
throw new InvalidArgumentException('This tag only accepts a Byte String object or a List object.');
|
||||
}
|
||||
if (count($object) !== 2) {
|
||||
throw new InvalidArgumentException('This tag only accepts a List object that contains 2 items.');
|
||||
}
|
||||
|
||||
$first = $object->get(0);
|
||||
$second = $object->get(1);
|
||||
if ($first instanceof UnsignedIntegerObject) {
|
||||
if (! $second instanceof ByteStringObject && ! $second instanceof IndefiniteLengthByteStringObject) {
|
||||
throw new InvalidArgumentException('Invalid prefix. Expected a Byte String object.');
|
||||
}
|
||||
if ($second->getLength() > $length) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Invalid prefix. Expected at most %d bytes.',
|
||||
$length
|
||||
));
|
||||
}
|
||||
if ((int) $first->normalize() > $length * 8) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Invalid prefix length. Expected at most %d.',
|
||||
$length * 8
|
||||
));
|
||||
}
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $first instanceof ByteStringObject && ! $first instanceof IndefiniteLengthByteStringObject) {
|
||||
throw new InvalidArgumentException('Invalid address. Expected a Byte String object.');
|
||||
}
|
||||
if ($first->getLength() !== $length) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Invalid address. Expected exactly %d bytes.',
|
||||
$length
|
||||
));
|
||||
}
|
||||
if (! $second instanceof UnsignedIntegerObject && ! $second instanceof TextStringObject && ! $second instanceof IndefiniteLengthTextStringObject) {
|
||||
throw new InvalidArgumentException('Invalid zone identifier. Expected a Text String or an Unsigned Integer object.');
|
||||
}
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string the address in its textual form, followed by "/length" for a prefix or by "%zone" for a
|
||||
* zoned address
|
||||
*/
|
||||
public function normalize(): string
|
||||
{
|
||||
$object = $this->object;
|
||||
if ($object instanceof ByteStringObject || $object instanceof IndefiniteLengthByteStringObject) {
|
||||
return self::toTextualAddress($object->normalize());
|
||||
}
|
||||
|
||||
/** @var IndefiniteLengthListObject|ListObject $object */
|
||||
$first = $object->get(0);
|
||||
/** @var ByteStringObject|IndefiniteLengthByteStringObject|TextStringObject|UnsignedIntegerObject $second */
|
||||
$second = $object->get(1);
|
||||
if ($first instanceof UnsignedIntegerObject) {
|
||||
// The bytes of a prefix are truncated after the last one that carries a bit of it, so they are
|
||||
// padded back to a full address before being printed.
|
||||
$bytes = str_pad($second->normalize(), static::getAddressLength(), "\0", STR_PAD_RIGHT);
|
||||
|
||||
return self::toTextualAddress($bytes) . '/' . $first->normalize();
|
||||
}
|
||||
|
||||
/** @var ByteStringObject|IndefiniteLengthByteStringObject $first */
|
||||
return self::toTextualAddress($first->normalize()) . '%' . $second->normalize();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int the size of an address of this family, in bytes
|
||||
*/
|
||||
abstract protected static function getAddressLength(): int;
|
||||
|
||||
private static function toTextualAddress(string $bytes): string
|
||||
{
|
||||
if (strlen($bytes) !== static::getAddressLength()) {
|
||||
throw new InvalidArgumentException('Invalid address. Cannot be converted into a textual address.');
|
||||
}
|
||||
$address = inet_ntop($bytes);
|
||||
if ($address === false) {
|
||||
throw new InvalidArgumentException('Invalid address. Cannot be converted into a textual address.');
|
||||
}
|
||||
|
||||
return $address;
|
||||
}
|
||||
}
|
||||
150
vendor/spomky-labs/cbor-php/src/Tag/AbstractMultiDimensionalArrayTag.php
vendored
Normal file
150
vendor/spomky-labs/cbor-php/src/Tag/AbstractMultiDimensionalArrayTag.php
vendored
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use function array_values;
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthListObject;
|
||||
use CBOR\ListObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
use CBOR\UnsignedIntegerObject;
|
||||
use function count;
|
||||
use InvalidArgumentException;
|
||||
use function is_array;
|
||||
|
||||
/**
|
||||
* Common behaviour of the two multi-dimensional array tags of RFC 8746: 40 (row-major) and 1040 (column-major).
|
||||
*
|
||||
* The content is the two element array [dimensions, values], where the values are a flat array -- a plain CBOR
|
||||
* array or one of the typed array tags -- and the dimensions say how to fold it. Only the order in which the
|
||||
* flat positions are walked separates the two tags, which is what getStrides() describes.
|
||||
*/
|
||||
abstract class AbstractMultiDimensionalArrayTag extends Tag implements Normalizable
|
||||
{
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! $object instanceof ListObject && ! $object instanceof IndefiniteLengthListObject) {
|
||||
throw new InvalidArgumentException('This tag only accepts a List object.');
|
||||
}
|
||||
if (count($object) !== 2) {
|
||||
throw new InvalidArgumentException('This tag only accepts a List object that contains 2 items.');
|
||||
}
|
||||
|
||||
$dimensions = $object->get(0);
|
||||
if (! $dimensions instanceof ListObject && ! $dimensions instanceof IndefiniteLengthListObject) {
|
||||
throw new InvalidArgumentException('Invalid dimensions. Expected a List object.');
|
||||
}
|
||||
if (count($dimensions) === 0) {
|
||||
throw new InvalidArgumentException('Invalid dimensions. Expected at least one dimension.');
|
||||
}
|
||||
foreach ($dimensions as $dimension) {
|
||||
if (! $dimension instanceof UnsignedIntegerObject) {
|
||||
throw new InvalidArgumentException('Invalid dimensions. Expected Unsigned Integer objects.');
|
||||
}
|
||||
}
|
||||
|
||||
$values = $object->get(1);
|
||||
// A typed array tag is the other half of RFC 8746 and the reason the values are not always a plain array.
|
||||
if (! $values instanceof ListObject && ! $values instanceof IndefiniteLengthListObject && ! ($values instanceof TagInterface && $values instanceof Normalizable)) {
|
||||
throw new InvalidArgumentException('Invalid values. Expected a List object or a typed array tag.');
|
||||
}
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, int> the size of each dimension, outermost first
|
||||
*/
|
||||
public function getDimensions(): array
|
||||
{
|
||||
/** @var IndefiniteLengthListObject|ListObject $object */
|
||||
$object = $this->object;
|
||||
/** @var IndefiniteLengthListObject|ListObject $dimensions */
|
||||
$dimensions = $object->get(0);
|
||||
|
||||
$result = [];
|
||||
foreach ($dimensions as $dimension) {
|
||||
/** @var UnsignedIntegerObject $dimension */
|
||||
$value = $dimension->normalize();
|
||||
$asInteger = (int) $value;
|
||||
// A CBOR unsigned integer goes up to 2^64-1; such a dimension could not be built anyway, and
|
||||
// silently wrapping it would produce a shape that has nothing to do with the document.
|
||||
if ((string) $asInteger !== $value) {
|
||||
throw new InvalidArgumentException('Invalid dimensions. The value is too large.');
|
||||
}
|
||||
$result[] = $asInteger;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, mixed> the values as they are laid out in the document, before folding
|
||||
*/
|
||||
public function getFlatValues(): array
|
||||
{
|
||||
/** @var IndefiniteLengthListObject|ListObject $object */
|
||||
$object = $this->object;
|
||||
/** @var CBORObject&Normalizable $values */
|
||||
$values = $object->get(1);
|
||||
$normalized = $values->normalize();
|
||||
if (! is_array($normalized)) {
|
||||
throw new InvalidArgumentException('Invalid values. Expected a List object or a typed array tag.');
|
||||
}
|
||||
|
||||
return array_values($normalized);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, mixed> the values folded into as many nested arrays as there are dimensions
|
||||
*/
|
||||
public function normalize(): array
|
||||
{
|
||||
$dimensions = $this->getDimensions();
|
||||
$values = $this->getFlatValues();
|
||||
|
||||
$expected = 1;
|
||||
foreach ($dimensions as $dimension) {
|
||||
$expected *= $dimension;
|
||||
if ($expected > count($values)) {
|
||||
throw new InvalidArgumentException('Invalid data. The dimensions do not match the number of values.');
|
||||
}
|
||||
}
|
||||
if ($expected !== count($values)) {
|
||||
throw new InvalidArgumentException('Invalid data. The dimensions do not match the number of values.');
|
||||
}
|
||||
|
||||
return self::fold($values, $dimensions, static::getStrides($dimensions), 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* The distance between two consecutive positions along each dimension.
|
||||
*
|
||||
* @param array<int, int> $dimensions
|
||||
*
|
||||
* @return array<int, int>
|
||||
*/
|
||||
abstract protected static function getStrides(array $dimensions): array;
|
||||
|
||||
/**
|
||||
* @param array<int, mixed> $values
|
||||
* @param array<int, int> $dimensions
|
||||
* @param array<int, int> $strides
|
||||
*
|
||||
* @return array<int, mixed>
|
||||
*/
|
||||
private static function fold(array $values, array $dimensions, array $strides, int $level, int $offset): array
|
||||
{
|
||||
$isLast = $level === count($dimensions) - 1;
|
||||
$result = [];
|
||||
for ($index = 0; $index < $dimensions[$level]; ++$index) {
|
||||
$position = $offset + $index * $strides[$level];
|
||||
$result[] = $isLast ? $values[$position] : self::fold($values, $dimensions, $strides, $level + 1, $position);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
28
vendor/spomky-labs/cbor-php/src/Tag/Base16EncodingTag.php
vendored
Normal file
28
vendor/spomky-labs/cbor-php/src/Tag/Base16EncodingTag.php
vendored
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\Tag;
|
||||
|
||||
final class Base16EncodingTag extends Tag
|
||||
{
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_ENCODED_BASE16;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): Tag
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_ENCODED_BASE16);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
}
|
||||
28
vendor/spomky-labs/cbor-php/src/Tag/Base64EncodingTag.php
vendored
Normal file
28
vendor/spomky-labs/cbor-php/src/Tag/Base64EncodingTag.php
vendored
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\Tag;
|
||||
|
||||
final class Base64EncodingTag extends Tag
|
||||
{
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_ENCODED_BASE64;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): Tag
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_ENCODED_BASE64);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
}
|
||||
40
vendor/spomky-labs/cbor-php/src/Tag/Base64Tag.php
vendored
Normal file
40
vendor/spomky-labs/cbor-php/src/Tag/Base64Tag.php
vendored
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthTextStringObject;
|
||||
use CBOR\Tag;
|
||||
use CBOR\TextStringObject;
|
||||
use InvalidArgumentException;
|
||||
|
||||
final class Base64Tag extends Tag
|
||||
{
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! $object instanceof TextStringObject && ! $object instanceof IndefiniteLengthTextStringObject) {
|
||||
throw new InvalidArgumentException('This tag only accepts a Text String object.');
|
||||
}
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_BASE64;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): Tag
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_BASE64);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
}
|
||||
28
vendor/spomky-labs/cbor-php/src/Tag/Base64UrlEncodingTag.php
vendored
Normal file
28
vendor/spomky-labs/cbor-php/src/Tag/Base64UrlEncodingTag.php
vendored
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\Tag;
|
||||
|
||||
final class Base64UrlEncodingTag extends Tag
|
||||
{
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_ENCODED_BASE64_URL;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): Tag
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_ENCODED_BASE64_URL);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
}
|
||||
40
vendor/spomky-labs/cbor-php/src/Tag/Base64UrlTag.php
vendored
Normal file
40
vendor/spomky-labs/cbor-php/src/Tag/Base64UrlTag.php
vendored
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthTextStringObject;
|
||||
use CBOR\Tag;
|
||||
use CBOR\TextStringObject;
|
||||
use InvalidArgumentException;
|
||||
|
||||
final class Base64UrlTag extends Tag
|
||||
{
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! $object instanceof TextStringObject && ! $object instanceof IndefiniteLengthTextStringObject) {
|
||||
throw new InvalidArgumentException('This tag only accepts a Text String object.');
|
||||
}
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_BASE64_URL;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): Tag
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_BASE64_URL);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
}
|
||||
220
vendor/spomky-labs/cbor-php/src/Tag/BigFloatTag.php
vendored
Normal file
220
vendor/spomky-labs/cbor-php/src/Tag/BigFloatTag.php
vendored
Normal file
|
|
@ -0,0 +1,220 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use Brick\Math\BigInteger;
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthListObject;
|
||||
use CBOR\ListObject;
|
||||
use CBOR\NegativeIntegerObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
use CBOR\UnsignedIntegerObject;
|
||||
use function count;
|
||||
use function extension_loaded;
|
||||
use InvalidArgumentException;
|
||||
use RuntimeException;
|
||||
use function sprintf;
|
||||
use function str_contains;
|
||||
|
||||
final class BigFloatTag extends Tag implements Normalizable
|
||||
{
|
||||
/**
|
||||
* The maximum absolute value accepted for the exponent.
|
||||
*
|
||||
* 2^e needs about e/3 decimal digits to write down and 2^-e exactly e of them, so the exponent alone decides how
|
||||
* much memory normalizing one item costs -- and the exponent is three bytes on the wire. At 8192 a six byte item
|
||||
* expanded to more than eight kilobytes, an amplification of over a thousand, and a document made of such items
|
||||
* exhausted the memory limit inside decode() with a fatal error no try/catch can intercept. The bound is now the
|
||||
* range of an IEEE 754 double, which is what a big float is meant to exceed in precision, not in magnitude.
|
||||
*/
|
||||
public const MAX_ABSOLUTE_EXPONENT = 1024;
|
||||
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! extension_loaded('bcmath')) {
|
||||
throw new RuntimeException('The extension "bcmath" is required to use this tag');
|
||||
}
|
||||
|
||||
$isList = $object instanceof ListObject || $object instanceof IndefiniteLengthListObject;
|
||||
if (! $isList || count($object) !== 2) {
|
||||
throw new InvalidArgumentException(
|
||||
'This tag only accepts a ListObject object that contains an exponent and a mantissa.'
|
||||
);
|
||||
}
|
||||
$e = $object->get(0);
|
||||
if (! $e instanceof UnsignedIntegerObject && ! $e instanceof NegativeIntegerObject) {
|
||||
throw new InvalidArgumentException('The exponent must be a Signed Integer or an Unsigned Integer object.');
|
||||
}
|
||||
$m = $object->get(1);
|
||||
if (! $m instanceof UnsignedIntegerObject && ! $m instanceof NegativeIntegerObject && ! $m instanceof NegativeBigIntegerTag && ! $m instanceof UnsignedBigIntegerTag) {
|
||||
throw new InvalidArgumentException(
|
||||
'The mantissa must be a Positive or Negative Signed Integer or an Unsigned Integer object.'
|
||||
);
|
||||
}
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_BIG_FLOAT;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): self
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_BIG_FLOAT);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
public static function createFromExponentAndMantissa(CBORObject $e, CBORObject $m): self
|
||||
{
|
||||
$object = ListObject::create()
|
||||
->add($e)
|
||||
->add($m)
|
||||
;
|
||||
|
||||
return self::create($object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a BigFloat from a PHP float value.
|
||||
* BigFloat represents: mantissa × 2^exponent
|
||||
* This method converts a float to the most compact BigFloat representation.
|
||||
*
|
||||
* @param float $value The float value to convert
|
||||
* @return self The BigFloat tag object
|
||||
*/
|
||||
public static function createFromFloat(float $value): self
|
||||
{
|
||||
if (! extension_loaded('bcmath')) {
|
||||
throw new RuntimeException('The extension "bcmath" is required to use this method');
|
||||
}
|
||||
|
||||
// Handle special cases
|
||||
if (is_nan($value) || is_infinite($value)) {
|
||||
throw new InvalidArgumentException('BigFloat cannot represent NaN or Infinity');
|
||||
}
|
||||
|
||||
if ($value === 0.0) {
|
||||
// 0 = 0 × 2^0
|
||||
return self::createFromExponentAndMantissa(
|
||||
UnsignedIntegerObject::create(0),
|
||||
UnsignedIntegerObject::create(0)
|
||||
);
|
||||
}
|
||||
|
||||
// Get the binary representation
|
||||
$negative = $value < 0;
|
||||
$absValue = abs($value);
|
||||
|
||||
// Convert to binary string representation
|
||||
// We'll use the fact that PHP floats are IEEE 754 double precision
|
||||
// Extract mantissa and exponent from the float
|
||||
$packed = pack('d', $absValue);
|
||||
if (unpack('S', "\x01\x00")[1] === 1) {
|
||||
$packed = strrev($packed); // Little-endian system
|
||||
}
|
||||
$bits = unpack('J', $packed)[1];
|
||||
|
||||
$biasedExponent = ($bits >> 52) & 0x7FF;
|
||||
$fraction = $bits & 0xFFFFFFFFFFFFF;
|
||||
|
||||
if ($biasedExponent === 0) {
|
||||
// Subnormal number
|
||||
$exponent = -1022 - 52;
|
||||
$mantissa = $fraction;
|
||||
} else {
|
||||
// Normal number
|
||||
$exponent = $biasedExponent - 1023 - 52;
|
||||
$mantissa = $fraction | (1 << 52); // Add implicit leading 1
|
||||
}
|
||||
|
||||
// Apply sign
|
||||
if ($negative) {
|
||||
$mantissa = -$mantissa;
|
||||
}
|
||||
|
||||
// Normalize: remove trailing zeros from mantissa by adjusting exponent
|
||||
while ($mantissa !== 0 && ($mantissa & 1) === 0) {
|
||||
$mantissa >>= 1;
|
||||
$exponent++;
|
||||
}
|
||||
|
||||
// Create exponent object
|
||||
if ($exponent >= 0) {
|
||||
$exponentObj = UnsignedIntegerObject::create($exponent);
|
||||
} else {
|
||||
$exponentObj = NegativeIntegerObject::create($exponent);
|
||||
}
|
||||
|
||||
// Create mantissa object
|
||||
if ($mantissa >= 0) {
|
||||
$mantissaObj = UnsignedIntegerObject::createFromString((string) $mantissa);
|
||||
} else {
|
||||
$mantissaObj = NegativeIntegerObject::createFromString((string) $mantissa);
|
||||
}
|
||||
|
||||
return self::createFromExponentAndMantissa($exponentObj, $mantissaObj);
|
||||
}
|
||||
|
||||
public function normalize()
|
||||
{
|
||||
/** @var ListObject|IndefiniteLengthListObject $object */
|
||||
$object = $this->object;
|
||||
/** @var UnsignedIntegerObject|NegativeIntegerObject $e */
|
||||
$e = $object->get(0);
|
||||
/** @var UnsignedIntegerObject|NegativeIntegerObject|NegativeBigIntegerTag|UnsignedBigIntegerTag $m */
|
||||
$m = $object->get(1);
|
||||
|
||||
$exponent = (string) $e->normalize();
|
||||
self::assertExponentIsWithinBounds($exponent);
|
||||
$mantissa = (string) $m->normalize();
|
||||
|
||||
// The exponent is bounded, so it fits a PHP integer and the scale below is finite.
|
||||
$exponentValue = (int) $exponent;
|
||||
if ($exponentValue >= 0) {
|
||||
// m x 2^e with e >= 0 is an integer: no scale is needed and none shall be printed.
|
||||
return bcmul($mantissa, bcpow('2', $exponent, 0), 0);
|
||||
}
|
||||
|
||||
// 2^-s is 5^s x 10^-s, so the exact quotient has s decimal digits and that scale loses nothing. A fixed
|
||||
// scale would either truncate the result to zero or, worse, report a rounded value as the decoded one.
|
||||
$scale = -$exponentValue;
|
||||
|
||||
return self::stripTrailingZeros(bcdiv($mantissa, bcpow('2', (string) $scale, 0), $scale));
|
||||
}
|
||||
|
||||
/**
|
||||
* bcdiv() pads the result up to the requested scale, so the exact value comes back with trailing zeros and,
|
||||
* once they are gone, a dangling decimal point. Both are stripped, but only from a value that has a decimal
|
||||
* point at all: trimming "512" would turn it into "51".
|
||||
*/
|
||||
private static function stripTrailingZeros(string $value): string
|
||||
{
|
||||
if (! str_contains($value, '.')) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return rtrim(rtrim($value, '0'), '.');
|
||||
}
|
||||
|
||||
private static function assertExponentIsWithinBounds(string $exponent): void
|
||||
{
|
||||
if (BigInteger::of($exponent)->abs()->isGreaterThan(BigInteger::of(self::MAX_ABSOLUTE_EXPONENT))) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'The exponent is out of range. Its absolute value shall not exceed %d, got "%s".',
|
||||
self::MAX_ABSOLUTE_EXPONENT,
|
||||
$exponent
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
57
vendor/spomky-labs/cbor-php/src/Tag/BinaryMimeTag.php
vendored
Normal file
57
vendor/spomky-labs/cbor-php/src/Tag/BinaryMimeTag.php
vendored
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\ByteStringObject;
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthByteStringObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Tag 257: a MIME message, headers included, carried as a byte string.
|
||||
*
|
||||
* It is the counterpart of tag 36 for a message that is not valid UTF-8 -- a transfer encoding of binary, say --
|
||||
* which a text string could not hold.
|
||||
*
|
||||
* @see \CBOR\Test\Tag\RegistryTagsTest
|
||||
*/
|
||||
final class BinaryMimeTag extends Tag implements Normalizable
|
||||
{
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! $object instanceof ByteStringObject && ! $object instanceof IndefiniteLengthByteStringObject) {
|
||||
throw new InvalidArgumentException('This tag only accepts a Byte String object.');
|
||||
}
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_BINARY_MIME;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_BINARY_MIME);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
public function normalize(): string
|
||||
{
|
||||
/** @var ByteStringObject|IndefiniteLengthByteStringObject $object */
|
||||
$object = $this->object;
|
||||
|
||||
return $object->normalize();
|
||||
}
|
||||
}
|
||||
40
vendor/spomky-labs/cbor-php/src/Tag/CBOREncodingTag.php
vendored
Normal file
40
vendor/spomky-labs/cbor-php/src/Tag/CBOREncodingTag.php
vendored
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\ByteStringObject;
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthByteStringObject;
|
||||
use CBOR\Tag;
|
||||
use InvalidArgumentException;
|
||||
|
||||
final class CBOREncodingTag extends Tag
|
||||
{
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! $object instanceof ByteStringObject && ! $object instanceof IndefiniteLengthByteStringObject) {
|
||||
throw new InvalidArgumentException('This tag only accepts a Byte String object.');
|
||||
}
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_ENCODED_CBOR;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): Tag
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_ENCODED_CBOR);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
}
|
||||
87
vendor/spomky-labs/cbor-php/src/Tag/CBORSequenceTag.php
vendored
Normal file
87
vendor/spomky-labs/cbor-php/src/Tag/CBORSequenceTag.php
vendored
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\ByteStringObject;
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\Decoder;
|
||||
use CBOR\DecoderInterface;
|
||||
use CBOR\IndefiniteLengthByteStringObject;
|
||||
use CBOR\StringStream;
|
||||
use CBOR\Tag;
|
||||
use InvalidArgumentException;
|
||||
use function strlen;
|
||||
use function substr;
|
||||
|
||||
/**
|
||||
* Tag 63: a byte string holding an encoded CBOR sequence (RFC 8742), i.e. zero or more data items one after the
|
||||
* other, with no enclosing array.
|
||||
*
|
||||
* It is to a sequence what tag 24 is to a single data item. The bytes are not decoded on the way in -- the tag
|
||||
* only says what they are -- so getSequence() is where a caller opts into parsing them.
|
||||
*
|
||||
* @see \CBOR\Test\Tag\RegistryTagsTest
|
||||
*/
|
||||
final class CBORSequenceTag extends Tag
|
||||
{
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! $object instanceof ByteStringObject && ! $object instanceof IndefiniteLengthByteStringObject) {
|
||||
throw new InvalidArgumentException('This tag only accepts a Byte String object.');
|
||||
}
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_ENCODED_CBOR_SEQUENCE;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_ENCODED_CBOR_SEQUENCE);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes the sequence the byte string holds.
|
||||
*
|
||||
* A CBOR sequence carries no framing, so the only way to find where an item ends is to decode it. The stream
|
||||
* abstraction of this library cannot report how far it was consumed, so each item is re-encoded and the bytes
|
||||
* it accounts for are dropped from the front. Every object of this library reproduces the head it was decoded
|
||||
* from, which makes the round trip exact; a mismatch would mean the item was rewritten on the way out, so it
|
||||
* is reported rather than silently used to advance past the wrong number of bytes.
|
||||
*
|
||||
* @return CBORObject[] the items, in the order they appear; an empty byte string is an empty sequence
|
||||
*/
|
||||
public function getSequence(?DecoderInterface $decoder = null): array
|
||||
{
|
||||
/** @var ByteStringObject|IndefiniteLengthByteStringObject $object */
|
||||
$object = $this->object;
|
||||
$remaining = $object->getValue();
|
||||
$decoder ??= Decoder::create();
|
||||
|
||||
$items = [];
|
||||
while ($remaining !== '') {
|
||||
$item = $decoder->decode(StringStream::create($remaining));
|
||||
$encoded = (string) $item;
|
||||
$length = strlen($encoded);
|
||||
if ($length === 0 || substr($remaining, 0, $length) !== $encoded) {
|
||||
throw new InvalidArgumentException('Invalid data. The sequence cannot be decoded.');
|
||||
}
|
||||
$items[] = $item;
|
||||
$remaining = substr($remaining, $length);
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
}
|
||||
46
vendor/spomky-labs/cbor-php/src/Tag/CBORTag.php
vendored
Normal file
46
vendor/spomky-labs/cbor-php/src/Tag/CBORTag.php
vendored
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
|
||||
/**
|
||||
* Tag 55799: Self-Described CBOR
|
||||
*
|
||||
* This tag is used to mark CBOR data to enable a decoder to rapidly identify
|
||||
* that the data item is in CBOR encoding. This tag is intentionally placed at
|
||||
* a high tag number to minimize collision with other applications.
|
||||
*
|
||||
* @see https://datatracker.ietf.org/doc/html/rfc8949#section-3.4.6
|
||||
*/
|
||||
final class CBORTag extends Tag implements Normalizable
|
||||
{
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_CBOR;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): Tag
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_CBOR);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|CBORObject|null
|
||||
*/
|
||||
public function normalize()
|
||||
{
|
||||
return $this->object instanceof Normalizable ? $this->object->normalize() : $this->object;
|
||||
}
|
||||
}
|
||||
48
vendor/spomky-labs/cbor-php/src/Tag/ColumnMajorMultiDimensionalArrayTag.php
vendored
Normal file
48
vendor/spomky-labs/cbor-php/src/Tag/ColumnMajorMultiDimensionalArrayTag.php
vendored
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\Tag;
|
||||
use function count;
|
||||
|
||||
/**
|
||||
* Tag 1040: a multi-dimensional array in column-major order, i.e. the first index varies fastest -- the layout
|
||||
* Fortran, MATLAB and most numerical libraries use.
|
||||
*
|
||||
* @see \CBOR\Test\Tag\TypedArrayTagTest
|
||||
* @see https://datatracker.ietf.org/doc/html/rfc8746#section-3.2
|
||||
*/
|
||||
final class ColumnMajorMultiDimensionalArrayTag extends AbstractMultiDimensionalArrayTag
|
||||
{
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_COLUMN_MAJOR_MULTI_DIMENSIONAL_ARRAY;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_COLUMN_MAJOR_MULTI_DIMENSIONAL_ARRAY);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
protected static function getStrides(array $dimensions): array
|
||||
{
|
||||
$strides = [];
|
||||
$stride = 1;
|
||||
for ($index = 0, $length = count($dimensions); $index < $length; ++$index) {
|
||||
$strides[$index] = $stride;
|
||||
$stride *= $dimensions[$index];
|
||||
}
|
||||
|
||||
return $strides;
|
||||
}
|
||||
}
|
||||
74
vendor/spomky-labs/cbor-php/src/Tag/CoseEncrypt0Tag.php
vendored
Normal file
74
vendor/spomky-labs/cbor-php/src/Tag/CoseEncrypt0Tag.php
vendored
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\ByteStringObject;
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthByteStringObject;
|
||||
use CBOR\ListObject;
|
||||
use CBOR\MapObject;
|
||||
use CBOR\OtherObject\NullObject;
|
||||
use CBOR\Tag;
|
||||
|
||||
/**
|
||||
* Tag 16: COSE_Encrypt0, a single recipient encrypted message -- the recipient is known out of band, so the
|
||||
* structure carries no recipient list.
|
||||
*
|
||||
* Content: [protected header, unprotected header, ciphertext].
|
||||
*/
|
||||
final class CoseEncrypt0Tag extends AbstractCoseTag
|
||||
{
|
||||
private const STRUCTURE_NAME = 'CoseEncrypt0';
|
||||
|
||||
private const ITEM_COUNT = 3;
|
||||
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
$structure = self::assertStructure($object, self::ITEM_COUNT, self::STRUCTURE_NAME);
|
||||
self::assertPayloadAt($structure, 2, self::STRUCTURE_NAME);
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_COSE_ENCRYPT0;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_COSE_ENCRYPT0);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the structure from its parts, serializing the protected header into the byte string COSE signs.
|
||||
*/
|
||||
public static function createFromComponents(MapObject $protectedHeader, MapObject $unprotectedHeader, ByteStringObject|NullObject $ciphertext): self
|
||||
{
|
||||
return self::create(ListObject::create([
|
||||
ByteStringObject::create((string) $protectedHeader),
|
||||
$unprotectedHeader,
|
||||
$ciphertext,
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* The ciphertext, or null when it is detached and carried out of band.
|
||||
*/
|
||||
public function getCiphertext(): ByteStringObject|IndefiniteLengthByteStringObject|NullObject
|
||||
{
|
||||
/** @var ByteStringObject|IndefiniteLengthByteStringObject|NullObject $item */
|
||||
$item = $this->itemAt(2);
|
||||
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
89
vendor/spomky-labs/cbor-php/src/Tag/CoseEncryptTag.php
vendored
Normal file
89
vendor/spomky-labs/cbor-php/src/Tag/CoseEncryptTag.php
vendored
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\ByteStringObject;
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthByteStringObject;
|
||||
use CBOR\IndefiniteLengthListObject;
|
||||
use CBOR\ListObject;
|
||||
use CBOR\MapObject;
|
||||
use CBOR\OtherObject\NullObject;
|
||||
use CBOR\Tag;
|
||||
|
||||
/**
|
||||
* Tag 96: COSE_Encrypt, an encrypted message with one or more recipients, each of which carries the content
|
||||
* encryption key in its own way.
|
||||
*
|
||||
* Content: [protected header, unprotected header, ciphertext, recipients].
|
||||
*/
|
||||
final class CoseEncryptTag extends AbstractCoseTag
|
||||
{
|
||||
private const STRUCTURE_NAME = 'CoseEncrypt';
|
||||
|
||||
private const ITEM_COUNT = 4;
|
||||
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
$structure = self::assertStructure($object, self::ITEM_COUNT, self::STRUCTURE_NAME);
|
||||
self::assertPayloadAt($structure, 2, self::STRUCTURE_NAME);
|
||||
self::assertListAt($structure, 3, self::STRUCTURE_NAME);
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_COSE_ENCRYPT;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_COSE_ENCRYPT);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the structure from its parts, serializing the protected header into the byte string COSE signs.
|
||||
*/
|
||||
public static function createFromComponents(
|
||||
MapObject $protectedHeader,
|
||||
MapObject $unprotectedHeader,
|
||||
ByteStringObject|NullObject $ciphertext,
|
||||
ListObject $recipients
|
||||
): self {
|
||||
return self::create(ListObject::create([
|
||||
ByteStringObject::create((string) $protectedHeader),
|
||||
$unprotectedHeader,
|
||||
$ciphertext,
|
||||
$recipients,
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* The ciphertext, or null when it is detached and carried out of band.
|
||||
*/
|
||||
public function getCiphertext(): ByteStringObject|IndefiniteLengthByteStringObject|NullObject
|
||||
{
|
||||
/** @var ByteStringObject|IndefiniteLengthByteStringObject|NullObject $item */
|
||||
$item = $this->itemAt(2);
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
public function getRecipients(): ListObject|IndefiniteLengthListObject
|
||||
{
|
||||
/** @var IndefiniteLengthListObject|ListObject $item */
|
||||
$item = $this->itemAt(3);
|
||||
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
88
vendor/spomky-labs/cbor-php/src/Tag/CoseMac0Tag.php
vendored
Normal file
88
vendor/spomky-labs/cbor-php/src/Tag/CoseMac0Tag.php
vendored
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\ByteStringObject;
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthByteStringObject;
|
||||
use CBOR\ListObject;
|
||||
use CBOR\MapObject;
|
||||
use CBOR\OtherObject\NullObject;
|
||||
use CBOR\Tag;
|
||||
|
||||
/**
|
||||
* Tag 17: COSE_Mac0, a MAC'd message with a single recipient -- the key is known out of band, so the structure
|
||||
* carries no recipient list.
|
||||
*
|
||||
* Content: [protected header, unprotected header, payload, tag].
|
||||
*/
|
||||
final class CoseMac0Tag extends AbstractCoseTag
|
||||
{
|
||||
private const STRUCTURE_NAME = 'CoseMac0';
|
||||
|
||||
private const ITEM_COUNT = 4;
|
||||
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
$structure = self::assertStructure($object, self::ITEM_COUNT, self::STRUCTURE_NAME);
|
||||
self::assertPayloadAt($structure, 2, self::STRUCTURE_NAME);
|
||||
self::assertByteStringAt($structure, 3, self::STRUCTURE_NAME);
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_COSE_MAC0;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_COSE_MAC0);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the structure from its parts, serializing the protected header into the byte string COSE signs.
|
||||
*/
|
||||
public static function createFromComponents(
|
||||
MapObject $protectedHeader,
|
||||
MapObject $unprotectedHeader,
|
||||
ByteStringObject|NullObject $payload,
|
||||
ByteStringObject $tag
|
||||
): self {
|
||||
return self::create(ListObject::create([
|
||||
ByteStringObject::create((string) $protectedHeader),
|
||||
$unprotectedHeader,
|
||||
$payload,
|
||||
$tag,
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* The payload, or null when it is detached and carried out of band.
|
||||
*/
|
||||
public function getPayload(): ByteStringObject|IndefiniteLengthByteStringObject|NullObject
|
||||
{
|
||||
/** @var ByteStringObject|IndefiniteLengthByteStringObject|NullObject $item */
|
||||
$item = $this->itemAt(2);
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
public function getTag(): ByteStringObject|IndefiniteLengthByteStringObject
|
||||
{
|
||||
/** @var ByteStringObject|IndefiniteLengthByteStringObject $item */
|
||||
$item = $this->itemAt(3);
|
||||
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
100
vendor/spomky-labs/cbor-php/src/Tag/CoseMacTag.php
vendored
Normal file
100
vendor/spomky-labs/cbor-php/src/Tag/CoseMacTag.php
vendored
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\ByteStringObject;
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthByteStringObject;
|
||||
use CBOR\IndefiniteLengthListObject;
|
||||
use CBOR\ListObject;
|
||||
use CBOR\MapObject;
|
||||
use CBOR\OtherObject\NullObject;
|
||||
use CBOR\Tag;
|
||||
|
||||
/**
|
||||
* Tag 97: COSE_Mac, a MAC'd message with one or more recipients, each of which carries the MAC key in its own
|
||||
* way.
|
||||
*
|
||||
* Content: [protected header, unprotected header, payload, tag, recipients].
|
||||
*/
|
||||
final class CoseMacTag extends AbstractCoseTag
|
||||
{
|
||||
private const STRUCTURE_NAME = 'CoseMac';
|
||||
|
||||
private const ITEM_COUNT = 5;
|
||||
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
$structure = self::assertStructure($object, self::ITEM_COUNT, self::STRUCTURE_NAME);
|
||||
self::assertPayloadAt($structure, 2, self::STRUCTURE_NAME);
|
||||
self::assertByteStringAt($structure, 3, self::STRUCTURE_NAME);
|
||||
self::assertListAt($structure, 4, self::STRUCTURE_NAME);
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_COSE_MAC;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_COSE_MAC);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the structure from its parts, serializing the protected header into the byte string COSE signs.
|
||||
*/
|
||||
public static function createFromComponents(
|
||||
MapObject $protectedHeader,
|
||||
MapObject $unprotectedHeader,
|
||||
ByteStringObject|NullObject $payload,
|
||||
ByteStringObject $tag,
|
||||
ListObject $recipients
|
||||
): self {
|
||||
return self::create(ListObject::create([
|
||||
ByteStringObject::create((string) $protectedHeader),
|
||||
$unprotectedHeader,
|
||||
$payload,
|
||||
$tag,
|
||||
$recipients,
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* The payload, or null when it is detached and carried out of band.
|
||||
*/
|
||||
public function getPayload(): ByteStringObject|IndefiniteLengthByteStringObject|NullObject
|
||||
{
|
||||
/** @var ByteStringObject|IndefiniteLengthByteStringObject|NullObject $item */
|
||||
$item = $this->itemAt(2);
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
public function getTag(): ByteStringObject|IndefiniteLengthByteStringObject
|
||||
{
|
||||
/** @var ByteStringObject|IndefiniteLengthByteStringObject $item */
|
||||
$item = $this->itemAt(3);
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
public function getRecipients(): ListObject|IndefiniteLengthListObject
|
||||
{
|
||||
/** @var IndefiniteLengthListObject|ListObject $item */
|
||||
$item = $this->itemAt(4);
|
||||
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
88
vendor/spomky-labs/cbor-php/src/Tag/CoseSign1Tag.php
vendored
Normal file
88
vendor/spomky-labs/cbor-php/src/Tag/CoseSign1Tag.php
vendored
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\ByteStringObject;
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthByteStringObject;
|
||||
use CBOR\ListObject;
|
||||
use CBOR\MapObject;
|
||||
use CBOR\OtherObject\NullObject;
|
||||
use CBOR\Tag;
|
||||
|
||||
/**
|
||||
* Tag 18: COSE_Sign1, a message signed by a single signer.
|
||||
*
|
||||
* Content: [protected header, unprotected header, payload, signature]. This is the structure WebAuthn and CWT
|
||||
* use most.
|
||||
*/
|
||||
final class CoseSign1Tag extends AbstractCoseTag
|
||||
{
|
||||
private const STRUCTURE_NAME = 'CoseSign1';
|
||||
|
||||
private const ITEM_COUNT = 4;
|
||||
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
$structure = self::assertStructure($object, self::ITEM_COUNT, self::STRUCTURE_NAME);
|
||||
self::assertPayloadAt($structure, 2, self::STRUCTURE_NAME);
|
||||
self::assertByteStringAt($structure, 3, self::STRUCTURE_NAME);
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_COSE_SIGN1;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_COSE_SIGN1);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the structure from its parts, serializing the protected header into the byte string COSE signs.
|
||||
*/
|
||||
public static function createFromComponents(
|
||||
MapObject $protectedHeader,
|
||||
MapObject $unprotectedHeader,
|
||||
ByteStringObject|NullObject $payload,
|
||||
ByteStringObject $signature
|
||||
): self {
|
||||
return self::create(ListObject::create([
|
||||
ByteStringObject::create((string) $protectedHeader),
|
||||
$unprotectedHeader,
|
||||
$payload,
|
||||
$signature,
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* The payload, or null when it is detached and carried out of band.
|
||||
*/
|
||||
public function getPayload(): ByteStringObject|IndefiniteLengthByteStringObject|NullObject
|
||||
{
|
||||
/** @var ByteStringObject|IndefiniteLengthByteStringObject|NullObject $item */
|
||||
$item = $this->itemAt(2);
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
public function getSignature(): ByteStringObject|IndefiniteLengthByteStringObject
|
||||
{
|
||||
/** @var ByteStringObject|IndefiniteLengthByteStringObject $item */
|
||||
$item = $this->itemAt(3);
|
||||
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
88
vendor/spomky-labs/cbor-php/src/Tag/CoseSignTag.php
vendored
Normal file
88
vendor/spomky-labs/cbor-php/src/Tag/CoseSignTag.php
vendored
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\ByteStringObject;
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthByteStringObject;
|
||||
use CBOR\IndefiniteLengthListObject;
|
||||
use CBOR\ListObject;
|
||||
use CBOR\MapObject;
|
||||
use CBOR\OtherObject\NullObject;
|
||||
use CBOR\Tag;
|
||||
|
||||
/**
|
||||
* Tag 98: COSE_Sign, a message signed by one or more signers, each with its own headers and signature.
|
||||
*
|
||||
* Content: [protected header, unprotected header, payload, signatures].
|
||||
*/
|
||||
final class CoseSignTag extends AbstractCoseTag
|
||||
{
|
||||
private const STRUCTURE_NAME = 'CoseSign';
|
||||
|
||||
private const ITEM_COUNT = 4;
|
||||
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
$structure = self::assertStructure($object, self::ITEM_COUNT, self::STRUCTURE_NAME);
|
||||
self::assertPayloadAt($structure, 2, self::STRUCTURE_NAME);
|
||||
self::assertListAt($structure, 3, self::STRUCTURE_NAME);
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_COSE_SIGN;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_COSE_SIGN);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the structure from its parts, serializing the protected header into the byte string COSE signs.
|
||||
*/
|
||||
public static function createFromComponents(
|
||||
MapObject $protectedHeader,
|
||||
MapObject $unprotectedHeader,
|
||||
ByteStringObject|NullObject $payload,
|
||||
ListObject $signatures
|
||||
): self {
|
||||
return self::create(ListObject::create([
|
||||
ByteStringObject::create((string) $protectedHeader),
|
||||
$unprotectedHeader,
|
||||
$payload,
|
||||
$signatures,
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* The payload, or null when it is detached and carried out of band.
|
||||
*/
|
||||
public function getPayload(): ByteStringObject|IndefiniteLengthByteStringObject|NullObject
|
||||
{
|
||||
/** @var ByteStringObject|IndefiniteLengthByteStringObject|NullObject $item */
|
||||
$item = $this->itemAt(2);
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
public function getSignatures(): ListObject|IndefiniteLengthListObject
|
||||
{
|
||||
/** @var IndefiniteLengthListObject|ListObject $item */
|
||||
$item = $this->itemAt(3);
|
||||
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
61
vendor/spomky-labs/cbor-php/src/Tag/CwtTag.php
vendored
Normal file
61
vendor/spomky-labs/cbor-php/src/Tag/CwtTag.php
vendored
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthListObject;
|
||||
use CBOR\IndefiniteLengthMapObject;
|
||||
use CBOR\ListObject;
|
||||
use CBOR\MapObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Tag 61: a CBOR Web Token (RFC 8392).
|
||||
*
|
||||
* The tag says "what follows is a CWT" and nothing more; the token itself is a COSE structure -- tagged, as
|
||||
* CoseSign1Tag and its siblings, or untagged, in which case it is the bare array COSE defines -- or, for an
|
||||
* unsecured token, the claims set as a plain map. All three are accepted; none is verified here, since
|
||||
* validating a signature needs keys this library knows nothing about.
|
||||
*
|
||||
* @see \CBOR\Test\Tag\CoseTagTest
|
||||
* @see https://datatracker.ietf.org/doc/html/rfc8392#section-6
|
||||
*/
|
||||
final class CwtTag extends Tag implements Normalizable
|
||||
{
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! $object instanceof MapObject && ! $object instanceof IndefiniteLengthMapObject && ! $object instanceof ListObject && ! $object instanceof IndefiniteLengthListObject && ! $object instanceof TagInterface) {
|
||||
throw new InvalidArgumentException('This tag only accepts a Map object, a List object or a COSE tag.');
|
||||
}
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_CWT;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_CWT);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
public function normalize(): mixed
|
||||
{
|
||||
$object = $this->object;
|
||||
|
||||
return $object instanceof Normalizable ? $object->normalize() : $object;
|
||||
}
|
||||
}
|
||||
74
vendor/spomky-labs/cbor-php/src/Tag/DateStringTag.php
vendored
Normal file
74
vendor/spomky-labs/cbor-php/src/Tag/DateStringTag.php
vendored
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthTextStringObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
use CBOR\TextStringObject;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeInterface;
|
||||
use DateTimeZone;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Tag 1004: a date in the RFC 3339 "full-date" form, i.e. YYYY-MM-DD, with no time and no time zone.
|
||||
*
|
||||
* It is the text counterpart of tag 100. As there, PHP has no date-only type, so normalize() returns midnight
|
||||
* UTC. A day that does not exist -- 2013-02-30, say -- is rejected rather than rolled over into the next month.
|
||||
*
|
||||
* @see \CBOR\Test\Tag\DateTagTest
|
||||
* @see https://datatracker.ietf.org/doc/html/rfc8943
|
||||
*/
|
||||
final class DateStringTag extends Tag implements Normalizable
|
||||
{
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! $object instanceof TextStringObject && ! $object instanceof IndefiniteLengthTextStringObject) {
|
||||
throw new InvalidArgumentException('This tag only accepts a Text String object.');
|
||||
}
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_DATE_STRING;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_DATE_STRING);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return DateTimeInterface midnight UTC on the day the tag denotes
|
||||
*/
|
||||
public function normalize(): DateTimeInterface
|
||||
{
|
||||
/** @var IndefiniteLengthTextStringObject|TextStringObject $object */
|
||||
$object = $this->object;
|
||||
|
||||
// The leading "!" resets every field the format does not carry, so the time is midnight rather than the
|
||||
// current one -- otherwise the same document would normalize to a different instant every second.
|
||||
$result = DateTimeImmutable::createFromFormat('!Y-m-d', $object->normalize(), new DateTimeZone('UTC'));
|
||||
$errors = DateTimeImmutable::getLastErrors();
|
||||
// As of PHP 8.2 a parse with nothing to report returns false instead of an array of empty counters.
|
||||
$exact = $errors === false || ($errors['warning_count'] === 0 && $errors['error_count'] === 0);
|
||||
if ($result === false || ! $exact) {
|
||||
throw new InvalidArgumentException('Invalid data. Cannot be converted into a datetime object');
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
82
vendor/spomky-labs/cbor-php/src/Tag/DateTag.php
vendored
Normal file
82
vendor/spomky-labs/cbor-php/src/Tag/DateTag.php
vendored
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use Brick\Math\BigInteger;
|
||||
use Brick\Math\Exception\MathException;
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\NegativeIntegerObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
use CBOR\UnsignedIntegerObject;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeInterface;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Tag 100: a date, as the number of days since 1970-01-01, with no time and no time zone.
|
||||
*
|
||||
* It is what tag 1 is to an instant: a birthday or an expiry date is a calendar day everywhere at once, and
|
||||
* pinning it to a moment -- which is what tag 0 or tag 1 would do -- moves it across a day boundary depending on
|
||||
* where it is read. PHP has no date-only type, so normalize() returns midnight UTC, the reading that keeps the
|
||||
* day intact as long as the time zone is left alone.
|
||||
*
|
||||
* @see \CBOR\Test\Tag\DateTagTest
|
||||
* @see https://datatracker.ietf.org/doc/html/rfc8943
|
||||
*/
|
||||
final class DateTag extends Tag implements Normalizable
|
||||
{
|
||||
private const SECONDS_PER_DAY = 86400;
|
||||
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! $object instanceof UnsignedIntegerObject && ! $object instanceof NegativeIntegerObject) {
|
||||
throw new InvalidArgumentException('This tag only accepts an Integer object.');
|
||||
}
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_DATE;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_DATE);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return DateTimeInterface midnight UTC on the day the tag denotes
|
||||
*/
|
||||
public function normalize(): DateTimeInterface
|
||||
{
|
||||
/** @var NegativeIntegerObject|UnsignedIntegerObject $object */
|
||||
$object = $this->object;
|
||||
|
||||
try {
|
||||
$seconds = BigInteger::of($object->normalize())
|
||||
->multipliedBy(self::SECONDS_PER_DAY)
|
||||
->toInt();
|
||||
} catch (MathException $throwable) {
|
||||
throw new InvalidArgumentException('Invalid data. Cannot be converted into a datetime object', 0, $throwable);
|
||||
}
|
||||
|
||||
$result = DateTimeImmutable::createFromFormat('U', (string) $seconds);
|
||||
if ($result === false) {
|
||||
throw new InvalidArgumentException('Invalid data. Cannot be converted into a datetime object');
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
95
vendor/spomky-labs/cbor-php/src/Tag/DatetimeTag.php
vendored
Normal file
95
vendor/spomky-labs/cbor-php/src/Tag/DatetimeTag.php
vendored
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthTextStringObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
use CBOR\TextStringObject;
|
||||
use const DATE_RFC3339;
|
||||
use DateInterval;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeInterface;
|
||||
use InvalidArgumentException;
|
||||
use function preg_match;
|
||||
use function str_contains;
|
||||
|
||||
/**
|
||||
* @see \CBOR\Test\Tag\DatetimeTagTest
|
||||
*/
|
||||
final class DatetimeTag extends Tag implements Normalizable
|
||||
{
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! $object instanceof TextStringObject && ! $object instanceof IndefiniteLengthTextStringObject) {
|
||||
throw new InvalidArgumentException('This tag only accepts a Byte String object.');
|
||||
}
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_STANDARD_DATETIME;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): Tag
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_STANDARD_DATETIME);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
public function normalize(): DateTimeInterface
|
||||
{
|
||||
/** @var TextStringObject|IndefiniteLengthTextStringObject $object */
|
||||
$object = $this->object;
|
||||
$value = $object->normalize();
|
||||
|
||||
// Since PHP 8, createFromFormat() rejects an argument holding a NUL byte with a ValueError -- an Error, so
|
||||
// neither the "=== false" guard below nor a caller catching InvalidArgumentException ever sees it. The byte
|
||||
// cannot appear in an RFC 3339 date-time anyway, so it is turned down here, within the error contract.
|
||||
if (str_contains($value, "\0")) {
|
||||
throw new InvalidArgumentException('Invalid data. Cannot be converted into a datetime object');
|
||||
}
|
||||
|
||||
// RFC 3339 allows a leap second, which no PHP date can hold. It is parsed as the second before it and
|
||||
// shifted forward again, which lands on the next minute: the closest instant this library can return.
|
||||
$isLeapSecond = preg_match('/^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:)60(.*)$/', $value, $matches) === 1;
|
||||
if ($isLeapSecond) {
|
||||
$value = $matches[1] . '59' . $matches[2];
|
||||
}
|
||||
|
||||
$result = DateTimeImmutable::createFromFormat(DATE_RFC3339, $value);
|
||||
if ($result === false || ! self::wasParsedExactly()) {
|
||||
$result = DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.uP', $value);
|
||||
if ($result === false || ! self::wasParsedExactly()) {
|
||||
throw new InvalidArgumentException('Invalid data. Cannot be converted into a datetime object');
|
||||
}
|
||||
}
|
||||
|
||||
return $isLeapSecond ? $result->add(new DateInterval('PT1S')) : $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells whether the last parse consumed the whole string and described an instant that exists.
|
||||
*
|
||||
* createFromFormat() does not reject a date-time that cannot be: it rolls it over and merely reports a warning,
|
||||
* so "2013-02-30" comes back as the 2nd of March and hour 25 as the next day. A document then decodes to an
|
||||
* instant it does not carry, which is worse than a rejection.
|
||||
*/
|
||||
private static function wasParsedExactly(): bool
|
||||
{
|
||||
$errors = DateTimeImmutable::getLastErrors();
|
||||
|
||||
// As of PHP 8.2 a parse with nothing to report returns false instead of an array of empty counters.
|
||||
return $errors === false || ($errors['warning_count'] === 0 && $errors['error_count'] === 0);
|
||||
}
|
||||
}
|
||||
276
vendor/spomky-labs/cbor-php/src/Tag/DecimalFractionTag.php
vendored
Normal file
276
vendor/spomky-labs/cbor-php/src/Tag/DecimalFractionTag.php
vendored
Normal file
|
|
@ -0,0 +1,276 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use Brick\Math\BigInteger;
|
||||
use CBOR\ByteStringObject;
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthListObject;
|
||||
use CBOR\ListObject;
|
||||
use CBOR\NegativeIntegerObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
use CBOR\UnsignedIntegerObject;
|
||||
use function count;
|
||||
use function extension_loaded;
|
||||
use InvalidArgumentException;
|
||||
use RuntimeException;
|
||||
use function sprintf;
|
||||
use function str_contains;
|
||||
use function str_starts_with;
|
||||
use function strlen;
|
||||
|
||||
final class DecimalFractionTag extends Tag implements Normalizable
|
||||
{
|
||||
/**
|
||||
* The maximum absolute value accepted for the exponent.
|
||||
*
|
||||
* 10^e needs about e decimal digits to write down, so the exponent alone decides how much memory normalizing
|
||||
* one item costs -- and the exponent is three bytes on the wire. At 8192 a six byte item expanded to more than
|
||||
* eight kilobytes, an amplification of over a thousand, and a document made of such items exhausted the memory
|
||||
* limit inside decode() with a fatal error no try/catch can intercept. The bound is now the range of an IEEE
|
||||
* 754 double, which is what a decimal fraction is meant to exceed in precision, not in magnitude.
|
||||
*/
|
||||
public const MAX_ABSOLUTE_EXPONENT = 1024;
|
||||
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! extension_loaded('bcmath')) {
|
||||
throw new RuntimeException('The extension "bcmath" is required to use this tag');
|
||||
}
|
||||
$isList = $object instanceof ListObject || $object instanceof IndefiniteLengthListObject;
|
||||
if (! $isList || count($object) !== 2) {
|
||||
throw new InvalidArgumentException(
|
||||
'This tag only accepts a ListObject object that contains an exponent and a mantissa.'
|
||||
);
|
||||
}
|
||||
$e = $object->get(0);
|
||||
if (! $e instanceof UnsignedIntegerObject && ! $e instanceof NegativeIntegerObject) {
|
||||
throw new InvalidArgumentException('The exponent must be a Signed Integer or an Unsigned Integer object.');
|
||||
}
|
||||
$m = $object->get(1);
|
||||
if (! $m instanceof UnsignedIntegerObject && ! $m instanceof NegativeIntegerObject && ! $m instanceof NegativeBigIntegerTag && ! $m instanceof UnsignedBigIntegerTag) {
|
||||
throw new InvalidArgumentException(
|
||||
'The mantissa must be a Positive or Negative Signed Integer or an Unsigned Integer object.'
|
||||
);
|
||||
}
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_DECIMAL_FRACTION);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_DECIMAL_FRACTION;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): self
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function createFromExponentAndMantissa(CBORObject $e, CBORObject $m): self
|
||||
{
|
||||
$object = ListObject::create()
|
||||
->add($e)
|
||||
->add($m)
|
||||
;
|
||||
|
||||
return self::create($object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a DecimalFraction from a PHP float value.
|
||||
* DecimalFraction represents: mantissa × 10^exponent
|
||||
* This method converts a float to a decimal representation with minimal precision loss.
|
||||
*
|
||||
* @param float $value The float value to convert
|
||||
* @param int $precision Maximum number of decimal places (default: 10)
|
||||
* @return self The DecimalFraction tag object
|
||||
*/
|
||||
public static function createFromFloat(float $value, int $precision = 10): self
|
||||
{
|
||||
if (! extension_loaded('bcmath')) {
|
||||
throw new RuntimeException('The extension "bcmath" is required to use this method');
|
||||
}
|
||||
|
||||
if ($precision < 0) {
|
||||
throw new InvalidArgumentException('Precision must be non-negative');
|
||||
}
|
||||
|
||||
// Handle special cases
|
||||
if (is_nan($value) || is_infinite($value)) {
|
||||
throw new InvalidArgumentException('DecimalFraction cannot represent NaN or Infinity');
|
||||
}
|
||||
|
||||
if ($value === 0.0) {
|
||||
// 0 = 0 × 10^0
|
||||
return self::createFromExponentAndMantissa(
|
||||
UnsignedIntegerObject::create(0),
|
||||
UnsignedIntegerObject::create(0)
|
||||
);
|
||||
}
|
||||
|
||||
// Convert float to string with appropriate precision
|
||||
// We use sprintf to get a decimal representation
|
||||
$str = sprintf("%.{$precision}F", $value);
|
||||
|
||||
// Remove trailing zeros after decimal point
|
||||
if (str_contains($str, '.')) {
|
||||
$str = rtrim($str, '0');
|
||||
$str = rtrim($str, '.');
|
||||
}
|
||||
|
||||
// Split into integer and fractional parts
|
||||
$parts = explode('.', $str);
|
||||
$integerPart = $parts[0];
|
||||
$fractionalPart = $parts[1] ?? '';
|
||||
|
||||
// Calculate exponent (negative = decimal places)
|
||||
$exponent = -strlen($fractionalPart);
|
||||
|
||||
// Keep the sign aside: it is not a digit and must not survive the normalisation below, which would
|
||||
// otherwise turn a value rounding to zero, such as -1e-12, into the unparsable mantissa "-".
|
||||
$isNegative = str_starts_with($integerPart, '-');
|
||||
|
||||
// Combine to form mantissa (drop the sign and the decimal point, then the leading zeros)
|
||||
$mantissa = ltrim(ltrim($integerPart, '-') . $fractionalPart, '0');
|
||||
if ($mantissa === '') {
|
||||
$mantissa = '0';
|
||||
}
|
||||
|
||||
// Normalize: remove trailing zeros from mantissa by adjusting exponent
|
||||
while ($mantissa !== '0' && str_ends_with($mantissa, '0')) {
|
||||
$mantissa = substr($mantissa, 0, -1);
|
||||
$exponent++;
|
||||
}
|
||||
|
||||
// A value that rounds to zero at the requested precision is zero, whatever its sign was.
|
||||
if ($mantissa === '0') {
|
||||
$isNegative = false;
|
||||
$exponent = 0;
|
||||
}
|
||||
|
||||
// Create exponent object
|
||||
if ($exponent >= 0) {
|
||||
$exponentObj = UnsignedIntegerObject::create($exponent);
|
||||
} else {
|
||||
$exponentObj = NegativeIntegerObject::create($exponent);
|
||||
}
|
||||
|
||||
// Put the sign back on the mantissa now that the normalisation above is done with it.
|
||||
return self::createFromExponentAndMantissa(
|
||||
$exponentObj,
|
||||
self::mantissaObject($isNegative ? '-' . $mantissa : $mantissa)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The mantissa of a decimal fraction is only bounded by the requested precision, so it routinely outgrows the
|
||||
* 8-byte argument an integer head can carry. RFC 8949 section 3.4.3 answers that with the bignum tags the
|
||||
* constructor already accepts, and this picks whichever of the four representations fits.
|
||||
*
|
||||
* The sign is read off the string rather than from a PHP integer cast, which saturates at PHP_INT_MIN or
|
||||
* PHP_INT_MAX and would report the wrong sign for exactly the long mantissas this has to handle.
|
||||
*/
|
||||
private static function mantissaObject(string $mantissa): CBORObject
|
||||
{
|
||||
$value = BigInteger::of($mantissa);
|
||||
if (str_starts_with($mantissa, '-')) {
|
||||
$argument = BigInteger::of(-1)->minus($value);
|
||||
|
||||
return $argument->isLessThanOrEqualTo(self::maximumHeadArgument())
|
||||
? NegativeIntegerObject::createFromString($mantissa)
|
||||
: NegativeBigIntegerTag::create(ByteStringObject::create(self::toBigEndianBytes($argument)));
|
||||
}
|
||||
|
||||
return $value->isLessThanOrEqualTo(self::maximumHeadArgument())
|
||||
? UnsignedIntegerObject::createFromString($mantissa)
|
||||
: UnsignedBigIntegerTag::create(ByteStringObject::create(self::toBigEndianBytes($value)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Largest argument an integer head can carry, 2^64 - 1. Beyond it a bignum tag is the only representation.
|
||||
*/
|
||||
private static function maximumHeadArgument(): BigInteger
|
||||
{
|
||||
return BigInteger::fromBase('FFFFFFFFFFFFFFFF', 16);
|
||||
}
|
||||
|
||||
/**
|
||||
* The network byte order, unsigned, no leading zero byte representation a bignum tag wraps.
|
||||
*/
|
||||
private static function toBigEndianBytes(BigInteger $value): string
|
||||
{
|
||||
$hex = $value->toBase(16);
|
||||
if (strlen($hex) % 2 === 1) {
|
||||
$hex = '0' . $hex;
|
||||
}
|
||||
$bytes = hex2bin($hex);
|
||||
if ($bytes === false) {
|
||||
throw new InvalidArgumentException('Unable to convert the data');
|
||||
}
|
||||
|
||||
return $bytes;
|
||||
}
|
||||
|
||||
public function normalize()
|
||||
{
|
||||
/** @var ListObject|IndefiniteLengthListObject $object */
|
||||
$object = $this->object;
|
||||
/** @var UnsignedIntegerObject|NegativeIntegerObject $e */
|
||||
$e = $object->get(0);
|
||||
/** @var UnsignedIntegerObject|NegativeIntegerObject|NegativeBigIntegerTag|UnsignedBigIntegerTag $m */
|
||||
$m = $object->get(1);
|
||||
|
||||
$exponent = (string) $e->normalize();
|
||||
self::assertExponentIsWithinBounds($exponent);
|
||||
$mantissa = (string) $m->normalize();
|
||||
|
||||
// The exponent is bounded, so it fits a PHP integer and the scale below is finite.
|
||||
$exponentValue = (int) $exponent;
|
||||
if ($exponentValue >= 0) {
|
||||
// m x 10^e with e >= 0 is an integer: no scale is needed and none shall be printed.
|
||||
return bcmul($mantissa, bcpow('10', $exponent, 0), 0);
|
||||
}
|
||||
|
||||
// m x 10^-s has exactly s decimal digits, so that scale makes the division exact. A fixed scale would
|
||||
// either truncate the result to zero or, worse, report a rounded value as if it were the decoded one.
|
||||
$scale = -$exponentValue;
|
||||
|
||||
return self::stripTrailingZeros(bcdiv($mantissa, bcpow('10', (string) $scale, 0), $scale));
|
||||
}
|
||||
|
||||
/**
|
||||
* bcdiv() pads the result up to the requested scale, so the exact value comes back with trailing zeros and,
|
||||
* once they are gone, a dangling decimal point. Both are stripped, but only from a value that has a decimal
|
||||
* point at all: trimming "500" would turn it into "5".
|
||||
*/
|
||||
private static function stripTrailingZeros(string $value): string
|
||||
{
|
||||
if (! str_contains($value, '.')) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return rtrim(rtrim($value, '0'), '.');
|
||||
}
|
||||
|
||||
private static function assertExponentIsWithinBounds(string $exponent): void
|
||||
{
|
||||
if (BigInteger::of($exponent)->abs()->isGreaterThan(BigInteger::of(self::MAX_ABSOLUTE_EXPONENT))) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'The exponent is out of range. Its absolute value shall not exceed %d, got "%s".',
|
||||
self::MAX_ABSOLUTE_EXPONENT,
|
||||
$exponent
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
60
vendor/spomky-labs/cbor-php/src/Tag/DurationTag.php
vendored
Normal file
60
vendor/spomky-labs/cbor-php/src/Tag/DurationTag.php
vendored
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthMapObject;
|
||||
use CBOR\MapObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Tag 1002: a duration, i.e. an amount of time with no anchor on the timeline.
|
||||
*
|
||||
* As with tag 1001, RFC 9581 encodes it as a map of components, and the components it allows do not all fit a
|
||||
* PHP DateInterval, so the map is returned as it stands.
|
||||
*
|
||||
* @see \CBOR\Test\Tag\RegistryTagsTest
|
||||
* @see https://datatracker.ietf.org/doc/html/rfc9581#section-4
|
||||
*/
|
||||
final class DurationTag extends Tag implements Normalizable
|
||||
{
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! $object instanceof MapObject && ! $object instanceof IndefiniteLengthMapObject) {
|
||||
throw new InvalidArgumentException('This tag only accepts a Map object.');
|
||||
}
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_DURATION;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_DURATION);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int|string, mixed>
|
||||
*/
|
||||
public function normalize(): array
|
||||
{
|
||||
/** @var IndefiniteLengthMapObject|MapObject $object */
|
||||
$object = $this->object;
|
||||
|
||||
return $object->normalize();
|
||||
}
|
||||
}
|
||||
59
vendor/spomky-labs/cbor-php/src/Tag/ExplicitMapTag.php
vendored
Normal file
59
vendor/spomky-labs/cbor-php/src/Tag/ExplicitMapTag.php
vendored
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthMapObject;
|
||||
use CBOR\MapObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Tag 259: a map meant as a key-value dictionary, as opposed to an object with named fields.
|
||||
*
|
||||
* The distinction matters when CBOR is converted to a format where the two are not spelled the same way. The
|
||||
* value is a plain map here, and normalize() returns it as one.
|
||||
*
|
||||
* @see \CBOR\Test\Tag\RegistryTagsTest
|
||||
*/
|
||||
final class ExplicitMapTag extends Tag implements Normalizable
|
||||
{
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! $object instanceof MapObject && ! $object instanceof IndefiniteLengthMapObject) {
|
||||
throw new InvalidArgumentException('This tag only accepts a Map object.');
|
||||
}
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_EXPLICIT_MAP;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_EXPLICIT_MAP);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int|string, mixed>
|
||||
*/
|
||||
public function normalize(): array
|
||||
{
|
||||
/** @var IndefiniteLengthMapObject|MapObject $object */
|
||||
$object = $this->object;
|
||||
|
||||
return $object->normalize();
|
||||
}
|
||||
}
|
||||
63
vendor/spomky-labs/cbor-php/src/Tag/ExtendedTimeTag.php
vendored
Normal file
63
vendor/spomky-labs/cbor-php/src/Tag/ExtendedTimeTag.php
vendored
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthMapObject;
|
||||
use CBOR\MapObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Tag 1001: an extended time, i.e. an instant with more than the seconds tag 1 can carry -- a clock quality, a
|
||||
* time scale, a UTC offset, a leap second indicator.
|
||||
*
|
||||
* RFC 9581 builds the value out of a map whose keys select what is present, and the combinations do not all map
|
||||
* onto a PHP date object: a leap second, a time scale that is not UTC or a resolution below the microsecond all
|
||||
* describe instants DateTimeImmutable cannot hold. The map is therefore returned as it stands rather than
|
||||
* flattened into a date that would silently lose one of them.
|
||||
*
|
||||
* @see \CBOR\Test\Tag\RegistryTagsTest
|
||||
* @see https://datatracker.ietf.org/doc/html/rfc9581#section-3
|
||||
*/
|
||||
final class ExtendedTimeTag extends Tag implements Normalizable
|
||||
{
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! $object instanceof MapObject && ! $object instanceof IndefiniteLengthMapObject) {
|
||||
throw new InvalidArgumentException('This tag only accepts a Map object.');
|
||||
}
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_EXTENDED_TIME;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_EXTENDED_TIME);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int|string, mixed>
|
||||
*/
|
||||
public function normalize(): array
|
||||
{
|
||||
/** @var IndefiniteLengthMapObject|MapObject $object */
|
||||
$object = $this->object;
|
||||
|
||||
return $object->normalize();
|
||||
}
|
||||
}
|
||||
33
vendor/spomky-labs/cbor-php/src/Tag/GenericTag.php
vendored
Normal file
33
vendor/spomky-labs/cbor-php/src/Tag/GenericTag.php
vendored
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
|
||||
final class GenericTag extends Tag implements Normalizable
|
||||
{
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
/**
|
||||
* The tag number carries no semantics known to this library, so the tagged item is normalized on its own. Use
|
||||
* getValue() when the tag number matters.
|
||||
*/
|
||||
public function normalize(): mixed
|
||||
{
|
||||
$object = $this->object;
|
||||
|
||||
return $object instanceof Normalizable ? $object->normalize() : $object;
|
||||
}
|
||||
}
|
||||
61
vendor/spomky-labs/cbor-php/src/Tag/HomogeneousArrayTag.php
vendored
Normal file
61
vendor/spomky-labs/cbor-php/src/Tag/HomogeneousArrayTag.php
vendored
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthListObject;
|
||||
use CBOR\ListObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Tag 41: an array whose items are all of the same type.
|
||||
*
|
||||
* The tag is a hint for the consumer -- it lets a statically typed decoder allocate one kind of slot -- and adds
|
||||
* nothing to the value, so the array normalizes exactly as it would untagged. Homogeneity is not checked: what
|
||||
* counts as "the same type" is the application's data model, not CBOR's major types.
|
||||
*
|
||||
* @see \CBOR\Test\Tag\RegistryTagsTest
|
||||
* @see https://datatracker.ietf.org/doc/html/rfc8746#section-3
|
||||
*/
|
||||
final class HomogeneousArrayTag extends Tag implements Normalizable
|
||||
{
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! $object instanceof ListObject && ! $object instanceof IndefiniteLengthListObject) {
|
||||
throw new InvalidArgumentException('This tag only accepts a List object.');
|
||||
}
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_HOMOGENEOUS_ARRAY;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_HOMOGENEOUS_ARRAY);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, mixed>
|
||||
*/
|
||||
public function normalize(): array
|
||||
{
|
||||
/** @var IndefiniteLengthListObject|ListObject $object */
|
||||
$object = $this->object;
|
||||
|
||||
return $object->normalize();
|
||||
}
|
||||
}
|
||||
45
vendor/spomky-labs/cbor-php/src/Tag/IdentifierTag.php
vendored
Normal file
45
vendor/spomky-labs/cbor-php/src/Tag/IdentifierTag.php
vendored
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
|
||||
/**
|
||||
* Tag 39: marks the item it wraps as an identifier -- a name to be compared, not a value to be interpreted.
|
||||
*
|
||||
* @see \CBOR\Test\Tag\RegistryTagsTest
|
||||
*/
|
||||
final class IdentifierTag extends Tag implements Normalizable
|
||||
{
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_IDENTIFIER;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_IDENTIFIER);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
/**
|
||||
* The tag says something about the item it wraps rather than about its value, so the item is normalized on
|
||||
* its own. Use getValue() when the tag itself matters.
|
||||
*/
|
||||
public function normalize(): mixed
|
||||
{
|
||||
$object = $this->object;
|
||||
|
||||
return $object instanceof Normalizable ? $object->normalize() : $object;
|
||||
}
|
||||
}
|
||||
58
vendor/spomky-labs/cbor-php/src/Tag/IpldContentIdentifierTag.php
vendored
Normal file
58
vendor/spomky-labs/cbor-php/src/Tag/IpldContentIdentifierTag.php
vendored
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\ByteStringObject;
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthByteStringObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Tag 42: an IPLD content identifier (CID), as used by IPFS and other content addressed systems.
|
||||
*
|
||||
* The byte string is a multibase prefixed CID: the leading 0x00 identity byte followed by the binary CID itself.
|
||||
* Neither the prefix nor the multihash inside it is interpreted here -- decoding them belongs to a CID library --
|
||||
* so the value is returned as the raw bytes it holds.
|
||||
*
|
||||
* @see \CBOR\Test\Tag\RegistryTagsTest
|
||||
*/
|
||||
final class IpldContentIdentifierTag extends Tag implements Normalizable
|
||||
{
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! $object instanceof ByteStringObject && ! $object instanceof IndefiniteLengthByteStringObject) {
|
||||
throw new InvalidArgumentException('This tag only accepts a Byte String object.');
|
||||
}
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_IPLD_CONTENT_IDENTIFIER;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_IPLD_CONTENT_IDENTIFIER);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
public function normalize(): string
|
||||
{
|
||||
/** @var ByteStringObject|IndefiniteLengthByteStringObject $object */
|
||||
$object = $this->object;
|
||||
|
||||
return $object->normalize();
|
||||
}
|
||||
}
|
||||
58
vendor/spomky-labs/cbor-php/src/Tag/Ipv4Tag.php
vendored
Normal file
58
vendor/spomky-labs/cbor-php/src/Tag/Ipv4Tag.php
vendored
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\ByteStringObject;
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\Tag;
|
||||
use function inet_pton;
|
||||
use InvalidArgumentException;
|
||||
use function strlen;
|
||||
|
||||
/**
|
||||
* Tag 52: an IPv4 address, prefix or zoned address (RFC 9164).
|
||||
*
|
||||
* @see \CBOR\Test\Tag\IpAddressTagTest
|
||||
* @see https://datatracker.ietf.org/doc/html/rfc9164
|
||||
*/
|
||||
final class Ipv4Tag extends AbstractIpAddressTag
|
||||
{
|
||||
private const ADDRESS_LENGTH = 4;
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_IPV4;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_IPV4);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the tag from the dotted quad form.
|
||||
*/
|
||||
public static function createFromAddress(string $address): self
|
||||
{
|
||||
$bytes = inet_pton($address);
|
||||
if ($bytes === false || strlen($bytes) !== self::ADDRESS_LENGTH) {
|
||||
throw new InvalidArgumentException('Invalid data. The value is not a valid IPv4 address.');
|
||||
}
|
||||
|
||||
return self::create(ByteStringObject::create($bytes));
|
||||
}
|
||||
|
||||
protected static function getAddressLength(): int
|
||||
{
|
||||
return self::ADDRESS_LENGTH;
|
||||
}
|
||||
}
|
||||
58
vendor/spomky-labs/cbor-php/src/Tag/Ipv6Tag.php
vendored
Normal file
58
vendor/spomky-labs/cbor-php/src/Tag/Ipv6Tag.php
vendored
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\ByteStringObject;
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\Tag;
|
||||
use function inet_pton;
|
||||
use InvalidArgumentException;
|
||||
use function strlen;
|
||||
|
||||
/**
|
||||
* Tag 54: an IPv6 address, prefix or zoned address (RFC 9164).
|
||||
*
|
||||
* @see \CBOR\Test\Tag\IpAddressTagTest
|
||||
* @see https://datatracker.ietf.org/doc/html/rfc9164
|
||||
*/
|
||||
final class Ipv6Tag extends AbstractIpAddressTag
|
||||
{
|
||||
private const ADDRESS_LENGTH = 16;
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_IPV6;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_IPV6);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the tag from the colon separated form.
|
||||
*/
|
||||
public static function createFromAddress(string $address): self
|
||||
{
|
||||
$bytes = inet_pton($address);
|
||||
if ($bytes === false || strlen($bytes) !== self::ADDRESS_LENGTH) {
|
||||
throw new InvalidArgumentException('Invalid data. The value is not a valid IPv6 address.');
|
||||
}
|
||||
|
||||
return self::create(ByteStringObject::create($bytes));
|
||||
}
|
||||
|
||||
protected static function getAddressLength(): int
|
||||
{
|
||||
return self::ADDRESS_LENGTH;
|
||||
}
|
||||
}
|
||||
81
vendor/spomky-labs/cbor-php/src/Tag/LanguageIndependentObjectTag.php
vendored
Normal file
81
vendor/spomky-labs/cbor-php/src/Tag/LanguageIndependentObjectTag.php
vendored
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthListObject;
|
||||
use CBOR\IndefiniteLengthTextStringObject;
|
||||
use CBOR\ListObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
use CBOR\TextStringObject;
|
||||
use function count;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Tag 27: a serialised language-independent object, encoded as an array whose first item is the type name and
|
||||
* whose remaining items are the arguments its constructor was called with.
|
||||
*
|
||||
* Nothing is instantiated here, and nothing should be: rebuilding an object graph from an untrusted document is
|
||||
* how deserialization vulnerabilities happen. The array is returned as data.
|
||||
*
|
||||
* @see \CBOR\Test\Tag\RegistryTagsTest
|
||||
*/
|
||||
final class LanguageIndependentObjectTag extends Tag implements Normalizable
|
||||
{
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! $object instanceof ListObject && ! $object instanceof IndefiniteLengthListObject) {
|
||||
throw new InvalidArgumentException('This tag only accepts a List object.');
|
||||
}
|
||||
if (count($object) < 1) {
|
||||
throw new InvalidArgumentException('This tag only accepts a List object that contains at least 1 item.');
|
||||
}
|
||||
$name = $object->get(0);
|
||||
if (! $name instanceof TextStringObject && ! $name instanceof IndefiniteLengthTextStringObject) {
|
||||
throw new InvalidArgumentException('Invalid type name. Expected a Text String object.');
|
||||
}
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_LANGUAGE_INDEPENDENT_OBJECT;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_LANGUAGE_INDEPENDENT_OBJECT);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
public function getTypeName(): string
|
||||
{
|
||||
/** @var IndefiniteLengthListObject|ListObject $object */
|
||||
$object = $this->object;
|
||||
/** @var IndefiniteLengthTextStringObject|TextStringObject $name */
|
||||
$name = $object->get(0);
|
||||
|
||||
return $name->normalize();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, mixed> the type name followed by the constructor arguments
|
||||
*/
|
||||
public function normalize(): array
|
||||
{
|
||||
/** @var IndefiniteLengthListObject|ListObject $object */
|
||||
$object = $this->object;
|
||||
|
||||
return $object->normalize();
|
||||
}
|
||||
}
|
||||
96
vendor/spomky-labs/cbor-php/src/Tag/LanguageTaggedStringTag.php
vendored
Normal file
96
vendor/spomky-labs/cbor-php/src/Tag/LanguageTaggedStringTag.php
vendored
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthListObject;
|
||||
use CBOR\IndefiniteLengthTextStringObject;
|
||||
use CBOR\ListObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
use CBOR\TextStringObject;
|
||||
use function count;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Tag 38: a string together with the language it is written in, as the two element array [language, text].
|
||||
*
|
||||
* The language is a BCP 47 tag such as "fr-CA". It is carried as written rather than parsed: matching and
|
||||
* fallback are the application's business.
|
||||
*
|
||||
* @see \CBOR\Test\Tag\RegistryTagsTest
|
||||
*/
|
||||
final class LanguageTaggedStringTag extends Tag implements Normalizable
|
||||
{
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! $object instanceof ListObject && ! $object instanceof IndefiniteLengthListObject) {
|
||||
throw new InvalidArgumentException('This tag only accepts a List object.');
|
||||
}
|
||||
if (count($object) !== 2) {
|
||||
throw new InvalidArgumentException('This tag only accepts a List object that contains 2 items.');
|
||||
}
|
||||
foreach ([$object->get(0), $object->get(1)] as $item) {
|
||||
if (! $item instanceof TextStringObject && ! $item instanceof IndefiniteLengthTextStringObject) {
|
||||
throw new InvalidArgumentException('This tag only accepts Text String objects.');
|
||||
}
|
||||
}
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_LANGUAGE_TAGGED_STRING;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_LANGUAGE_TAGGED_STRING);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
public static function createFromLanguageAndText(string $language, string $text): self
|
||||
{
|
||||
return self::create(ListObject::create([
|
||||
TextStringObject::create($language),
|
||||
TextStringObject::create($text),
|
||||
]));
|
||||
}
|
||||
|
||||
public function getLanguage(): string
|
||||
{
|
||||
return $this->textAt(0);
|
||||
}
|
||||
|
||||
public function getText(): string
|
||||
{
|
||||
return $this->textAt(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{0: string, 1: string} the language tag and the text, in the order they are encoded
|
||||
*/
|
||||
public function normalize(): array
|
||||
{
|
||||
return [$this->getLanguage(), $this->getText()];
|
||||
}
|
||||
|
||||
private function textAt(int $index): string
|
||||
{
|
||||
/** @var IndefiniteLengthListObject|ListObject $object */
|
||||
$object = $this->object;
|
||||
/** @var IndefiniteLengthTextStringObject|TextStringObject $item */
|
||||
$item = $object->get($index);
|
||||
|
||||
return $item->normalize();
|
||||
}
|
||||
}
|
||||
52
vendor/spomky-labs/cbor-php/src/Tag/MimeTag.php
vendored
Normal file
52
vendor/spomky-labs/cbor-php/src/Tag/MimeTag.php
vendored
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthTextStringObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
use CBOR\TextStringObject;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* @see \CBOR\Test\Tag\MimeTagTest
|
||||
*/
|
||||
final class MimeTag extends Tag implements Normalizable
|
||||
{
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! $object instanceof TextStringObject && ! $object instanceof IndefiniteLengthTextStringObject) {
|
||||
throw new InvalidArgumentException('This tag only accepts a Byte String object.');
|
||||
}
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_MIME;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): Tag
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_MIME);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
public function normalize(): string
|
||||
{
|
||||
/** @var TextStringObject|IndefiniteLengthTextStringObject $object */
|
||||
$object = $this->object;
|
||||
|
||||
return $object->normalize();
|
||||
}
|
||||
}
|
||||
89
vendor/spomky-labs/cbor-php/src/Tag/NegativeBigIntegerTag.php
vendored
Normal file
89
vendor/spomky-labs/cbor-php/src/Tag/NegativeBigIntegerTag.php
vendored
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use Brick\Math\BigInteger;
|
||||
use CBOR\ByteStringObject;
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthByteStringObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
use CBOR\Utils;
|
||||
use InvalidArgumentException;
|
||||
use function sprintf;
|
||||
use function strlen;
|
||||
|
||||
final class NegativeBigIntegerTag extends Tag implements Normalizable
|
||||
{
|
||||
/**
|
||||
* The maximum length, in bytes, accepted for the wrapped byte string.
|
||||
*
|
||||
* Turning the big-endian byte string into the decimal string this tag normalizes to is a base conversion, and
|
||||
* brick/math performs it in time quadratic in the length on every calculator but GMP. ext-gmp and ext-bcmath are
|
||||
* only suggested by this package, so the slowest of the three is what a default installation runs: a 500 byte
|
||||
* big number already cost 1.8 s there, and 2 kB close to a hundred. Since a big number used as a map key is
|
||||
* normalized by decode() itself, that was reachable without the application ever asking for the value. 256 bytes
|
||||
* is a 2048 bit integer, past any number this tag is used to carry, and bounds the conversion to well under a
|
||||
* second on the slowest calculator.
|
||||
*/
|
||||
public const MAX_BYTE_LENGTH = 256;
|
||||
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! $object instanceof ByteStringObject && ! $object instanceof IndefiniteLengthByteStringObject) {
|
||||
throw new InvalidArgumentException('This tag only accepts a Byte String object.');
|
||||
}
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_NEGATIVE_BIG_NUM;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): Tag
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_NEGATIVE_BIG_NUM);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
public function normalize(): string
|
||||
{
|
||||
/** @var ByteStringObject|IndefiniteLengthByteStringObject $object */
|
||||
$object = $this->object;
|
||||
$value = $object->getValue();
|
||||
self::assertLengthIsWithinBounds($value);
|
||||
// RFC 8949 section 3.4.3: the empty byte string is the preferred serialization of zero, so tag 3 wrapping
|
||||
// it is -1 - 0.
|
||||
$integer = $value === '' ? BigInteger::zero() : Utils::hexToBigInteger(bin2hex($value));
|
||||
$minusOne = BigInteger::of(-1);
|
||||
|
||||
return $minusOne->minus($integer)
|
||||
->toBase(10)
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value the big-endian byte string this tag wraps
|
||||
*/
|
||||
private static function assertLengthIsWithinBounds(string $value): void
|
||||
{
|
||||
$length = strlen($value);
|
||||
if ($length > self::MAX_BYTE_LENGTH) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'The big number is out of range. Its byte string shall not exceed %d bytes, got %d.',
|
||||
self::MAX_BYTE_LENGTH,
|
||||
$length
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
68
vendor/spomky-labs/cbor-php/src/Tag/NetworkAddressPrefixTag.php
vendored
Normal file
68
vendor/spomky-labs/cbor-php/src/Tag/NetworkAddressPrefixTag.php
vendored
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthMapObject;
|
||||
use CBOR\MapObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
use function count;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Tag 261: a network address prefix, given as the single entry map {address bytes: prefix length}.
|
||||
*
|
||||
* As with tag 260, RFC 9164 supersedes it with the prefix form of tags 52 and 54. It is kept for the documents
|
||||
* that predate the RFC.
|
||||
*
|
||||
* @see \CBOR\Test\Tag\IpAddressTagTest
|
||||
* @see https://datatracker.ietf.org/doc/html/rfc9164#appendix-A
|
||||
*/
|
||||
final class NetworkAddressPrefixTag extends Tag implements Normalizable
|
||||
{
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! $object instanceof MapObject && ! $object instanceof IndefiniteLengthMapObject) {
|
||||
throw new InvalidArgumentException('This tag only accepts a Map object.');
|
||||
}
|
||||
if (count($object) !== 1) {
|
||||
throw new InvalidArgumentException('This tag only accepts a Map object that contains 1 item.');
|
||||
}
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_NETWORK_ADDRESS_PREFIX;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_NETWORK_ADDRESS_PREFIX);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
/**
|
||||
* The key of the single entry is the address, in the same raw byte form NetworkAddressTag carries, so it is
|
||||
* returned as it stands rather than printed: a binary map key has no textual form to fall back on.
|
||||
*
|
||||
* @return array<int|string, mixed>
|
||||
*/
|
||||
public function normalize(): array
|
||||
{
|
||||
/** @var IndefiniteLengthMapObject|MapObject $object */
|
||||
$object = $this->object;
|
||||
|
||||
return $object->normalize();
|
||||
}
|
||||
}
|
||||
87
vendor/spomky-labs/cbor-php/src/Tag/NetworkAddressTag.php
vendored
Normal file
87
vendor/spomky-labs/cbor-php/src/Tag/NetworkAddressTag.php
vendored
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use function bin2hex;
|
||||
use CBOR\ByteStringObject;
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthByteStringObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
use function implode;
|
||||
use function in_array;
|
||||
use function inet_ntop;
|
||||
use InvalidArgumentException;
|
||||
use function str_split;
|
||||
use function strlen;
|
||||
|
||||
/**
|
||||
* Tag 260: a network address, given as its bytes -- 4 for IPv4, 16 for IPv6, 6 for a MAC address.
|
||||
*
|
||||
* This is the earlier, length-discriminated form. RFC 9164 replaced it with tags 52 and 54, which name the
|
||||
* family in the tag number instead, and this one is kept because documents that predate the RFC still use it.
|
||||
* Prefer Ipv4Tag and Ipv6Tag for anything new.
|
||||
*
|
||||
* @see \CBOR\Test\Tag\IpAddressTagTest
|
||||
* @see https://datatracker.ietf.org/doc/html/rfc9164#appendix-A
|
||||
*/
|
||||
final class NetworkAddressTag extends Tag implements Normalizable
|
||||
{
|
||||
private const MAC_LENGTH = 6;
|
||||
|
||||
private const IPV4_LENGTH = 4;
|
||||
|
||||
private const IPV6_LENGTH = 16;
|
||||
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! $object instanceof ByteStringObject && ! $object instanceof IndefiniteLengthByteStringObject) {
|
||||
throw new InvalidArgumentException('This tag only accepts a Byte String object.');
|
||||
}
|
||||
if (! in_array($object->getLength(), [self::IPV4_LENGTH, self::MAC_LENGTH, self::IPV6_LENGTH], true)) {
|
||||
throw new InvalidArgumentException('This tag only accepts a 4, 6 or 16 byte Byte String object.');
|
||||
}
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_NETWORK_ADDRESS;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_NETWORK_ADDRESS);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string a dotted quad, a colon separated IPv6 address, or a colon separated MAC address
|
||||
*/
|
||||
public function normalize(): string
|
||||
{
|
||||
/** @var ByteStringObject|IndefiniteLengthByteStringObject $object */
|
||||
$object = $this->object;
|
||||
$value = $object->normalize();
|
||||
|
||||
if (strlen($value) === self::MAC_LENGTH) {
|
||||
return implode(':', str_split(bin2hex($value), 2));
|
||||
}
|
||||
|
||||
$address = inet_ntop($value);
|
||||
if ($address === false) {
|
||||
throw new InvalidArgumentException('Invalid address. Cannot be converted into a textual address.');
|
||||
}
|
||||
|
||||
return $address;
|
||||
}
|
||||
}
|
||||
62
vendor/spomky-labs/cbor-php/src/Tag/PeriodTag.php
vendored
Normal file
62
vendor/spomky-labs/cbor-php/src/Tag/PeriodTag.php
vendored
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthListObject;
|
||||
use CBOR\ListObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Tag 1003: a period, i.e. a stretch of the timeline delimited by two of the time values RFC 9581 defines.
|
||||
*
|
||||
* The array holds a start and an end, a start and a duration, or one of the two and a null standing for an open
|
||||
* end. Which of those it is depends on the items themselves, and the items may be extended times (tag 1001) that
|
||||
* this library deliberately leaves unflattened, so the array is returned as it stands.
|
||||
*
|
||||
* @see \CBOR\Test\Tag\RegistryTagsTest
|
||||
* @see https://datatracker.ietf.org/doc/html/rfc9581#section-5
|
||||
*/
|
||||
final class PeriodTag extends Tag implements Normalizable
|
||||
{
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! $object instanceof ListObject && ! $object instanceof IndefiniteLengthListObject) {
|
||||
throw new InvalidArgumentException('This tag only accepts a List object.');
|
||||
}
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_PERIOD;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_PERIOD);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, mixed>
|
||||
*/
|
||||
public function normalize(): array
|
||||
{
|
||||
/** @var IndefiniteLengthListObject|ListObject $object */
|
||||
$object = $this->object;
|
||||
|
||||
return $object->normalize();
|
||||
}
|
||||
}
|
||||
81
vendor/spomky-labs/cbor-php/src/Tag/PerlObjectTag.php
vendored
Normal file
81
vendor/spomky-labs/cbor-php/src/Tag/PerlObjectTag.php
vendored
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CBOR\Tag;
|
||||
|
||||
use CBOR\CBORObject;
|
||||
use CBOR\IndefiniteLengthListObject;
|
||||
use CBOR\IndefiniteLengthTextStringObject;
|
||||
use CBOR\ListObject;
|
||||
use CBOR\Normalizable;
|
||||
use CBOR\Tag;
|
||||
use CBOR\TextStringObject;
|
||||
use function count;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Tag 26: a serialised Perl object, encoded as an array whose first item is the class name and whose remaining
|
||||
* items are the arguments its constructor was called with.
|
||||
*
|
||||
* Nothing is instantiated here, and nothing should be: rebuilding an object graph from an untrusted document is
|
||||
* how deserialization vulnerabilities happen. The array is returned as data.
|
||||
*
|
||||
* @see \CBOR\Test\Tag\RegistryTagsTest
|
||||
*/
|
||||
final class PerlObjectTag extends Tag implements Normalizable
|
||||
{
|
||||
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
|
||||
{
|
||||
if (! $object instanceof ListObject && ! $object instanceof IndefiniteLengthListObject) {
|
||||
throw new InvalidArgumentException('This tag only accepts a List object.');
|
||||
}
|
||||
if (count($object) < 1) {
|
||||
throw new InvalidArgumentException('This tag only accepts a List object that contains at least 1 item.');
|
||||
}
|
||||
$name = $object->get(0);
|
||||
if (! $name instanceof TextStringObject && ! $name instanceof IndefiniteLengthTextStringObject) {
|
||||
throw new InvalidArgumentException('Invalid class name. Expected a Text String object.');
|
||||
}
|
||||
|
||||
parent::__construct($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function getTagId(): int
|
||||
{
|
||||
return self::TAG_PERL_OBJECT;
|
||||
}
|
||||
|
||||
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
|
||||
{
|
||||
return new self($additionalInformation, $data, $object);
|
||||
}
|
||||
|
||||
public static function create(CBORObject $object): self
|
||||
{
|
||||
[$ai, $data] = self::determineComponents(self::TAG_PERL_OBJECT);
|
||||
|
||||
return new self($ai, $data, $object);
|
||||
}
|
||||
|
||||
public function getClassName(): string
|
||||
{
|
||||
/** @var IndefiniteLengthListObject|ListObject $object */
|
||||
$object = $this->object;
|
||||
/** @var IndefiniteLengthTextStringObject|TextStringObject $name */
|
||||
$name = $object->get(0);
|
||||
|
||||
return $name->normalize();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, mixed> the class name followed by the constructor arguments
|
||||
*/
|
||||
public function normalize(): array
|
||||
{
|
||||
/** @var IndefiniteLengthListObject|ListObject $object */
|
||||
$object = $this->object;
|
||||
|
||||
return $object->normalize();
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue