diff --git a/.github/workflows/actions.lock b/.github/workflows/actions.lock new file mode 100644 index 000000000..acbdeff60 --- /dev/null +++ b/.github/workflows/actions.lock @@ -0,0 +1,188 @@ +# This file is machine-generated by `gh actions-lock`. +# Do not edit by hand; run `gh actions-lock` to update. +# Docs: https://gh.io/actions-lockfile +version: 'v0.0.3' +workflows: + '.github/workflows/ai-core.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/atomic.yml': + - 'actions/checkout@v4.4.0' + - 'actions/checkout@v7.0.1' + - 'dtolnay/rust-toolchain@stable' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/audio.yml': + - 'actions/cache@v6.1.0' + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + '.github/workflows/cms.yml': + - 'actions/checkout@v4.4.0' + - 'dtolnay/rust-toolchain@stable' + '.github/workflows/crw.yml': + - 'actions/cache@v6.1.0' + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + '.github/workflows/dialogs.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/doc.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/docpath.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/ecproj-format.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/font-store.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/formats.yml': + - 'actions/checkout@v7.0.1' + - 'dtolnay/rust-toolchain@stable' + - 'step-security/harden-runner@v2.22.4' + - 'swatinem/rust-cache@v2.9.3' + '.github/workflows/house-rules.yml': [] + '.github/workflows/i18n.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/jpeg.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + '.github/workflows/keymap.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + '.github/workflows/lockfile.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/media.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + '.github/workflows/menu.yml': + - 'actions/checkout@v7.0.1' + - 'dtolnay/rust-toolchain@stable' + - 'swatinem/rust-cache@v2.9.3' + '.github/workflows/pcraft-format.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/pdf.yml': + - 'actions/checkout@v7.0.1' + - 'actions/setup-python@v7.1.0' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + - 'step-security/taiki-e-install-action@v2.87.32' + '.github/workflows/print.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/proof.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + '.github/workflows/sonarcloud.yml': [] + '.github/workflows/ui.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + '.github/workflows/update.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/vectorcraft-format.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/video.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + '.github/workflows/web-api.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/taiki-e-install-action@v2.87.32' + '.github/workflows/web-host.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + - 'step-security/rust-cache@v2.9.2' + - 'step-security/taiki-e-install-action@v2.87.32' + '.github/workflows/window.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/dtolnay-rust-toolchain@v1.0.0' + - 'step-security/harden-runner@v2.22.4' + '.github/workflows/workspace.yml': [] +dependencies: + 'actions/cache@v6.1.0': + ref: 'v6.1.0' + commit: 'sha1-55cc8345863c7cc4c66a329aec7e433d2d1c52a9' + owner_id: 44036562 + repo_id: 215566462 + 'actions/checkout@v4.4.0': + ref: 'v4.4.0' + commit: 'sha1-11d5960a326750d5838078e36cf38b85af677262' + owner_id: 44036562 + repo_id: 197814629 + 'actions/checkout@v7.0.1': + ref: 'v7.0.1' + commit: 'sha1-3d3c42e5aac5ba805825da76410c181273ba90b1' + owner_id: 44036562 + repo_id: 197814629 + 'actions/setup-python@v7.1.0': + ref: 'v7.1.0' + commit: 'sha1-6d4fd22b20465ccc0f7354476588e8df7c9ca0e0' + owner_id: 44036562 + repo_id: 192625525 + 'dtolnay/rust-toolchain@stable': + ref: 'stable' + commit: 'sha1-b725eda525362e7cf34af9a7243cac1e3f01a7ec' + owner_id: 1940490 + repo_id: 260749683 + 'step-security/dtolnay-rust-toolchain@v1.0.0': + ref: 'v1.0.0' + commit: 'sha1-8dd607977d06adf4ed49b190381e74c17a9dd25f' + owner_id: 88700172 + repo_id: 1249865941 + 'step-security/harden-runner@v2.22.4': + ref: 'v2.22.4' + commit: 'sha1-1d88384fd173af6ecb012dfd0264a306b711241a' + owner_id: 88700172 + repo_id: 422287306 + 'step-security/rust-cache@v2.9.2': + ref: 'v2.9.2' + commit: 'sha1-8871c8ba063416da7c32246932ea6ae08700453e' + owner_id: 88700172 + repo_id: 822636713 + 'step-security/taiki-e-install-action@v2.87.32': + ref: 'v2.87.32' + commit: 'sha1-76747d22a7567cc4438546a84f11bdf18e670ab8' + owner_id: 88700172 + repo_id: 1382753260 + 'swatinem/rust-cache@v2.9.3': + ref: 'v2.9.3' + commit: 'sha1-d1527c8f67ef9af357290475d4af4b0fab8f12e0' + owner_id: 580492 + repo_id: 298565987 diff --git a/.github/workflows/ai-core.yml b/.github/workflows/ai-core.yml index cc8d738ac..5471bf1ec 100644 --- a/.github/workflows/ai-core.yml +++ b/.github/workflows/ai-core.yml @@ -28,11 +28,11 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/atomic.yml b/.github/workflows/atomic.yml index 1a20fa679..f72a64f1e 100644 --- a/.github/workflows/atomic.yml +++ b/.github/workflows/atomic.yml @@ -28,11 +28,11 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy @@ -50,7 +50,7 @@ jobs: name: Atomic WASM runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v4.4.0 - uses: dtolnay/rust-toolchain@stable with: targets: wasm32-unknown-unknown diff --git a/.github/workflows/audio.yml b/.github/workflows/audio.yml index 0517efe6f..d6a7ff5e2 100644 --- a/.github/workflows/audio.yml +++ b/.github/workflows/audio.yml @@ -25,18 +25,18 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-audio-lint save-if: ${{ github.ref == 'refs/heads/main' }} @@ -69,15 +69,15 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false # The workspace's rust-version. - run: rustup toolchain install 1.95.0 --profile minimal - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-audio-msrv save-if: ${{ github.ref == 'refs/heads/main' }} @@ -88,16 +88,16 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-audio-features save-if: ${{ github.ref == 'refs/heads/main' }} @@ -113,20 +113,20 @@ jobs: runs-on: ubuntu-26.04 steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-audio-test save-if: ${{ github.ref == 'refs/heads/main' }} - - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + - uses: actions/cache@v6.1.0 with: path: ${{ runner.temp }}/media-fixtures/opus/vectors # The archive's SHA-256 (scripts/fetch-opus-vectors.sh checks it). diff --git a/.github/workflows/cms.yml b/.github/workflows/cms.yml index a15a48c9d..74ce0815f 100644 --- a/.github/workflows/cms.yml +++ b/.github/workflows/cms.yml @@ -16,7 +16,7 @@ jobs: timeout-minutes: 20 runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v4.4.0 - uses: dtolnay/rust-toolchain@stable with: components: clippy diff --git a/.github/workflows/crw.yml b/.github/workflows/crw.yml index 8fec98661..54be7ba3b 100644 --- a/.github/workflows/crw.yml +++ b/.github/workflows/crw.yml @@ -29,17 +29,17 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-crw-${{ matrix.os }} save-if: ${{ github.ref == 'refs/heads/main' }} @@ -53,17 +53,17 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-crw-wasm save-if: ${{ github.ref == 'refs/heads/main' }} @@ -75,21 +75,21 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-crw-samples save-if: ${{ github.ref == 'refs/heads/main' }} - name: Cache the CC0 sample files - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + uses: actions/cache@v6.1.0 with: path: ${{ runner.temp }}/crw-samples key: crw-samples-${{ hashFiles('crates/crw/tests/samples.json') }} diff --git a/.github/workflows/dialogs.yml b/.github/workflows/dialogs.yml index 97a931aec..c8d43d725 100644 --- a/.github/workflows/dialogs.yml +++ b/.github/workflows/dialogs.yml @@ -23,15 +23,15 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: # The token isn't needed after checkout; cargo runs build scripts and proc macros from # dependencies, so don't leave it in .git/config for that third-party code to read. persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: clippy diff --git a/.github/workflows/doc.yml b/.github/workflows/doc.yml index a0c619c1e..6caa4f0ee 100644 --- a/.github/workflows/doc.yml +++ b/.github/workflows/doc.yml @@ -28,13 +28,13 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/docpath.yml b/.github/workflows/docpath.yml index 571b65eca..e0115f6f5 100644 --- a/.github/workflows/docpath.yml +++ b/.github/workflows/docpath.yml @@ -28,11 +28,11 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/ecproj-format.yml b/.github/workflows/ecproj-format.yml index 16bab05c7..24a3350af 100644 --- a/.github/workflows/ecproj-format.yml +++ b/.github/workflows/ecproj-format.yml @@ -28,13 +28,13 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/font-store.yml b/.github/workflows/font-store.yml index f246f621a..0578f18ec 100644 --- a/.github/workflows/font-store.yml +++ b/.github/workflows/font-store.yml @@ -28,11 +28,11 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/formats.yml b/.github/workflows/formats.yml index e19902e8c..84d2c0d7e 100644 --- a/.github/workflows/formats.yml +++ b/.github/workflows/formats.yml @@ -72,17 +72,17 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable + - uses: dtolnay/rust-toolchain@stable with: components: rustfmt, clippy targets: wasm32-unknown-unknown - - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 + - uses: Swatinem/rust-cache@v2.9.3 with: key: ${{ matrix.crate }} save-if: ${{ github.ref == 'refs/heads/main' }} diff --git a/.github/workflows/i18n.yml b/.github/workflows/i18n.yml index 5f717aeff..7e6903188 100644 --- a/.github/workflows/i18n.yml +++ b/.github/workflows/i18n.yml @@ -28,11 +28,11 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/jpeg.yml b/.github/workflows/jpeg.yml index 900baf678..21db0ab79 100644 --- a/.github/workflows/jpeg.yml +++ b/.github/workflows/jpeg.yml @@ -25,18 +25,18 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-jpeg save-if: ${{ github.ref == 'refs/heads/main' }} diff --git a/.github/workflows/keymap.yml b/.github/workflows/keymap.yml index aba535265..41fcc0502 100644 --- a/.github/workflows/keymap.yml +++ b/.github/workflows/keymap.yml @@ -27,24 +27,24 @@ jobs: CRAFT_FONTS_DIR: ${{ github.workspace }}/.test-inputs/craft-fonts steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - name: Checkout test fonts - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + uses: actions/checkout@v7.0.1 with: repository: storytold/craft-fonts ref: b076417b5880bd4154bd22f00695b1d28c6bea83 path: .test-inputs/craft-fonts persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-keymap-linux save-if: ${{ github.ref == 'refs/heads/main' }} @@ -71,23 +71,23 @@ jobs: CRAFT_FONTS_DIR: ${{ github.workspace }}/.test-inputs/craft-fonts steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - name: Checkout test fonts - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + uses: actions/checkout@v7.0.1 with: repository: storytold/craft-fonts ref: b076417b5880bd4154bd22f00695b1d28c6bea83 path: .test-inputs/craft-fonts persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-keymap-${{ matrix.os }} save-if: ${{ github.ref == 'refs/heads/main' }} @@ -98,17 +98,17 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-keymap-wasm save-if: ${{ github.ref == 'refs/heads/main' }} diff --git a/.github/workflows/lockfile.yml b/.github/workflows/lockfile.yml new file mode 100644 index 000000000..e630c9a8f --- /dev/null +++ b/.github/workflows/lockfile.yml @@ -0,0 +1,34 @@ +name: Lockfile + +# Fails when .github/workflows/actions.lock is out of sync with the workflows, or when a locked pin +# no longer verifies upstream, so a moved tag cannot slip a different commit into a run. +# Docs: https://gh.io/actions-lockfile +on: + pull_request: + push: + branches: [main] + workflow_dispatch: + +permissions: + contents: read + +env: + GH_TOKEN: ${{ github.token }} + +jobs: + lockfile: + name: Lockfile is in sync + runs-on: ubuntu-24.04 + timeout-minutes: 10 + steps: + - name: Harden runner + uses: step-security/harden-runner@v2.22.4 + with: + egress-policy: audit + - uses: actions/checkout@v7.0.1 + with: + persist-credentials: false + - name: Install gh-actions-lock + run: gh extension install github/gh-actions-lock --pin v0.4.1 + - name: Verify the lockfile + run: gh actions-lock --verify diff --git a/.github/workflows/media.yml b/.github/workflows/media.yml index cb3912d54..4814e35ca 100644 --- a/.github/workflows/media.yml +++ b/.github/workflows/media.yml @@ -25,18 +25,18 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-media-lint save-if: ${{ github.ref == 'refs/heads/main' }} @@ -56,16 +56,16 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-media-test save-if: ${{ github.ref == 'refs/heads/main' }} diff --git a/.github/workflows/menu.yml b/.github/workflows/menu.yml index 6d0e082cb..8d1058731 100644 --- a/.github/workflows/menu.yml +++ b/.github/workflows/menu.yml @@ -21,14 +21,14 @@ jobs: name: Menu (Linux) runs-on: ubuntu-latest steps: - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable + - uses: dtolnay/rust-toolchain@stable with: components: rustfmt, clippy targets: aarch64-apple-darwin, wasm32-unknown-unknown - - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 + - uses: Swatinem/rust-cache@v2.9.3 with: key: craft-menu-linux - run: cargo fmt -p craft-menu --check diff --git a/.github/workflows/pcraft-format.yml b/.github/workflows/pcraft-format.yml index 177bb4593..9c66b5a10 100644 --- a/.github/workflows/pcraft-format.yml +++ b/.github/workflows/pcraft-format.yml @@ -28,13 +28,13 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: "1.95" components: rustfmt, clippy diff --git a/.github/workflows/pdf.yml b/.github/workflows/pdf.yml index a494d085c..09ddfabca 100644 --- a/.github/workflows/pdf.yml +++ b/.github/workflows/pdf.yml @@ -31,19 +31,19 @@ jobs: RUSTFLAGS: --cfg craft_pdf_unicode_fixtures steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-pdf save-if: ${{ github.ref == 'refs/heads/main' }} - - uses: actions/setup-python@v7 + - uses: actions/setup-python@v7.1.0 with: python-version: "3.12" - name: Provision licensed PDF test fonts @@ -61,14 +61,14 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-pdf-backends save-if: ${{ github.ref == 'refs/heads/main' }} @@ -81,19 +81,19 @@ jobs: CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER: wasm-bindgen-test-runner steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-pdf-wasm save-if: ${{ github.ref == 'refs/heads/main' }} - - uses: actions/setup-python@v7 + - uses: actions/setup-python@v7.1.0 with: python-version: "3.12" - name: Provision licensed PDF test fonts @@ -111,7 +111,7 @@ jobs: - name: Install the matching WASM test runner # A prebuilt, checksum-verified binary instead of building it from source on every run. # A version the pinned action does not list yet falls back to `cargo install --locked`. - uses: step-security/taiki-e-install-action@76747d22a7567cc4438546a84f11bdf18e670ab8 # v2.87.32 + uses: step-security/taiki-e-install-action@v2.87.32 with: tool: wasm-bindgen@${{ steps.wasm-bindgen.outputs.version }} fallback: cargo-install @@ -122,19 +122,19 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: clippy - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-plate-test save-if: ${{ github.ref == 'refs/heads/main' }} - - uses: actions/setup-python@v7 + - uses: actions/setup-python@v7.1.0 with: python-version: "3.12" - name: Provision licensed PDF test fonts diff --git a/.github/workflows/print.yml b/.github/workflows/print.yml index 3838e6c42..8e2108e09 100644 --- a/.github/workflows/print.yml +++ b/.github/workflows/print.yml @@ -24,11 +24,11 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/proof.yml b/.github/workflows/proof.yml index 0035b1c04..10365a230 100644 --- a/.github/workflows/proof.yml +++ b/.github/workflows/proof.yml @@ -25,27 +25,27 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false # The Language data (craft-i18n `language-data/`), embedded by crates/proof/build.rs. # Pinned to storytold/craft-i18n#114's head (#109's SCOWL dictionaries + judgment/judgement, # CP2.1); TODO(CP2.1): move to #114's merge commit once it merges. - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: repository: storytold/craft-i18n ref: c239f934ace90b7939afaceb23b472ceeafd6b8a path: craft-i18n persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-proof save-if: ${{ github.ref == 'refs/heads/main' }} diff --git a/.github/workflows/ui.yml b/.github/workflows/ui.yml index 0892f5a2c..f15ef6ddc 100644 --- a/.github/workflows/ui.yml +++ b/.github/workflows/ui.yml @@ -30,21 +30,21 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 - name: Install software Vulkan renderer if: runner.os == 'Linux' run: | sudo apt-get update sudo apt-get install -y mesa-vulkan-drivers libvulkan1 echo 'WGPU_BACKEND=vulkan' >> "$GITHUB_ENV" - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-ui save-if: ${{ github.ref == 'refs/heads/main' }} @@ -59,15 +59,15 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: actions/checkout@v7.0.1 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-ui-wasm save-if: ${{ github.ref == 'refs/heads/main' }} diff --git a/.github/workflows/update.yml b/.github/workflows/update.yml index f9c8e1498..9a7139026 100644 --- a/.github/workflows/update.yml +++ b/.github/workflows/update.yml @@ -28,15 +28,15 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: # The token isn't needed after checkout; cargo runs build scripts and proc macros from # dependencies, so don't leave it in .git/config for that third-party code to read. persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/.github/workflows/vectorcraft-format.yml b/.github/workflows/vectorcraft-format.yml index b2b20463a..74144f30f 100644 --- a/.github/workflows/vectorcraft-format.yml +++ b/.github/workflows/vectorcraft-format.yml @@ -28,13 +28,13 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: "1.95" components: rustfmt, clippy diff --git a/.github/workflows/video.yml b/.github/workflows/video.yml index ec1c16eda..fa667bdfc 100644 --- a/.github/workflows/video.yml +++ b/.github/workflows/video.yml @@ -22,18 +22,18 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: "1.95" components: rustfmt, clippy targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-video-lint - run: cargo fmt -p craft-video --check @@ -60,16 +60,16 @@ jobs: name: Video tests (${{ matrix.os }}) steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: "1.95" - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-video-${{ matrix.os }} - name: Install ffmpeg (Linux) diff --git a/.github/workflows/web-api.yml b/.github/workflows/web-api.yml index 2460fb569..4bf1a30fe 100644 --- a/.github/workflows/web-api.yml +++ b/.github/workflows/web-api.yml @@ -26,13 +26,13 @@ jobs: CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER: wasm-bindgen-test-runner steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy @@ -46,7 +46,7 @@ jobs: - name: Install the matching WASM test runner # A prebuilt, checksum-verified binary instead of building it from source on every run. # A version the pinned action does not list yet falls back to `cargo install --locked`. - uses: step-security/taiki-e-install-action@76747d22a7567cc4438546a84f11bdf18e670ab8 # v2.87.32 + uses: step-security/taiki-e-install-action@v2.87.32 with: tool: wasm-bindgen@${{ steps.wasm-bindgen.outputs.version }} fallback: cargo-install diff --git a/.github/workflows/web-host.yml b/.github/workflows/web-host.yml index b780ee76b..76e6701e5 100644 --- a/.github/workflows/web-host.yml +++ b/.github/workflows/web-host.yml @@ -27,18 +27,18 @@ jobs: CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER: wasm-bindgen-test-runner steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/checkout@v7.0.1 with: persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2 + - uses: step-security/rust-cache@v2.9.2 with: key: craft-web-host save-if: ${{ github.ref == 'refs/heads/main' }} @@ -55,7 +55,7 @@ jobs: - name: Install the matching WASM test runner # A prebuilt, checksum-verified binary instead of building it from source on every run. # A version the pinned action does not list yet falls back to `cargo install --locked`. - uses: step-security/taiki-e-install-action@76747d22a7567cc4438546a84f11bdf18e670ab8 # v2.87.32 + uses: step-security/taiki-e-install-action@v2.87.32 with: tool: wasm-bindgen@${{ steps.wasm-bindgen.outputs.version }} fallback: cargo-install diff --git a/.github/workflows/window.yml b/.github/workflows/window.yml index f570ef5ce..fdd28e203 100644 --- a/.github/workflows/window.yml +++ b/.github/workflows/window.yml @@ -28,15 +28,15 @@ jobs: runs-on: ${{ matrix.os }} steps: - name: Harden runner - uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + uses: step-security/harden-runner@v2.22.4 with: egress-policy: audit - - uses: actions/checkout@v7 + - uses: actions/checkout@v7.0.1 with: # The token isn't needed after checkout; cargo runs build scripts and proc macros from # dependencies, so don't leave it in .git/config for that third-party code to read. persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0 + - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable components: rustfmt, clippy diff --git a/Cargo.lock b/Cargo.lock index 1314c7745..1e45e7a3c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1857,18 +1857,22 @@ dependencies = [ name = "craft-image-codecs" version = "0.1.0" dependencies = [ + "avif-serialize", "craft-heif", "craft-jpeg", "craft-metadata", "exr", "flate2", "half", + "hayro-jpeg2000", "image", "image-webp", "jpeg-encoder 0.6.1", "jxl-oxide", "png 0.18.1", "proptest", + "rav1e", + "ravif", "rayon", "serde_json", "thiserror 2.0.21", @@ -2192,6 +2196,7 @@ dependencies = [ "bytemuck", "chrono", "craft-stream-codecs", + "craft-svg", "flate2", "hayro", "hayro-cmap", @@ -2199,6 +2204,7 @@ dependencies = [ "hayro-syntax", "js-sys", "krilla", + "krilla-svg", "kurbo 0.13.1", "md-5 0.11.0", "proptest", @@ -2208,6 +2214,7 @@ dependencies = [ "stringprep", "thiserror 2.0.21", "ttf-parser", + "usvg 0.47.0", "wasm-bindgen", "wasm-bindgen-test", "zeroize", @@ -2591,6 +2598,14 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "craft-texture" +version = "0.1.0" +dependencies = [ + "rayon", + "thiserror 2.0.21", +] + [[package]] name = "craft-tga" version = "0.1.0" @@ -2831,6 +2846,7 @@ name = "craft-xml" version = "0.1.0" dependencies = [ "quick-xml 0.41.0", + "serde", "thiserror 2.0.21", ] @@ -4064,6 +4080,20 @@ dependencies = [ "roxmltree 0.20.0", ] +[[package]] +name = "fontdb" +version = "0.23.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "457e789b3d1202543297a350643cf459f836cade38934e7a4cf6a39e7cde2905" +dependencies = [ + "fontconfig-parser", + "log", + "memmap2", + "slotmap", + "tinyvec", + "ttf-parser", +] + [[package]] name = "fontdb" version = "0.24.0" @@ -5659,6 +5689,22 @@ dependencies = [ "zune-jpeg", ] +[[package]] +name = "krilla-svg" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1237d7c37b16ca9fbc2e72dde13a10d321f9a44aceee35c062bc0a43bfc7ce16" +dependencies = [ + "flate2", + "fontdb 0.23.0", + "krilla", + "resvg 0.47.0", + "skrifa 0.42.1", + "smallvec", + "tiny-skia 0.12.0", + "usvg 0.47.0", +] + [[package]] name = "kurbo" version = "0.11.3" @@ -7875,6 +7921,23 @@ dependencies = [ "usvg 0.45.1", ] +[[package]] +name = "resvg" +version = "0.47.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9be183ad6a216aa96f33e4c8033b0988b8b3ea6fd2359d19af5bac4643fd8e81" +dependencies = [ + "gif", + "image-webp", + "log", + "pico-args", + "rgb", + "svgtypes 0.16.2", + "tiny-skia 0.12.0", + "usvg 0.47.0", + "zune-jpeg", +] + [[package]] name = "resvg" version = "0.48.1" @@ -9921,6 +9984,34 @@ dependencies = [ "xmlwriter", ] +[[package]] +name = "usvg" +version = "0.47.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d46cf96c5f498d36b7a9693bc6a7075c0bb9303189d61b2249b0dc3d309c07de" +dependencies = [ + "base64 0.22.1", + "data-url", + "flate2", + "fontdb 0.23.0", + "imagesize 0.14.0", + "kurbo 0.13.1", + "log", + "pico-args", + "roxmltree 0.21.2", + "rustybuzz", + "simplecss", + "siphasher", + "strict-num", + "svgtypes 0.16.2", + "tiny-skia-path 0.12.0", + "ttf-parser", + "unicode-bidi", + "unicode-script", + "unicode-vo", + "xmlwriter", +] + [[package]] name = "usvg" version = "0.48.1" @@ -9929,7 +10020,7 @@ checksum = "977d0a4abdef933f424a99fe09f95576e089b90aebc6f016a3bc813762493e91" dependencies = [ "base64 0.23.1", "data-url", - "fontdb", + "fontdb 0.24.0", "harfrust 0.12.0", "imagesize 0.15.0", "kurbo 0.13.1", diff --git a/Cargo.toml b/Cargo.toml index 38b82c843..576efca6f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -91,10 +91,10 @@ craft-keymap = { path = "crates/keymap" } craft-assuan = { path = "crates/assuan" } craft-kra = { path = "crates/kra" } craft-audio = { path = "crates/audio" } -# No default features, so craft-audio's `encode` builds craft-media without `isobmff`. craft-latex = { path = "crates/latex" } craft-markdown = { path = "crates/markdown" } craft-math = { path = "crates/math" } +# No default features: craft-audio turns on the craft-media modules each of its features needs. craft-media = { path = "crates/media", default-features = false } craft-materialx = { path = "crates/materialx" } craft-media-testkit = { path = "crates/media-testkit" } @@ -139,6 +139,7 @@ craft-stencilcraft-document = { path = "crates/stencilcraft-document" } craft-svg-paths = { path = "crates/svg-paths" } craft-stream-codecs = { path = "crates/stream-codecs" } craft-text = { path = "crates/text" } +craft-texture = { path = "crates/texture" } craft-tga = { path = "crates/tga" } craft-timeline = { path = "crates/timeline" } craft-tiff = { path = "crates/tiff" } diff --git a/NOTICE b/NOTICE index e1cffb26c..4fa5e4754 100644 --- a/NOTICE +++ b/NOTICE @@ -254,9 +254,10 @@ applications retain their own document-font selection and licensing policy. linesweeper linesweeper 0.5.0 (https://crates.io/crates/linesweeper/0.5.0), MIT OR Apache-2.0, is stored in -vendor/linesweeper with its upstream license files and one local patch. Its manifest names Joe Neeman -as author, and its LICENSE-MIT reads "Copyright (c) 2018 Raph Levien". See -vendor/linesweeper/README.craft.md and vendor/linesweeper/SOURCE.json for provenance, the local patch +vendor/linesweeper with its upstream license files and local patches, to src/sweep/sweep_line.rs and +docs/sweep.typ. Its manifest names Joe Neeman as author, and its LICENSE-MIT reads "Copyright (c) 2018 +Raph Levien". See vendor/linesweeper/README.craft.md and vendor/linesweeper/SOURCE.json for +provenance, the local patches and the files left out. Platform lifecycle source provenance diff --git a/README.md b/README.md index d094847d0..c6e95bd4f 100644 --- a/README.md +++ b/README.md @@ -348,6 +348,9 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit - [`craft-text`](crates/text/README.md): text processing below the composer. Its `hyphen` module finds hyphenation points, with a bundled US English dictionary and patterns and optional hypher patterns for 47 more languages (one cargo feature each). +- [`craft-texture`](crates/texture/README.md): linear float image planes, gray or RGBA, with + nearest and bilinear sampling that repeats or clamps, resizing and display conversion. Sizes are + bounded by `Limits`. From MaterialCraft. - [`craft-ui`](crates/ui/README.md): SVG catalogs, icon buttons, tabs, pane controls, transactional docking, print settings and theme helpers for egui frontends; assets, themes, localization and command handling stay in each app. Requires Rust 1.95 or later. - [`craft-vcard`](crates/vcard/README.md): vCard contacts, from MailCraft: vCard 2.1, 3.0 and 4.0 read diff --git a/crates/audio/Cargo.toml b/crates/audio/Cargo.toml index 63e55d232..6e673b39d 100644 --- a/crates/audio/Cargo.toml +++ b/crates/audio/Cargo.toml @@ -23,6 +23,7 @@ decode-native = ["aac-decode", "alac-decode", "opus-decode", "ac3-decode", "mpa- # The symphonia fallback for the formats with no native decoder yet. symphonia = ["decode-native", "dep:symphonia"] # Every encoder: the lossless file writers and the AAC, ALAC, AC-3/E-AC-3, MP3 and Opus encoders. +# All but Opus are also file formats of `encode` and `FileEncoder`. encode = ["lossless-encode", "aac-encode", "alac-encode", "ac3-encode", "eac3-encode", "mp3-encode", "opus-encode"] # Encode WAV/BWF/RF64, AIFF/AIFF-C and FLAC files. lossless-encode = ["pcm-encode", "flac-encode"] @@ -34,16 +35,18 @@ flac-encode = ["craft-media/bitstream"] vorbis-encode = ["dep:vorbis_rs"] # Codec modules on their own, for packet-level use. aac-decode = ["craft-media/bitstream"] -aac-encode = ["craft-media/bitstream"] +# The AAC encoder (`aac::Encoder`), and AAC in M4A through `encode` and `FileEncoder`. +aac-encode = ["craft-media/bitstream", "craft-media/isobmff"] opus-decode = [] opus-encode = [] ac3-decode = ["craft-media/bitstream"] mpa-decode = ["craft-media/bitstream"] # MPEG-1/2 Layer III (MP3) decoder (`mp3::Decoder`). mp3-decode = ["mpa-decode"] -# ALAC packet decoder and encoder (`alac`), ported from Apple's reference implementation. +# ALAC packet decoder and encoder (`alac`), ported from Apple's reference implementation. The +# encoder also writes ALAC in M4A through `encode` and `FileEncoder`. alac-decode = [] -alac-encode = [] +alac-encode = ["craft-media/isobmff"] ac3-encode = ["craft-media/bitstream"] eac3-encode = ["craft-media/bitstream"] # MPEG-1/2 Layer III (MP3) encoder (`mp3`). @@ -61,10 +64,12 @@ thiserror = { workspace = true } serde = { workspace = true, optional = true } log = { version = "0.4", optional = true } rayon = { workspace = true, optional = true } -vorbis_rs = { version = "0.5", optional = true } # symphonia keeps its AAC decoder for AAC in CAF, a container craft-media does not read. symphonia = { version = "0.6.1", optional = true, default-features = false, features = ["mp3", "flac", "vorbis", "ogg", "wav", "pcm", "aiff", "isomp4", "aac", "alac", "adpcm", "caf", "mkv"] } +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +vorbis_rs = { version = "0.5", optional = true } + [dev-dependencies] craft-media-testkit = { workspace = true } proptest = { workspace = true } diff --git a/crates/audio/README.md b/crates/audio/README.md index 518dbeb4c..5a5df4f2f 100644 --- a/crates/audio/README.md +++ b/crates/audio/README.md @@ -29,19 +29,19 @@ No default features. The shared types (`AudioBuffer`, `AudioInfo`, `ChannelLayou | `decode` | `decode`, `probe`, `Decoder`, `tracks`, `TrackDecoder`; every format in [Format support](#format-support) | `decode-native`, `symphonia` | | `decode-native` | the same, native formats only (others are `Unsupported`); `wav_pcm`, `aiff_pcm` (a file's integer PCM bit for bit, as `RawPcm`) | the six decoders below, craft-media `isobmff`/`matroska`/`ogg`, `log` | | `symphonia` | the fallback for formats with no native decoder; turning it on is the same as `decode` | `decode-native`, symphonia | -| `encode` | every encoder: the WAV, AIFF and FLAC file writers, and the AAC, ALAC, AC-3/E-AC-3, MP3 and Opus encoders | `lossless-encode`, `aac-encode`, `alac-encode`, `ac3-encode`, `eac3-encode`, `mp3-encode`, `opus-encode` | +| `encode` | every encoder, and every format of `encode` and `FileEncoder` | `lossless-encode`, `aac-encode`, `alac-encode`, `ac3-encode`, `eac3-encode`, `mp3-encode`, `opus-encode` | | `lossless-encode` | WAV, AIFF and FLAC writing | `pcm-encode`, `flac-encode` | | `pcm-encode` | `encode`, `FileEncoder` for WAV and AIFF; `wav_float_header`; `pcm_wav_file`, `pcm_wav_header`, `pcm_aiff_file`, `pcm_aiff_header` (minimal files around integer PCM bytes, for essence embedded in AAF / OMF) | none | | `flac-encode` | `encode`, `FileEncoder` for FLAC; `flac::Encoder`, the packet encoder for callers that mux FLAC themselves | craft-media `bitstream` | -| `aac-decode`, `aac-encode` | `aac::Decoder`, `aac::Encoder`; with `aac-decode`, `aac::latm_config` / `latm_split` for LOAS / LATM frames (AAC in MPEG transport streams) | craft-media `bitstream` | +| `aac-decode`, `aac-encode` | `aac::Decoder`, `aac::Encoder`; with `aac-decode`, `aac::latm_config` / `latm_split` for LOAS / LATM frames (AAC in MPEG transport streams); with `aac-encode`, `encode`, `FileEncoder` for AAC in M4A | craft-media `bitstream`; `aac-encode` also `isobmff` | | `alac-decode` | `alac::Decoder` | none | -| `alac-encode` | `alac::Encoder` | none | +| `alac-encode` | `alac::Encoder`; `encode`, `FileEncoder` for ALAC in M4A | craft-media `isobmff` | | `opus-decode`, `opus-encode` | `opus::Decoder`/`StreamDecoder`, `opus::Encoder` | none | | `ac3-decode` | `ac3::Decoder`, `ac3::ProgrammeDecoder` | craft-media `bitstream` | -| `ac3-encode`, `eac3-encode` | `ac3::encoder::Encoder` | craft-media `bitstream` | +| `ac3-encode`, `eac3-encode` | `ac3::encoder::Encoder`; `encode`, `FileEncoder` for AC-3 and E-AC-3 streams | craft-media `bitstream` | | `mpa-decode` | `mpa::Decoder` | craft-media `bitstream` | | `mp3-decode` | `mp3::Decoder`, `mp3::InfoTag` | `mpa-decode` | -| `mp3-encode` | `mp3::Encoder` | craft-media `bitstream` | +| `mp3-encode` | `mp3::Encoder`; `encode`, `FileEncoder` for MP3 | craft-media `bitstream` | | `peaks` | `peaks::Peaks` | none | | `rayon` | parallel AAC encoding (with `aac-encode`) | `rayon` | | `serde` | `Serialize`/`Deserialize` on the shared types | `serde` | @@ -230,17 +230,86 @@ let wav = encode(&surround, &EncodeOptions::default().with_layout(ChannelLayout: let mut enc = FileEncoder::new(std::fs::File::create("take.wav")?, &EncodeOptions::default(), 48_000, 2)?; enc.write(&buf.channels, buf.frames())?; // one slice (or Vec) per channel enc.finish()?; + +// The coded formats go the same way: AAC in M4A at 192 kbit/s, 5.1 as E-AC-3, 16-bit ALAC. +let m4a = encode(&buf, &EncodeOptions::new(EncodeFormat::Aac).with_bitrate(192_000))?; +let eac3 = encode(&surround, &EncodeOptions::new(EncodeFormat::Eac3).with_layout(ChannelLayout::SURROUND_5_1_SIDE))?; +let alac = encode(&buf, &EncodeOptions::new(EncodeFormat::Alac).with_bit_depth(BitDepth::Int16))?; ``` `EncodeOptions::default()` is 24-bit WAV. `with_bwf` adds a BWF `bext` chunk and `with_force_rf64` writes RF64 below 4 GiB; both apply to WAV only. `encode_format_for("aiff")` -maps an extension to an `EncodeFormat`, and `ENCODE_EXTENSIONS` lists the extensions the enabled -features can write. `EncodeFormat::ALL` lists the formats; `extension()` is the extension a new -file gets and `mime_type()` its media type. `write` refuses a block without one slice per channel, or with a slice -shorter than `frames`, and writes nothing. After a write fails for another reason, every later +maps an extension to an `EncodeFormat` (`"m4a"` to AAC; `"alac"` names ALAC), and `ENCODE_EXTENSIONS` lists the extensions the enabled +features can write. `EncodeFormat::ALL` lists the formats and `is_built()` says whether this build +writes one; `extension()` is the extension a new file gets and `mime_type()` its media type. +`write` refuses a block without one slice per channel, or with a slice shorter than `frames`, and +writes nothing. After a write fails for another reason, every later `write` and `finish` returns an error. -### Encode AAC +| `EncodeFormat` | File | Audio | Options used | +|---|---|---|---| +| `Wav`, `Aiff`, `Flac` | WAV / BWF / RF64, AIFF / AIFF-C, FLAC | any rate; FLAC up to 8 channels | `bit_depth`, `dither`, `layout` (WAV, FLAC), `bwf`, `force_rf64` (WAV) | +| `Alac` | M4A | 1–8 channels, any rate | `bit_depth` (16, 24, 32), `dither`, `layout` | +| `Aac` | M4A | AAC-LC, 1–8 channels, the 13 AAC rates (7350–96000 Hz) | `rate_control`, `layout` | +| `Mp3` | `.mp3` stream | mono or stereo; 32, 44.1, 48 kHz or 16, 22.05, 24 kHz | `rate_control` (bit rate), `layout` | +| `Ac3`, `Eac3` | `.ac3` / `.eac3` stream | up to 5.1 (E-AC-3: 7.1); 32, 44.1, 48 kHz | `rate_control` (bit rate), `layout` | + +`with_bitrate(bits_per_second)` sets a constant bit rate for all channels together. A rate the +stream cannot carry is moved to the nearest one it can: MP3 and AC-3 use the highest rate of their +tables not above it, AAC and E-AC-3 keep it inside their range (AAC from 6 kbit/s or a quarter of +the sample rate per channel, whichever is more: from about half of that down the encoder codes +silence). `with_quality(0.0..=1.0)` is AAC's +constant quality (VBR); the other lossy formats return `Unsupported` for it. Without either, AAC +is written at 64 kbit/s per channel, MP3 at 96 kbit/s per channel, and AC-3 and E-AC-3 at +96 kbit/s per full-bandwidth channel (the LFE not counted). + +The audio is in WAV channel order for every format; `layout` (the usual layout for the channel +count when unset) names the speakers, and the channels are reordered into the codec's own order. +Each coded format has a fixed set of layouts, and audio with other speakers is `Unsupported` +rather than written to the wrong ones: + +- AAC: C; L R; L R C; L R C Cb; L R C Ls Rs; 5.1; 6.1; and for 8 channels L R C LFE Lb Rb Lc Rc + (a front pair left and right of centre, mask `0xFF`). ALAC: the same, with 6.1's back centre last. + WAV 7.1 (backs and sides) is not that 7.1, and quad (the usual 4-channel layout) has no centre: + both need another format. +- AC-3 and E-AC-3: the layout picks the coding mode (1/0 to 3/2, with or without LFE; E-AC-3 also + 7.1 with backs and sides). +- A surround pair is a surround pair: side and back surrounds are interchangeable where the format + has one pair. + +Before a file is created, the options and the format say what will happen: + +```rust +let opts = EncodeOptions::new(EncodeFormat::Ac3).with_layout(info.layout).with_bitrate(200_000); +opts.check(info.sample_rate, info.channels)?; // the error FileEncoder::new would return, nothing written +let bps = opts.bitrate(info.sample_rate, info.channels)?; // Some(192_000): the rate actually used; None when lossless or by quality + +EncodeFormat::Ac3.sample_rates(); // Some(&[32_000, 44_100, 48_000]); None when any rate will do +EncodeFormat::Ac3.max_channels(); // 6 +EncodeFormat::Ac3.fixed_layouts(3); // L R C, L R Cb, L R LFE; None for WAV, AIFF and FLAC (any layout) +EncodeFormat::Ac3.supports_layout(&info.layout); // one of those, sides and backs standing for each other +EncodeFormat::Ac3.supports_quality(); // false: AAC only +``` + +`FileEncoder::bitrate()` gives the rate of a file being written. The `EncodeFormat` queries answer +for a format whose feature is off too; `check` and `EncodeOptions::bitrate` (there with any +encoder feature) return `Unsupported` for it. + +The layout `decode` reports (`AudioInfo::layout`) can be passed to `with_layout` as it is. When a +coded format refuses it (2.1 or quad into AAC, a single channel tagged as left, channels with no +speaker assignment), the choices are another format, or audio rearranged into one of the layouts +above with that layout set; leaving `layout` unset declares the usual layout for the count. This +crate's decoder returns 7- and 8-channel AAC without a speaker assignment, in AAC order, so such a +file needs that step before it is written again. + +An M4A's edit list skips the AAC encoder's priming (1024 frames) and ends at the last frame +written, so the file decodes to the length that went in. An MP3, AC-3 or E-AC-3 stream has no +place for that (see [Limits](#limits)). For a container of your own, use the codec modules below. + +### Encode AAC packets + +For an `.m4a` file, use `EncodeFormat::Aac` above. `aac::Encoder` is the packet encoder behind it, +for muxing into a container of your own: ```rust use craft_audio::AudioError; @@ -343,14 +412,14 @@ match craft_audio::decode(&bytes, None) { | WAV, BWF, RF64/BW64 | native | native | | AIFF, AIFF-C | native | native | | FLAC | symphonia | native | -| AAC-LC | native | native | +| AAC-LC | native | native; M4A through `encode` / `FileEncoder`, packets through `aac::Encoder` | | HE-AAC v1 | native (SBR) | — | | HE-AAC v2 | native, without parametric stereo (see [Limits](#limits)) | — | -| ALAC | native | native, packets (`alac::Encoder`; no file writer) | +| ALAC | native | native; M4A through `encode` / `FileEncoder`, packets through `alac::Encoder` | | MP1, MP2 (MPEG audio Layer I/II) | native | — | -| MP3 (Layer III) | native | native, frames (`mp3::Encoder`; the frames in a row are an `.mp3` file) | -| Opus | native | native | -| AC-3, E-AC-3 | native | native packet encoder | +| MP3 (Layer III) | native | native; `.mp3` through `encode` / `FileEncoder`, frames through `mp3::Encoder` | +| Opus | native | native, packets (`opus::Encoder`; no file writer) | +| AC-3, E-AC-3 | native | native; `.ac3` / `.eac3` through `encode` / `FileEncoder`, packets through `ac3::encoder::Encoder` | | Vorbis, ADPCM, A-law, µ-law | symphonia | — | | Container | Demuxer | Timing | @@ -410,10 +479,26 @@ match craft_audio::decode(&bytes, None) { - `TrackDecoder` returns `Unsupported` for FLAC and Vorbis; `decode` and `Decoder` read those through symphonia. ALAC in CAF still goes through symphonia (craft-media has no CAF demuxer). -- ALAC: `EncodeFormat` has no ALAC entry; `alac::Encoder` writes packets and the configuration, - and the caller muxes them (MP4 `alac` box, Matroska `A_ALAC`). craft-media's MP4 muxer writes no - `chan` box, so an MP4 of more than two channels carries no explicit layout; decoders then use the - standard ALAC layout for the count. +- ALAC: craft-media's MP4 muxer writes no `chan` box, so an M4A of more than two channels (from + `EncodeFormat::Alac` or muxed from `alac::Encoder` packets) carries no explicit layout; decoders + then use the standard ALAC layout for the count, which is the one written. +- `EncodeFormat::Mp3`, `Ac3` and `Eac3` write raw streams, which cannot signal the codec's delay or + the length: the file decodes with 1057 (MP3) or 256 (AC-3, E-AC-3) frames of delay in front and + padded to whole frames at the end. MP3 gets no Xing / Info frame. `EncodeFormat::Aac` and `Alac` + (M4A) decode to the length written. +- An AAC M4A's length is in the edit list and the last packet's duration. This crate, Apple's + decoders and ffmpeg 9 end the audio there; ffmpeg 8 skips the priming but plays the last + packet's padding (under 1024 frames of near silence). +- `EncodeFormat::Aac` and `Alac` write the `moov` box after the audio (no fast start), and no + metadata. Raw ADTS AAC is not a file format of `encode`; `aac::EncoderConfig::with_adts` gives + the frames. +- This crate's file reader plays a raw E-AC-3 7.1 stream as its 5.1 independent substream. +- `EncodeFormat::Aac` with 8 channels is AAC channel configuration 7 (C, Lc Rc, L R, Ls Rs, LFE), + which Apple's decoders read as written. ffmpeg assumes such a stream is a mislabelled 7.1 with + backs and sides and moves the channels accordingly, unless run with `-strict 1`. +- A file with no audio: MP3, AC-3 and E-AC-3 are then zero bytes, which nothing can probe. An + empty AAC M4A (a track with no samples) is read by ffmpeg; this crate's native reader passes it + on, so `decode-native` alone refuses it and symphonia reads only some as zero frames. - ALAC 32-bit streams: ffmpeg does not decode their uncompressed (escaped) stereo frames, which Apple's encoder and this one write for audio that does not compress; Apple's decoder and this one do. @@ -481,7 +566,7 @@ finite count as silence and the rest are clamped to ±4, so no input makes an in decoder's 7680-bit input buffer. - Not yet: block switching (short blocks; transients pre-echo), `scfsi` and `preflag`, intensity stereo, a tonality-aware model, VBR/ABR, the Xing/Info and LAME gapless tags (the delay is not - signalled), CRC, free format, and an `EncodeFormat` entry for `encode`/`FileEncoder`. + signalled), CRC and free format. ### AC-3 and E-AC-3 encoding diff --git a/crates/audio/examples/encode.rs b/crates/audio/examples/encode.rs index 28de0f5ad..51cdecc84 100644 --- a/crates/audio/examples/encode.rs +++ b/crates/audio/examples/encode.rs @@ -1,4 +1,4 @@ -//! Write a tone as WAV and FLAC, whole and streamed, and as AAC packets. +//! Write a tone as WAV, FLAC and AAC in M4A, whole and streamed, and as AAC packets. //! //! `cargo run -p craft-audio --example encode --features lossless-encode,aac-encode -- out` @@ -28,7 +28,11 @@ fn main() -> Result<(), Box> { } enc.finish()?; - // AAC packets for an MP4 or Matroska muxer, with the configuration its sample entry needs. + // AAC in M4A at 160 kbit/s: the same call, with a bit rate in place of a bit depth. + let m4a = encode(&stereo, &EncodeOptions::new(EncodeFormat::Aac).with_bitrate(160_000))?; + std::fs::write(dir.join("tone.m4a"), m4a)?; + + // AAC packets for a muxer of your own, with the configuration its sample entry needs. let mut aac = craft_audio::aac::Encoder::new(craft_audio::aac::EncoderConfig::cbr(rate as u32, 2, 128_000))?; let mut units = aac.encode(&[&tone, &tone]); units.extend(aac.flush()); diff --git a/crates/audio/src/aac/encoder/mod.rs b/crates/audio/src/aac/encoder/mod.rs index 5a0ea5ac8..c641b790b 100644 --- a/crates/audio/src/aac/encoder/mod.rs +++ b/crates/audio/src/aac/encoder/mod.rs @@ -162,6 +162,16 @@ const HISTORY: usize = 2048; /// Bits per channel in a silent frame, rounded up: the smallest frame the encoder writes. const MIN_FRAME_BITS_PER_CHANNEL: u64 = 48; +/// The lowest and highest constant bit rate (bits per second) [`Encoder::new`] accepts for a +/// stream without ADTS headers: at least 1 kbit/s per channel and a silent frame, at most the +/// 6144 bits per channel and frame of the decoder buffer. +pub(crate) fn cbr_range(sample_rate: u32, channels: usize) -> (u32, u32) { + let (rate, channels) = (u64::from(sample_rate), channels as u64); + let silent = (MIN_FRAME_BITS_PER_CHANNEL * channels * rate).div_ceil(FRAME_LEN as u64); + let most = 6144 * channels * rate / FRAME_LEN as u64; + (u32::try_from(silent.max(1000 * channels)).unwrap_or(u32::MAX), u32::try_from(most).unwrap_or(u32::MAX)) +} + /// AAC-LC encoder. pub struct Encoder { cfg: EncoderConfig, diff --git a/crates/audio/src/aac/mod.rs b/crates/audio/src/aac/mod.rs index 297aa7972..f423a4e22 100644 --- a/crates/audio/src/aac/mod.rs +++ b/crates/audio/src/aac/mod.rs @@ -64,6 +64,8 @@ pub use config::{AdtsHeader, AudioSpecificConfig, ElementType, ProgramConfig, co #[cfg(feature = "aac-decode")] pub use decoder::Decoder; #[cfg(feature = "aac-encode")] +pub(crate) use encoder::cbr_range; +#[cfg(feature = "aac-encode")] pub use encoder::{BitrateMode, Encoder, EncoderConfig, EncoderStats}; #[cfg(feature = "aac-decode")] pub use latm::{LatmConfig, latm_config, latm_split}; diff --git a/crates/audio/src/ac3/mod.rs b/crates/audio/src/ac3/mod.rs index a8e8920d2..6bb19df8d 100644 --- a/crates/audio/src/ac3/mod.rs +++ b/crates/audio/src/ac3/mod.rs @@ -61,7 +61,7 @@ pub enum Error { pub type Result = std::result::Result; /// Data rate in kb/s by frmsizecod / 2 (Table 5.18). -const BITRATES: [u32; 19] = [32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 448, 512, 576, 640]; +pub(crate) const BITRATES: [u32; 19] = [32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 448, 512, 576, 640]; /// Full-bandwidth channels per acmod (Table 5.8). const NFCHANS: [usize; 8] = [2, 1, 2, 3, 3, 4, 4, 5]; diff --git a/crates/audio/src/layout.rs b/crates/audio/src/layout.rs index f3cab1b7f..d0c5ae39e 100644 --- a/crates/audio/src/layout.rs +++ b/crates/audio/src/layout.rs @@ -283,6 +283,40 @@ pub(crate) fn flac_assumed_mask(channels: usize) -> u32 { } } +/// The surround speaker a layout may name in place of `speaker`: formats with one surround pair +/// do not tell side from back, so a pair of either kind feeds it. +fn surround_twin(speaker: Speaker) -> Option { + Some(match speaker { + Speaker::SideLeft => Speaker::BackLeft, + Speaker::SideRight => Speaker::BackRight, + Speaker::BackLeft => Speaker::SideLeft, + Speaker::BackRight => Speaker::SideRight, + _ => return None, + }) +} + +/// How audio with `layout` feeds a format whose layout for the channel count is `mask`: for each +/// channel the format takes, the channel of the audio that feeds it. `order` is the format's +/// channel order over `mask` (for each of its channels, the WAV-order channel of `mask`; `None` +/// when it takes WAV order). `None` when `layout` is not the format's layout: it is unordered, or +/// has a speaker the format has not. +pub(crate) fn channel_map(mask: u32, order: Option<&[usize]>, layout: &ChannelLayout) -> Option> { + let speakers = ChannelLayout::from_mask(mask).speakers(); + if speakers.len() != usize::from(layout.channels()) { + return None; + } + let mut map = Vec::with_capacity(speakers.len()); + for j in 0..speakers.len() { + let speaker = *speakers.get(order.and_then(|o| o.get(j).copied()).unwrap_or(j))?; + let at = layout.index_of(speaker).or_else(|| surround_twin(speaker).and_then(|t| layout.index_of(t)))?; + if map.contains(&at) { + return None; + } + map.push(at); + } + Some(map) +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/audio/src/lib.rs b/crates/audio/src/lib.rs index 3c578242f..697568201 100644 --- a/crates/audio/src/lib.rs +++ b/crates/audio/src/lib.rs @@ -16,9 +16,10 @@ //! - [`TrackDecoder`] (any decoder feature): one track's packets from any demuxer to planar //! `f32` in WAV channel order. //! - [`encode`], [`FileEncoder`]: WAV/BWF/RF64 and AIFF/AIFF-C (feature `pcm-encode`, which also -//! gives [`wav_float_header`]) and FLAC (feature `flac-encode`), whole-buffer or incremental into -//! any `Write + Seek`. Feature `lossless-encode` turns on both; feature `encode` turns on -//! `lossless-encode` and every codec's encoder. +//! gives [`wav_float_header`]), FLAC (feature `flac-encode`), AAC-LC and ALAC in M4A +//! (`aac-encode`, `alac-encode`) and MP3, AC-3 and E-AC-3 streams (`mp3-encode`, `ac3-encode`, +//! `eac3-encode`), whole-buffer or incremental into any `Write + Seek`. Feature +//! `lossless-encode` turns on WAV, AIFF and FLAC; feature `encode` turns on every format and encoder. //! - [`wav_pcm`], [`aiff_pcm`] (feature `decode-native`) and [`pcm_wav_file`], [`pcm_wav_header`], //! [`pcm_aiff_file`], [`pcm_aiff_header`] (feature `pcm-encode`): the integer PCM of a WAV or AIFF //! file bit for bit, and minimal files around such PCM, for essence embedded in other containers. @@ -54,12 +55,19 @@ mod read; mod track; #[cfg(any(feature = "decode-native", feature = "pcm-encode"))] mod wav; -#[cfg(any(feature = "pcm-encode", feature = "flac-encode", feature = "vorbis-encode"))] +#[cfg(any( + feature = "pcm-encode", + feature = "flac-encode", + feature = "aac-encode", + feature = "alac-encode", + feature = "mp3-encode", + feature = "ac3-encode", + feature = "eac3-encode", + feature = "vorbis-encode" +))] mod write; #[cfg(feature = "vorbis-encode")] -pub use write::OGG_BITRATES; -#[cfg(feature = "vorbis-encode")] -pub use write::encode_ogg; +pub use write::{OGG_BITRATES, encode_ogg}; /// The FLAC packet encoder (RFC 9639), for a caller that muxes FLAC itself (into MP4, Matroska or /// its own `.flac` writer): planar integer samples in, frames out, and the STREAMINFO to patch in @@ -102,10 +110,29 @@ pub use read::{ pub use read::{RawPcm, aiff_pcm, wav_pcm}; #[cfg(any(feature = "aac-decode", feature = "alac-decode", feature = "opus-decode", feature = "ac3-decode", feature = "mpa-decode"))] pub use track::{LOOKAHEAD_PACKETS, TrackDecoder}; +#[cfg(any( + feature = "pcm-encode", + feature = "flac-encode", + feature = "aac-encode", + feature = "alac-encode", + feature = "mp3-encode", + feature = "ac3-encode", + feature = "eac3-encode" +))] +pub use write::FileEncoder; #[cfg(feature = "pcm-encode")] pub use write::wav_float_header; -#[cfg(any(feature = "pcm-encode", feature = "flac-encode"))] -pub use write::{ENCODE_EXTENSIONS, FileEncoder, encode, encode_format_for}; +#[cfg(any( + feature = "pcm-encode", + feature = "flac-encode", + feature = "aac-encode", + feature = "alac-encode", + feature = "mp3-encode", + feature = "ac3-encode", + feature = "eac3-encode", + feature = "vorbis-encode" +))] +pub use write::{ENCODE_EXTENSIONS, encode, encode_format_for}; #[cfg(feature = "pcm-encode")] pub use write::{pcm_aiff_file, pcm_aiff_header, pcm_wav_file, pcm_wav_header}; @@ -219,7 +246,8 @@ impl From for AudioError { } /// The file formats [`encode`] writes. Each needs its feature: WAV and AIFF `pcm-encode`, FLAC -/// `flac-encode`; asking for one that is not built returns [`AudioError::Unsupported`]. +/// `flac-encode`, AAC `aac-encode`, ALAC `alac-encode`, MP3 `mp3-encode`, AC-3 `ac3-encode` and +/// E-AC-3 `eac3-encode`; asking for one that is not built returns [`AudioError::Unsupported`]. #[non_exhaustive] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] @@ -230,29 +258,49 @@ pub enum EncodeFormat { Aiff, /// FLAC (integer depths). Flac, - /// Ogg Vorbis (variable bitrate). Whole-buffer only: use [`encode_ogg`] directly. - #[cfg(feature = "vorbis-encode")] + /// AAC-LC in an M4A (MP4) file, 1–8 channels at one of the 13 AAC sample rates. The edit list + /// skips the encoder's priming and ends at the last frame, so the file decodes to the length + /// that was written. + Aac, + /// Apple Lossless in an M4A (MP4) file (integer depths), 1–8 channels. + Alac, + /// An MP3 stream: mono or stereo at 32, 44.1 or 48 kHz, or 16, 22.05 or 24 kHz. + Mp3, + /// An AC-3 stream: up to 5.1 at 32, 44.1 or 48 kHz. + Ac3, + /// An E-AC-3 stream: up to 7.1 at 32, 44.1 or 48 kHz. + Eac3, + /// Ogg Vorbis, whole-buffer encoding only. Ogg, } impl EncodeFormat { /// Every format, in the order menus list them. Whether [`encode`] writes one depends on its - /// feature. - #[cfg(not(feature = "vorbis-encode"))] - pub const ALL: &[EncodeFormat] = &[EncodeFormat::Wav, EncodeFormat::Aiff, EncodeFormat::Flac]; - /// Every format, in the order menus list them. Whether [`encode`] writes one depends on its - /// feature. - #[cfg(feature = "vorbis-encode")] - pub const ALL: &[EncodeFormat] = &[EncodeFormat::Wav, EncodeFormat::Aiff, EncodeFormat::Flac, EncodeFormat::Ogg]; + /// feature ([`EncodeFormat::is_built`]). + pub const ALL: &[EncodeFormat] = &[ + EncodeFormat::Wav, + EncodeFormat::Aiff, + EncodeFormat::Flac, + EncodeFormat::Alac, + EncodeFormat::Aac, + EncodeFormat::Mp3, + EncodeFormat::Ac3, + EncodeFormat::Eac3, + EncodeFormat::Ogg, + ]; /// The extension a new file of this format gets, lower case and without the dot: `"wav"`, - /// `"aif"`, `"flac"`. [`encode_format_for`] maps it back. + /// `"aif"`, `"flac"`, `"m4a"` (AAC and ALAC), `"mp3"`, `"ac3"`, `"eac3"`. + /// [`encode_format_for`] maps it back, `"m4a"` to AAC. pub fn extension(self) -> &'static str { match self { EncodeFormat::Wav => "wav", EncodeFormat::Aiff => "aif", EncodeFormat::Flac => "flac", - #[cfg(feature = "vorbis-encode")] + EncodeFormat::Aac | EncodeFormat::Alac => "m4a", + EncodeFormat::Mp3 => "mp3", + EncodeFormat::Ac3 => "ac3", + EncodeFormat::Eac3 => "eac3", EncodeFormat::Ogg => "ogg", } } @@ -263,7 +311,10 @@ impl EncodeFormat { EncodeFormat::Wav => "audio/wav", EncodeFormat::Aiff => "audio/aiff", EncodeFormat::Flac => "audio/flac", - #[cfg(feature = "vorbis-encode")] + EncodeFormat::Aac | EncodeFormat::Alac => "audio/mp4", + EncodeFormat::Mp3 => "audio/mpeg", + EncodeFormat::Ac3 => "audio/ac3", + EncodeFormat::Eac3 => "audio/eac3", EncodeFormat::Ogg => "audio/ogg", } } @@ -274,10 +325,158 @@ impl EncodeFormat { EncodeFormat::Wav => Container::Wav, EncodeFormat::Aiff => Container::Aiff, EncodeFormat::Flac => Container::Flac, - #[cfg(feature = "vorbis-encode")] + EncodeFormat::Aac | EncodeFormat::Alac => Container::Mp4, + EncodeFormat::Mp3 => Container::MpegAudio, + EncodeFormat::Ac3 | EncodeFormat::Eac3 => Container::Ac3, EncodeFormat::Ogg => Container::Ogg, } } + + /// The codec of the audio this format writes. + pub fn codec(self) -> CodecId { + match self { + EncodeFormat::Wav | EncodeFormat::Aiff => CodecId::Pcm, + EncodeFormat::Flac => CodecId::Flac, + EncodeFormat::Aac => CodecId::Aac, + EncodeFormat::Alac => CodecId::Alac, + EncodeFormat::Mp3 => CodecId::Mp3, + EncodeFormat::Ac3 => CodecId::Ac3, + EncodeFormat::Eac3 => CodecId::Eac3, + EncodeFormat::Ogg => CodecId::Vorbis, + } + } + + /// The feature that builds this format's writer. + pub fn feature(self) -> &'static str { + match self { + EncodeFormat::Wav | EncodeFormat::Aiff => "pcm-encode", + EncodeFormat::Flac => "flac-encode", + EncodeFormat::Aac => "aac-encode", + EncodeFormat::Alac => "alac-encode", + EncodeFormat::Mp3 => "mp3-encode", + EncodeFormat::Ac3 => "ac3-encode", + EncodeFormat::Eac3 => "eac3-encode", + EncodeFormat::Ogg => "vorbis-encode", + } + } + + /// Whether [`encode`] writes this format in this build (its + /// [`feature`](Self::feature) is on): what to filter [`EncodeFormat::ALL`] by for a menu. + pub fn is_built(self) -> bool { + match self { + EncodeFormat::Wav | EncodeFormat::Aiff => cfg!(feature = "pcm-encode"), + EncodeFormat::Flac => cfg!(feature = "flac-encode"), + EncodeFormat::Aac => cfg!(feature = "aac-encode"), + EncodeFormat::Alac => cfg!(feature = "alac-encode"), + EncodeFormat::Mp3 => cfg!(feature = "mp3-encode"), + EncodeFormat::Ac3 => cfg!(feature = "ac3-encode"), + EncodeFormat::Eac3 => cfg!(feature = "eac3-encode"), + EncodeFormat::Ogg => cfg!(all(feature = "vorbis-encode", not(target_arch = "wasm32"))), + } + } + + /// The sample rates the format takes, ascending, or `None` when it has no fixed list (WAV, + /// AIFF, FLAC and ALAC take any rate their headers can hold). + pub fn sample_rates(self) -> Option<&'static [u32]> { + match self { + EncodeFormat::Aac => Some(&[7350, 8000, 11_025, 12_000, 16_000, 22_050, 24_000, 32_000, 44_100, 48_000, 64_000, 88_200, 96_000]), + EncodeFormat::Mp3 => Some(&[16_000, 22_050, 24_000, 32_000, 44_100, 48_000]), + EncodeFormat::Ac3 | EncodeFormat::Eac3 => Some(&[32_000, 44_100, 48_000]), + EncodeFormat::Wav | EncodeFormat::Aiff | EncodeFormat::Flac | EncodeFormat::Alac | EncodeFormat::Ogg => None, + } + } + + /// The most channels a file of this format holds. A WAV's frame size is a 16-bit field, so it + /// holds 65535 / bytes per sample: 32767 at 16 bits (the count given here), 21845 at 24 and + /// 16383 at 32; [`EncodeOptions::check`] answers for the depth chosen. + pub fn max_channels(self) -> u16 { + match self { + EncodeFormat::Wav | EncodeFormat::Aiff => 32_767, + EncodeFormat::Flac | EncodeFormat::Aac | EncodeFormat::Alac | EncodeFormat::Eac3 => 8, + EncodeFormat::Ac3 => 6, + EncodeFormat::Mp3 | EncodeFormat::Ogg => 2, + } + } + + /// The speaker layouts of a format that has a fixed set of them, as WAV masks, for a channel + /// count: one each for AAC, ALAC and MP3, one per coding mode for AC-3 and E-AC-3. Empty for a + /// count the format has not, and for WAV, AIFF and FLAC, which have no such set. + pub(crate) fn layout_masks(self, channels: u16) -> &'static [u32] { + let per_count: &[&[u32]] = match self { + // Channel configurations 1–6, the 6.1 program config element, and configuration 7 + // (7.1 with a front pair left and right of centre). + EncodeFormat::Aac => &[&[0x4], &[0x3], &[0x7], &[0x107], &[0x607], &[0x60F], &[0x70F], &[0xFF]], + // Apple's layout tags for 1–8 channels, surrounds as back speakers. + EncodeFormat::Alac => &[&[0x4], &[0x3], &[0x7], &[0x107], &[0x37], &[0x3F], &[0x13F], &[0xFF]], + EncodeFormat::Mp3 | EncodeFormat::Ogg => &[&[0x4], &[0x3]], + // The coding modes 1/0 to 3/2 with and without an LFE, by channel count. + EncodeFormat::Ac3 => &[&[0x4], &[0x3, 0xC], &[0x7, 0x103, 0xB], &[0x107, 0x603, 0xF, 0x10B], &[0x607, 0x10F, 0x60B], &[0x60F]], + // The same, and 7.1 with backs and sides. + EncodeFormat::Eac3 => { + &[&[0x4], &[0x3, 0xC], &[0x7, 0x103, 0xB], &[0x107, 0x603, 0xF, 0x10B], &[0x607, 0x10F, 0x60B], &[0x60F], &[], &[0x63F]] + } + EncodeFormat::Wav | EncodeFormat::Aiff | EncodeFormat::Flac => &[], + }; + usize::from(channels).checked_sub(1).and_then(|i| per_count.get(i)).copied().unwrap_or_default() + } + + /// The speaker layouts a format with a fixed set of them has for `channels` channels: one for + /// AAC, ALAC and MP3, one per coding mode for AC-3 and E-AC-3; empty when the format cannot + /// hold that many channels. `None` for WAV, AIFF and FLAC, which take any layout. Audio in + /// another layout has to be rearranged into one of these before it is written. + /// + /// ``` + /// use craft_audio::{ChannelLayout, EncodeFormat}; + /// + /// assert_eq!(EncodeFormat::Aac.fixed_layouts(6), Some(vec![ChannelLayout::SURROUND_5_1_SIDE])); + /// assert_eq!(EncodeFormat::Mp3.fixed_layouts(6), Some(vec![])); + /// assert_eq!(EncodeFormat::Wav.fixed_layouts(6), None); + /// ``` + pub fn fixed_layouts(self, channels: u16) -> Option> { + match self { + EncodeFormat::Wav | EncodeFormat::Aiff | EncodeFormat::Flac => None, + _ => Some(self.layout_masks(channels).iter().map(|&mask| ChannelLayout::from_mask(mask)).collect()), + } + } + + /// Whether the format holds audio with `layout`: for WAV, AIFF and FLAC any layout of up to + /// [`max_channels`](Self::max_channels) channels, for the others one of + /// [`fixed_layouts`](Self::fixed_layouts), where side and back surrounds stand for each other + /// in a layout with one surround pair. + pub fn supports_layout(self, layout: &ChannelLayout) -> bool { + match self { + EncodeFormat::Wav | EncodeFormat::Aiff | EncodeFormat::Flac => (1..=self.max_channels()).contains(&layout.channels()), + _ => self.layout_masks(layout.channels()).iter().any(|&mask| layout::channel_map(mask, None, layout).is_some()), + } + } + + /// Whether the format takes [`RateControl::Quality`]: AAC. The other lossy formats are + /// written at a constant bit rate only. + pub fn supports_quality(self) -> bool { + self == EncodeFormat::Aac + } + + /// Whether the format loses audio: AAC, MP3, AC-3 and E-AC-3. These take + /// [`EncodeOptions::rate_control`] and ignore the bit depth and dither. + pub fn is_lossy(self) -> bool { + matches!(self, EncodeFormat::Aac | EncodeFormat::Mp3 | EncodeFormat::Ac3 | EncodeFormat::Eac3 | EncodeFormat::Ogg) + } +} + +/// How a lossy format spends bits ([`EncodeOptions::rate_control`]). +#[non_exhaustive] +#[derive(Debug, Clone, Copy, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub enum RateControl { + /// A constant bit rate in bits per second, for all channels together. A format with a fixed + /// set of rates (MP3, AC-3) uses the highest one not above it; a rate outside what the + /// stream can carry is moved to the nearest one inside. AAC is written at 6 kbit/s or a + /// quarter of the sample rate per channel at least: from about half of that down its encoder + /// codes silence. + Bitrate(u32), + /// A constant quality from 0.0 (smallest) to 1.0 (best), at whatever bit rate each frame + /// needs. AAC only; values outside the range are clamped, and NaN is an error. + Quality(f32), } /// Stored sample representation. @@ -436,9 +635,9 @@ pub enum BitDepth { pub struct EncodeOptions { /// Output file format. pub format: EncodeFormat, - /// Sample depth to write. + /// Sample depth to write (ignored by the lossy formats). pub bit_depth: BitDepth, - /// Add TPDF dither when quantizing to an integer depth. + /// Add TPDF dither when quantizing to an integer depth (ignored by the lossy formats). pub dither: bool, /// Write a BWF `bext` chunk (WAV only; ignored for other formats). pub bwf: Option, @@ -449,15 +648,27 @@ pub struct EncodeOptions { /// the count (they read 5 and 6 channels with side surrounds and 7 as 6.1 with sides, so the /// usual layouts for those counts get one; see the README's limits); AIFF has a fixed channel /// order and ignores it. It must have as many channels as the audio. + /// + /// AAC, ALAC, MP3, AC-3 and E-AC-3 store channels in their own order, so here the layout says + /// which speaker each channel of the audio (in WAV order) is, and the channels are reordered + /// to match. Each of these formats has a fixed set of layouts (AAC and ALAC one per channel + /// count, AC-3 one per coding mode); a layout outside it is [`AudioError::Unsupported`]. Side + /// and back surrounds are taken as the same pair where the format has only one. pub layout: Option, /// Write WAV in the RF64 layout even below 4 GiB (WAV only; ignored for other formats). pub force_rf64: bool, + /// The bit rate or quality of a lossy format (ignored by the others). `None` is a constant + /// 64 kbit/s per channel for AAC, 96 kbit/s per channel for MP3, and 96 kbit/s per + /// full-bandwidth channel (the LFE not counted) for AC-3 and E-AC-3, moved to the nearest rate + /// the stream can carry. + pub rate_control: Option, } impl EncodeOptions { - /// Options for `format`: 24-bit, no dither, no BWF chunk, the usual layout. + /// Options for `format`: 24-bit, no dither, no BWF chunk, the usual layout, the format's + /// default bit rate. pub fn new(format: EncodeFormat) -> Self { - Self { format, bit_depth: BitDepth::Int24, dither: false, bwf: None, layout: None, force_rf64: false } + Self { format, bit_depth: BitDepth::Int24, dither: false, bwf: None, layout: None, force_rf64: false, rate_control: None } } /// Sets `format`. @@ -495,6 +706,18 @@ impl EncodeOptions { self.force_rf64 = force_rf64; self } + + /// Sets `rate_control` to a constant bit rate in bits per second ([`RateControl::Bitrate`]). + pub fn with_bitrate(mut self, bits_per_second: u32) -> Self { + self.rate_control = Some(RateControl::Bitrate(bits_per_second)); + self + } + + /// Sets `rate_control` to a constant quality from 0.0 to 1.0 ([`RateControl::Quality`]). + pub fn with_quality(mut self, quality: f32) -> Self { + self.rate_control = Some(RateControl::Quality(quality)); + self + } } impl Default for EncodeOptions { diff --git a/crates/audio/src/write/coded.rs b/crates/audio/src/write/coded.rs new file mode 100644 index 000000000..0d0dd626f --- /dev/null +++ b/crates/audio/src/write/coded.rs @@ -0,0 +1,506 @@ +//! The coded formats of [`FileEncoder`](crate::FileEncoder): AAC-LC and ALAC in M4A (through +//! craft-media's MP4 muxer) and MP3, AC-3 and E-AC-3 as raw streams. [`Writer`] maps the caller's +//! WAV-order channels onto the encoder's order, drives the packet encoder, and for M4A records the +//! encoder's priming and the exact length in the edit list. + +// Each codec is its own feature, and so are the two sinks: with only some of them built, the +// other halves of the shared code are unused. +#![cfg_attr( + not(all(feature = "aac-encode", feature = "alac-encode", feature = "mp3-encode", feature = "ac3-encode", feature = "eac3-encode")), + allow(dead_code, unused_imports, unused_variables) +)] + +use super::failed_write_error; +use super::pcm::validate_layout; +use crate::layout::channel_map; +use crate::{AudioBuffer, AudioError, ChannelLayout, EncodeFormat, EncodeOptions, RateControl, Result}; +use std::io::{Cursor, Seek, Write}; + +/// Frames handed to the encoder at a time, so its buffers and the packets waiting to be written +/// stay small however long a block is. Also the frames of an ALAC packet. +const CHUNK: usize = 4096; + +/// Bits per second per channel when no rate is asked for: AAC. +const AAC_DEFAULT_BPS_PER_CHANNEL: u32 = 64_000; +/// Kilobits per second per channel when no rate is asked for: MP3, and AC-3 and E-AC-3 (per +/// full-bandwidth channel). +const DEFAULT_KBPS_PER_CHANNEL: u32 = 96; + +/// The speakers of `mask` as labels: `"L R C LFE Ls Rs"`. +fn labels(mask: u32) -> String { + ChannelLayout::from_mask(mask).speakers().iter().map(|s| s.label()).collect::>().join(" ") +} + +/// [`channel_map`] for a format with one layout per channel count ([`EncodeFormat::layout_masks`]), +/// or why the audio does not fit. +fn fixed_map(format: EncodeFormat, name: &str, order: Option<&[usize]>, layout: &ChannelLayout) -> Result> { + let Some(&mask) = format.layout_masks(layout.channels()).first() else { + return Err(AudioError::Unsupported(format!("{name} cannot hold {} channels", layout.channels()))); + }; + channel_map(mask, order, layout).ok_or_else(|| { + AudioError::Unsupported(format!("{name} holds {} channels as {}; the audio is {}", layout.channels(), labels(mask), given(layout))) + }) +} + +/// The speakers of the caller's layout, for an error message. +fn given(layout: &ChannelLayout) -> String { + layout.mask().map_or_else(|| "without speaker assignment".into(), labels) +} + +/// The encoder's channel order over AAC's layout for the count: [`crate::aac::aac_order`], and for 8 channels +/// configuration 7's C, Lc Rc, L R, Ls Rs, LFE over the WAV order L R C LFE Ls Rs Lc Rc. +#[cfg(feature = "aac-encode")] +fn aac_order(channels: usize) -> Option<&'static [usize]> { + if channels == 8 { Some(&[2, 6, 7, 0, 1, 4, 5, 3]) } else { crate::aac::aac_order(channels) } +} + +/// The lowest AAC bit rate the writer uses, per channel: 6 kbit/s, and a quarter of the sample +/// rate (256 bits a frame). The encoder accepts less, and from about half of this down codes +/// silence; a file of silence is no answer to a low bit rate (or to kbit/s given as bit/s). +#[cfg(feature = "aac-encode")] +fn aac_floor_per_channel(sample_rate: u32) -> u32 { + (sample_rate / 4).max(6000) +} + +/// The AAC rate control for `rate`: a bit rate is kept within what the stream can carry, a +/// quality of 0–1 spans the encoder's VBR range. +#[cfg(feature = "aac-encode")] +fn aac_mode(rate: Option, sample_rate: u32, channels: usize) -> crate::aac::BitrateMode { + use crate::aac::BitrateMode; + let (lo, hi) = crate::aac::cbr_range(sample_rate, channels); + let lo = lo.max(aac_floor_per_channel(sample_rate).saturating_mul(channels as u32)).min(hi.max(lo)); + // An empty range (a rate or channel count the encoder refuses anyway) leaves the rate alone. + let fit = |bps: u32| if lo <= hi { bps.clamp(lo, hi) } else { bps }; + match rate { + None => BitrateMode::Cbr(fit(AAC_DEFAULT_BPS_PER_CHANNEL.saturating_mul(channels as u32))), + Some(RateControl::Bitrate(bps)) => BitrateMode::Cbr(fit(bps)), + // NaN stays NaN, which the encoder refuses. + Some(RateControl::Quality(q)) => BitrateMode::Vbr(1.0 + 4.0 * q.clamp(0.0, 1.0)), + } +} + +/// The bit rate in kbit/s for a codec with constant bit rates only. +fn constant_kbps(name: &str, rate: Option, default: u32) -> Result { + match rate { + None => Ok(default), + Some(RateControl::Bitrate(bps)) => Ok(bps / 1000), + Some(RateControl::Quality(_)) => { + Err(AudioError::Unsupported(format!("{name} is encoded at a constant bit rate; set a bit rate, not a quality"))) + } + } +} + +/// The AC-3 or E-AC-3 settings for audio with `layout`, and the channel map onto them: the coding +/// mode whose speakers the layout names, at the nearest bit rate the stream can carry. +#[cfg(any(feature = "ac3-encode", feature = "eac3-encode"))] +fn ac3_config( + codec: crate::ac3::encoder::Codec, + sample_rate: u32, + layout: &ChannelLayout, + rate: Option, +) -> Result<(crate::ac3::encoder::Config, Vec)> { + use crate::ac3::encoder::{Codec, CodingMode, Config}; + use CodingMode::*; + let name = if codec == Codec::Ac3 { "AC-3" } else { "E-AC-3" }; + let modes = [Mono, Stereo, ThreeFront, StereoSurround, ThreeFrontSurround, StereoTwoSurround, ThreeFrontTwoSurround, Surround71]; + let found = modes.iter().flat_map(|&mode| [false, true].map(|lfe| (mode, lfe))).find_map(|(mode, lfe)| { + // 7.1 is an E-AC-3 programme with an LFE; the encoder has no other. + if mode == Surround71 && (codec != Codec::Eac3 || !lfe) { + return None; + } + let config = Config { codec, sample_rate, bitrate_kbps: 0, mode, lfe }; + let map = channel_map(config.channel_layout().mask()?, None, layout)?; + Some((config, map)) + }); + let Some((mut config, map)) = found else { + return Err(AudioError::Unsupported(format!("{name} has no coding mode for {} channels that are {}", layout.channels(), given(layout)))); + }; + let full = config.mode.channels() as u32; + let want = constant_kbps(name, rate, DEFAULT_KBPS_PER_CHANNEL * full)?; + // What `Config::validate` asks of the rate: enough for the channels, and for AC-3 an entry of + // the frame size table, for E-AC-3 a syncframe its 11-bit word count can hold. + let least = if config.mode == Surround71 { 384 } else { 32 * full }; + config.bitrate_kbps = match codec { + Codec::Ac3 => { + let floor = crate::ac3::BITRATES.iter().copied().find(|&r| r >= least).unwrap_or(least); + crate::ac3::BITRATES.iter().rev().copied().find(|&r| r <= want).filter(|&r| r >= floor).unwrap_or(floor) + } + Codec::Eac3 => { + let most = if config.mode == Surround71 { 1024 } else { (2048 * u64::from(sample_rate) / 96_000).min(1024) as u32 }; + want.clamp(least, most.max(least)) + } + }; + Ok((config, map)) +} + +/// One encoded packet and the frames it decodes to. +struct Packet { + data: Vec, + frames: u32, +} + +/// A packet encoder of any of the codecs, taking planar `f32` in its own channel order. +enum Codec { + #[cfg(feature = "aac-encode")] + Aac(Box), + #[cfg(feature = "alac-encode")] + Alac { + enc: Box, + quantizers: Vec, + /// Quantized samples of the packet being filled, per channel. + pending: Vec>, + }, + #[cfg(feature = "mp3-encode")] + Mp3(Box), + #[cfg(any(feature = "ac3-encode", feature = "eac3-encode"))] + Ac3 { + enc: Box, + /// The block with every sample finite and within ±1, as the encoder requires. + planes: Vec>, + }, +} + +impl Codec { + /// Encodes `planes` (one slice per encoder channel, equally long, at most [`CHUNK`] frames) + /// and appends the packets completed. + fn push(&mut self, planes: &[&[f32]], out: &mut Vec) -> Result<()> { + match self { + #[cfg(feature = "aac-encode")] + Codec::Aac(enc) => out.extend(enc.encode(planes).into_iter().map(|data| Packet { data, frames: crate::aac::FRAME_LEN as u32 })), + #[cfg(feature = "alac-encode")] + Codec::Alac { enc, quantizers, pending } => { + let frames = planes.first().map_or(0, |p| p.len()); + let mut at = 0; + while at < frames { + let filled = pending.first().map_or(0, Vec::len); + let end = frames.min(at + CHUNK.saturating_sub(filled).max(1)); + for ((dst, q), src) in pending.iter_mut().zip(quantizers.iter_mut()).zip(planes) { + dst.extend(src.get(at..end).unwrap_or_default().iter().map(|&s| q.quantize(s))); + } + at = end; + if pending.first().is_some_and(|p| p.len() >= CHUNK) { + alac_packet(enc, pending, out)?; + } + } + } + #[cfg(feature = "mp3-encode")] + Codec::Mp3(enc) => { + let data = enc.encode(planes); + if !data.is_empty() { + out.push(Packet { data, frames: 0 }); + } + } + #[cfg(any(feature = "ac3-encode", feature = "eac3-encode"))] + Codec::Ac3 { enc, planes: clean } => { + for (dst, src) in clean.iter_mut().zip(planes) { + dst.clear(); + dst.extend(src.iter().map(|&s| if s.is_nan() { 0.0 } else { s.clamp(-1.0, 1.0) })); + } + let refs: Vec<&[f32]> = clean.iter().map(Vec::as_slice).collect(); + out.extend(enc.push(&refs)?.into_iter().map(|p| Packet { data: p.data, frames: p.samples })); + } + } + Ok(()) + } + + /// Encodes what the encoder still holds and appends the last packets. + fn finish(&mut self, out: &mut Vec) -> Result<()> { + match self { + #[cfg(feature = "aac-encode")] + Codec::Aac(enc) => out.extend(enc.flush().into_iter().map(|data| Packet { data, frames: crate::aac::FRAME_LEN as u32 })), + #[cfg(feature = "alac-encode")] + Codec::Alac { enc, pending, .. } => { + if pending.first().is_some_and(|p| !p.is_empty()) { + alac_packet(enc, pending, out)?; + } + } + #[cfg(feature = "mp3-encode")] + Codec::Mp3(enc) => { + let data = enc.flush(); + if !data.is_empty() { + out.push(Packet { data, frames: 0 }); + } + } + #[cfg(any(feature = "ac3-encode", feature = "eac3-encode"))] + Codec::Ac3 { enc, .. } => out.extend(enc.finish()?.packets.into_iter().map(|p| Packet { data: p.data, frames: p.samples })), + } + Ok(()) + } + + /// Decoded frames before the first frame of the audio. + fn priming(&self) -> u32 { + match self { + #[cfg(feature = "aac-encode")] + Codec::Aac(enc) => enc.priming_samples(), + #[allow(unreachable_patterns)] + _ => 0, + } + } + + /// The MP4 sample entry of the stream as encoded so far (`bytes` in packets of `frames` + /// frames in all): the configuration a decoder needs, with the average bit rate. + #[cfg(any(feature = "aac-encode", feature = "alac-encode"))] + fn sample_entry(&self, sample_rate: u32, channels: u32, bytes: u64, frames: u64) -> Option { + use craft_media::isobmff::{CodecConfig, FourCc, SampleEntry}; + match self { + #[cfg(feature = "aac-encode")] + Codec::Aac(enc) => { + let mut entry = SampleEntry::aac(enc.audio_specific_config(), channels, sample_rate); + if let CodecConfig::Aac(config) = &mut entry.codec + && frames > 0 + { + let bps = u128::from(bytes) * 8 * u128::from(sample_rate) / u128::from(frames); + config.avg_bitrate = u32::try_from(bps).unwrap_or(u32::MAX); + } + Some(entry) + } + #[cfg(feature = "alac-encode")] + Codec::Alac { enc, .. } => { + let bits = u16::from(enc.encoder_config().bit_depth); + Some(SampleEntry::audio(FourCc(*b"alac"), CodecConfig::Alac { cookie: enc.cookie() }, channels, f64::from(sample_rate), bits)) + } + #[allow(unreachable_patterns)] + _ => None, + } + } +} + +/// Encodes the pending samples as one ALAC packet and empties them. +#[cfg(feature = "alac-encode")] +fn alac_packet(enc: &mut crate::alac::Encoder, pending: &mut [Vec], out: &mut Vec) -> Result<()> { + let refs: Vec<&[i32]> = pending.iter().map(Vec::as_slice).collect(); + let frames = refs.first().map_or(0, |p| p.len()) as u32; + let data = enc.encode(&refs)?; + pending.iter_mut().for_each(Vec::clear); + out.push(Packet { data, frames }); + Ok(()) +} + +/// A muxer's error as this crate's: a failed write stays an I/O error. +#[cfg(any(feature = "aac-encode", feature = "alac-encode"))] +fn mux_error(e: craft_media::isobmff::Error) -> AudioError { + match e { + craft_media::isobmff::Error::Io(e) => AudioError::Io(e), + e => AudioError::Encode(format!("M4A: {e}")), + } +} + +/// An M4A file being written: one audio track whose samples are the packets. +/// +/// The last packet is held back until the length is known: its duration is cut to the audio's +/// last frame, so the sample durations add up to the priming plus the audio, and the edit that +/// skips the priming ends where the audio does. The movie timescale is the sample rate, which +/// keeps that edit exact. +#[cfg(any(feature = "aac-encode", feature = "alac-encode"))] +struct M4a { + mux: craft_media::isobmff::Mp4Writer, + /// The track, added with the first packet: a file with no audio gets a track without an edit. + track: Option, + entry: craft_media::isobmff::SampleEntry, + sample_rate: u32, + channels: u32, + priming: u32, + held: Option, + /// Frames and bytes of the packets written, the held one not counted. + frames: u64, + bytes: u64, +} + +#[cfg(any(feature = "aac-encode", feature = "alac-encode"))] +impl M4a { + fn new(out: W, codec: &Codec, sample_rate: u32, channels: u32) -> Result { + use craft_media::isobmff::{Brand, Mp4Writer, WriterOptions}; + let entry = codec.sample_entry(sample_rate, channels, 0, 0).ok_or_else(|| AudioError::Unsupported("M4A holds AAC and ALAC".into()))?; + let mut options = WriterOptions::new(Brand::Mp4); + options.movie_timescale = sample_rate; + let mux = Mp4Writer::new(out, options).map_err(mux_error)?; + Ok(M4a { mux, track: None, entry, sample_rate, channels, priming: codec.priming(), held: None, frames: 0, bytes: 0 }) + } + + fn track(&mut self, has_audio: bool) -> Result { + if let Some(track) = self.track { + return Ok(track); + } + let mut config = craft_media::isobmff::TrackConfig::new(self.entry.clone(), self.sample_rate); + if has_audio && self.priming > 0 { + config.media_start = Some(i64::from(self.priming)); + // AAC's overlap: a packet decodes right only after the one before it. Apple's + // decoders take the priming from the edit only when the track says so. + config.roll_distance = Some(-1); + } + let track = self.mux.add_track(config).map_err(mux_error)?; + self.track = Some(track); + Ok(track) + } + + fn sample(&mut self, packet: &Packet, duration: u32) -> Result<()> { + let track = self.track(true)?; + let sample = craft_media::isobmff::WriteSample { data: &packet.data, duration, composition_offset: 0, is_sync: true }; + self.mux.write_sample(track, sample).map_err(mux_error)?; + self.frames += u64::from(duration); + self.bytes += packet.data.len() as u64; + Ok(()) + } + + fn write(&mut self, packet: Packet) -> Result<()> { + if let Some(held) = self.held.replace(packet) { + self.sample(&held, held.frames)?; + } + Ok(()) + } + + /// Writes the last packet, cut to the end of `audio_frames` frames of audio, and the `moov`. + fn finish(mut self, codec: &Codec, audio_frames: u64) -> Result { + if let Some(held) = self.held.take() { + let rest = (u64::from(self.priming) + audio_frames).saturating_sub(self.frames); + self.sample(&held, rest.min(u64::from(held.frames)) as u32)?; + } + let track = self.track(false)?; + // The configuration is complete only now: the bit rate, and ALAC's largest packet. + if let Some(entry) = codec.sample_entry(self.sample_rate, self.channels, self.bytes, self.frames) { + self.mux.set_sample_entry(track, entry).map_err(mux_error)?; + } + self.mux.finish().map_err(mux_error) + } +} + +/// Where the packets go. +enum Sink { + #[cfg(any(feature = "aac-encode", feature = "alac-encode"))] + M4a(Box>), + /// The packets in a row are the file. + Raw(W), +} + +/// A file of one of the coded formats written block by block. +pub(crate) struct Writer { + codec: Codec, + sink: Sink, + /// For each encoder channel, the caller's channel that feeds it. + map: Vec, + /// The constant bit rate the encoder was set to, bits per second; `None` for ALAC and for + /// AAC at a constant quality. + bitrate: Option, + frames: u64, + packets: Vec, + /// Set when a write has failed: the encoder and the sink no longer agree, so the writer + /// refuses every later call. + failed: bool, +} + +impl Writer { + pub(crate) fn new(out: W, opts: &EncodeOptions, sample_rate: u32, channels: usize) -> Result { + validate_layout(channels, sample_rate)?; + let layout = opts.layout.unwrap_or_else(|| ChannelLayout::default_for(channels as u16)); + let (codec, map, bitrate) = match opts.format { + #[cfg(feature = "aac-encode")] + EncodeFormat::Aac => { + let map = fixed_map(EncodeFormat::Aac, "AAC", aac_order(channels), &layout)?; + let mode = aac_mode(opts.rate_control, sample_rate, channels); + let config = crate::aac::EncoderConfig { mode, ..crate::aac::EncoderConfig::cbr(sample_rate, channels, 0) }; + let bitrate = match mode { + crate::aac::BitrateMode::Cbr(bps) => Some(bps), + crate::aac::BitrateMode::Vbr(_) => None, + }; + (Codec::Aac(Box::new(crate::aac::Encoder::new(config)?)), map, bitrate) + } + #[cfg(feature = "alac-encode")] + EncodeFormat::Alac => { + let count = u8::try_from(channels).unwrap_or(u8::MAX); + let map = fixed_map(EncodeFormat::Alac, "ALAC", crate::alac::alac_order(count), &layout)?; + let bits = match opts.bit_depth { + crate::BitDepth::Int16 => 16, + crate::BitDepth::Int24 => 24, + crate::BitDepth::Int32 => 32, + crate::BitDepth::Float32 => return Err(AudioError::Unsupported("ALAC cannot store 32-bit float samples".into())), + }; + let enc = crate::alac::Encoder::new(crate::alac::EncoderConfig::new(sample_rate, count, bits).with_frame_length(CHUNK as u32))?; + let quantizers = super::pcm::Quantizer::per_channel(u32::from(bits), opts.dither, channels); + (Codec::Alac { enc: Box::new(enc), quantizers, pending: vec![Vec::new(); channels] }, map, None) + } + #[cfg(feature = "mp3-encode")] + EncodeFormat::Mp3 => { + let map = fixed_map(EncodeFormat::Mp3, "MP3", None, &layout)?; + let want = constant_kbps("MP3", opts.rate_control, DEFAULT_KBPS_PER_CHANNEL * channels as u32)?; + // A sample rate MP3 has not is the encoder's to refuse. + let kbps = crate::mp3::nearest_bitrate(sample_rate, want).unwrap_or(want); + let enc = crate::mp3::Encoder::new(crate::mp3::Config::cbr(sample_rate, channels, kbps))?; + (Codec::Mp3(Box::new(enc)), map, Some(kbps.saturating_mul(1000))) + } + #[cfg(any(feature = "ac3-encode", feature = "eac3-encode"))] + EncodeFormat::Ac3 | EncodeFormat::Eac3 => { + let codec = if opts.format == EncodeFormat::Ac3 { crate::ac3::encoder::Codec::Ac3 } else { crate::ac3::encoder::Codec::Eac3 }; + let (config, map) = ac3_config(codec, sample_rate, &layout, opts.rate_control)?; + let enc = crate::ac3::encoder::Encoder::new(config)?; + (Codec::Ac3 { enc: Box::new(enc), planes: vec![Vec::new(); channels] }, map, Some(config.bitrate_kbps.saturating_mul(1000))) + } + #[allow(unreachable_patterns)] + f => return Err(AudioError::Unsupported(format!("writing {f:?}"))), + }; + let sink = match opts.format { + #[cfg(any(feature = "aac-encode", feature = "alac-encode"))] + EncodeFormat::Aac | EncodeFormat::Alac => Sink::M4a(Box::new(M4a::new(out, &codec, sample_rate, channels as u32)?)), + _ => Sink::Raw(out), + }; + Ok(Writer { codec, sink, map, bitrate, frames: 0, packets: Vec::new(), failed: false }) + } + + pub(crate) fn bitrate(&self) -> Option { + self.bitrate + } + + /// Hands the packets waiting to the sink. + fn drain(&mut self) -> Result<()> { + for packet in self.packets.drain(..) { + match &mut self.sink { + #[cfg(any(feature = "aac-encode", feature = "alac-encode"))] + Sink::M4a(m4a) => m4a.write(packet)?, + Sink::Raw(out) => out.write_all(&packet.data)?, + } + } + Ok(()) + } + + /// Appends `frames` frames of `block`, which holds every channel at least that long. + pub(crate) fn write>(&mut self, block: &[C], frames: usize) -> Result<()> { + if self.failed { + return Err(failed_write_error()); + } + for at in (0..frames).step_by(CHUNK) { + let end = frames.min(at + CHUNK); + let planes: Vec<&[f32]> = self.map.iter().map(|&c| block.get(c).and_then(|p| p.as_ref().get(at..end)).unwrap_or_default()).collect(); + let written = self.codec.push(&planes, &mut self.packets).and_then(|()| self.drain()); + if let Err(e) = written { + self.failed = true; + return Err(e); + } + } + self.frames = self.frames.saturating_add(frames as u64); + Ok(()) + } + + pub(crate) fn finish(mut self) -> Result { + if self.failed { + return Err(failed_write_error()); + } + self.codec.finish(&mut self.packets)?; + self.drain()?; + match self.sink { + #[cfg(any(feature = "aac-encode", feature = "alac-encode"))] + Sink::M4a(m4a) => m4a.finish(&self.codec, self.frames), + Sink::Raw(mut out) => { + out.flush()?; + Ok(out) + } + } + } +} + +pub(crate) fn encode(buf: &AudioBuffer, opts: &EncodeOptions) -> Result> { + let mut w = Writer::new(Cursor::new(Vec::new()), opts, buf.sample_rate, buf.channels.len())?; + w.write(&buf.channels, buf.frames())?; + Ok(w.finish()?.into_inner()) +} + +#[cfg(test)] +mod tests; diff --git a/crates/audio/src/write/coded/tests.rs b/crates/audio/src/write/coded/tests.rs new file mode 100644 index 000000000..cbdbd3375 --- /dev/null +++ b/crates/audio/src/write/coded/tests.rs @@ -0,0 +1,167 @@ +use super::*; + +use crate::Speaker; + +fn map(mask: u32, order: Option<&[usize]>, layout: u32) -> Option> { + channel_map(mask, order, &ChannelLayout::from_mask(layout)) +} + +/// Channels are matched by speaker, not by position: a layout that lists the same speakers in +/// another WAV order is reordered, and one with other speakers is refused. +#[test] +fn channels_are_mapped_by_speaker() { + // L R C LFE Ls Rs onto itself, and onto an encoder order C L R Ls Rs LFE. + assert_eq!(map(0x60F, None, 0x60F), Some(vec![0, 1, 2, 3, 4, 5])); + assert_eq!(map(0x60F, Some(&[2, 0, 1, 4, 5, 3]), 0x60F), Some(vec![2, 0, 1, 4, 5, 3])); + // Back surrounds feed a format that has one surround pair, and the other way round. + assert_eq!(map(0x60F, None, 0x3F), Some(vec![0, 1, 2, 3, 4, 5])); + assert_eq!(map(0x3F, None, 0x60F), Some(vec![0, 1, 2, 3, 4, 5])); + // 6.1 with the back centre after the pair (L R C LFE Lb Rb Cb) onto L R C LFE Cb Ls Rs. + assert_eq!(map(0x70F, None, 0x13F), Some(vec![0, 1, 2, 3, 6, 4, 5])); + // Quad has no centre; 7.1 with sides and backs has no front pair left and right of centre. + assert_eq!(map(0x107, None, 0x33), None); + assert_eq!(map(0xFF, None, 0x63F), None); + // With both pairs in the format, one pair cannot feed both. + assert_eq!(map(0x63F, None, 0xFF), None); + assert_eq!(channel_map(0x3, None, &ChannelLayout::unordered(2)), None); + assert_eq!(map(0x3, None, 0x7), None); +} + +#[cfg(feature = "aac-encode")] +#[test] +fn every_aac_channel_count_has_a_layout_and_an_order() { + for channels in 1..=8 { + let mask = EncodeFormat::Aac.layout_masks(channels as u16)[0]; + assert_eq!(mask.count_ones() as usize, channels); + let mut order = map(mask, aac_order(channels), mask).unwrap(); + order.sort_unstable(); + assert_eq!(order, (0..channels).collect::>()); + } + assert!(EncodeFormat::Aac.layout_masks(9).is_empty() && EncodeFormat::Aac.layout_masks(0).is_empty()); + // The layouts this crate's decoder reports for what the encoder writes (`track::aac_layout`), + // and configuration 7's speakers for 8 channels. + let masks: Vec = (1..=8).map(|channels| EncodeFormat::Aac.layout_masks(channels)[0]).collect(); + assert_eq!(masks, [0x4, 0x3, 0x7, 0x107, 0x607, 0x60F, 0x70F, 0xFF]); + assert_eq!( + (EncodeFormat::Mp3.layout_masks(1), EncodeFormat::Mp3.layout_masks(2), EncodeFormat::Mp3.layout_masks(3)), + (&[0x4][..], &[0x3][..], &[][..]) + ); + // Configuration 7: C, Lc Rc, L R, Ls Rs, LFE. + let wide: Vec = ChannelLayout::from_mask(0xFF).speakers(); + let named: Vec = aac_order(8).unwrap().iter().map(|&c| wide[c]).collect(); + use Speaker::*; + assert_eq!(named, [FrontCenter, FrontLeftOfCenter, FrontRightOfCenter, FrontLeft, FrontRight, BackLeft, BackRight, Lfe]); +} + +/// The range the writer keeps a bit rate in is the one the encoder accepts. +#[cfg(feature = "aac-encode")] +#[test] +fn the_aac_bit_rate_range_is_the_encoders() { + use crate::aac::{Encoder, EncoderConfig, SAMPLE_RATES}; + for &rate in &SAMPLE_RATES { + for channels in 1..=8 { + let (lo, hi) = crate::aac::cbr_range(rate, channels); + assert!(lo <= hi, "{rate} Hz, {channels} channels"); + let accepts = |bps| Encoder::new(EncoderConfig::cbr(rate, channels, bps)).is_ok(); + assert!(accepts(lo) && accepts(hi), "{rate} Hz, {channels} channels: {lo}..={hi}"); + assert!(!accepts(lo - 1) && !accepts(hi + 1), "{rate} Hz, {channels} channels: {lo}..={hi}"); + for want in [None, Some(RateControl::Bitrate(0)), Some(RateControl::Bitrate(u32::MAX))] { + let mode = aac_mode(want, rate, channels); + assert!( + Encoder::new(EncoderConfig { mode, ..EncoderConfig::cbr(rate, channels, 0) }).is_ok(), + "{rate} Hz, {channels} channels, {want:?}" + ); + } + } + } +} + +/// Every layout the AC-3 encoders have is found from its speakers, at a bit rate the encoder +/// accepts whatever rate was asked for. +#[cfg(all(feature = "ac3-encode", feature = "eac3-encode"))] +#[test] +fn ac3_settings_follow_the_layout_and_validate() { + use crate::ac3::encoder::{Codec, CodingMode}; + let layouts = [ + (0x4, CodingMode::Mono, false), + (0x3, CodingMode::Stereo, false), + (0xB, CodingMode::Stereo, true), + (0x7, CodingMode::ThreeFront, false), + (0x103, CodingMode::StereoSurround, false), + (0x107, CodingMode::ThreeFrontSurround, false), + (0x33, CodingMode::StereoTwoSurround, false), + (0x603, CodingMode::StereoTwoSurround, false), + (0x37, CodingMode::ThreeFrontTwoSurround, false), + (0x3F, CodingMode::ThreeFrontTwoSurround, true), + (0x60F, CodingMode::ThreeFrontTwoSurround, true), + ]; + for codec in [Codec::Ac3, Codec::Eac3] { + for sample_rate in [32_000, 44_100, 48_000] { + for (mask, mode, lfe) in layouts { + for want in [None, Some(RateControl::Bitrate(0)), Some(RateControl::Bitrate(200_000)), Some(RateControl::Bitrate(u32::MAX))] { + let layout = ChannelLayout::from_mask(mask); + let (config, map) = ac3_config(codec, sample_rate, &layout, want).unwrap(); + assert_eq!((config.mode, config.lfe), (mode, lfe), "{mask:#x}"); + assert_eq!(map, (0..usize::from(layout.channels())).collect::>()); + assert_eq!(config.validate(), Ok(()), "{codec:?} {sample_rate} {mask:#x} {want:?}: {} kbit/s", config.bitrate_kbps); + } + } + let seven_one = ac3_config(codec, sample_rate, &ChannelLayout::SURROUND_7_1, Some(RateControl::Bitrate(u32::MAX))); + match codec { + Codec::Ac3 => assert!(matches!(seven_one, Err(AudioError::Unsupported(_)))), + Codec::Eac3 => assert_eq!(seven_one.unwrap().0.validate(), Ok(())), + } + } + } + // The highest table rate not above the one asked for; the default is 96 kbit/s per channel. + let rate = |codec, mask, want| ac3_config(codec, 48_000, &ChannelLayout::from_mask(mask), want).unwrap().0.bitrate_kbps; + assert_eq!(rate(Codec::Ac3, 0x3, Some(RateControl::Bitrate(200_000))), 192); + assert_eq!(rate(Codec::Ac3, 0x3, None), 192); + assert_eq!(rate(Codec::Ac3, 0x60F, None), 448); + assert_eq!(rate(Codec::Ac3, 0x60F, Some(RateControl::Bitrate(1))), 160); + assert_eq!(rate(Codec::Eac3, 0x3, Some(RateControl::Bitrate(200_500))), 200); + assert!(matches!(ac3_config(Codec::Ac3, 48_000, &ChannelLayout::STEREO, Some(RateControl::Quality(0.5))), Err(AudioError::Unsupported(_)))); + assert!(matches!(ac3_config(Codec::Ac3, 48_000, &ChannelLayout::unordered(2), None), Err(AudioError::Unsupported(_)))); +} + +/// The layouts `EncodeFormat` lists are the encoders' own: ALAC's layout per channel count, and +/// the speakers of every AC-3 and E-AC-3 coding mode the encoder has. +#[cfg(all(feature = "alac-encode", feature = "ac3-encode", feature = "eac3-encode"))] +#[test] +fn the_listed_layouts_are_the_encoders() { + use crate::ac3::encoder::{Codec, CodingMode::*, Config}; + for channels in 0..=10u16 { + let alac = crate::alac::channel_mask(channels as u8); + assert_eq!(EncodeFormat::Alac.layout_masks(channels), if alac == 0 { &[][..] } else { std::slice::from_ref(&alac) }, "ALAC, {channels}"); + for (format, codec) in [(EncodeFormat::Ac3, Codec::Ac3), (EncodeFormat::Eac3, Codec::Eac3)] { + let modes = [Mono, Stereo, ThreeFront, StereoSurround, ThreeFrontSurround, StereoTwoSurround, ThreeFrontTwoSurround, Surround71]; + let mut encoder: Vec = modes + .iter() + .flat_map(|&mode| [false, true].map(|lfe| Config { codec, sample_rate: 48_000, bitrate_kbps: 640, mode, lfe })) + .filter(|config| config.validate().is_ok() && config.channels() == usize::from(channels)) + .filter_map(|config| config.channel_layout().mask()) + .collect(); + let mut listed = format.layout_masks(channels).to_vec(); + encoder.sort_unstable(); + listed.sort_unstable(); + assert_eq!(listed, encoder, "{format:?}, {channels} channels"); + for &mask in &listed { + let layout = ChannelLayout::from_mask(mask); + assert_eq!(ac3_config(codec, 48_000, &layout, None).unwrap().0.channel_layout(), layout); + } + } + } +} + +/// The sample rates `EncodeFormat` lists are the encoders'. +#[cfg(all(feature = "aac-encode", feature = "mp3-encode"))] +#[test] +fn the_listed_sample_rates_are_the_encoders() { + let sorted = |rates: &[u32]| { + let mut rates = rates.to_vec(); + rates.sort_unstable(); + rates + }; + assert_eq!(EncodeFormat::Aac.sample_rates().unwrap(), sorted(&crate::aac::SAMPLE_RATES)); + assert_eq!(EncodeFormat::Mp3.sample_rates().unwrap(), sorted(&crate::mp3::SAMPLE_RATES)); +} diff --git a/crates/audio/src/write/mod.rs b/crates/audio/src/write/mod.rs index d95b8fe0f..b8372be1c 100644 --- a/crates/audio/src/write/mod.rs +++ b/crates/audio/src/write/mod.rs @@ -1,28 +1,45 @@ -//! Encoding: WAV/BWF/RF64 and AIFF/AIFF-C writers (`pcm-encode`) and the FLAC encoder -//! (`flac-encode`), whole-buffer and streaming. +//! Encoding: WAV/BWF/RF64 and AIFF/AIFF-C writers (`pcm-encode`), the FLAC encoder +//! (`flac-encode`) and the coded formats (AAC and ALAC in M4A, MP3, AC-3 and E-AC-3 streams, each +//! with its codec's feature), whole-buffer and streaming. #[cfg(feature = "pcm-encode")] mod aiff; +#[cfg(any(feature = "aac-encode", feature = "alac-encode", feature = "mp3-encode", feature = "ac3-encode", feature = "eac3-encode"))] +mod coded; #[cfg(feature = "flac-encode")] pub(crate) mod flac; mod pcm; +#[cfg(feature = "vorbis-encode")] +mod vorbis; +#[cfg(feature = "vorbis-encode")] +pub use vorbis::{OGG_BITRATES, encode as encode_ogg}; #[cfg(feature = "pcm-encode")] mod raw; -#[cfg(feature = "vorbis-encode")] -pub(crate) mod vorbis; #[cfg(feature = "pcm-encode")] mod wav; -#[cfg(feature = "vorbis-encode")] -pub use vorbis::OGG_BITRATES; use crate::{AudioBuffer, AudioError, ChannelLayout, EncodeFormat, EncodeOptions, Result}; #[cfg(feature = "pcm-encode")] pub use raw::{pcm_aiff_file, pcm_aiff_header, pcm_wav_file, pcm_wav_header}; +#[cfg(any( + feature = "pcm-encode", + feature = "flac-encode", + feature = "aac-encode", + feature = "alac-encode", + feature = "mp3-encode", + feature = "ac3-encode", + feature = "eac3-encode" +))] use std::io::{Seek, Write}; -/// Encode `buf` as WAV/BWF, AIFF/AIFF-C or FLAC. Integer depths clamp samples to [-1, 1]; float +/// Encode `buf` as the file `opts.format` names. Integer depths clamp samples to [-1, 1]; float /// keeps values above full scale. `opts.layout`, when set, must have `buf`'s channel count. /// +/// The lossy formats take `opts.rate_control`. AAC and ALAC are written as M4A, which records the +/// encoder's priming and the exact length, so the file decodes to `buf`'s frames. MP3, AC-3 and +/// E-AC-3 are raw streams, which have no place for that: they decode with the codec's delay in +/// front and padded to whole frames. +/// /// ``` /// # #[cfg(feature = "pcm-encode")] { /// use craft_audio::{AudioBuffer, BitDepth, EncodeFormat, EncodeOptions, encode}; @@ -36,6 +53,7 @@ use std::io::{Seek, Write}; pub fn encode(buf: &AudioBuffer, opts: &EncodeOptions) -> Result> { check_built(opts.format)?; // Checked for every format; WAV and FLAC record it. + #[cfg_attr(not(any(feature = "pcm-encode", feature = "flac-encode")), allow(unused_variables))] let mask = opts.layout.map(|l| wav_mask(&l, buf.channels.len())).transpose()?; match opts.format { #[cfg(feature = "pcm-encode")] @@ -44,6 +62,20 @@ pub fn encode(buf: &AudioBuffer, opts: &EncodeOptions) -> Result> { EncodeFormat::Aiff => aiff::encode(buf, opts.bit_depth, opts.dither), #[cfg(feature = "flac-encode")] EncodeFormat::Flac => flac::encode(buf, opts.bit_depth, opts.dither, mask), + #[cfg(any(feature = "aac-encode", feature = "alac-encode", feature = "mp3-encode", feature = "ac3-encode", feature = "eac3-encode"))] + EncodeFormat::Aac | EncodeFormat::Alac | EncodeFormat::Mp3 | EncodeFormat::Ac3 | EncodeFormat::Eac3 => coded::encode(buf, opts), + #[cfg(feature = "vorbis-encode")] + EncodeFormat::Ogg => { + if opts.layout.is_some_and(|layout| !EncodeFormat::Ogg.supports_layout(&layout)) { + return Err(AudioError::Unsupported("Vorbis requires a mono or stereo speaker layout".into())); + } + let kbps = match opts.rate_control { + None => 192, + Some(crate::RateControl::Bitrate(bps)) => bps / 1000, + Some(crate::RateControl::Quality(_)) => return Err(AudioError::Unsupported("Ogg export takes a target bitrate".into())), + }; + vorbis::encode(buf, kbps) + } #[allow(unreachable_patterns)] f => Err(AudioError::Unsupported(format!("writing {f:?}"))), } @@ -51,65 +83,49 @@ pub fn encode(buf: &AudioBuffer, opts: &EncodeOptions) -> Result> { /// Refuses a format whose writer is not built, before anything else about the request is checked. fn check_built(format: EncodeFormat) -> Result<()> { - let (built, feature) = match format { - EncodeFormat::Flac => (cfg!(feature = "flac-encode"), "flac-encode"), - #[cfg(feature = "vorbis-encode")] - EncodeFormat::Ogg => (cfg!(feature = "vorbis-encode"), "vorbis-encode"), - _ => (cfg!(feature = "pcm-encode"), "pcm-encode"), - }; - if built { Ok(()) } else { Err(AudioError::Unsupported(format!("writing {format:?} needs the `{feature}` feature"))) } + if format.is_built() { Ok(()) } else { Err(AudioError::Unsupported(format!("writing {format:?} needs the `{}` feature", format.feature()))) } } -/// The file extensions of the formats [`encode`] can write (those whose features are on). -#[cfg(all(feature = "pcm-encode", feature = "flac-encode", feature = "vorbis-encode"))] -pub const ENCODE_EXTENSIONS: &[&str] = &["wav", "wave", "bwf", "aif", "aiff", "aifc", "flac", "ogg"]; -/// The file extensions of the formats [`encode`] can write (those whose features are on). -#[cfg(all(feature = "pcm-encode", feature = "flac-encode", not(feature = "vorbis-encode")))] -pub const ENCODE_EXTENSIONS: &[&str] = &["wav", "wave", "bwf", "aif", "aiff", "aifc", "flac"]; -/// The file extensions of the formats [`encode`] can write (those whose features are on). -#[cfg(all(feature = "pcm-encode", feature = "vorbis-encode", not(feature = "flac-encode")))] -pub const ENCODE_EXTENSIONS: &[&str] = &["wav", "wave", "bwf", "aif", "aiff", "aifc", "ogg"]; -/// The file extensions of the formats [`encode`] can write (those whose features are on). -#[cfg(all(feature = "pcm-encode", not(feature = "flac-encode"), not(feature = "vorbis-encode")))] -pub const ENCODE_EXTENSIONS: &[&str] = &["wav", "wave", "bwf", "aif", "aiff", "aifc"]; -/// The file extensions of the formats [`encode`] can write (those whose features are on). -#[cfg(all(feature = "flac-encode", feature = "vorbis-encode", not(feature = "pcm-encode")))] -pub const ENCODE_EXTENSIONS: &[&str] = &["flac", "ogg"]; -/// The file extensions of the formats [`encode`] can write (those whose features are on). -#[cfg(all(feature = "flac-encode", not(feature = "pcm-encode"), not(feature = "vorbis-encode")))] -pub const ENCODE_EXTENSIONS: &[&str] = &["flac"]; -/// The file extensions of the formats [`encode`] can write (those whose features are on). -#[cfg(all(feature = "vorbis-encode", not(feature = "pcm-encode"), not(feature = "flac-encode")))] -pub const ENCODE_EXTENSIONS: &[&str] = &["ogg"]; - -/// Encode `buf` as an Ogg Vorbis stream at about `kbps` (variable, as Vorbis is): stereo, or mono -/// for a mono buffer. A bitrate the encoder has no setup for at that rate and channel count falls -/// back to a quality that suits it. Whole-buffer only: no streaming encoder is provided for Vorbis. -/// -/// ``` -/// # #[cfg(feature = "vorbis-encode")] { -/// use craft_audio::{AudioBuffer, encode_ogg}; -/// -/// let buf = AudioBuffer::new(48_000, 2, 48_000); // one second of stereo silence -/// let ogg = encode_ogg(&buf, 192).unwrap(); -/// assert_eq!(&ogg[..4], b"OggS"); -/// # } -/// ``` -#[cfg(feature = "vorbis-encode")] -pub fn encode_ogg(buf: &AudioBuffer, kbps: u32) -> Result> { - vorbis::encode(buf, kbps) +/// Every extension [`ENCODE_EXTENSIONS`] can list, and how many of them the features built give. +const fn extensions() -> ([&'static str; 13], usize) { + let mut all = [""; 13]; + let mut n = 0; + macro_rules! add { + ($built:expr, $($extension:literal),+) => { + if $built { + $( + all[n] = $extension; + n += 1; + )+ + } + }; + } + add!(cfg!(feature = "pcm-encode"), "wav", "wave", "bwf", "aif", "aiff", "aifc"); + add!(cfg!(feature = "flac-encode"), "flac"); + add!(cfg!(any(feature = "aac-encode", feature = "alac-encode")), "m4a"); + add!(cfg!(feature = "mp3-encode"), "mp3"); + add!(cfg!(feature = "ac3-encode"), "ac3"); + add!(cfg!(feature = "eac3-encode"), "eac3", "ec3"); + add!(cfg!(all(feature = "vorbis-encode", not(target_arch = "wasm32"))), "ogg"); + (all, n) } -/// The format [`encode`] writes for a file extension or format name (`wav`, `aiff`, `flac`, -/// case-insensitive, with or without the dot), or `None` when it cannot write it. `aifc` is -/// AIFF: float AIFF is written as AIFF-C. +const EXTENSIONS: ([&str; 13], usize) = extensions(); + +/// The file extensions of the formats [`encode`] can write (those whose features are on). +pub const ENCODE_EXTENSIONS: &[&str] = EXTENSIONS.0.split_at(EXTENSIONS.1).0; + +/// The format [`encode`] writes for a file extension or format name (`wav`, `aiff`, `flac`, `m4a`, +/// `mp3`, `ac3`, `eac3`, case-insensitive, with or without the dot), or `None` when it cannot +/// write it. `aifc` is AIFF: float AIFF is written as AIFF-C. `m4a` is AAC when the AAC encoder +/// is built and ALAC otherwise; `alac` always names ALAC. /// /// ``` /// # #[cfg(feature = "pcm-encode")] { /// use craft_audio::{EncodeFormat, encode_format_for}; /// /// assert_eq!(encode_format_for(".AIFF"), Some(EncodeFormat::Aiff)); -/// assert_eq!(encode_format_for("mp3"), None); +/// assert_eq!(encode_format_for("unknown"), None); /// # } /// ``` pub fn encode_format_for(name: &str) -> Option { @@ -120,7 +136,19 @@ pub fn encode_format_for(name: &str) -> Option { "aif" | "aiff" | "aifc" => Some(EncodeFormat::Aiff), #[cfg(feature = "flac-encode")] "flac" => Some(EncodeFormat::Flac), - #[cfg(feature = "vorbis-encode")] + #[cfg(feature = "aac-encode")] + "m4a" => Some(EncodeFormat::Aac), + #[cfg(all(feature = "alac-encode", not(feature = "aac-encode")))] + "m4a" => Some(EncodeFormat::Alac), + #[cfg(feature = "alac-encode")] + "alac" => Some(EncodeFormat::Alac), + #[cfg(feature = "mp3-encode")] + "mp3" => Some(EncodeFormat::Mp3), + #[cfg(feature = "ac3-encode")] + "ac3" => Some(EncodeFormat::Ac3), + #[cfg(feature = "eac3-encode")] + "eac3" | "ec3" => Some(EncodeFormat::Eac3), + #[cfg(all(feature = "vorbis-encode", not(target_arch = "wasm32")))] "ogg" => Some(EncodeFormat::Ogg), _ => None, } @@ -138,15 +166,24 @@ fn wav_mask(layout: &ChannelLayout, channels: usize) -> Result { /// The error a stream encoder returns for every call after one of its writes has failed: the /// sink may already hold a prefix of the failed block, so the encoder's counters can no longer /// describe the bytes on the sink, and later writes or `finish` must not claim success. +#[cfg(any( + feature = "pcm-encode", + feature = "flac-encode", + feature = "aac-encode", + feature = "alac-encode", + feature = "mp3-encode", + feature = "ac3-encode", + feature = "eac3-encode" +))] fn failed_write_error() -> AudioError { AudioError::Encode("the stream encoder already failed on an earlier write; its output is incomplete".into()) } -/// Writes WAV/BWF (RF64 above 4 GiB), AIFF / AIFF-C `fl32` or FLAC block by block, producing -/// the same bytes [`encode`] would for the whole buffer: a file of any length never -/// has to fit in memory. The file starts at the sink's position, and nothing may follow that -/// position (a new file, or one truncated there): the encoder cannot truncate a `Write + Seek`, -/// so the bytes of a longer file already there would stay after the new one. +/// Writes a file of any [`EncodeFormat`] block by block, producing the same bytes [`encode`] +/// would for the whole buffer: a file of any length never has to fit in memory. The file starts +/// at the sink's position, and nothing may follow that position (a new file, or one truncated +/// there): the encoder cannot truncate a `Write + Seek`, so the bytes of a longer file already +/// there would stay after the new one. /// /// ``` /// # #[cfg(feature = "flac-encode")] { @@ -163,44 +200,67 @@ fn failed_write_error() -> AudioError { /// assert_eq!(&flac[..4], b"fLaC"); /// # } /// ``` -#[cfg(any(feature = "pcm-encode", feature = "flac-encode"))] +#[cfg(any( + feature = "pcm-encode", + feature = "flac-encode", + feature = "aac-encode", + feature = "alac-encode", + feature = "mp3-encode", + feature = "ac3-encode", + feature = "eac3-encode" +))] pub struct FileEncoder { writer: Writer, + format: EncodeFormat, channels: usize, } -#[cfg(any(feature = "pcm-encode", feature = "flac-encode"))] +#[cfg(any( + feature = "pcm-encode", + feature = "flac-encode", + feature = "aac-encode", + feature = "alac-encode", + feature = "mp3-encode", + feature = "ac3-encode", + feature = "eac3-encode" +))] impl std::fmt::Debug for FileEncoder { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let format = match &self.writer { - #[cfg(feature = "pcm-encode")] - Writer::Wav(_) => EncodeFormat::Wav, - #[cfg(feature = "pcm-encode")] - Writer::Aiff(_) => EncodeFormat::Aiff, - #[cfg(feature = "flac-encode")] - Writer::Flac(_) => EncodeFormat::Flac, - }; - f.debug_struct("FileEncoder").field("format", &format).finish_non_exhaustive() + f.debug_struct("FileEncoder").field("format", &self.format).finish_non_exhaustive() } } -#[cfg(any(feature = "pcm-encode", feature = "flac-encode"))] -enum Writer { +// The writers differ in size with the features built; the coded one is boxed for its encoder. +#[allow(clippy::large_enum_variant)] +enum Writer { #[cfg(feature = "pcm-encode")] Wav(wav::Writer), #[cfg(feature = "pcm-encode")] Aiff(aiff::Writer), #[cfg(feature = "flac-encode")] Flac(flac::Writer), + #[cfg(any(feature = "aac-encode", feature = "alac-encode", feature = "mp3-encode", feature = "ac3-encode", feature = "eac3-encode"))] + Coded(Box>), } -#[cfg(any(feature = "pcm-encode", feature = "flac-encode"))] +#[cfg(any( + feature = "pcm-encode", + feature = "flac-encode", + feature = "aac-encode", + feature = "alac-encode", + feature = "mp3-encode", + feature = "ac3-encode", + feature = "eac3-encode" +))] impl FileEncoder { /// Starts a file of `channels` channels at `sample_rate` at the current position of `out`. /// `opts.layout`, when set, must have `channels` channels. pub fn new(out: W, opts: &EncodeOptions, sample_rate: u32, channels: u16) -> Result { check_built(opts.format)?; - let (depth, dither, channels) = (opts.bit_depth, opts.dither, usize::from(channels)); + let channels = usize::from(channels); + #[cfg_attr(not(any(feature = "pcm-encode", feature = "flac-encode")), allow(unused_variables))] + let (depth, dither) = (opts.bit_depth, opts.dither); + #[cfg_attr(not(any(feature = "pcm-encode", feature = "flac-encode")), allow(unused_variables))] let mask = opts.layout.map(|l| wav_mask(&l, channels)).transpose()?; let writer = match opts.format { #[cfg(feature = "pcm-encode")] @@ -216,24 +276,45 @@ impl FileEncoder { EncodeFormat::Aiff => Writer::Aiff(aiff::Writer::new(out, channels, sample_rate, depth, dither)?), #[cfg(feature = "flac-encode")] EncodeFormat::Flac => Writer::Flac(flac::Writer::new(out, channels, sample_rate, depth, dither, mask)?), + #[cfg(any(feature = "aac-encode", feature = "alac-encode", feature = "mp3-encode", feature = "ac3-encode", feature = "eac3-encode"))] + EncodeFormat::Aac | EncodeFormat::Alac | EncodeFormat::Mp3 | EncodeFormat::Ac3 | EncodeFormat::Eac3 => { + Writer::Coded(Box::new(coded::Writer::new(out, opts, sample_rate, channels)?)) + } #[allow(unreachable_patterns)] f => return Err(AudioError::Unsupported(format!("writing {f:?}"))), }; - Ok(FileEncoder { writer, channels }) + Ok(FileEncoder { writer, format: opts.format, channels }) + } + + /// The constant bit rate the file is written at, in bits per second for all channels + /// together: the rate asked for or the format's default, moved to one the stream + /// can carry as [`RateControl::Bitrate`](crate::RateControl::Bitrate) describes. `None` for the lossless + /// formats and for AAC at a constant quality. + pub fn bitrate(&self) -> Option { + match &self.writer { + #[cfg(any(feature = "aac-encode", feature = "alac-encode", feature = "mp3-encode", feature = "ac3-encode", feature = "eac3-encode"))] + Writer::Coded(w) => w.bitrate(), + #[allow(unreachable_patterns)] + _ => None, + } } /// Fails, without writing anything, when `frames` more frames would not fit the format (an - /// AIFF holds 4 GiB, a FLAC 2^36 - 1 frames). + /// AIFF holds 4 GiB, a FLAC 2^36 - 1 frames). The coded formats have no limit a file reaches. pub fn check_room(&self, frames: u64) -> Result<()> { match &self.writer { #[cfg(feature = "pcm-encode")] - Writer::Wav(w) => w.check_room(frames), + Writer::Wav(w) => w.check_room(frames).map(drop), #[cfg(feature = "pcm-encode")] - Writer::Aiff(w) => w.check_room(frames), + Writer::Aiff(w) => w.check_room(frames).map(drop), #[cfg(feature = "flac-encode")] - Writer::Flac(w) => w.check_room(frames), + Writer::Flac(w) => w.check_room(frames).map(drop), + #[cfg(any(feature = "aac-encode", feature = "alac-encode", feature = "mp3-encode", feature = "ac3-encode", feature = "eac3-encode"))] + Writer::Coded(_) => { + let _ = frames; + Ok(()) + } } - .map(drop) } /// Appends the first `frames` frames of `block`, one slice per channel (`&[Vec]`, @@ -256,10 +337,13 @@ impl FileEncoder { Writer::Aiff(w) => w.write(block, frames), #[cfg(feature = "flac-encode")] Writer::Flac(w) => w.write(block, frames), + #[cfg(any(feature = "aac-encode", feature = "alac-encode", feature = "mp3-encode", feature = "ac3-encode", feature = "eac3-encode"))] + Writer::Coded(w) => w.write(block, frames), } } - /// Completes the headers and hands back `out`, positioned at the end of the file. + /// Encodes what is still buffered, completes the headers and hands back `out`, positioned at + /// the end of the file. pub fn finish(self) -> Result { match self.writer { #[cfg(feature = "pcm-encode")] @@ -268,10 +352,50 @@ impl FileEncoder { Writer::Aiff(w) => w.finish(), #[cfg(feature = "flac-encode")] Writer::Flac(w) => w.finish(), + #[cfg(any(feature = "aac-encode", feature = "alac-encode", feature = "mp3-encode", feature = "ac3-encode", feature = "eac3-encode"))] + Writer::Coded(w) => w.finish(), } } } +#[cfg(any( + feature = "pcm-encode", + feature = "flac-encode", + feature = "aac-encode", + feature = "alac-encode", + feature = "mp3-encode", + feature = "ac3-encode", + feature = "eac3-encode" +))] +impl EncodeOptions { + /// Whether a file of `channels` channels at `sample_rate` can be written with these options: + /// `Ok`, or the error [`FileEncoder::new`] and [`encode`] would return, with nothing written. + /// For choosing a format before a file is created; [`EncodeFormat::sample_rates`], + /// [`EncodeFormat::max_channels`] and [`EncodeFormat::supports_layout`] say what a format takes. + /// + /// ``` + /// # #[cfg(feature = "mp3-encode")] { + /// use craft_audio::{EncodeFormat, EncodeOptions}; + /// + /// let mp3 = EncodeOptions::new(EncodeFormat::Mp3).with_bitrate(200_000); + /// assert!(mp3.check(48_000, 2).is_ok()); + /// assert!(mp3.check(96_000, 2).is_err()); // not an MP3 sample rate + /// assert_eq!(mp3.bitrate(48_000, 2).unwrap(), Some(192_000)); // the table rate below 200 kbit/s + /// # } + /// ``` + pub fn check(&self, sample_rate: u32, channels: u16) -> Result<()> { + self.bitrate(sample_rate, channels).map(drop) + } + + /// The constant bit rate such a file would be written at ([`FileEncoder::bitrate`]), or the + /// error [`EncodeOptions::check`] returns. + pub fn bitrate(&self, sample_rate: u32, channels: u16) -> Result> { + // The encoder a file would get, on a sink that is thrown away: what it accepts and the + // rate it settles on are then the writer's by construction. + Ok(FileEncoder::new(std::io::Cursor::new(Vec::new()), self, sample_rate, channels)?.bitrate()) + } +} + /// The header of a 32-bit float WAV holding `frames` frames (little-endian samples follow it). Its /// length doesn't depend on `frames`, so a recorder can rewrite it in place as the file grows; it /// switches to RF64 above 4 GiB. @@ -309,6 +433,7 @@ mod tests { let enc = FileEncoder::new(std::io::Cursor::new(Vec::new()), &opts, 48_000, 1); assert!(matches!(enc, Err(AudioError::Unsupported(_)))); assert_eq!(encode_format_for(name), None); + assert!(!format.is_built() && EncodeFormat::ALL.iter().any(|f| f.is_built())); // Also with a layout it could not use. let opts = opts.with_layout(ChannelLayout::STEREO); assert!(matches!(encode(&buf, &opts), Err(AudioError::Unsupported(_)))); diff --git a/crates/audio/src/write/pcm.rs b/crates/audio/src/write/pcm.rs index 614d5a4a7..402b9bebc 100644 --- a/crates/audio/src/write/pcm.rs +++ b/crates/audio/src/write/pcm.rs @@ -1,6 +1,9 @@ //! PCM helpers shared by the WAV, AIFF and FLAC writers: quantization (with optional TPDF //! dither), interleaving and layout checks. +// The coded formats use the layout check, and of those only ALAC the quantizer. +#![cfg_attr(not(any(feature = "pcm-encode", feature = "flac-encode")), allow(dead_code))] + use crate::{AudioBuffer, AudioError, Result}; #[cfg(feature = "pcm-encode")] use {crate::BitDepth, std::io::Write}; diff --git a/crates/audio/src/write/vorbis.rs b/crates/audio/src/write/vorbis.rs index 5466a5b92..c264e1a54 100644 --- a/crates/audio/src/write/vorbis.rs +++ b/crates/audio/src/write/vorbis.rs @@ -9,8 +9,10 @@ pub const OGG_BITRATES: (u32, u32) = (64, 500); /// The block size the encoder processes at a time: small enough that memory stays bounded /// regardless of file length. +#[cfg(not(target_arch = "wasm32"))] const BLOCK: usize = 4096; +#[cfg(not(target_arch = "wasm32"))] fn encode_error(e: vorbis_rs::VorbisError) -> AudioError { AudioError::Encode(e.to_string()) } @@ -22,7 +24,13 @@ fn encode_error(e: vorbis_rs::VorbisError) -> AudioError { pub fn encode(buf: &AudioBuffer, kbps: u32) -> Result> { use std::num::{NonZeroU8, NonZeroU32}; - let channels = buf.channels.len().clamp(1, 2); + let channels = buf.channels.len(); + if !(1..=2).contains(&channels) { + return Err(AudioError::Unsupported("Ogg Vorbis export requires mono or stereo audio".into())); + } + if buf.channels.iter().any(|c| c.len() != buf.frames() || c.iter().any(|s| !s.is_finite())) { + return Err(AudioError::Encode("Vorbis samples must be finite and channels equally long".into())); + } let (Some(rate), Some(count)) = (NonZeroU32::new(buf.sample_rate), NonZeroU8::new(channels as u8)) else { return Err(AudioError::Encode("there is no audio to encode".into())); }; @@ -44,7 +52,7 @@ pub fn encode(buf: &AudioBuffer, kbps: u32) -> Result> { encoder.finish().map_err(encode_error) }; - // The library is C: whatever it does wrong ends here as an error, never as a crash of the app. + // Convert Rust unwinds to an error. This does not catch native crashes or aborts. let attempt = |strategy| { std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| encode(strategy))) .unwrap_or_else(|_| Err(AudioError::Encode("the Vorbis encoder failed".into()))) @@ -58,5 +66,41 @@ pub fn encode(buf: &AudioBuffer, kbps: u32) -> Result> { #[cfg(target_arch = "wasm32")] pub fn encode(_buf: &AudioBuffer, _kbps: u32) -> Result> { - Err("Ogg Vorbis files are exported by the desktop app".into()) + Err(AudioError::Unsupported("Ogg Vorbis files are exported by the desktop app".into())) +} + +#[cfg(all(test, not(target_arch = "wasm32")))] +mod tests { + use super::*; + + #[test] + fn rejects_unsupported_or_malformed_channels_without_dropping_audio() { + for channels in [0, 3, 6] { + assert!(matches!(encode(&AudioBuffer::new(48_000, channels, 16), 192), Err(AudioError::Unsupported(_)))); + } + let ragged = AudioBuffer { sample_rate: 48_000, channels: vec![vec![0.0; 16], vec![0.0; 15]] }; + assert!(encode(&ragged, 192).is_err()); + assert!(encode(&AudioBuffer::new(0, 1, 16), 192).is_err()); + let invalid = AudioBuffer { sample_rate: 48_000, channels: vec![vec![f32::NAN]] }; + assert!(encode(&invalid, 192).is_err()); + } + + #[test] + fn generic_and_direct_encoders_write_mono_and_stereo_ogg() { + for channels in [1, 2] { + let audio = AudioBuffer::new(48_000, channels, 512); + let opts = crate::EncodeOptions::new(crate::EncodeFormat::Ogg); + let direct = encode(&audio, 192).unwrap(); + let generic = crate::encode(&audio, &opts).unwrap(); + assert!(direct.starts_with(b"OggS")); + assert_eq!(direct, generic); + #[cfg(feature = "decode")] + { + let (info, decoded) = crate::decode(&generic, Some("ogg")).unwrap(); + assert_eq!(info.channels, channels); + assert_eq!(decoded.num_channels(), channels); + assert_eq!(decoded.frames(), audio.frames()); + } + } + } } diff --git a/crates/audio/tests/audio/decode_extensions.rs b/crates/audio/tests/audio/decode_extensions.rs index c7c8615e4..fc961cc39 100644 --- a/crates/audio/tests/audio/decode_extensions.rs +++ b/crates/audio/tests/audio/decode_extensions.rs @@ -62,11 +62,21 @@ fn every_decode_extension_probes_and_decodes() { fn every_encode_format_has_an_extension_it_is_written_and_read_under() { for &format in EncodeFormat::ALL { let ext = format.extension(); - assert_eq!(craft_audio::encode_format_for(ext), Some(format), "{ext}"); - assert!(craft_audio::ENCODE_EXTENSIONS.contains(&ext), "{ext}"); + // The extension names the format, or for `m4a`, which two formats share, AAC before ALAC. + // A format whose feature is off has no name. + match craft_audio::encode_format_for(ext) { + Some(named) => { + let shares_m4a = |f| matches!(f, EncodeFormat::Aac | EncodeFormat::Alac); + assert!(named == format || (shares_m4a(named) && shares_m4a(format)), "{ext}"); + assert!(named.is_built(), "{ext}"); + assert!(craft_audio::ENCODE_EXTENSIONS.contains(&ext), "{ext}"); + } + None => assert!(!matches!(format, EncodeFormat::Wav | EncodeFormat::Aiff | EncodeFormat::Flac), "{ext}"), + } assert!(DECODE_EXTENSIONS.contains(&ext), "{ext}: what is written reads back"); assert!(format.mime_type().starts_with("audio/"), "{format:?}"); } - assert_eq!(EncodeFormat::ALL.iter().map(|f| f.extension()).collect::>(), ["wav", "aif", "flac", "ogg"]); + let extensions: Vec<_> = EncodeFormat::ALL.iter().map(|f| f.extension()).collect(); + assert_eq!(extensions, ["wav", "aif", "flac", "m4a", "m4a", "mp3", "ac3", "eac3", "ogg"]); assert_eq!(EncodeFormat::Aiff.mime_type(), "audio/aiff"); } diff --git a/crates/audio/tests/audio/encode_format.rs b/crates/audio/tests/audio/encode_format.rs index 098dc025a..e91f93623 100644 --- a/crates/audio/tests/audio/encode_format.rs +++ b/crates/audio/tests/audio/encode_format.rs @@ -14,7 +14,15 @@ fn only_formats_the_encoder_writes_are_named() { for name in craft_audio::ENCODE_EXTENSIONS { assert!(encode_format_for(name).is_some(), "{name}"); } - for other in ["mp3", "m4a", "xyz", ""] { + for other in ["opus", "aac", "mp4", "xyz", ""] { assert_eq!(encode_format_for(other), None, "{other}"); } } + +#[test] +fn ogg_names_follow_the_native_feature() { + let built = cfg!(all(feature = "vorbis-encode", not(target_arch = "wasm32"))); + assert_eq!(EncodeFormat::Ogg.is_built(), built); + assert_eq!(encode_format_for("ogg"), built.then_some(EncodeFormat::Ogg)); + assert_eq!(craft_audio::ENCODE_EXTENSIONS.contains(&"ogg"), built); +} diff --git a/crates/audio/tests/codecs/file_formats.rs b/crates/audio/tests/codecs/file_formats.rs new file mode 100644 index 000000000..53947df12 --- /dev/null +++ b/crates/audio/tests/codecs/file_formats.rs @@ -0,0 +1,571 @@ +//! The coded formats of `encode` and `FileEncoder` (AAC and ALAC in M4A; MP3, AC-3 and E-AC-3 +//! streams), read back with this crate's decoder and, where ffmpeg is installed, with ffmpeg. + +use std::io::{Cursor, Seek, SeekFrom, Write}; +use std::process::Command; + +use craft_audio::{ + AudioBuffer, AudioError, BitDepth, ChannelLayout, CodecId, Container, ENCODE_EXTENSIONS, EncodeFormat, EncodeOptions, FileEncoder, RateControl, + decode, encode, encode_format_for, +}; + +const CODED: [EncodeFormat; 5] = [EncodeFormat::Aac, EncodeFormat::Alac, EncodeFormat::Mp3, EncodeFormat::Ac3, EncodeFormat::Eac3]; + +/// `channels` channels of `frames` frames, each channel a tone of its own frequency, on the +/// 16-bit grid (so a lossless 16-bit format returns it exactly). +fn tones(sample_rate: u32, channels: usize, frames: usize) -> AudioBuffer { + let channel = |c: usize| { + let step = (60.0 + 17.0 * c as f32) * std::f32::consts::TAU / sample_rate as f32; + (0..frames).map(|i| ((i as f32 * step).sin() * 0.4 * 32768.0).round() / 32768.0).collect() + }; + AudioBuffer { sample_rate, channels: (0..channels).map(channel).collect() } +} + +/// Signal-to-noise ratio in dB of `got` against `want`, over the frames both have. +fn snr(want: &[f32], got: &[f32]) -> f64 { + let (signal, noise) = + want.iter().zip(got).fold((0f64, 0f64), |(s, n), (&w, &g)| (s + f64::from(w).powi(2), n + (f64::from(w) - f64::from(g)).powi(2))); + 10.0 * (signal / noise.max(1e-30)).log10() +} + +fn is_unsupported(r: Result) -> bool { + matches!(r, Err(AudioError::Unsupported(_))) +} + +/// An AAC file decodes to the frames that were written, in place: the edit list skips the +/// encoder's priming and cuts the padding of the last packet. +#[test] +fn aac_decodes_to_the_frames_written() { + for frames in [1, 1000, 1024, 1025, 5000, 48_017] { + let buf = tones(48_000, 2, frames); + let file = encode(&buf, &EncodeOptions::new(EncodeFormat::Aac)).unwrap(); + let (info, out) = decode(&file, Some("m4a")).unwrap(); + assert_eq!((info.container, info.codec, info.sample_rate, info.damaged), (Container::Mp4, CodecId::Aac, 48_000, 0)); + assert_eq!((info.frames, out.frames(), info.layout), (frames as u64, frames, ChannelLayout::STEREO), "{frames} frames"); + if frames >= 5000 { + for (want, got) in buf.channels.iter().zip(&out.channels) { + assert!(snr(want, got) > 30.0, "{frames} frames: {} dB", snr(want, got)); + } + } + } +} + +/// The M4A track's durations add up to the priming and the audio, and its one edit skips the +/// priming and lasts as long as the audio, in a movie timescale that is the sample rate. +#[test] +fn aac_timing_is_in_the_sample_table_and_the_edit_list() { + use craft_audio::craft_media::isobmff; + let file = encode(&tones(44_100, 1, 3000), &EncodeOptions::new(EncodeFormat::Aac)).unwrap(); + let mp4 = isobmff::open(file.as_slice()).unwrap(); + let track = &mp4.tracks[0]; + assert_eq!((mp4.timescale, track.timescale), (44_100, 44_100)); + assert_eq!(track.samples.iter().map(|s| u64::from(s.duration)).sum::(), 1024 + 3000); + assert_eq!(track.edits.len(), 1); + assert_eq!((track.edits[0].media_time, track.edits[0].segment_duration), (1024, 3000)); + // Every packet is in a `roll` group of distance -1, which Apple's decoders need to apply the edit. + let groups = &track.sample_groups; + assert_eq!((groups.len(), groups[0].grouping_type), (1, isobmff::FourCc(*b"roll"))); + assert_eq!((groups[0].runs.as_slice(), &groups[0].descriptions), ([(track.samples.len() as u32, 1)].as_slice(), &vec![vec![0xFF, 0xFF]])); +} + +/// The sample entry is completed when the file is: AAC's average bit rate, and the largest packet +/// and average bit rate in ALAC's cookie. +#[test] +fn the_sample_entry_describes_the_finished_stream() { + use craft_audio::craft_media::isobmff::{self, CodecConfig}; + let buf = tones(48_000, 2, 48_000); + let file = encode(&buf, &EncodeOptions::new(EncodeFormat::Aac).with_bitrate(96_000)).unwrap(); + let mp4 = isobmff::open(file.as_slice()).unwrap(); + let CodecConfig::Aac(aac) = &mp4.tracks[0].entries[0].codec else { panic!("not AAC") }; + assert!((90_000..100_000).contains(&aac.avg_bitrate), "{} bit/s", aac.avg_bitrate); + + let file = encode(&buf, &EncodeOptions::new(EncodeFormat::Alac).with_bit_depth(BitDepth::Int16)).unwrap(); + let mp4 = isobmff::open(file.as_slice()).unwrap(); + let CodecConfig::Alac { cookie } = &mp4.tracks[0].entries[0].codec else { panic!("not ALAC") }; + let config = craft_audio::alac::SpecificConfig::parse(cookie).unwrap(); + let largest = mp4.tracks[0].samples.iter().map(|s| s.size).max().unwrap(); + assert_eq!((config.max_frame_bytes, config.bit_depth, config.frame_length), (largest, 16, 4096)); + assert!(config.avg_bit_rate > 0); +} + +/// Dither reaches ALAC's quantizer: a level below one 16-bit step is silence without it. +#[test] +fn alac_dithers_when_asked() { + let buf = AudioBuffer { sample_rate: 48_000, channels: vec![vec![0.3 / 32768.0; 4096]] }; + let opts = EncodeOptions::new(EncodeFormat::Alac).with_bit_depth(BitDepth::Int16); + let (_, plain) = decode(&encode(&buf, &opts).unwrap(), Some("m4a")).unwrap(); + let (_, dithered) = decode(&encode(&buf, &opts.with_dither(true)).unwrap(), Some("m4a")).unwrap(); + assert!(plain.channels[0].iter().all(|&s| s == 0.0)); + assert!(dithered.channels[0].iter().any(|&s| s != 0.0)); +} + +/// ALAC returns the quantized samples exactly, for every channel count, with each channel where +/// its speaker is. +#[test] +fn alac_is_lossless_in_every_layout() { + for channels in 1..=8u8 { + let layout = ChannelLayout::from_mask(craft_audio::alac::channel_mask(channels)); + let buf = tones(44_100, usize::from(channels), 10_000); + let opts = EncodeOptions::new(EncodeFormat::Alac).with_bit_depth(BitDepth::Int16).with_layout(layout); + let file = encode(&buf, &opts).unwrap(); + let (info, out) = decode(&file, Some("m4a")).unwrap(); + assert_eq!((info.container, info.codec, info.frames, info.layout), (Container::Mp4, CodecId::Alac, 10_000, layout), "{channels} channels"); + assert_eq!(out, buf, "{channels} channels"); + } + // 24 bits, and a length that is no whole number of packets. + let buf = tones(96_000, 2, 4096 * 2 + 1); + let (_, out) = decode(&encode(&buf, &EncodeOptions::new(EncodeFormat::Alac)).unwrap(), Some("m4a")).unwrap(); + assert_eq!(out, buf); +} + +/// Channels go to the encoder by speaker. ALAC's 6.1 has the back centre last and AAC's has it +/// before the surround pair; 5.1 with side surrounds is the format's one 5.1. +#[test] +fn channels_reach_their_speakers() { + // WAV 6.1 with a back pair (L R C LFE Lb Rb Cb), the usual 7-channel layout, into ALAC: the same. + let buf = tones(48_000, 7, 8000); + let (info, out) = decode(&encode(&buf, &EncodeOptions::new(EncodeFormat::Alac).with_bit_depth(BitDepth::Int16)).unwrap(), Some("m4a")).unwrap(); + assert_eq!(info.layout, ChannelLayout::from_mask(0x13F)); + assert_eq!(out, buf); + // 5.1 with side surrounds decodes from ALAC as 5.1 with back surrounds, channel for channel. + let buf = tones(48_000, 6, 8000); + let opts = EncodeOptions::new(EncodeFormat::Alac).with_bit_depth(BitDepth::Int16).with_layout(ChannelLayout::SURROUND_5_1_SIDE); + assert_eq!(decode(&encode(&buf, &opts).unwrap(), Some("m4a")).unwrap().1, buf); + + // AAC: the decoded channel each input channel comes back as. 5.1 decodes in WAV order; the + // 6.1 and 7.1 streams have no speaker assignment and decode in AAC order (C L R Ls Rs Cs LFE + // and C Lc Rc L R Ls Rs LFE). + let wide = ChannelLayout::from_mask(0xFF); + let cases: [(usize, Option, &[usize]); 3] = + [(6, None, &[0, 1, 2, 3, 4, 5]), (7, None, &[2, 0, 1, 4, 5, 6, 3]), (8, Some(wide), &[2, 6, 7, 0, 1, 4, 5, 3])]; + for (channels, layout, decoded_from) in cases { + let buf = tones(48_000, channels, 48_000); + let mut opts = EncodeOptions::new(EncodeFormat::Aac); + opts.layout = layout; + let (info, out) = decode(&encode(&buf, &opts).unwrap(), Some("m4a")).unwrap(); + assert_eq!((usize::from(info.channels), out.frames()), (channels, 48_000)); + for (decoded, &input) in decoded_from.iter().enumerate() { + let db = snr(&buf.channels[input], &out.channels[decoded]); + assert!(db > 20.0, "{channels} channels: decoded {decoded} is not input {input} ({db} dB)"); + } + } +} + +/// MP3, AC-3 and E-AC-3 are raw streams: the audio comes back behind the codec's delay. +#[test] +fn raw_streams_decode_behind_the_codec_delay() { + let buf = tones(48_000, 2, 48_000); + for (format, codec, container, delay, floor) in [ + (EncodeFormat::Mp3, CodecId::Mp3, Container::MpegAudio, craft_audio::mp3::DELAY as usize, 25.0), + (EncodeFormat::Ac3, CodecId::Ac3, Container::Ac3, 256, 15.0), + (EncodeFormat::Eac3, CodecId::Eac3, Container::Ac3, 256, 15.0), + ] { + let file = encode(&buf, &EncodeOptions::new(format)).unwrap(); + let (info, out) = decode(&file, Some(format.extension())).unwrap(); + assert_eq!((info.codec, info.container, info.sample_rate, info.layout, info.damaged), (codec, container, 48_000, ChannelLayout::STEREO, 0)); + assert_eq!((format.codec(), format.container()), (codec, container)); + assert!(out.frames() >= 48_000 + delay, "{format:?}: {} frames", out.frames()); + for (want, got) in buf.channels.iter().zip(&out.channels) { + let db = snr(want, &got[delay..]); + assert!(db > floor, "{format:?}: {db} dB"); + } + } + // 5.1 with back surrounds is AC-3's 3/2 mode with an LFE, channel for channel. + let buf = tones(48_000, 6, 48_000); + let (info, out) = decode(&encode(&buf, &EncodeOptions::new(EncodeFormat::Ac3)).unwrap(), Some("ac3")).unwrap(); + assert_eq!(info.layout, ChannelLayout::SURROUND_5_1_SIDE); + for (c, (want, got)) in buf.channels.iter().zip(&out.channels).enumerate() { + // The encoder low-pass filters the LFE, which shifts its phase: it is compared by level. + if c == 3 { + let rms = |x: &[f32]| (x.iter().map(|&s| f64::from(s).powi(2)).sum::() / x.len() as f64).sqrt(); + let level = rms(&got[4800..]) / rms(want); + assert!((0.5..1.5).contains(&level), "LFE at {level} of its level"); + } else { + assert!(snr(want, &got[256..]) > 12.0, "channel {c}: {} dB", snr(want, &got[256..])); + } + } + // 7.1 is an E-AC-3 programme. + let buf = tones(48_000, 8, 4800); + assert!(is_unsupported(encode(&buf, &EncodeOptions::new(EncodeFormat::Ac3)))); + // (Read back by ffmpeg in `ffmpeg_reads_the_files`: this crate's file reader plays the 5.1 + // independent substream of a raw 7.1 stream.) + assert!(!encode(&buf, &EncodeOptions::new(EncodeFormat::Eac3)).unwrap().is_empty()); +} + +/// Blocks of any sizes give the bytes `encode` gives. +#[test] +fn streaming_writes_the_bytes_of_encode() { + let buf = tones(44_100, 2, 30_000); + for format in CODED { + let opts = EncodeOptions::new(format); + let whole = encode(&buf, &opts).unwrap(); + let mut enc = FileEncoder::new(Cursor::new(Vec::new()), &opts, 44_100, 2).unwrap(); + assert!(format!("{enc:?}").contains(&format!("{format:?}"))); + let mut at = 0; + for n in [1, 4095, 4097, 0, 9000, 12_807] { + let block: Vec<&[f32]> = buf.channels.iter().map(|c| &c[at..]).collect(); + enc.check_room(n as u64).unwrap(); + enc.write(&block, n).unwrap(); + at += n; + } + assert_eq!(at, buf.frames()); + assert!(enc.finish().unwrap().into_inner() == whole, "{format:?}"); + } +} + +/// A file starts at the sink's position. +#[test] +fn a_file_starts_at_the_sinks_position() { + let buf = tones(48_000, 1, 6000); + for format in CODED { + let opts = EncodeOptions::new(format); + let mut sink = Cursor::new(b"junk".to_vec()); + sink.seek(SeekFrom::End(0)).unwrap(); + let mut enc = FileEncoder::new(sink, &opts, 48_000, 1).unwrap(); + enc.write(&buf.channels, 6000).unwrap(); + let mut sink = enc.finish().unwrap(); + let end = sink.stream_position().unwrap(); + let bytes = sink.into_inner(); + assert_eq!(end, bytes.len() as u64, "{format:?}"); + assert!(bytes[4..] == encode(&buf, &opts).unwrap(), "{format:?}"); + } +} + +/// A file with no audio is still a file of its format. +#[test] +fn no_audio_gives_an_empty_file() { + let buf = AudioBuffer::new(48_000, 2, 0); + for format in CODED { + let file = encode(&buf, &EncodeOptions::new(format)).unwrap(); + if format.container() == Container::Mp4 { + let mp4 = craft_audio::craft_media::isobmff::open(file.as_slice()).unwrap(); + assert_eq!((mp4.tracks.len(), mp4.tracks[0].samples.len(), mp4.tracks[0].edits.len()), (1, 0, 0), "{format:?}"); + } else { + assert!(file.is_empty(), "{format:?}"); + } + } +} + +/// The bit rate asked for is the one written, moved to the nearest the format has. +#[test] +fn the_bit_rate_is_followed() { + let buf = tones(48_000, 2, 48_000); + let size = |opts: &EncodeOptions| encode(&buf, opts).unwrap().len(); + // MP3 at 48 kHz: 24 bytes per frame and kbit/s, 43 frames for a second and the delay. + assert_eq!(size(&EncodeOptions::new(EncodeFormat::Mp3)), 43 * 192 * 3); + assert_eq!(size(&EncodeOptions::new(EncodeFormat::Mp3).with_bitrate(130_000)), 43 * 128 * 3); + assert_eq!(size(&EncodeOptions::new(EncodeFormat::Mp3).with_bitrate(1)), 43 * 32 * 3); + assert_eq!(size(&EncodeOptions::new(EncodeFormat::Mp3).with_bitrate(u32::MAX)), 43 * 320 * 3); + // AC-3: 32 packets of 1536 frames, 4 bytes per packet and kbit/s. + assert_eq!(size(&EncodeOptions::new(EncodeFormat::Ac3)), 32 * 192 * 4); + assert_eq!(size(&EncodeOptions::new(EncodeFormat::Ac3).with_bitrate(100_000)), 32 * 96 * 4); + assert_eq!(size(&EncodeOptions::new(EncodeFormat::Eac3).with_bitrate(100_000)), 32 * 100 * 4); + // AAC: a second of audio at about the rate asked for, around the 128 kbit/s default. + let (low, default, high) = ( + size(&EncodeOptions::new(EncodeFormat::Aac).with_bitrate(64_000)), + size(&EncodeOptions::new(EncodeFormat::Aac)), + size(&EncodeOptions::new(EncodeFormat::Aac).with_bitrate(256_000)), + ); + assert!((7000..10_000).contains(&low) && (15_000..18_500).contains(&default) && (30_000..35_000).contains(&high), "{low} {default} {high}"); + // A rate no stream can carry is moved into range, not refused. + encode(&buf, &EncodeOptions::new(EncodeFormat::Aac).with_bitrate(u32::MAX)).unwrap(); + // The lowest rate still codes the audio: the encoder accepts rates that leave only silence + // (128 for 128 kbit/s is one), and the writer does not go that low. + for (sample_rate, channels) in [(8000, 1), (16_000, 2), (44_100, 2), (48_000, 6), (96_000, 2)] { + let buf = tones(sample_rate, channels, sample_rate as usize); + let (_, out) = decode(&encode(&buf, &EncodeOptions::new(EncodeFormat::Aac).with_bitrate(128)).unwrap(), Some("m4a")).unwrap(); + let rms = |x: &[f32]| (x.iter().map(|&s| f64::from(s).powi(2)).sum::() / x.len() as f64).sqrt(); + let level = rms(&out.channels[0]) / rms(&buf.channels[0]); + assert!(level > 0.5, "{sample_rate} Hz, {channels} channels: {level} of the level"); + } + // Quality is AAC's; ALAC has no rate to control. + let (small, large) = + (size(&EncodeOptions::new(EncodeFormat::Aac).with_quality(0.0)), size(&EncodeOptions::new(EncodeFormat::Aac).with_quality(1.0))); + assert!(small < large, "{small} {large}"); + assert!(matches!(encode(&buf, &EncodeOptions::new(EncodeFormat::Aac).with_quality(f32::NAN)), Err(AudioError::Encode(_)))); + for format in [EncodeFormat::Mp3, EncodeFormat::Ac3, EncodeFormat::Eac3] { + assert!(is_unsupported(encode(&buf, &EncodeOptions::new(format).with_quality(0.5))), "{format:?}"); + } + let alac = EncodeOptions::new(EncodeFormat::Alac); + assert_eq!(size(&alac), size(&alac.clone().with_bitrate(64_000))); + assert_eq!(alac.rate_control, None); + assert_eq!(alac.with_quality(0.5).rate_control, Some(RateControl::Quality(0.5))); +} + +/// What a format cannot hold is refused before anything is written. +#[test] +fn what_a_format_cannot_hold_is_refused() { + let refused = |format, sample_rate, channels: u16, layout: Option| { + let mut opts = EncodeOptions::new(format); + opts.layout = layout; + let mut sink = Cursor::new(Vec::new()); + let r = FileEncoder::new(&mut sink, &opts, sample_rate, channels).map(drop); + assert!(r.is_ok() || sink.into_inner().is_empty(), "{format:?}: bytes written for a refused file"); + assert_eq!(r.is_err(), encode(&AudioBuffer::new(sample_rate, channels, 100), &opts).is_err()); + r + }; + // Quad has no centre, which AAC's and ALAC's four channels have. + assert!(is_unsupported(refused(EncodeFormat::Aac, 48_000, 4, None))); + assert!(is_unsupported(refused(EncodeFormat::Alac, 48_000, 4, None))); + refused(EncodeFormat::Aac, 48_000, 4, Some(ChannelLayout::from_mask(0x107))).unwrap(); + // WAV 7.1 (backs and sides) is not the 7.1 of AAC and ALAC (a front pair left and right of centre). + assert!(is_unsupported(refused(EncodeFormat::Aac, 48_000, 8, None))); + assert!(is_unsupported(refused(EncodeFormat::Alac, 48_000, 8, Some(ChannelLayout::SURROUND_7_1)))); + assert!(is_unsupported(refused(EncodeFormat::Aac, 48_000, 2, Some(ChannelLayout::unordered(2))))); + for format in CODED { + assert!(is_unsupported(refused(format, 48_000, 9, None)), "{format:?}"); + assert!(matches!(refused(format, 48_000, 2, Some(ChannelLayout::MONO)), Err(AudioError::Encode(_))), "{format:?}"); + assert!(matches!(refused(format, 0, 2, None), Err(AudioError::Encode(_))), "{format:?}"); + assert!(matches!(refused(format, 48_000, 0, None), Err(AudioError::Encode(_))), "{format:?}"); + } + assert!(is_unsupported(refused(EncodeFormat::Mp3, 48_000, 3, None))); + assert!(is_unsupported(refused(EncodeFormat::Ac3, 48_000, 7, None))); + // Sample rates the codec has not. + for format in [EncodeFormat::Aac, EncodeFormat::Mp3, EncodeFormat::Ac3, EncodeFormat::Eac3] { + assert!(matches!(refused(format, 44_000, 2, None), Err(AudioError::Encode(_))), "{format:?}"); + } + refused(EncodeFormat::Alac, 44_000, 2, None).unwrap(); + let float = EncodeOptions::new(EncodeFormat::Alac).with_bit_depth(BitDepth::Float32); + assert!(is_unsupported(encode(&AudioBuffer::new(48_000, 2, 100), &float))); +} + +/// Samples no encoder should see: NaN is silence, and values past full scale do not fail a +/// write (AC-3, whose encoder refuses them, gets them clamped). +#[test] +fn non_finite_and_loud_samples_are_written() { + let mut buf = tones(48_000, 2, 6000); + buf.channels[0][100] = f32::NAN; + buf.channels[0][200] = f32::INFINITY; + buf.channels[1][300] = -7.5; + for format in CODED { + let file = encode(&buf, &EncodeOptions::new(format)).unwrap(); + let (_, out) = decode(&file, Some(format.extension())).unwrap(); + assert!(out.channels.iter().flatten().all(|s| s.is_finite()), "{format:?}"); + } +} + +/// A sink that fails once `room` bytes are written. +struct Full { + inner: Cursor>, + room: usize, +} + +impl Write for Full { + fn write(&mut self, data: &[u8]) -> std::io::Result { + if self.inner.get_ref().len() + data.len() > self.room { + return Err(std::io::Error::other("disk full")); + } + self.inner.write(data) + } + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } +} + +impl Seek for Full { + fn seek(&mut self, pos: SeekFrom) -> std::io::Result { + self.inner.seek(pos) + } +} + +/// After a failed write the encoder is unusable: later writes and `finish` fail too. +#[test] +fn a_failed_write_ends_the_encoder() { + let buf = tones(48_000, 2, 48_000); + for format in CODED { + let mut enc = FileEncoder::new(Full { inner: Cursor::new(Vec::new()), room: 2000 }, &EncodeOptions::new(format), 48_000, 2).unwrap(); + assert!(matches!(enc.write(&buf.channels, 48_000), Err(AudioError::Io(_))), "{format:?}"); + assert!(matches!(enc.write(&buf.channels, 1), Err(AudioError::Encode(_))), "{format:?}"); + assert!(matches!(enc.finish(), Err(AudioError::Encode(_))), "{format:?}"); + } +} + +#[test] +fn the_formats_are_named() { + assert_eq!(encode_format_for("m4a"), Some(EncodeFormat::Aac)); + assert_eq!(encode_format_for(".ALAC"), Some(EncodeFormat::Alac)); + assert_eq!(encode_format_for("mp3"), Some(EncodeFormat::Mp3)); + assert_eq!(encode_format_for("ac3"), Some(EncodeFormat::Ac3)); + assert_eq!(encode_format_for("eac3"), Some(EncodeFormat::Eac3)); + assert_eq!(encode_format_for("ec3"), Some(EncodeFormat::Eac3)); + for &format in EncodeFormat::ALL { + assert!(ENCODE_EXTENSIONS.contains(&format.extension()), "{format:?}"); + let named = encode_format_for(format.extension()).unwrap(); + assert_eq!((named.extension(), named.mime_type()), (format.extension(), format.mime_type())); + assert_eq!(format.is_lossy(), !matches!(format.codec(), CodecId::Pcm | CodecId::Flac | CodecId::Alac)); + } + for name in ENCODE_EXTENSIONS { + assert!(encode_format_for(name).is_some(), "{name}"); + } + assert_eq!(ENCODE_EXTENSIONS.len(), 12); + for format in CODED { + assert!(EncodeFormat::ALL.contains(&format) && format.is_built()); + } + let features: Vec<_> = EncodeFormat::ALL.iter().map(|f| f.feature()).collect(); + assert_eq!(features, ["pcm-encode", "pcm-encode", "flac-encode", "alac-encode", "aac-encode", "mp3-encode", "ac3-encode", "eac3-encode"]); +} + +/// ffmpeg's decode of `file` as interleaved `f32`, split into `channels` channels. +fn ffmpeg_decode(ff: &std::path::Path, file: &[u8], name: &str, channels: usize) -> Vec> { + let path = crate::testkit::fixtures_dir("file_formats").join(name); + std::fs::write(&path, file).unwrap(); + let out = Command::new(ff).args(["-v", "error", "-i"]).arg(&path).args(["-f", "f32le", "-"]).output().unwrap(); + assert!(out.status.success() && out.stderr.is_empty(), "{name}: {}", String::from_utf8_lossy(&out.stderr)); + let mut planar = vec![Vec::new(); channels]; + for (i, s) in out.stdout.as_chunks::<4>().0.iter().enumerate() { + planar[i % channels].push(f32::from_le_bytes(*s)); + } + planar +} + +/// ffmpeg reads every format: the M4A files without their priming (it applies the edit list) and, +/// from ffmpeg 9, at the length written; ALAC exactly; the raw streams behind their delay. +#[test] +fn ffmpeg_reads_the_files() { + let ff = require_ffmpeg!(); + for (channels, layout) in [(1, None), (2, None), (6, None), (8, Some(ChannelLayout::from_mask(0xFF)))] { + let buf = tones(48_000, channels, 20_000); + let mut opts = EncodeOptions::new(EncodeFormat::Aac); + opts.layout = layout; + let out = ffmpeg_decode(&ff, &encode(&buf, &opts).unwrap(), &format!("aac_{channels}.m4a"), channels); + // The priming is skipped by every ffmpeg (the channels below line up). The padding of the + // last packet is cut by ffmpeg 9; ffmpeg 8 plays it, to the end of the 20th packet. + assert!([20_000, 20_480].contains(&out[0].len()), "AAC, {channels} channels: {} frames", out[0].len()); + if channels <= 6 { + // ffmpeg also returns 5.1 in WAV order. + for (c, (want, got)) in buf.channels.iter().zip(&out).enumerate() { + assert!(snr(want, got) > 20.0, "AAC, {channels} channels, channel {c}: {} dB", snr(want, got)); + } + } + let opts = opts.with_format(EncodeFormat::Alac).with_bit_depth(BitDepth::Int16); + let opts = if channels == 6 { opts.with_layout(ChannelLayout::SURROUND_5_1) } else { opts }; + let out = ffmpeg_decode(&ff, &encode(&buf, &opts).unwrap(), &format!("alac_{channels}.m4a"), channels); + assert!(out == buf.channels, "ALAC, {channels} channels"); + } + let buf = tones(44_100, 2, 20_000); + for (format, delay, floor) in + [(EncodeFormat::Mp3, craft_audio::mp3::DELAY as usize, 25.0), (EncodeFormat::Ac3, 256, 15.0), (EncodeFormat::Eac3, 256, 15.0)] + { + let name = format!("stream.{}", format.extension()); + let out = ffmpeg_decode(&ff, &encode(&buf, &EncodeOptions::new(format)).unwrap(), &name, 2); + for (want, got) in buf.channels.iter().zip(&out) { + assert!( + got.len() >= 20_000 + delay && snr(want, &got[delay..]) > floor, + "{format:?}: {} frames, {} dB", + got.len(), + snr(want, &got[delay..]) + ); + } + } + // E-AC-3 7.1: the front channels, which the 5.1 downmix leaves as they are. + let buf = tones(48_000, 8, 20_000); + let out = ffmpeg_decode(&ff, &encode(&buf, &EncodeOptions::new(EncodeFormat::Eac3)).unwrap(), "stream_7_1.eac3", 8); + assert!(out[7].len() >= 20_256); + for c in 0..3 { + assert!(snr(&buf.channels[c], &out[c][256..]) > 12.0, "E-AC-3 7.1, channel {c}: {} dB", snr(&buf.channels[c], &out[c][256..])); + } +} + +/// Apple's decoder (macOS `afinfo`; skipped elsewhere) takes the priming and the length of an AAC +/// file from the edit list, which it does only for a track with a `roll` sample group. +#[test] +fn apple_reads_the_aac_priming_and_length() { + let Some(afinfo) = crate::testkit::oracle::find_tool("afinfo") else { return }; + for (frames, remainder) in [(20_000, 480), (1, 1023), (4096, 0)] { + let path = crate::testkit::fixtures_dir("file_formats").join(format!("apple_{frames}.m4a")); + std::fs::write(&path, encode(&tones(48_000, 2, frames), &EncodeOptions::new(EncodeFormat::Aac)).unwrap()).unwrap(); + let out = Command::new(&afinfo).arg(&path).output().unwrap(); + let text = String::from_utf8_lossy(&out.stdout); + assert!(text.contains(&format!("{frames} valid frames + 1024 priming + {remainder} remainder")), "{frames} frames: {text}"); + } +} + +/// What `EncodeFormat` says a format takes is what the writer accepts: for every format, a file +/// can be begun exactly when the sample rate, the channel count and the layout are listed. +#[test] +fn the_format_queries_agree_with_the_writer() { + // Every rate a format lists, each with its neighbours, and some that none lists. + let mut rates: Vec = EncodeFormat::ALL.iter().filter_map(|f| f.sample_rates()).flatten().flat_map(|&r| [r - 1, r, r + 1]).collect(); + rates.extend([44_000, 176_400, 192_000]); + rates.sort_unstable(); + rates.dedup(); + let masks = [ + 0x4, 0x1, 0x3, 0xC, 0x600, 0x7, 0xB, 0x103, 0x33, 0x107, 0x603, 0xF, 0x10B, 0x37, 0x607, 0x10F, 0x60B, 0x3F, 0x60F, 0x13F, 0x70F, 0x63F, 0xFF, + ]; + let layouts = masks.iter().map(|&mask| ChannelLayout::from_mask(mask)).chain((1..=9).map(ChannelLayout::unordered)); + for layout in layouts { + for &format in EncodeFormat::ALL { + // 24 bits is a depth every format has. + let opts = EncodeOptions::new(format).with_layout(layout); + for &sample_rate in &rates { + let listed = format.sample_rates().is_none_or(|r| r.contains(&sample_rate)) + && layout.channels() <= format.max_channels() + && format.supports_layout(&layout); + let checked = opts.check(sample_rate, layout.channels()); + assert_eq!(checked.is_ok(), listed, "{format:?}, {sample_rate} Hz, {layout:?}: {checked:?}"); + let begun = FileEncoder::new(Cursor::new(Vec::new()), &opts, sample_rate, layout.channels()); + assert_eq!(begun.is_ok(), listed, "{format:?}, {sample_rate} Hz, {layout:?}"); + } + if let Some(fixed) = format.fixed_layouts(layout.channels()) { + assert!(fixed.iter().all(|l| format.supports_layout(l) && l.channels() == layout.channels())); + assert_eq!(format.supports_layout(&layout), fixed.iter().any(|l| same_speakers(l, &layout)), "{format:?}, {layout:?}"); + } + } + } + for &format in EncodeFormat::ALL { + assert!(!format.supports_layout(&ChannelLayout::unordered(0))); + assert_eq!(format.supports_quality(), EncodeOptions::new(format).with_quality(0.5).check(48_000, 2).is_ok() && format.is_lossy()); + assert_eq!(format.fixed_layouts(2).is_none(), matches!(format, EncodeFormat::Wav | EncodeFormat::Aiff | EncodeFormat::Flac)); + } + // The most channels is the most: one more is refused, by the layouts of a format that has + // a fixed set and by the writer of one that has not. + let most: Vec = EncodeFormat::ALL.iter().map(|f| f.max_channels()).collect(); + assert_eq!(most, [32_767, 32_767, 8, 8, 8, 2, 6, 8]); + for &format in EncodeFormat::ALL { + let most = format.max_channels(); + match format.fixed_layouts(most) { + Some(layouts) => assert!(!layouts.is_empty() && format.fixed_layouts(most + 1).unwrap().is_empty(), "{format:?}"), + None => { + let opts = EncodeOptions::new(format).with_bit_depth(BitDepth::Int16); + assert!(opts.check(48_000, most).is_ok() && opts.check(48_000, most + 1).is_err(), "{format:?}"); + assert!(format.supports_layout(&ChannelLayout::unordered(most)) && !format.supports_layout(&ChannelLayout::unordered(most + 1))); + } + } + } + assert_eq!(EncodeFormat::Ac3.fixed_layouts(3).unwrap().len(), 3); + assert!(EncodeFormat::Aac.fixed_layouts(9).unwrap().is_empty()); +} + +/// Whether two layouts name the same speakers, a surround pair being either sides or backs. +fn same_speakers(a: &ChannelLayout, b: &ChannelLayout) -> bool { + let one_pair = |mask: u32| if mask & 0x630 == 0x30 { mask & !0x30 | 0x600 } else { mask }; + matches!((a.mask(), b.mask()), (Some(a), Some(b)) if one_pair(a) == one_pair(b)) +} + +/// `bitrate` is the rate the file is written at, for the options and for the encoder they give. +#[test] +fn the_bit_rate_used_can_be_asked_for() { + let rate = |opts: EncodeOptions, sample_rate, channels| { + let asked = opts.bitrate(sample_rate, channels).unwrap(); + assert_eq!(FileEncoder::new(Cursor::new(Vec::new()), &opts, sample_rate, channels).unwrap().bitrate(), asked); + asked + }; + assert_eq!(rate(EncodeOptions::new(EncodeFormat::Mp3), 48_000, 2), Some(192_000)); + assert_eq!(rate(EncodeOptions::new(EncodeFormat::Mp3).with_bitrate(130_000), 48_000, 2), Some(128_000)); + assert_eq!(rate(EncodeOptions::new(EncodeFormat::Mp3).with_bitrate(u32::MAX), 22_050, 1), Some(160_000)); + assert_eq!(rate(EncodeOptions::new(EncodeFormat::Ac3).with_bitrate(191_999), 48_000, 2), Some(160_000)); + assert_eq!(rate(EncodeOptions::new(EncodeFormat::Ac3), 48_000, 6), Some(448_000)); + assert_eq!(rate(EncodeOptions::new(EncodeFormat::Eac3).with_bitrate(100_999), 48_000, 2), Some(100_000)); + assert_eq!(rate(EncodeOptions::new(EncodeFormat::Aac), 48_000, 2), Some(128_000)); + assert_eq!(rate(EncodeOptions::new(EncodeFormat::Aac).with_bitrate(128), 48_000, 2), Some(24_000)); + assert_eq!(rate(EncodeOptions::new(EncodeFormat::Aac).with_bitrate(u32::MAX), 8000, 1), Some(48_000)); + assert_eq!(rate(EncodeOptions::new(EncodeFormat::Aac).with_quality(0.5), 48_000, 2), None); + for format in [EncodeFormat::Wav, EncodeFormat::Aiff, EncodeFormat::Flac, EncodeFormat::Alac] { + assert_eq!(rate(EncodeOptions::new(format).with_bitrate(96_000), 48_000, 2), None, "{format:?}"); + } + // The size of a second of MP3 is the rate reported. + let buf = tones(48_000, 2, 48_000); + let opts = EncodeOptions::new(EncodeFormat::Mp3).with_bitrate(130_000); + assert_eq!(encode(&buf, &opts).unwrap().len() as u32, 43 * 1152 / 8 * opts.bitrate(48_000, 2).unwrap().unwrap() / 48_000); + assert!(is_unsupported(EncodeOptions::new(EncodeFormat::Mp3).bitrate(48_000, 6))); +} diff --git a/crates/audio/tests/codecs/main.rs b/crates/audio/tests/codecs/main.rs index 279246dc6..c1c9e2131 100644 --- a/crates/audio/tests/codecs/main.rs +++ b/crates/audio/tests/codecs/main.rs @@ -12,6 +12,7 @@ mod ac3_encoder_oracle; mod ac3_oracle; mod alac; mod concealment; +mod file_formats; mod mp3_decoder_oracle; mod mp3_encoder_oracle; mod mpa_oracle; diff --git a/crates/doc-binary/src/fib.rs b/crates/doc-binary/src/fib.rs index 7bb3ec149..4224f6962 100644 --- a/crates/doc-binary/src/fib.rs +++ b/crates/doc-binary/src/fib.rs @@ -138,7 +138,9 @@ impl Fib { } let w_ident = u16le(0); if w_ident == 0xA5DC { - return Err(DocbinError::NotWord("Word 6.0/95 format, only Word 97-2003 is supported".into())); + // `read` sends Word 6.0/95 streams to `word6` before this FIB is parsed. This keeps any + // other caller from reading one with the Word 97 layout, which it doesn't have. + return Err(DocbinError::NotWord("a Word 6.0/95 file doesn't have the Word 97 FIB layout".into())); } if w_ident != W_IDENT { return Err(DocbinError::NotWord(format!("magic 0x{w_ident:04X}, expected 0x{W_IDENT:04X}"))); diff --git a/crates/doc-binary/src/fkp.rs b/crates/doc-binary/src/fkp.rs index 3de272da2..3a795d4a4 100644 --- a/crates/doc-binary/src/fkp.rs +++ b/crates/doc-binary/src/fkp.rs @@ -29,6 +29,19 @@ pub(crate) struct Bins { sorted: bool, /// Page number (× 512 = byte offset in the WordDocument stream). pns: Vec, + /// Runs read ahead of time instead of pages (Word 6.0/95, whose FKPs and sprms are another + /// layout, translated by [`crate::word6`]): sorted by `lo`, not overlapping. + translated: Option>, +} + +/// One run of a [`Bins::translated`] table: the FCs `[lo, hi)` it covers, the paragraph style +/// (for PAPX runs) and a Word 97 grpprl. +#[derive(Clone, Debug, PartialEq)] +pub(crate) struct TranslatedRun { + pub(crate) lo: u32, + pub(crate) hi: u32, + pub(crate) istd: u16, + pub(crate) grpprl: Vec, } impl Bins { @@ -37,7 +50,7 @@ impl Bins { let fc = fc as usize; let lcb = lcb as usize; if lcb < 4 || !(lcb - 4).is_multiple_of(8) { - return Ok(Bins { fcs: Vec::new(), sorted: true, pns: Vec::new() }); + return Ok(Bins { fcs: Vec::new(), sorted: true, pns: Vec::new(), translated: None }); } let end = fc.checked_add(lcb).ok_or_else(|| DocbinError::Malformed("bin table overflow".into()))?; let plc = table.get(fc..end).ok_or_else(|| DocbinError::Malformed("bin table outside the Table stream".into()))?; @@ -56,7 +69,31 @@ impl Bins { pns.push(at(n + 1 + i).ok_or_else(|| DocbinError::Malformed("truncated bin aPn".into()))? & 0x003F_FFFF); } let sorted = fcs.windows(2).all(|w| w.first() <= w.get(1)); - Ok(Bins { fcs, sorted, pns }) + Ok(Bins { fcs, sorted, pns, translated: None }) + } + + /// A table of runs read ahead of time ([`TranslatedRun`]). Runs are sorted by start; one that + /// overlaps an earlier one (a corrupt file) or covers nothing is dropped. + pub(crate) fn translated(mut runs: Vec) -> Bins { + runs.sort_by_key(|r| r.lo); + let mut kept: Vec = Vec::with_capacity(runs.len()); + for r in runs { + if r.lo < r.hi && kept.last().is_none_or(|last| r.lo >= last.hi) { + kept.push(r); + } + } + Bins { fcs: Vec::new(), sorted: true, pns: Vec::new(), translated: Some(kept) } + } + + /// For a translated table: the run covering `fc`, or the gap `[lo, hi)` between runs that + /// holds it. + fn translated_at(runs: &[TranslatedRun], fc: u32) -> Result<&TranslatedRun, (u32, u32)> { + let i = runs.partition_point(|r| r.lo <= fc); + let prev = i.checked_sub(1).and_then(|i| runs.get(i)); + match prev { + Some(r) if fc < r.hi => Ok(r), + _ => Err((prev.map_or(0, |r| r.hi), runs.get(i).map_or(u32::MAX, |r| r.lo))), + } } /// The page containing `fc`, as a slice of the WordDocument stream, and the FC range @@ -77,13 +114,19 @@ impl Bins { /// The grpprl of the Chpx covering `fc` ([MS-DOC] ChpxFkp: rgfc u32s, rgb offsets ×2, /// crun in the last byte). - pub(crate) fn chpx<'w>(&self, word: &'w [u8], fc: u32) -> &'w [u8] { + pub(crate) fn chpx<'w>(&'w self, word: &'w [u8], fc: u32) -> &'w [u8] { self.chpx_run(word, fc).grpprl } /// [`Bins::chpx`] with the FC range over which the same grpprl is the answer, so a walk /// over consecutive characters looks it up once per run, not once per character. - pub(crate) fn chpx_run<'w>(&self, word: &'w [u8], fc: u32) -> ChpxRun<'w> { + pub(crate) fn chpx_run<'w>(&'w self, word: &'w [u8], fc: u32) -> ChpxRun<'w> { + if let Some(runs) = &self.translated { + return match Self::translated_at(runs, fc) { + Ok(r) => ChpxRun { grpprl: &r.grpprl, lo: r.lo, hi: r.hi }, + Err((lo, hi)) => ChpxRun { grpprl: &[], lo, hi }, + }; + } let none = ChpxRun { grpprl: &[], lo: 1, hi: 0 }; let Some((page, page_lo, page_hi)) = self.page(word, fc) else { return none }; let Some(&crun) = page.get(511) else { return none }; @@ -113,7 +156,13 @@ impl Bins { /// ([MS-DOC] PapxFkp: rgfc u32s — each the FC *following* a paragraph mark —, rgbx BX /// entries (1 offset byte + 12 PHE bytes), crun in the last byte; the PAPX itself is a /// PapxInFkp with the cb/cb' size quirk). Query with the FC of the character after the mark. - pub(crate) fn papx<'w>(&self, word: &'w [u8], fc: u32) -> (u16, &'w [u8]) { + pub(crate) fn papx<'w>(&'w self, word: &'w [u8], fc: u32) -> (u16, &'w [u8]) { + if let Some(runs) = &self.translated { + return match Self::translated_at(runs, fc) { + Ok(r) => (r.istd, &r.grpprl), + Err(_) => (0, &[]), + }; + } let Some((page, _, _)) = self.page(word, fc) else { return (0, &[]) }; let Some(&crun) = page.get(511) else { return (0, &[]) }; // BX is 13 bytes; cap crun at what fits before byte 511. diff --git a/crates/doc-binary/src/lib.rs b/crates/doc-binary/src/lib.rs index 386e753f5..b2d7f3729 100644 --- a/crates/doc-binary/src/lib.rs +++ b/crates/doc-binary/src/lib.rs @@ -6,6 +6,9 @@ //! like the other Craft parsers, is lenient by design (unknown structures are skipped, bad numbers //! fall back to defaults) and bounded (stream sizes, piece counts and property counts are //! capped), so hostile input yields an error or a best-effort document, never a panic. +//! +//! Word 6.0/95 files, which that specification does not cover, are read by `word6` from their +//! observed layout: their text, formatting and tables so far. #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] // The code came from an app whose lints allow this. #![allow(clippy::type_complexity)] @@ -29,6 +32,7 @@ mod sections; mod sprm; mod stsh; mod table; +mod word6; use std::sync::Arc; @@ -220,6 +224,9 @@ fn read_limited(bytes: &[u8], password: Option<&str>, max_paragraphs: usize) -> if word.len() as u64 > MAX_STREAM { return Err(DocbinError::Limit(format!("WordDocument stream is {} bytes", word.len()))); } + if word6::is_word6(&word) { + return read_word6(&comp, &word, max_paragraphs); + } let enc = fib::peek_encryption(&word); // The FIB names the Table stream (0Table or 1Table); fall back to whichever exists, // since files written by other writers sometimes carry the wrong bit. @@ -431,6 +438,55 @@ fn read_limited(bytes: &[u8], password: Option<&str>, max_paragraphs: usize) -> Ok(doc) } +/// Read a Word 6.0/95 file ([`word6`]): its main text, with its character and paragraph +/// formatting and table rows translated to Word 97 sprms, through the same walk as a Word 97 file. +/// Its paragraph styles are read too; character styles and the other stories are not yet, and a +/// section mark reads as a page break. +fn read_word6(comp: &craft_cfb::CompoundFile<'_>, word: &[u8], max_paragraphs: usize) -> Result { + let fib = word6::Fib6::parse(word)?; + let content = word6::content(word, &fib)?; + let mut doc = Document::new(); + // Word opens these files in compatibility mode 11 too. + doc.settings.compat_mode = 11; + if let Some(core) = metadata::read(comp) { + doc.core = core; + } + doc.body.clear(); + // The paragraph styles, built as for Word 97 files from their translated formatting. + doc.styles = Arc::new(fmt::stylesheet(&content.styles, &content.fonts, stsh::StandardFonts::default())); + let sheet: StyleSheet = StyleSheet::clone(&doc.styles); + // As for Word 97 files: the text read can't exceed twice the stream, nor the absolute ceiling. + let budget = std::cell::Cell::new((word.len() as u64).saturating_mul(2).min(MAX_TOTAL_CHARS)); + let work = Work::new(MAX_WORK); + let paragraphs = ParaBudget { left: std::cell::Cell::new(max_paragraphs), exceeded: std::cell::Cell::new(false) }; + let mut pics = media::Pictures::for_input(word.len()); + let ctx = WalkCtx { + word, + data: &[], + pieces: &content.pieces, + chpx_bins: &content.chpx, + papx_bins: &content.papx, + fonts: &content.fonts, + raw_styles: &content.styles, + sheet: &sheet, + notes: &[], + marks: &[], + drawn: &[], + sect_ends: &[], + budget: &budget, + paragraphs: ¶graphs, + work: &work, + }; + let paras = walk(&ctx, 0, fib.ccp_text, &mut pics); + if paragraphs.exceeded.get() { + return Err(paragraph_limit(max_paragraphs)); + } + doc.body = table::assemble(paras); + doc.ensure_nonempty(); + work.log(); + Ok(doc) +} + fn paragraph_limit(max_paragraphs: usize) -> DocbinError { DocbinError::Limit(format!("the document has more than {max_paragraphs} paragraphs and table cells")) } @@ -648,6 +704,20 @@ fn walk<'a>(ctx: &WalkCtx<'a>, cp_start: u32, cp_end: u32, pics: &mut media::Pic // querying at the mark's own FC finds that paragraph's properties. let fc_mark = ctx.pieces.fc_of_cp(cp).unwrap_or(0); let (istd, papx) = ctx.papx_bins.papx(ctx.word, fc_mark); + // The Prm of the piece holding the mark adds its paragraph sprms after the PAPX's + // ([MS-DOC] §2.4.6.1, step 5), as Word 6/95's fast saves record a paragraph change. + let extra = prm_paragraph_sprms(ctx.pieces.piece_prm(cp), ctx.work); + let combined; + let papx: &[u8] = if extra.is_empty() { + papx + } else { + // Re-framed from its Prls, so that a pad byte at its end can't swallow the + // first appended opcode. + let mut v: Vec = sprm::iter(papx).flat_map(|p| p.op.to_le_bytes().into_iter().chain(p.operand.iter().copied())).collect(); + v.extend_from_slice(&extra); + combined = v; + &combined + }; if let Some(st) = ctx.raw_styles.get(istd as usize).filter(|s| !s.name.is_empty()) { pb.props.style = Some(fmt::style_id(&st.name)); } @@ -991,13 +1061,42 @@ struct FmtState<'a> { /// paragraph style stay the same, so a Prm grpprl (up to 64 KB) is interpreted once per /// formatting change, not once per character (#3692). struct FmtMemo<'a> { - /// The CHPX grpprl, compared by address: it is a slice of the WordDocument stream. + /// The CHPX grpprl, compared by address: a slice of the WordDocument stream, or of a Word 6.0/95 + /// file's translated runs, both of which stay put for the walk. chpx: &'a [u8], prm: u16, style: Option, props: CharProps, } +/// The paragraph and table sprms (sgc 1 and 5) of a piece's Prm, as Prls to append to a +/// paragraph's PAPX: a Prm0's single sprm, or those of the Prm1's grpprl. A Prm1 grpprl is +/// charged to `work`, as for [`apply_prm`]. +fn prm_paragraph_sprms(prm: PrmRef<'_>, work: &Work) -> Vec { + let mut out = Vec::new(); + match prm { + PrmRef::Sprm0 { isprm, val } => { + if let Some(op) = sprm::from_prm0(isprm) + && matches!(Prl { op, operand: &[] }.sgc(), 1 | 5) + { + out.extend_from_slice(&op.to_le_bytes()); + out.push(val); + } + } + PrmRef::Grpprl(Some(g)) => { + if !work.charge(g.len() as u64, Cost::Formatting) { + return out; + } + for prl in sprm::iter(g).filter(|p| matches!(p.sgc(), 1 | 5)) { + out.extend_from_slice(&prl.op.to_le_bytes()); + out.extend_from_slice(prl.operand); + } + } + PrmRef::None | PrmRef::Grpprl(None) => {} + } + out +} + /// Apply a piece's Prm (Prm0 short sprm or Prm1 grpprl) to character properties. A Prm1 /// grpprl is charged to `work`; once that is spent the grpprl is skipped. fn apply_prm(props: &mut CharProps, prm: PrmRef<'_>, fonts: &[String], base: &CharProps, work: &Work) { diff --git a/crates/doc-binary/src/piece.rs b/crates/doc-binary/src/piece.rs index 5f36a8198..701a4bc67 100644 --- a/crates/doc-binary/src/piece.rs +++ b/crates/doc-binary/src/piece.rs @@ -1,20 +1,71 @@ //! The piece table ([MS-DOC] `Clx`/`Pcdt`/`PlcPcd`): where every character position lives in //! the WordDocument stream, and how it is encoded. +//! +//! Word 6.0/95 files ([`crate::word6`]) keep the same `Clx` shape, but every piece's text is at a +//! plain byte offset: 8-bit text in the code page of its piece's or run's font (or the document's), +//! or UTF-16 in a file with the FIB's fExtChar flag. -use craft_codepage::cp1252; +use craft_codepage::{code_page_byte, cp1252, is_byte_code_page}; use crate::DocbinError; /// Most pieces any real file has; more means a corrupt `PlcPcd`. const MAX_PIECES: usize = 1_000_000; -/// One piece: text at FcCompressed `fc` (30-bit offset + compression bit). +/// How a piece's characters are stored. +#[derive(Clone, Copy, Debug, PartialEq)] +enum Text { + /// UTF-16LE at `fc`. + Utf16, + /// Windows-1252, one byte per character at `fc / 2` (Word 97 FcCompressed). + Compressed, + /// One byte per character at `fc` itself, in the code page [`PieceTable::code_page_at`] gives + /// that byte (Word 6.0/95). + Bytes, +} + +/// One piece: its text at `fc` (for Word 97, the 30-bit offset of FcCompressed). #[derive(Clone, Copy, Debug)] struct Pcd { fc: u32, - compressed: bool, - /// `Prm`: either a single sprm (Prm0) or an index into the Clx's Prc grpprls (Prm1). + text: Text, + /// `Prm`: either a single sprm (Prm0) or an index into the Clx's Prc grpprls (Prm1). In a Word + /// 6.0/95 table the Prcs hold translated grpprls ([`PieceTable::parse_word6`]), and a Prm0's + /// isprm is already the opcode [MS-DOC]'s isprm table maps. prm: u16, + /// For a Word 6.0/95 [`Text::Bytes`] piece whose Prm names a font: that font's code page, + /// which outranks the runs' ([`PieceTable::code_page_at`]). + code_page: Option, +} + +impl Pcd { + /// The byte offset of the character `step / 2` characters into the piece. `step` is twice + /// the CP offset, so one formula serves all three encodings. + fn fc_at(&self, step: u32) -> Option { + match self.text { + Text::Utf16 => self.fc.checked_add(step), + Text::Compressed => self.fc.checked_add(step)?.checked_div(2), + Text::Bytes => self.fc.checked_add(step / 2), + } + } +} + +/// The code page of the 8-bit text in the byte range `[lo, hi)` of the WordDocument stream. +#[derive(Clone, Copy, Debug, PartialEq)] +pub(crate) struct CodePageRun { + pub(crate) lo: u32, + pub(crate) hi: u32, + pub(crate) code_page: u16, +} + +/// How a Word 6.0/95 file's text is stored and decoded ([`crate::word6`]). +pub(crate) struct Word6Text { + /// UTF-16 rather than 8-bit (the FIB's fExtChar flag). + pub(crate) wide: bool, + /// The code page of 8-bit text that no piece or run gives one for. + pub(crate) default_code_page: u16, + /// The code page of each byte range, from its run's font (sorted by `lo`, not overlapping). + pub(crate) code_pages: Vec, } /// The parsed piece table plus the grpprls of the Prcs that Prm1 references. @@ -23,11 +74,58 @@ pub(crate) struct PieceTable { cps: Vec, pieces: Vec, prc_grpprls: Vec>, + /// For [`Text::Bytes`] pieces: the code page of each byte range, sorted by `lo`, not + /// overlapping. Bytes outside every range are in `default_code_page`. + code_pages: Vec, + default_code_page: u16, } impl PieceTable { /// Parse the `Clx` at `table[fc .. fc+lcb]`. pub(crate) fn parse(table: &[u8], fc: u32, lcb: u32) -> Result { + Self::parse_clx(table, fc, lcb, None) + } + + /// Parse a Word 6.0/95 `Clx` at `word[fc .. fc+lcb]`: the Word 97 shape, but every piece's text + /// is at its plain byte offset. `piece_code_page` gives the code page a piece's raw Prm + /// imposes (with the Clx's raw Prc grpprls to look Prm1 indices up in), if any; then each Prc + /// grpprl is replaced by `translate_prc`'s Word 97 sprms, which the walk applies to the piece. + pub(crate) fn parse_word6( + word: &[u8], + fc: u32, + lcb: u32, + text: Word6Text, + piece_code_page: impl Fn(u16, &[Vec]) -> Option, + translate_prc: impl Fn(&[u8]) -> Vec, + ) -> Result { + let mut t = Self::parse_clx(word, fc, lcb, Some(text.wide))?; + if !text.wide { + for p in &mut t.pieces { + p.code_page = piece_code_page(p.prm, &t.prc_grpprls); + } + } + t.prc_grpprls = t.prc_grpprls.iter().map(|g| translate_prc(g)).collect(); + t.code_pages = text.code_pages; + t.default_code_page = text.default_code_page; + Ok(t) + } + + /// One Word 6.0/95 piece: `ccp` characters from byte `fc` on. This covers a file that was not + /// fast-saved, which has no `Clx`. + pub(crate) fn contiguous_word6(fc: u32, ccp: u32, text: Word6Text) -> PieceTable { + let kind = if text.wide { Text::Utf16 } else { Text::Bytes }; + PieceTable { + cps: vec![0, ccp], + pieces: vec![Pcd { fc, text: kind, prm: 0, code_page: None }], + prc_grpprls: Vec::new(), + code_pages: text.code_pages, + default_code_page: text.default_code_page, + } + } + + /// `word6`: `None` for Word 97 and later, `Some(wide)` for a Word 6.0/95 file (whose raw Prms + /// and Prcs are kept for [`PieceTable::parse_word6`]). + fn parse_clx(table: &[u8], fc: u32, lcb: u32, word6: Option) -> Result { let fc = fc as usize; let lcb = lcb as usize; if lcb == 0 { @@ -95,9 +193,26 @@ impl PieceTable { return Err(DocbinError::Malformed("truncated aPcd".into())); }; let prm = plc.get(at + 4..at + 6).map(|b| u16::from_le_bytes([b[0], b[1]])).unwrap_or(0); - pieces.push(Pcd { fc: fc_raw & 0x3FFF_FFFF, compressed: fc_raw & 0x4000_0000 != 0, prm }); + pieces.push(match word6 { + // A plain byte offset, with no compression bit: 8-bit text, or UTF-16 in a file with + // fExtChar. The raw Prm is kept for `parse_word6`. + Some(wide) => Pcd { fc: fc_raw, text: if wide { Text::Utf16 } else { Text::Bytes }, prm, code_page: None }, + None => { + let text = if fc_raw & 0x4000_0000 != 0 { Text::Compressed } else { Text::Utf16 }; + Pcd { fc: fc_raw & 0x3FFF_FFFF, text, prm, code_page: None } + } + }); + } + Ok(PieceTable { cps, pieces, prc_grpprls, code_pages: Vec::new(), default_code_page: 1252 }) + } + + /// The code page of the 8-bit text byte at `fc` (in [`Text::Bytes`] pieces). + fn code_page_at(&self, fc: u32) -> u16 { + let i = self.code_pages.partition_point(|r| r.lo <= fc); + match i.checked_sub(1).and_then(|i| self.code_pages.get(i)) { + Some(r) if fc < r.hi => r.code_page, + _ => self.default_code_page, } - Ok(PieceTable { cps, pieces, prc_grpprls }) } /// The Prls this piece's `Prm` adds. Bit 0 (`fComplex`) discriminates: a Prm0 @@ -133,8 +248,7 @@ impl PieceTable { let p = self.piece_of(cp)?; let i = self.cps.partition_point(|&c| c <= cp).checked_sub(1)?; let cp0 = *self.cps.get(i)?; - let step = cp.checked_sub(cp0)?.checked_mul(2)?; - if p.compressed { p.fc.checked_add(step)?.checked_div(2) } else { p.fc.checked_add(step) } + p.fc_at(cp.checked_sub(cp0)?.checked_mul(2)?) } /// The piece holding `cp`, with the CP range it covers: for every CP in that range the @@ -167,13 +281,24 @@ impl PieceTable { let (Some(p), Some(&cp0)) = (self.pieces.get(i), self.cps.get(i)) else { return Err(u32::MAX) }; let Some(off) = cp.checked_sub(cp0) else { return Err(next) }; let off = off as usize; - if p.compressed { - // 8-bit, Windows-1252, one byte per character, stored at fc/2. - let at = ((p.fc / 2) as usize).checked_add(off); - return match at.and_then(|a| word.get(a)) { - Some(&b) => Ok((cp1252(b), 1)), - None => Err(next), - }; + match p.text { + Text::Compressed => { + // 8-bit, Windows-1252, one byte per character, stored at fc/2. + let at = ((p.fc / 2) as usize).checked_add(off); + return match at.and_then(|a| word.get(a)) { + Some(&b) => Ok((cp1252(b), 1)), + None => Err(next), + }; + } + Text::Bytes => { + // 8-bit, one byte per character at fc, in its run's code page. + let at = (p.fc as usize).checked_add(off); + return match at.and_then(|a| Some((u32::try_from(a).ok()?, *word.get(a)?))) { + Some((a, b)) => Ok((byte_char(p.code_page.unwrap_or_else(|| self.code_page_at(a)), b), 1)), + None => Err(next), + }; + } + Text::Utf16 => {} } // UTF-16LE, one 16-bit unit per character, stored at fc. let unit = |k: usize| -> Option { @@ -207,8 +332,7 @@ impl PieceSpan<'_> { /// [`PieceTable::fc_of_cp`] for a CP inside the span. pub(crate) fn fc_at(&self, cp: u32) -> Option { - let step = cp.checked_sub(self.cp_lo)?.checked_mul(2)?; - if self.pcd.compressed { self.pcd.fc.checked_add(step)?.checked_div(2) } else { self.pcd.fc.checked_add(step) } + self.pcd.fc_at(cp.checked_sub(self.cp_lo)?.checked_mul(2)?) } /// The raw Prm of the piece: two CPs with the same value get the same [`PrmRef`]. @@ -228,3 +352,23 @@ pub(crate) enum PrmRef<'a> { /// Prm1: a grpprl from a Prc (absent if out of range). Grpprl(Option<&'a [u8]>), } + +/// Windows' code page number for symbol fonts (CP_SYMBOL). +pub(crate) const SYMBOL_CODE_PAGE: u16 = 42; + +/// One byte of 8-bit text in `code_page`. The East Asian code pages store a character as a lead +/// and a trail byte, which one CP per byte can't represent, so their bytes above ASCII read as +/// U+FFFD rather than as unrelated Windows-1252 letters. +fn byte_char(code_page: u16, b: u8) -> char { + if code_page == SYMBOL_CODE_PAGE { + // Each character from the space up to its private-use code point, as Word reads a document + // marked as Symbol; control characters (paragraph marks, fields) stay themselves. + if b < 0x20 { char::from(b) } else { char::from_u32(0xF000 + u32::from(b)).unwrap_or('\u{FFFD}') } + } else if is_byte_code_page(code_page) { + code_page_byte(code_page, b) + } else if b.is_ascii() { + char::from(b) + } else { + '\u{FFFD}' + } +} diff --git a/crates/doc-binary/src/tests.rs b/crates/doc-binary/src/tests.rs index 80cef3257..ec5650695 100644 --- a/crates/doc-binary/src/tests.rs +++ b/crates/doc-binary/src/tests.rs @@ -10,6 +10,7 @@ use super::read; mod drawing; mod encrypted; mod metadata; +mod word6; /// Offset in the WordDocument stream where test text pieces are placed (past the FIB). const TEXT_FC: u32 = 0x600; @@ -176,12 +177,15 @@ fn wrong_magic_rejected() { } #[test] -fn word_6_95_rejected() { - let mut fib = fib_bytes(&FibSpec::default()); +fn word_6_95_magic_goes_to_the_word6_reader() { + // A Word 6.0/95 signature is no longer refused: the stream is read as that version (here an + // empty document, since a Word 97 FIB holds no Word 6 text length). The flags are cleared: + // the Word 97 default sets fExtChar, which the Word 6 reader refuses. + let mut fib = fib_bytes(&FibSpec { flags: 0, ..FibSpec::default() }); fib[0] = 0xDC; fib[1] = 0xA5; let f = cfb_file(&[("WordDocument", &fib)]); - assert!(matches!(read(&f), Err(super::DocbinError::NotWord(m)) if m.contains("6.0/95"))); + assert!(read(&f).is_ok()); } #[test] @@ -882,8 +886,6 @@ fn corpus_files_open() { let bytes = std::fs::read(&p).expect("read"); let doc = match super::read(&bytes) { Ok(d) => d, - // Word 6/95 files are a documented, intentional rejection. - Err(super::DocbinError::NotWord(m)) if m.contains("6.0/95") => continue, Err(e) => panic!("{}: {e}", p.display()), }; assert!(!doc.styles.styles.is_empty(), "{}: no styles", p.display()); @@ -2106,6 +2108,29 @@ fn one_section(text: &'static str, sepx: Vec, papx_entries: Vec<(u32, u16, V } } +#[test] +fn piece_prm_paragraph_formatting_applies_to_the_marks_paragraph() { + // [MS-DOC] §2.4.6.1 step 5: the Prm of the piece holding the paragraph mark adds its paragraph + // sprms after the PAPX's. Prm1 -> a Prc with sprmPDxaLeft80 (1440 twips) and sprmPJc80 + // (centred); Prm0 isprm 0x05 (sprmPJc) with operand 2 (right). + let prcs = vec![grpprl(&[(0x840F, &1440i16.to_le_bytes()), (0x2403, &[0x01])])]; + let para = |prm: u16| { + let spec = RichSpec { + text: "Text\r", + styles: vec![(1, 0x0FFF, "Normal", Vec::new(), Vec::new())], + fonts: vec!["Times New Roman"], + chpx_runs: vec![(0, Vec::new())], + papx_entries: vec![(4, 0, grpprl(&[(0x840F, &360i16.to_le_bytes())]))], + }; + let doc = read(&rich_doc_with(&spec, &RichExtra { prcs: prcs.clone(), prm, ..Default::default() })).expect("opens"); + let p = para0(&doc); + (p.props.indent_left, p.props.align) + }; + assert_eq!(para(0x0000), (Some(18.0), None), "no Prm: the PAPX's indent"); + assert_eq!(para(0x0001), (Some(72.0), Some(craft_wordmodel::props::Align::Center)), "Prm1: the Prc's sprms override the PAPX's"); + assert_eq!(para(0x020A), (Some(18.0), Some(craft_wordmodel::props::Align::Right)), "Prm0: sprmPJc right"); +} + #[test] fn piece_prm_character_formatting_applied() { // #458. Bit 0 of a Prm is fComplex ([MS-DOC] §2.9.215/§2.9.216): Prm0 = isprm in bits diff --git a/crates/doc-binary/src/tests/word6.rs b/crates/doc-binary/src/tests/word6.rs new file mode 100644 index 000000000..fa3f68a51 --- /dev/null +++ b/crates/doc-binary/src/tests/word6.rs @@ -0,0 +1,747 @@ +//! Word 6.0/95 fixtures, built in code. No public specification describes this version; these +//! build the layout observed in real files and described in `crate::word6`. + +use proptest::prelude::*; + +use super::{body_texts, cfb_file}; +use crate::{DocbinError, read}; + +/// Where the text starts: right after the FIB, as in every observed file. +const TEXT_AT: u32 = 0x300; + +/// One stylesheet entry as Word 6/95 stores it. +#[derive(Clone, Copy, Default)] +struct Std<'a> { + /// The built-in style id, or 0x0FFE for a user-defined style. + sti: u16, + /// 1 paragraph, 2 character. + sgc: u16, + /// The base style's index, or 0x0FFF. + base: u16, + name: &'a str, + /// Paragraph sprms (paragraph styles only). + papx: &'a [u8], + /// Character sprms. + chpx: &'a [u8], +} + +/// The stylesheet bytes for `styles`: the 14-byte header (count, fixed-part size 8, …), then per +/// style a size and the entry, or 0 for an empty slot. +fn stylesheet(styles: &[Option]) -> Vec { + let mut v = vec![14u8, 0]; + for w in [styles.len() as u16, 8, 1, 0x4B, 0x0F, 0, 0] { + v.extend_from_slice(&w.to_le_bytes()); + } + for (istd, st) in styles.iter().enumerate() { + let Some(st) = st else { + v.extend_from_slice(&[0, 0]); + continue; + }; + let cupx: u16 = if st.sgc == 1 { 2 } else { 1 }; + let mut e = Vec::new(); + for w in [st.sti, st.sgc | st.base << 4, cupx, 0] { + e.extend_from_slice(&w.to_le_bytes()); + } + e.push(st.name.len() as u8); + e.extend_from_slice(st.name.as_bytes()); + e.push(0); + let upx = |e: &mut Vec, bytes: &[u8]| { + if e.len() % 2 == 1 { + e.push(0); + } + e.extend_from_slice(&(bytes.len() as u16).to_le_bytes()); + e.extend_from_slice(bytes); + }; + if st.sgc == 1 { + let mut papx = (istd as u16).to_le_bytes().to_vec(); + papx.extend_from_slice(st.papx); + upx(&mut e, &papx); + } + upx(&mut e, st.chpx); + v.extend_from_slice(&(e.len() as u16).to_le_bytes()); + v.extend_from_slice(&e); + } + v +} + +/// What a test Word 6.0/95 file holds. Offsets in `runs` and `pieces` are into `text`. +#[derive(Clone, Default)] +struct Doc6<'a> { + nfib: u16, + flags: u16, + /// The stored 8-bit text. + text: &'a [u8], + /// (charset, name) per font index. + fonts: &'a [(u8, &'a str)], + /// The font table's entries as raw bytes instead of `fonts` (the size word is added). + raw_fonts: Option<&'a [u8]>, + /// Contiguous character runs (lo, hi, grpprl), all in one FKP page. + runs: &'a [(u32, u32, &'a [u8])], + /// Contiguous paragraph runs (lo, hi, grpprl), style 0, all in one FKP page. Each covers the + /// paragraph whose mark lies in it. + paras: &'a [(u32, u32, &'a [u8])], + /// For a fast-saved file: (length, offset) per piece, in CP order. + pieces: Option<&'a [(u32, u32)]>, + /// Each piece's Prm (0 when absent or shorter than `pieces`). + prms: &'a [u16], + /// The Clx's Prc grpprls, which a Prm1 indexes. + prcs: &'a [&'a [u8]], + /// The FIB's character set word at 0x14. + chse: u16, + /// The style of each paragraph run (0, Normal, when absent or shorter than `paras`). + para_styles: &'a [u16], + /// The stylesheet: one entry per style index, `None` for an empty slot. + styles: &'a [Option>], + /// The main text's length in characters, when it isn't the stored bytes' (16-bit text). + ccp: Option, +} + +fn put16(v: &mut [u8], at: usize, x: u16) { + v[at..at + 2].copy_from_slice(&x.to_le_bytes()); +} + +fn put32(v: &mut [u8], at: usize, x: u32) { + v[at..at + 4].copy_from_slice(&x.to_le_bytes()); +} + +/// Append one FKP page for `runs` and its bin table, and return the bin table's (fc, lcb). A +/// character page has a 1-byte entry per run and CHPXs (byte count + grpprl); a paragraph page +/// has 7-byte entries and PAPXs (word count + style 0 + grpprl padded to whole words). +fn fkp(w: &mut Vec, runs: &[(u32, u32, &[u8])], papx: bool, istds: &[u16]) -> (u32, u32) { + let (Some(first), Some(last)) = (runs.first(), runs.last()) else { return (0, 0) }; + w.resize(w.len().div_ceil(512) * 512, 0); + let pn = (w.len() / 512) as u16; + let crun = runs.len(); + let entry = if papx { 7 } else { 1 }; + let mut page = [0u8; 512]; + for (k, (lo, _, _)) in runs.iter().enumerate() { + put32(&mut page, 4 * k, TEXT_AT + lo); + } + put32(&mut page, 4 * crun, TEXT_AT + last.1); + // Records from the end of the page down, each at an even offset. + let mut top = 511usize; + for (k, (_, _, g)) in runs.iter().enumerate() { + let body: Vec = if papx { + let mut b = istds.get(k).copied().unwrap_or(0).to_le_bytes().to_vec(); + b.extend_from_slice(g); + if b.len() % 2 == 1 { + b.push(0); + } + b + } else if g.is_empty() { + continue; + } else { + g.to_vec() + }; + let at = (top - 1 - body.len()) & !1; + page[at] = if papx { (body.len() / 2) as u8 } else { body.len() as u8 }; + page[at + 1..at + 1 + body.len()].copy_from_slice(&body); + page[4 * (crun + 1) + entry * k] = (at / 2) as u8; + top = at; + } + page[511] = crun as u8; + w.extend_from_slice(&page); + let mut bte = Vec::new(); + bte.extend_from_slice(&(TEXT_AT + first.0).to_le_bytes()); + bte.extend_from_slice(&(TEXT_AT + last.1).to_le_bytes()); + bte.extend_from_slice(&pn.to_le_bytes()); + let at = w.len() as u32; + w.extend_from_slice(&bte); + (at, bte.len() as u32) +} + +/// The WordDocument stream: FIB, text, then the FKP pages, font table and piece table. +fn word_stream(d: &Doc6) -> Vec { + let mut w = vec![0u8; TEXT_AT as usize]; + w.extend_from_slice(d.text); + let ccp = match (d.ccp, d.pieces) { + (Some(ccp), _) => ccp, + (None, pieces) => match pieces { + Some(p) => p.iter().map(|(len, _)| len).sum(), + None => d.text.len() as u32, + }, + }; + let mut pairs: Vec<(usize, u32, u32)> = Vec::new(); + for (pair, runs, papx) in [(12, d.runs, false), (13, d.paras, true)] { + let (fc, lcb) = fkp(&mut w, runs, papx, d.para_styles); + if lcb > 0 { + pairs.push((pair, fc, lcb)); + } + } + if !d.styles.is_empty() { + let st = stylesheet(d.styles); + pairs.push((1, w.len() as u32, st.len() as u32)); + w.extend_from_slice(&st); + } + if !d.fonts.is_empty() || d.raw_fonts.is_some() { + let mut ffn = vec![0u8, 0]; + ffn.extend_from_slice(d.raw_fonts.unwrap_or_default()); + for (charset, name) in d.fonts { + let mut e = vec![0u8, 0x16, 0x90, 0x01, *charset, 0x00]; + e.extend_from_slice(name.as_bytes()); + e.push(0); + e[0] = (e.len() - 1) as u8; + ffn.extend_from_slice(&e); + } + let size = ffn.len() as u16; + put16(&mut ffn, 0, size); + pairs.push((15, w.len() as u32, ffn.len() as u32)); + w.extend_from_slice(&ffn); + } + if let Some(pieces) = d.pieces { + let mut plc = Vec::new(); + let mut cp = 0u32; + plc.extend_from_slice(&cp.to_le_bytes()); + for (len, _) in pieces { + cp += len; + plc.extend_from_slice(&cp.to_le_bytes()); + } + for (i, (_, off)) in pieces.iter().enumerate() { + plc.extend_from_slice(&[0, 0]); + plc.extend_from_slice(&(TEXT_AT + off).to_le_bytes()); + plc.extend_from_slice(&d.prms.get(i).copied().unwrap_or(0).to_le_bytes()); + } + let mut clx = Vec::new(); + for g in d.prcs { + clx.push(0x01); + clx.extend_from_slice(&(g.len() as u16).to_le_bytes()); + clx.extend_from_slice(g); + } + clx.push(0x02); + clx.extend_from_slice(&(plc.len() as u32).to_le_bytes()); + clx.extend_from_slice(&plc); + pairs.push((33, w.len() as u32, clx.len() as u32)); + w.extend_from_slice(&clx); + } + put16(&mut w, 0x00, 0xA5DC); + put16(&mut w, 0x02, if d.nfib == 0 { 0x0065 } else { d.nfib }); + put16(&mut w, 0x06, 0x0409); + put16(&mut w, 0x0A, d.flags | if d.pieces.is_some() { 0x0004 } else { 0 }); + put16(&mut w, 0x0C, 0x0065); + put16(&mut w, 0x14, d.chse); + put32(&mut w, 0x18, TEXT_AT); + put32(&mut w, 0x1C, TEXT_AT + d.text.len() as u32); + let len = w.len() as u32; + put32(&mut w, 0x20, len); + put32(&mut w, 0x34, ccp); + for (i, fc, lcb) in pairs { + put32(&mut w, 0x58 + i * 8, fc); + put32(&mut w, 0x58 + i * 8 + 4, lcb); + } + w +} + +/// A Word 6.0/95 file holding `d`: the WordDocument stream only, as the real ones have no Table +/// stream. +fn word6_file(d: &Doc6) -> Vec { + cfb_file(&[("WordDocument", &word_stream(d))]) +} + +fn texts(d: &Doc6) -> Vec { + body_texts(&read(&word6_file(d)).expect("a Word 6.0/95 file opens")) +} + +/// Fonts 0 (Western) and 1 (Cyrillic, charset 204), as the Russian files have. +const FONTS: &[(u8, &str)] = &[(0, "Times New Roman"), (204, "Times New Roman Cyr")]; + +#[test] +fn word_6_and_95_files_open_as_paragraphs() { + for nfib in [0x0065, 0x0068] { + let d = Doc6 { nfib, text: b"Hello\rWorld\r", ..Default::default() }; + assert_eq!(texts(&d), ["Hello", "World"], "nFib 0x{nfib:04X}"); + } +} + +#[test] +fn fast_saved_text_follows_the_piece_table() { + // Stored out of order; the pieces' fcs are plain byte offsets (Word 97 would halve them). + let d = Doc6 { text: b"World\rHello\r", pieces: Some(&[(6, 6), (6, 0)]), ..Default::default() }; + assert_eq!(texts(&d), ["Hello", "World"]); +} + +#[test] +fn fast_saved_file_without_a_piece_table_is_malformed() { + let d = Doc6 { flags: 0x0004, text: b"Hello\r", ..Default::default() }; + assert!(matches!(read(&word6_file(&d)), Err(DocbinError::Malformed(_)))); +} + +#[test] +fn a_runs_font_charset_chooses_its_code_page() { + // "Da " in font 0, then 0xC4 0xE0 ("Да" in Windows-1251) in the Cyrillic font 1. + let text = b"Da \xC4\xE0\r"; + let runs: &[(u32, u32, &[u8])] = &[(0, 3, &[]), (3, 5, &[0x5D, 0x01, 0x00]), (5, 6, &[])]; + let d = Doc6 { text, fonts: FONTS, runs, ..Default::default() }; + assert_eq!(texts(&d), ["Da Да"]); +} + +/// Text in an East Asian code page (a Japanese font, charset 128: Shift-JIS) is refused, not +/// read a byte at a time into replacement characters (review of wordcraft#6345). +#[test] +fn east_asian_text_is_refused_not_lost() { + let fonts: &[(u8, &str)] = &[(0, "Times New Roman"), (128, "MS Mincho")]; + let runs: &[(u32, u32, &[u8])] = &[(0, 2, &[0x5D, 0x01, 0x00]), (2, 3, &[])]; + let d = Doc6 { text: b"\x93\xfa\r", fonts, runs, ..Default::default() }; + assert!(matches!(read(&word6_file(&d)), Err(DocbinError::NotWord(e)) if e.contains("East Asian"))); + // The same file in a Western font still opens. + let d = Doc6 { text: b"\x93\xfa\r", fonts, runs: &[], ..Default::default() }; + assert_eq!(texts(&d).len(), 1); +} + +#[test] +fn the_font_sprm_is_found_after_other_sprms() { + // As in real files: bold-like toggles, a size, a picture anchor's length-prefixed sprm. + let text = b"\xC4\xE0\r"; + for g in [&[0x55, 0x81, 0x63, 0x18, 0x00, 0x5D, 0x01, 0x00][..], &[0x75, 0x01, 0x44, 0x04, 0, 0, 0, 0, 0x5D, 0x01, 0x00][..]] { + let runs: &[(u32, u32, &[u8])] = &[(0, 2, g), (2, 3, &[])]; + let d = Doc6 { text, fonts: FONTS, runs, ..Default::default() }; + assert_eq!(texts(&d), ["Да"], "grpprl {g:02X?}"); + } +} + +#[test] +fn the_last_font_sprm_of_a_run_chooses_its_code_page() { + // Font 0, then font 1 (Cyrillic): the later sprm is the one the formatting applies. + let runs: &[(u32, u32, &[u8])] = &[(0, 2, &[0x5D, 0x00, 0x00, 0x5D, 0x01, 0x00]), (2, 3, &[])]; + let d = Doc6 { text: b"\xC4\xE0\r", fonts: FONTS, runs, ..Default::default() }; + assert_eq!(texts(&d), ["Да"]); +} + +#[test] +fn an_unknown_sprm_leaves_font_0() { + // 0x99 has no known operand length, so the rest can't be framed: font 0 (Windows-1252). + let text = b"\xC4\xE0\r"; + let runs: &[(u32, u32, &[u8])] = &[(0, 2, &[0x99, 0x5D, 0x01, 0x00]), (2, 3, &[])]; + let d = Doc6 { text, fonts: FONTS, runs, ..Default::default() }; + assert_eq!(texts(&d), ["Äà"]); +} + +#[test] +fn the_toggle_sprms_before_the_font_are_framed() { + // 0x57 (strike, a one-byte toggle in [MS-DOC]'s Prm0 isprm table) before the font sprm: the + // font must still be found, or the Cyrillic text reads as Windows-1252. + let text = b"\xC4\xE0\r"; + for op in [0x57, 0x58, 0x59, 0x5C] { + let g = [op, 0x01, 0x5D, 0x01, 0x00]; + let runs: &[(u32, u32, &[u8])] = &[(0, 2, &g), (2, 3, &[])]; + let d = Doc6 { text, fonts: FONTS, runs, ..Default::default() }; + assert_eq!(texts(&d), ["Да"], "opcode 0x{op:02X}"); + } +} + +#[test] +fn a_font_entry_too_short_for_its_charset_ends_the_font_table() { + // Entry 0 says it is 3 bytes long, which can't hold the charset at offset 4. The bytes after + // it (the next entry, whose flags byte is 0xCC) must not be read as font 0's charset. + let raw: &[u8] = &[0x02, 0x16, 0x90, 0x05, 0xCC, 0x90, 0x01, 0xCC, 0x00, b'X', 0]; + let d = Doc6 { text: b"\xC4\xE0\r", raw_fonts: Some(raw), runs: &[(0, 3, &[])], ..Default::default() }; + assert_eq!(texts(&d), ["Äà"], "font 0 has no charset: Windows-1252"); +} + +#[test] +fn a_bin_table_naming_one_page_many_times_reads_it_once() { + // A hostile bin table: 1,000,000 entries, all naming one page of 50 Cyrillic runs. The text still + // reads right; that the page is read once is pinned in `word6::tests`. + let text: Vec = std::iter::repeat_n(0xC4u8, 50).chain(*b"\r").collect(); + let g: &[u8] = &[0x5D, 0x01, 0x00]; + let runs: Vec<(u32, u32, &[u8])> = (0..50).map(|i| (i, i + 1, g)).chain([(50, 51, &[][..])]).collect(); + let mut w = word_stream(&Doc6 { text: &text, fonts: FONTS, runs: &runs, ..Default::default() }); + let u32_at = |w: &[u8], at: usize| u32::from_le_bytes([w[at], w[at + 1], w[at + 2], w[at + 3]]); + let bte_fc = u32_at(&w, 0x58 + 12 * 8) as usize; + let pn = u16::from_le_bytes([w[bte_fc + 8], w[bte_fc + 9]]); + let n = 1_000_000usize; + let mut bte = Vec::with_capacity(4 * (n + 1) + 2 * n); + bte.extend_from_slice(&TEXT_AT.to_le_bytes()); + for _ in 0..n { + bte.extend_from_slice(&(TEXT_AT + 51).to_le_bytes()); + } + for _ in 0..n { + bte.extend_from_slice(&pn.to_le_bytes()); + } + let at = w.len() as u32; + w.extend_from_slice(&bte); + put32(&mut w, 0x58 + 12 * 8, at); + put32(&mut w, 0x58 + 12 * 8 + 4, bte.len() as u32); + let doc = read(&cfb_file(&[("WordDocument", &w)])).expect("opens"); + assert_eq!(body_texts(&doc), ["Д".repeat(50)]); +} + +/// `s` as UTF-16LE bytes. +fn utf16(s: &str) -> Vec { + s.encode_utf16().flat_map(u16::to_le_bytes).collect() +} + +#[test] +fn a_file_with_extended_characters_holds_utf16_text() { + // fExtChar (0x1000): Word reads the text as 16-bit units. + let text = utf16("Да ok\r"); + let d = Doc6 { flags: 0x1000, text: &text, ccp: Some(6), ..Default::default() }; + assert_eq!(texts(&d), ["Да ok"]); +} + +#[test] +fn utf16_text_stops_at_the_end_of_its_bytes() { + // Word reads no more 16-bit characters than the text's byte range (0x18..0x1C) holds, even + // when the character count says more. + let text = utf16("Hi\r"); + let d = Doc6 { flags: 0x1000, text: &text, ccp: Some(50), ..Default::default() }; + assert_eq!(texts(&d), ["Hi"]); +} + +#[test] +fn text_stops_at_the_end_of_its_bytes() { + // The same for 8-bit text: Word read 19 characters of a file whose count said 40. The font + // table after the text must not be read as body text. + let d = Doc6 { text: b"Hi\r", ccp: Some(50), fonts: FONTS, ..Default::default() }; + assert_eq!(texts(&d), ["Hi"]); +} + +#[test] +fn fast_saved_utf16_pieces_are_at_plain_byte_offsets() { + let text = utf16("World\rHello\r"); + let d = Doc6 { flags: 0x1000, text: &text, pieces: Some(&[(6, 12), (6, 0)]), ..Default::default() }; + assert_eq!(texts(&d), ["Hello", "World"]); +} + +#[test] +fn a_macintosh_document_reads_as_mac_roman() { + // FIB 0x14 = 0x0100: byte 0x8E is "é" (Mac Roman), not "Ž" (Windows-1252), even in a run whose + // font is Cyrillic. + let runs: &[(u32, u32, &[u8])] = &[(0, 2, &[0x5D, 0x01, 0x00]), (2, 3, &[])]; + let d = Doc6 { chse: 0x0100, text: b"H\x8E\r", fonts: FONTS, runs, ..Default::default() }; + assert_eq!(texts(&d), ["Hé"]); +} + +#[test] +fn a_symbol_document_maps_characters_to_private_use() { + // FIB 0x14 = 0x0002: every character from the space up is U+F000 + byte; marks stay marks. + let d = Doc6 { chse: 0x0002, text: b"Hi\x8E\rA\r", ..Default::default() }; + assert_eq!(texts(&d), ["\u{F048}\u{F069}\u{F08E}", "\u{F041}"]); +} + +#[test] +fn another_character_set_leaves_only_ascii_readable() { + let d = Doc6 { chse: 0x0001, text: b"H\x8Ei\r", ..Default::default() }; + assert_eq!(texts(&d), ["H\u{FFFD}i"]); +} + +#[test] +fn a_prm_on_the_marks_piece_formats_its_paragraph() { + // As Word 95 fast-saves a 1" left indent on the first paragraph: the piece holding its mark + // has Prm1 -> Prc `11 a0 05` (left indent 1440), and its PAPX says nothing. Word shows 72 pt; + // the second paragraph keeps none. + let text = b"First\rSecond\r"; + let pieces: &[(u32, u32)] = &[(5, 0), (1, 5), (7, 6)]; + let d = Doc6 { text, pieces: Some(pieces), prms: &[0, 0x0001], prcs: &[&[0x11, 0xA0, 0x05]], ..Default::default() }; + let doc = read(&word6_file(&d)).expect("opens"); + let paras: Vec<_> = doc.body.iter().filter_map(|b| b.as_para()).collect(); + assert_eq!(paras.iter().map(|p| p.text.as_str()).collect::>(), ["First", "Second"]); + assert_eq!((paras[0].props.indent_left, paras[1].props.indent_left), (Some(72.0), None)); +} + +#[test] +fn a_pieces_prm_formats_its_text() { + // As Word 95 fast-saves formatting: piece 0 has Prm1 -> a Prc setting font 1 and bold, which + // its character run doesn't. Piece 0 shows in font 1, bold (and decodes in its code page); + // piece 1 keeps the run's plain formatting. + let text = b"\xC4\xE0\xC4\xE0\r"; + let d = + Doc6 { text, fonts: FONTS, pieces: Some(&[(2, 0), (3, 2)]), prms: &[0x0001], prcs: &[&[0x5D, 0x01, 0x00, 0x55, 0x01]], ..Default::default() }; + let doc = read(&word6_file(&d)).expect("opens"); + let p = first_para(&doc); + assert_eq!(p.text, "ДаÄà"); + let first = &p.runs.first().expect("a run").props; + assert_eq!((first.font.as_deref(), first.bold), (Some("Times New Roman Cyr"), Some(true))); + let last = &p.runs.last().expect("a run").props; + assert_eq!((last.font.as_deref(), last.bold), (None, None)); +} + +#[test] +fn a_font_in_a_pieces_prm_chooses_its_code_page() { + // As Word 95 fast-saves a font change: piece 0 ("\xC4\xE0") has Prm1 -> Prc 0 = sprm 0x5D font 1 + // (Cyrillic), while its character run names no font. Piece 1 keeps the run's font 0. + let text = b"\xC4\xE0\xC4\xE0\r"; + let d = Doc6 { text, fonts: FONTS, pieces: Some(&[(2, 0), (3, 2)]), prms: &[0x0001], prcs: &[&[0x5D, 0x01, 0x00]], ..Default::default() }; + assert_eq!(texts(&d), ["ДаÄà"]); +} + +#[test] +fn a_prm_index_past_the_prcs_is_ignored() { + let d = Doc6 { text: b"\xC4\xE0\r", fonts: FONTS, pieces: Some(&[(3, 0)]), prms: &[0x0011], prcs: &[&[0x5D, 0x01, 0x00]], ..Default::default() }; + assert_eq!(texts(&d), ["Äà"]); +} + +#[test] +fn password_protected_files_are_refused() { + let d = Doc6 { flags: 0x0100, text: b"secret\r", ..Default::default() }; + assert!(matches!(read(&word6_file(&d)), Err(DocbinError::Encryption(_)))); +} + +/// The first paragraph of `doc`'s body. +fn first_para(doc: &craft_wordmodel::Document) -> &craft_wordmodel::para::Paragraph { + doc.body.iter().find_map(|b| b.as_para()).expect("a paragraph") +} + +#[test] +fn character_formatting_is_read() { + // Bold on, italic as the opposite of the (unread, so plain) style, 14 pt, red, single + // underline, font 2: the Word 6 opcodes as observed. + let g: &[u8] = &[0x55, 0x01, 0x56, 0x81, 0x63, 0x1C, 0x00, 0x62, 0x06, 0x5E, 0x01, 0x5D, 0x02, 0x00]; + let runs: &[(u32, u32, &[u8])] = &[(0, 2, g), (2, 3, &[])]; + let fonts = &[(0, "Times New Roman"), (2, "Symbol"), (0, "Arial")]; + let doc = read(&word6_file(&Doc6 { text: b"Hi\r", fonts, runs, ..Default::default() })).expect("opens"); + let p = first_para(&doc); + let c = &p.runs.first().expect("a run").props; + assert_eq!((c.bold, c.italic, c.size), (Some(true), Some(true), Some(14.0))); + assert_eq!(c.color, Some(craft_wordmodel::props::TextColor::Rgb(craft_wordmodel::props::Rgb(0xFF, 0, 0)))); + assert_eq!(c.underline, Some(craft_wordmodel::props::Underline::Single)); + assert_eq!(c.font.as_deref(), Some("Arial")); +} + +#[test] +fn unseen_operand_values_are_left_unread() { + // Underline 3 and "multiple" line spacing were never seen in a real file, so they aren't + // guessed at. + let runs: &[(u32, u32, &[u8])] = &[(0, 3, &[0x5E, 0x03])]; + let paras: &[(u32, u32, &[u8])] = &[(0, 3, &[0x14, 0xF0, 0x00, 0x01, 0x00])]; + let doc = read(&word6_file(&Doc6 { text: b"Hi\r", runs, paras, ..Default::default() })).expect("opens"); + let p = first_para(&doc); + assert_eq!(p.runs.first().and_then(|r| r.props.underline), None); + assert_eq!(p.props.line_spacing, None); +} + +#[test] +fn an_unseen_alignment_value_is_left_unread() { + // Only 0-3 were seen; 4 would otherwise become "distribute" and 0xFF "left". + for jc in [0x04u8, 0xFF] { + let g = [0x05, jc]; + let paras: &[(u32, u32, &[u8])] = &[(0, 3, &g)]; + let doc = read(&word6_file(&Doc6 { text: b"Hi\r", paras, ..Default::default() })).expect("opens"); + assert_eq!(first_para(&doc).props.align, None, "jc {jc:#04x}"); + } +} + +#[test] +fn paragraph_formatting_is_read() { + // Justified, 0.5" left and 0.25" right indents, a 0.5" hanging first line, at least 27 pt. + let g: &[u8] = &[0x05, 0x03, 0x11, 0xD0, 0x02, 0x10, 0x68, 0x01, 0x13, 0x30, 0xFD, 0x14, 0x1C, 0x02, 0x00, 0x00]; + let paras: &[(u32, u32, &[u8])] = &[(0, 3, g), (3, 6, &[0x05, 0x01])]; + let doc = read(&word6_file(&Doc6 { text: b"Hi\rHo\r", paras, ..Default::default() })).expect("opens"); + let ps: Vec<_> = doc.body.iter().filter_map(|b| b.as_para()).collect(); + let p = &ps[0].props; + assert_eq!(p.align, Some(craft_wordmodel::props::Align::Justify)); + assert_eq!((p.indent_left, p.indent_right, p.indent_first), (Some(36.0), Some(18.0), Some(-36.0))); + assert_eq!(p.line_spacing, Some(craft_wordmodel::props::LineSpacing::AtLeast(27.0))); + assert_eq!(ps[1].props.align, Some(craft_wordmodel::props::Align::Center)); +} + +/// A Word 6 table definition sprm (0xBE): the cell edges, then a 10-byte descriptor per cell +/// whose first word holds the merge flags. +fn def_table(edges: &[i16], rgfs: &[u16]) -> Vec { + let mut rest = vec![rgfs.len() as u8]; + for e in edges { + rest.extend_from_slice(&e.to_le_bytes()); + } + for r in rgfs { + rest.extend_from_slice(&r.to_le_bytes()); + rest.extend_from_slice(&[0; 8]); + } + let mut v = vec![0xBE]; + v.extend_from_slice(&((rest.len() + 1) as u16).to_le_bytes()); + v.extend_from_slice(&rest); + v +} + +/// The rows of the first table in `doc`'s body, each as its cells' texts. +fn table_rows(doc: &craft_wordmodel::Document) -> Vec> { + let t = doc.body.iter().find_map(|b| match b.as_ref() { + craft_wordmodel::Block::Table(t) => Some(t), + _ => None, + }); + let t = t.expect("a table"); + t.rows + .iter() + .map(|r| r.cells.iter().map(|c| c.blocks.iter().filter_map(|b| b.as_para()).map(|p| p.text.clone()).collect::>().join("|")).collect()) + .collect() +} + +#[test] +fn table_rows_and_merged_cells_are_read() { + // Row 1: "A", "B". Row 2: "C" merged across both cells. In-table marks carry 0x18, the row + // ends 0x18, 0x19 and the table definition. + let text = b"A\x07B\x07\x07C\x07\x07\x07after\r"; + let mut row1 = vec![0x18, 1, 0x19, 1]; + row1.extend(def_table(&[0, 1440, 2880], &[0, 0])); + let mut row2 = vec![0x18, 1, 0x19, 1]; + row2.extend(def_table(&[0, 1440, 2880], &[0x0001, 0x0002])); + let cell: &[u8] = &[0x18, 1]; + let paras: &[(u32, u32, &[u8])] = &[(0, 2, cell), (2, 4, cell), (4, 5, &row1), (5, 7, cell), (7, 8, cell), (8, 9, &row2), (9, 15, &[])]; + let doc = read(&word6_file(&Doc6 { text, paras, ..Default::default() })).expect("opens"); + assert_eq!(table_rows(&doc), [vec!["A", "B"], vec!["C"]]); + assert_eq!(body_texts(&doc), ["after"], "the paragraph after the table stays outside it"); +} + +#[test] +fn several_merges_in_one_row_each_keep_their_cells() { + // Five cells merged as [A B] [C] [D E]: the second merge must not be shifted by the first. + let text = b"A\x07B\x07C\x07D\x07E\x07\x07"; + let mut row = vec![0x18, 1, 0x19, 1]; + row.extend(def_table(&[0, 100, 200, 300, 400, 500], &[0x0001, 0x0002, 0x0000, 0x0001, 0x0002])); + let cell: &[u8] = &[0x18, 1]; + let paras: &[(u32, u32, &[u8])] = &[(0, 2, cell), (2, 4, cell), (4, 6, cell), (6, 8, cell), (8, 10, cell), (10, 11, &row)]; + let doc = read(&word6_file(&Doc6 { text, paras, ..Default::default() })).expect("opens"); + assert_eq!(table_rows(&doc), [vec!["A|B", "C", "D|E"]]); +} + +#[test] +fn a_field_shows_its_cached_result() { + let d = Doc6 { text: b"Date \x13DATE \x1412/12/97\x15\r", ..Default::default() }; + let text = read(&word6_file(&d)).expect("opens").plain_text(craft_wordmodel::StoryRef::Body); + assert!(text.contains("Date 12/12/97"), "{text:?}"); + assert!(!text.contains("DATE"), "{text:?}"); +} + +/// A fixture's paragraph styles: Normal, an empty slot, then "Mine" (user-defined, centred, bold, on +/// Normal) and "Heading" stored under a local name with sti 1. +const STYLES: &[Option] = &[ + Some(Std { sti: 0, sgc: 1, base: 0x0FFF, name: "Standard", papx: &[], chpx: &[0x61, 0x09, 0x04] }), + None, + Some(Std { sti: 0x0FFE, sgc: 1, base: 0, name: "Mine", papx: &[0x05, 0x01], chpx: &[0x55, 0x01] }), + Some(Std { sti: 1, sgc: 1, base: 0, name: "Überschrift 1", papx: &[0x08, 0x01], chpx: &[0x55, 0x81, 0x63, 0x20, 0x00] }), +]; + +/// Each paragraph's style name, through the document's style sheet. +fn style_names(doc: &craft_wordmodel::Document) -> Vec { + doc.body + .iter() + .filter_map(|b| b.as_para()) + .map(|p| p.props.style.as_deref().and_then(|id| doc.styles.get(id)).map_or(String::new(), |s| s.name.clone())) + .collect() +} + +#[test] +fn paragraphs_take_their_styles() { + let paras: &[(u32, u32, &[u8])] = &[(0, 2, &[]), (2, 4, &[]), (4, 6, &[])]; + let d = Doc6 { text: b"A\rB\rC\r", paras, para_styles: &[0, 2, 3], styles: STYLES, ..Default::default() }; + let doc = read(&word6_file(&d)).expect("opens"); + // A built-in style goes by its built-in name, whatever the file stores (as in Word). + assert_eq!(style_names(&doc), ["Normal", "Mine", "Heading 1"]); +} + +#[test] +fn styles_carry_their_formatting_and_base() { + let d = Doc6 { text: b"A\r", styles: STYLES, ..Default::default() }; + let doc = read(&word6_file(&d)).expect("opens"); + let mine = doc.styles.styles.iter().find(|s| s.name == "Mine").expect("Mine"); + assert_eq!(mine.para.align, Some(craft_wordmodel::props::Align::Center)); + assert_eq!(mine.chr.bold, Some(true)); + assert_eq!(mine.based_on.as_deref().and_then(|id| doc.styles.get(id)).map(|s| s.name.as_str()), Some("Normal")); + let heading = doc.styles.styles.iter().find(|s| s.name == "Heading 1").expect("Heading 1"); + assert_eq!((heading.para.keep_next, heading.chr.size), (Some(true), Some(16.0))); +} + +#[test] +fn bulleted_styles_frame_past_their_numbering() { + // 0x0D (1 byte) and 0x0C (a count byte and its bytes) before the indents, as in the corpus. + let mut papx = vec![0x05, 0x03, 0x0D, 0x0B, 0x11, 0x1C, 0x01, 0x13, 0xE4, 0xFE, 0x0C, 4, 0xFF, 0x01, 0x00, 0x08]; + papx.extend_from_slice(&[0x0E, 0x01, 0x2C, 0x00, 0x1D, 0x60, 0x1A, 0xF8, 0xFF, 0x25, 0x02, 0x10, 0x68, 0x01]); + let styles = &[Some(Std { sgc: 1, base: 0x0FFF, name: "Normal", papx: &papx, ..Default::default() })]; + let doc = read(&word6_file(&Doc6 { text: b"A\r", styles, ..Default::default() })).expect("opens"); + let normal = doc.styles.get("Normal").expect("Normal"); + assert_eq!((normal.para.indent_left, normal.para.indent_first, normal.para.indent_right), (Some(14.2), Some(-14.2), Some(18.0))); +} + +#[test] +fn a_runs_language_or_its_styles_chooses_the_code_page_without_a_font() { + // No font named anywhere: the style's language decides (Russian -> Windows-1251), as in Word. + let styles = &[Some(Std { sgc: 1, base: 0x0FFF, name: "Normal", chpx: &[0x61, 0x19, 0x04], ..Default::default() })]; + let d = Doc6 { text: b"\xC4\xE0\r", fonts: FONTS, styles, ..Default::default() }; + assert_eq!(texts(&d), ["Да"]); + // The run's own language outranks the style's. + let runs: &[(u32, u32, &[u8])] = &[(0, 3, &[0x61, 0x09, 0x04])]; + let d = Doc6 { text: b"\xC4\xE0\r", fonts: FONTS, styles, runs, ..Default::default() }; + assert_eq!(texts(&d), ["Äà"]); +} + +#[test] +fn font_0_is_not_a_fallback() { + // Font 0 is Cyrillic, but nothing names it and the style's language is English: Word reads + // Windows-1252. + let fonts = &[(204, "Times New Roman Cyr"), (0, "Arial")]; + let styles = &[Some(Std { sgc: 1, base: 0x0FFF, name: "Normal", chpx: &[0x61, 0x09, 0x04], ..Default::default() })]; + // A character run that names no font (where the reader used to take font 0). + let runs: &[(u32, u32, &[u8])] = &[(0, 3, &[])]; + let d = Doc6 { text: b"\xC4\xE0\r", fonts, styles, runs, ..Default::default() }; + assert_eq!(texts(&d), ["Äà"]); +} + +#[test] +fn a_font_named_in_the_style_chooses_the_code_page() { + // The base style names the Cyrillic font 1; the run and its own style name none. + let styles = &[ + Some(Std { sgc: 1, base: 0x0FFF, name: "Normal", chpx: &[0x5D, 0x01, 0x00], ..Default::default() }), + Some(Std { sti: 0x0FFE, sgc: 1, base: 0, name: "Child", ..Default::default() }), + ]; + let paras: &[(u32, u32, &[u8])] = &[(0, 3, &[])]; + let d = Doc6 { text: b"\xC4\xE0\r", fonts: FONTS, styles, paras, para_styles: &[1], ..Default::default() }; + assert_eq!(texts(&d), ["Да"]); +} + +#[test] +fn an_ansi_font_leaves_the_code_page_to_the_language() { + // Arial (ANSI) named on the run, the style's language Russian: Word reads Cyrillic. + let fonts = &[(0, "Times New Roman"), (0, "Arial")]; + let styles = &[Some(Std { sgc: 1, base: 0x0FFF, name: "Normal", chpx: &[0x61, 0x19, 0x04], ..Default::default() })]; + let runs: &[(u32, u32, &[u8])] = &[(0, 2, &[0x5D, 0x01, 0x00]), (2, 3, &[])]; + let d = Doc6 { text: b"\xC4\xE0\r", fonts, styles, runs, ..Default::default() }; + assert_eq!(texts(&d), ["Да"]); +} + +/// A fixture with every structure read: runs, fonts and a piece table. +fn full_stream() -> Vec { + let runs: &[(u32, u32, &[u8])] = &[(0, 6, &[0x55, 0x81]), (6, 9, &[0x5D, 0x01, 0x00]), (9, 12, &[])]; + let styles = &[ + Some(Std { sgc: 1, base: 0x0FFF, name: "Normal", chpx: &[0x61, 0x09, 0x04], ..Default::default() }), + None, + Some(Std { sti: 0x0FFE, sgc: 1, base: 0, name: "Mine", papx: &[0x05, 0x01], chpx: &[0x55, 0x01] }), + ]; + let paras: &[(u32, u32, &[u8])] = &[(0, 6, &[]), (6, 12, &[])]; + word_stream(&Doc6 { + text: b"Hello\r\xC4\xE0\rx\r\r", + fonts: FONTS, + runs, + paras, + para_styles: &[2, 0], + styles, + pieces: Some(&[(6, 0), (6, 6)]), + ..Default::default() + }) +} + +#[test] +fn the_full_fixture_reads() { + let f = cfb_file(&[("WordDocument", &full_stream())]); + let doc = read(&f).expect("opens"); + assert_eq!(body_texts(&doc), ["Hello", "Да", "x", ""]); + assert_eq!(style_names(&doc)[0], "Mine"); +} + +proptest! { + #![proptest_config(ProptestConfig::with_cases(256))] + + #[test] + fn corrupt_word6_streams_never_panic(edits in proptest::collection::vec((0..0x700usize, any::()), 1..16)) { + let mut w = full_stream(); + for (at, b) in edits { + if let Some(x) = w.get_mut(at) { + *x = b; + } + } + let _ = read(&cfb_file(&[("WordDocument", &w)])); + } + + #[test] + fn truncated_word6_streams_never_panic(n in 0..0x700usize) { + let w = full_stream(); + let _ = read(&cfb_file(&[("WordDocument", &w[..n.min(w.len())])])); + } +} diff --git a/crates/doc-binary/src/word6.rs b/crates/doc-binary/src/word6.rs new file mode 100644 index 000000000..e9fab2d74 --- /dev/null +++ b/crates/doc-binary/src/word6.rs @@ -0,0 +1,842 @@ +//! Word 6.0 and Word 95 `.doc` files (`wIdent` 0xA5DC, `nFib` 0x0065 and 0x0068). +//! +//! No public specification describes this version: [MS-DOC] covers Word 97 and later. Everything +//! here is observed, from the layout of real files checked against what Word shows when it opens +//! them (its text through COM, its formatting in the `.docx` Word saves from them), plus the Word +//! 97 structures in [MS-DOC] that these files visibly share. Nothing else is assumed. +//! +//! What is read: the main text, its paragraphs and fields, the code page of each run, the +//! character and paragraph formatting listed below, table rows, and the paragraph styles (with +//! their formatting). Character styles, sections, headers and footers, notes, pictures, borders, +//! shading and tabs are not read yet. +//! +//! The layout as observed: +//! +//! - A compound file with a `WordDocument` stream and **no Table stream**: the structures that +//! Word 97 keeps in `0Table`/`1Table` live in the WordDocument stream itself. +//! - The FIB is one fixed block. The flags at 0x0A have the Word 97 meaning for bit 2 (fast-saved, +//! set exactly in the files that carry a piece table) and, as far as can be told, bit 8 +//! (encrypted). The text starts at the u32 at 0x18, and the main text's length in characters is +//! the u32 at 0x34. +//! - From 0x58, (u32 fc, u32 lcb) pairs, in the order of Word 97's `FibRgFcLcb97`. Index 12 is the +//! character bin table, 13 the paragraph bin table, 15 the font table and 33 the piece table. +//! - Text is one byte per character, in the code page of the font of its run. The FIB's language +//! does not choose it: files marked English hold Russian and Czech text that Word shows as such. +//! - Both bin tables hold 2-byte page numbers (Word 97: 4 bytes). The FKP pages have the Word 97 +//! layout, except that a paragraph page's entries are 7 bytes (an offset byte and 6 more; Word +//! 97: 13), and a PAPX is a count of 2-byte words followed by exactly that many words: the +//! 2-byte style index, then the grpprl (padded with one 0 byte to whole words). +//! - Sprms have **1-byte opcodes**. Those with a 1-byte operand match the opcodes [MS-DOC]'s +//! `Prm0` table lists for the same properties (bold 0x55, colour 0x62, in-table 0x18…); the +//! others were identified against Word. See [`chp`] and [`pap`]. +//! - A font table entry is a size byte (the entry's length − 1), a flags byte, a 2-byte weight, the +//! charset byte, one more byte, then the name in 8-bit characters. +//! - The stylesheet (pair 1) has the Word 97 shape with smaller parts: a u16 header size (14), +//! then a header of that many bytes whose first two words are the style count and the size of +//! a style's fixed part (8, Word 97: 10); +//! then per style a u16 size (0: an empty slot) and that many bytes: the fixed part (sti, then +//! kind and base style, then UPX count and next style, then a size), the name (a length byte, +//! 8-bit characters, a 0), and the UPXs, each a u16 size and its bytes, padded to even. A +//! paragraph style's first UPX is its style index and paragraph sprms, the second its character +//! sprms; a character style has one, its character sprms. + +use craft_codepage::charset_code_page; + +use crate::DocbinError; +use crate::fkp::{Bins, TranslatedRun}; +use crate::piece::{CodePageRun, PieceTable, SYMBOL_CODE_PAGE, Word6Text}; +use crate::stsh::RawStyle; + +/// `wIdent` of a Word 6.0/95 file. +const W_IDENT: u16 = 0xA5DC; +const OFF_FLAGS: usize = 0x0A; +/// Fast-saved: the text is in pieces listed by a piece table. +const F_COMPLEX: u16 = 0x0004; +/// Password-protected (Word 97's bit; no unprotected file sets it). +const F_ENCRYPTED: u16 = 0x0100; +/// Word 97's `fExtChar` bit. Word reads a Word 6.0/95 file that sets it as UTF-16 text (each +/// piece's fc a plain byte offset), and so does this reader. +const F_EXT_CHAR: u16 = 0x1000; +/// The document's character set (u16): 0 Windows, 0x0100 Macintosh (all 8-bit text is Mac Roman, +/// whatever the fonts say), 0x0002 Symbol (every byte a private-use character). Word reads the +/// bytes above ASCII of any other value as U+FFFD. +const OFF_CHSE: usize = 0x14; +const OFF_FC_MAC: usize = 0x1C; +/// Bin table entries read, as for Word 97 (`fkp::MAX_BINS`). +const MAX_BINS: usize = 100_000; +/// FKP runs read (character or paragraph): far more than any real file's, and few enough to stay small. +const MAX_RUNS: usize = 1_000_000; +const OFF_FC_MIN: usize = 0x18; +const OFF_CCP_TEXT: usize = 0x34; +const OFF_PAIRS: usize = 0x58; +/// Pairs read: up to and including the piece table's. +const PAIRS: usize = 34; +const PAIR_STSH: usize = 1; +const PAIR_BTE_CHPX: usize = 12; +const PAIR_BTE_PAPX: usize = 13; +const PAIR_FONTS: usize = 15; +const PAIR_CLX: usize = 33; +/// More characters than Word's own documents get near: the FIB is corrupt (as for Word 97). +const MAX_CCP: u32 = 100_000_000; +/// The font table's entries: more than any real file has. +const MAX_FONTS: usize = 0x1_0000; +/// FKP page size in bytes. +const PAGE: usize = 512; +/// Size of a paragraph FKP entry (offset byte + 6). +const PAPX_BX: usize = 7; +/// Most cells a row's table definition is read for (the model's own cap is 63). +const MAX_CELLS: usize = 63; +/// Styles read: style indexes stop below 0x0FFF ("no style"). +const MAX_STYLES: usize = 0x0FFE; +/// Base-style links followed when a style inherits a font or language. +const MAX_STYLE_DEPTH: usize = 16; + +/// Character sprms read, by Word 6.0/95 opcode. Each one was matched against Word's own reading of +/// the corpus files (the `.docx` it saves from them). +mod chp { + /// Bold, a toggle (0 off, 1 on, 0x80 as the style, 0x81 the opposite of the style). + pub(super) const BOLD: u8 = 0x55; + /// Italic, a toggle. + pub(super) const ITALIC: u8 = 0x56; + /// Small capitals, a toggle. + pub(super) const SMALL_CAPS: u8 = 0x5A; + /// All capitals, a toggle. + pub(super) const CAPS: u8 = 0x5B; + /// The font: a 2-byte index into the font table. + pub(super) const FONT: u8 = 0x5D; + /// Underline: 0 none, 1 single (the only values seen). + pub(super) const UNDERLINE: u8 = 0x5E; + /// Colour: an index into Word's 16-colour palette, as Word 97's `Ico`. + pub(super) const COLOR: u8 = 0x62; + /// Size in half-points (2 bytes). + pub(super) const SIZE: u8 = 0x63; + /// The language (2 bytes, a Windows LCID): with no non-ANSI font, it gives the code page. + pub(super) const LID: u8 = 0x61; +} + +/// Paragraph and table sprms read, by Word 6.0/95 opcode, matched like [`chp`]'s. +mod pap { + /// Alignment: 0 left, 1 centred, 2 right, 3 justified. + pub(super) const JC: u8 = 0x05; + /// Keep with the next paragraph (1 byte; on the heading styles H1-H6, which Word shows with + /// keepNext; [MS-DOC]'s isprm table has sprmPFKeepFollow at 0x08). + pub(super) const KEEP_NEXT: u8 = 0x08; + /// Right indent in twips (signed, 2 bytes). + pub(super) const RIGHT: u8 = 0x10; + /// Left indent in twips. + pub(super) const LEFT: u8 = 0x11; + /// First-line indent in twips, relative to the left one (negative: hanging). + pub(super) const FIRST: u8 = 0x13; + /// Line spacing: the 4-byte Word 97 `LSPD` (dyaLine, fMultLinespace). + pub(super) const LINE: u8 = 0x14; + /// The paragraph is in a table. + pub(super) const IN_TABLE: u8 = 0x18; + /// The mark ends a table row. + pub(super) const TTP: u8 = 0x19; + /// Row alignment (2 bytes): 1 centred. + pub(super) const ROW_JC: u8 = 0xB6; + /// Row height in twips (signed, 2 bytes): positive at least, negative exactly. + pub(super) const ROW_HEIGHT: u8 = 0xBD; + /// The row's cells: u16 count, cell count, (count + 1) i16 cell edges, then a 10-byte + /// descriptor per cell whose first word's bit 0 starts a horizontal merge and bit 1 continues + /// it. + pub(super) const DEF_TABLE: u8 = 0xBE; +} + +/// Word 97 opcodes the translations produce ([MS-DOC] §2.6). +mod w97 { + pub(super) const C_BOLD: u16 = 0x0835; + pub(super) const C_ITALIC: u16 = 0x0836; + pub(super) const C_SMALL_CAPS: u16 = 0x083A; + pub(super) const C_CAPS: u16 = 0x083B; + pub(super) const C_KUL: u16 = 0x2A3E; + pub(super) const C_ICO: u16 = 0x2A42; + pub(super) const C_HPS: u16 = 0x4A43; + pub(super) const C_FTC0: u16 = 0x4A4F; + pub(super) const P_JC80: u16 = 0x2403; + pub(super) const P_KEEP_FOLLOW: u16 = 0x2406; + pub(super) const P_DXA_RIGHT80: u16 = 0x840E; + pub(super) const P_DXA_LEFT80: u16 = 0x840F; + pub(super) const P_DXA_LEFT1_80: u16 = 0x8411; + pub(super) const P_DYA_LINE: u16 = 0x6412; + pub(super) const P_IN_TABLE: u16 = 0x2416; + pub(super) const P_TTP: u16 = 0x2417; + pub(super) const T_JC: u16 = 0x5400; + pub(super) const T_ROW_HEIGHT: u16 = 0x9407; + pub(super) const T_DEF_TABLE: u16 = 0xD608; + pub(super) const T_MERGE: u16 = 0x5624; +} + +/// Is this WordDocument stream a Word 6.0/95 one? +pub(crate) fn is_word6(word: &[u8]) -> bool { + word.get(0..2) == Some(&W_IDENT.to_le_bytes()[..]) +} + +/// The fields of a Word 6.0/95 FIB that are read. +#[derive(Clone, Debug)] +pub(crate) struct Fib6 { + complex: bool, + /// Text is UTF-16 (fExtChar). + wide: bool, + chse: u16, + fc_min: u32, + fc_mac: u32, + /// Characters in the main text. + pub(crate) ccp_text: u32, + pairs: [(u32, u32); PAIRS], +} + +impl Fib6 { + pub(crate) fn parse(word: &[u8]) -> Result { + let u16_at = |at: usize| word.get(at..at + 2).map(|b| u16::from_le_bytes([b[0], b[1]])); + let u32_at = |at: usize| word.get(at..at + 4).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]])); + if word.len() < OFF_PAIRS + PAIRS * 8 { + return Err(DocbinError::NotWord(format!("Word 6.0/95 WordDocument stream is {} bytes", word.len()))); + } + let flags = u16_at(OFF_FLAGS).unwrap_or(0); + if flags & F_ENCRYPTED != 0 { + return Err(DocbinError::Encryption("password-protected Word 6.0/95 files can't be opened".into())); + } + let ccp_text = u32_at(OFF_CCP_TEXT).unwrap_or(0); + if ccp_text > MAX_CCP { + return Err(DocbinError::Limit(format!("document claims {ccp_text} characters"))); + } + let mut pairs = [(0, 0); PAIRS]; + for (i, p) in pairs.iter_mut().enumerate() { + let at = OFF_PAIRS + i * 8; + *p = (u32_at(at).unwrap_or(0), u32_at(at + 4).unwrap_or(0)); + } + Ok(Fib6 { + complex: flags & F_COMPLEX != 0, + wide: flags & F_EXT_CHAR != 0, + chse: u16_at(OFF_CHSE).unwrap_or(0), + fc_min: u32_at(OFF_FC_MIN).unwrap_or(0), + fc_mac: u32_at(OFF_FC_MAC).unwrap_or(0), + ccp_text, + pairs, + }) + } + + /// The (fc, lcb) pair at index `i`, if it points at anything. + fn pair(&self, i: usize) -> Option<(u32, u32)> { + self.pairs.get(i).copied().filter(|&(_, lcb)| lcb > 0) + } +} + +/// Everything the walk needs from a Word 6.0/95 file besides its text stream. +pub(crate) struct Content { + pub(crate) pieces: PieceTable, + /// Character formatting, translated to Word 97 grpprls. + pub(crate) chpx: Bins, + /// Paragraph formatting and table rows, translated to Word 97 grpprls. + pub(crate) papx: Bins, + /// Font names by index (the walk resolves translated font sprms against them). + pub(crate) fonts: Vec, + /// The styles by index, their formatting translated to Word 97 grpprls. + pub(crate) styles: Vec, +} + +/// Read the text, formatting and fonts of a Word 6.0/95 file. +/// +/// 8-bit text decodes, as Word does, in the code page of: the document's character set when it +/// isn't Windows ([`OFF_CHSE`]); else the font a piece's Prm names (as Word 95 writes when a fast +/// save changes a font) when its charset has a code page; else, the same for the font of the +/// character run, or the one its paragraph style (or a base of it) names; else the language of +/// the run, or of its paragraph style; else Windows-1252. A font is only one named somewhere: +/// font 0 is not a fallback, and a font with the ANSI charset leaves the choice to the language. +pub(crate) fn content(word: &[u8], fib: &Fib6) -> Result { + let fonts = fib.pair(PAIR_FONTS).map(|(fc, lcb)| font_table(word, fc, lcb)).unwrap_or_default(); + let chp_runs = fib.pair(PAIR_BTE_CHPX).map(|(fc, lcb)| fkp_runs(word, fc, lcb, false)).unwrap_or_default(); + let pap_runs = fib.pair(PAIR_BTE_PAPX).map(|(fc, lcb)| fkp_runs(word, fc, lcb, true)).unwrap_or_default(); + let (styles, facts) = fib.pair(PAIR_STSH).map(|(fc, lcb)| stylesheet(word, fc, lcb)).unwrap_or_default(); + let windows = fib.chse == 0; + let default_code_page = match fib.chse { + // Text that no run covers is formatted by the Normal style. + 0 => text_code_page(&fonts, &facts, 0, None, None), + 0x0100 => MAC_ROMAN, + 0x0002 => SYMBOL_CODE_PAGE, + // Not a code page: bytes above ASCII read as U+FFFD. + _ => 0, + }; + let code_pages = if windows { code_page_runs(&chp_runs, &pap_runs, &facts, &fonts, default_code_page) } else { Vec::new() }; + // shortcut: East Asian (lead and trail byte) text is refused, as all Word 6.0/95 files were + // before; read it a character at a time when the piece walk can decode byte pairs. + let east_asian = || DocbinError::NotWord("Word 6.0/95 text in an East Asian code page is not read yet".into()); + if craft_codepage::is_multibyte_code_page(default_code_page) || code_pages.iter().any(|r| craft_codepage::is_multibyte_code_page(r.code_page)) { + return Err(east_asian()); + } + let multibyte_piece = std::cell::Cell::new(false); + let text = Word6Text { wide: fib.wide, default_code_page, code_pages }; + let pieces = if fib.complex { + let (fc, lcb) = fib.pair(PAIR_CLX).ok_or_else(|| DocbinError::Malformed("the fast-saved file has no piece table".into()))?; + // A Prm1's Prc may name the piece's font (sprm 0x5D); a Prm0 can't, its operand being one + // byte. + let piece_code_page = |prm: u16, prcs: &[Vec]| -> Option { + if !windows || prm & 1 == 0 { + return None; + } + let cp = last_font(prcs.get(usize::from(prm >> 1))?).and_then(|f| font_code_page(&fonts, f)); + if cp.is_some_and(craft_codepage::is_multibyte_code_page) { + multibyte_piece.set(true); + } + cp + }; + // The Prcs then carry Word 97 sprms, as the CHPXs and PAPXs do: the walk applies their + // character sprms to the piece's text and their paragraph sprms to the paragraph whose mark + // the piece holds. + let table = PieceTable::parse_word6(word, fc, lcb, text, piece_code_page, translate_prc)?; + if multibyte_piece.get() { + return Err(east_asian()); + } + table + } else { + // Word reads no more characters than the text's byte range (fcMin..fcMac) holds, one byte + // each or two for UTF-16, whatever the character count says. + let unit = if fib.wide { 2 } else { 1 }; + let ccp = fib.ccp_text.min(fib.fc_mac.saturating_sub(fib.fc_min) / unit); + PieceTable::contiguous_word6(fib.fc_min, ccp, text) + }; + let chpx = Bins::translated(chp_runs.iter().map(|r| TranslatedRun { lo: r.lo, hi: r.hi, istd: 0, grpprl: translate_chp(r.grpprl) }).collect()); + let papx = + Bins::translated(pap_runs.iter().map(|r| TranslatedRun { lo: r.lo, hi: r.hi, istd: r.istd, grpprl: translate_pap(r.grpprl) }).collect()); + Ok(Content { pieces, chpx, papx, fonts: fonts.into_iter().map(|f| f.name).collect(), styles }) +} + +/// Mac OS Roman, the 8-bit text of a Macintosh Word document. +const MAC_ROMAN: u16 = craft_codepage::MAC_ROMAN_CODE_PAGE; + +/// The code page of font `font`'s charset, if it has one (not for ANSI, default or symbol +/// charsets, whose text Word decodes by its language). +fn font_code_page(fonts: &[Font], font: u16) -> Option { + fonts.get(usize::from(font)).and_then(|f| charset_code_page(i32::from(f.charset))) +} + +/// Windows' ANSI code page for the language `lid` (an LCID), which Word uses for 8-bit text in a +/// font without a charset of its own. Russian (1251) and English (1252) were checked against Word; +/// the rest is the standard Windows assignment. +fn lid_code_page(lid: u16) -> u16 { + match lid & 0x03FF { + // Serbian in Cyrillic (0x0C1A, 0x1C1A); Croatian and Serbian in Latin otherwise. + 0x1A => match lid { + 0x0C1A | 0x1C1A => 1251, + _ => 1250, + }, + 0x02 | 0x19 | 0x22 | 0x23 | 0x2F | 0x3F | 0x40 | 0x44 | 0x50 => 1251, + 0x05 | 0x0E | 0x15 | 0x18 | 0x1B | 0x1C | 0x24 => 1250, + 0x08 => 1253, + 0x1F => 1254, + 0x0D => 1255, + 0x01 | 0x20 | 0x29 => 1256, + 0x25..=0x27 => 1257, + 0x2A => 1258, + 0x1E => 874, + 0x11 => 932, + 0x12 => 949, + 0x04 => match lid { + 0x0804 | 0x1004 => 936, + _ => 950, + }, + _ => 1252, + } +} + +/// A style's own font and language sprms and its base, for inheritance ([`StyleFacts::resolve`]). +#[derive(Clone, Copy, Debug, Default)] +struct StyleFacts { + base: u16, + font: Option, + lid: Option, +} + +impl StyleFacts { + /// Style `istd`'s font and language, each from the style itself or the nearest base that has + /// one. + fn resolve(facts: &[StyleFacts], istd: u16) -> (Option, Option) { + let (mut font, mut lid) = (None, None); + let mut at = istd; + for _ in 0..MAX_STYLE_DEPTH { + let Some(f) = facts.get(usize::from(at)) else { break }; + font = font.or(f.font); + lid = lid.or(f.lid); + if font.is_some() && lid.is_some() { + break; + } + at = f.base; + } + (font, lid) + } +} + +/// The styles of the stylesheet at `word[fc..fc+lcb]`, by index (an empty slot is a default +/// style, which keeps the indexes aligned), with their formatting translated, and each style's +/// own font and language. Reading stops at the first entry that runs past the table. +fn stylesheet(word: &[u8], fc: u32, lcb: u32) -> (Vec, Vec) { + let Some(st) = (fc as usize).checked_add(lcb as usize).and_then(|end| word.get(fc as usize..end)) else { return Default::default() }; + let u16_at = |b: &[u8], at: usize| b.get(at..at + 2).map(|x| u16::from_le_bytes([x[0], x[1]])); + let Some(cb_header) = u16_at(st, 0).map(usize::from) else { return Default::default() }; + let (Some(cstd), Some(cb_base)) = (u16_at(st, 2), u16_at(st, 4)) else { return Default::default() }; + let cb_base = usize::from(cb_base); + let (mut styles, mut facts) = (Vec::new(), Vec::new()); + let mut at = 2 + cb_header; + for _ in 0..usize::from(cstd).min(MAX_STYLES) { + let Some(cb) = u16_at(st, at).map(usize::from) else { break }; + let Some(std) = st.get(at + 2..at + 2 + cb) else { break }; + at += 2 + cb; + let (style, fact) = style(std, cb_base).unwrap_or_else(|| (RawStyle::default(), StyleFacts { base: 0x0FFF, ..Default::default() })); + styles.push(style); + facts.push(fact); + } + (styles, facts) +} + +/// The names of the built-in styles by sti: the "Index within built-in styles" of the reserved +/// style names in [MS-OE376] (Part 4 §2.7.3.9), which [MS-DOC]'s StdfBase says sti is. 0x0FFE +/// (user-defined) and anything past the table keep the name stored in the file. +const BUILT_IN_NAMES: [&str; 107] = [ + "Normal", + "heading 1", + "heading 2", + "heading 3", + "heading 4", + "heading 5", + "heading 6", + "heading 7", + "heading 8", + "heading 9", + "index 1", + "index 2", + "index 3", + "index 4", + "index 5", + "index 6", + "index 7", + "index 8", + "index 9", + "toc 1", + "toc 2", + "toc 3", + "toc 4", + "toc 5", + "toc 6", + "toc 7", + "toc 8", + "toc 9", + "Normal Indent", + "footnote text", + "annotation text", + "header", + "footer", + "index heading", + "caption", + "table of figures", + "envelope address", + "envelope return", + "footnote reference", + "annotation reference", + "line number", + "page number", + "endnote reference", + "endnote text", + "table of authorities", + "macro", + "toa heading", + "List", + "List Bullet", + "List Number", + "List 2", + "List 3", + "List 4", + "List 5", + "List Bullet 2", + "List Bullet 3", + "List Bullet 4", + "List Bullet 5", + "List Number 2", + "List Number 3", + "List Number 4", + "List Number 5", + "Title", + "Closing", + "Signature", + "Default Paragraph Font", + "Body Text", + "Body Text Indent", + "List Continue", + "List Continue 2", + "List Continue 3", + "List Continue 4", + "List Continue 5", + "Message Header", + "Subtitle", + "Salutation", + "Date", + "Body Text First Indent", + "Body Text First Indent 2", + "Note Heading", + "Body Text 2", + "Body Text 3", + "Body Text Indent 2", + "Body Text Indent 3", + "Block Text", + "Hyperlink", + "FollowedHyperlink", + "Strong", + "Emphasis", + "Document Map", + "Plain Text", + "E-mail Signature", + "HTML Top of Form", + "HTML Bottom of Form", + "Normal (Web)", + "HTML Acronym", + "HTML Address", + "HTML Cite", + "HTML Code", + "HTML Definition", + "HTML Keyboard", + "HTML Preformatted", + "HTML Sample", + "HTML Typewriter", + "HTML Variable", + "Normal Table", + "annotation subject", +]; + +/// One style entry (`None` for an empty slot or one too short for its fixed part and name). +fn style(std: &[u8], cb_base: usize) -> Option<(RawStyle, StyleFacts)> { + let u16_at = |at: usize| std.get(at..at + 2).map(|x| u16::from_le_bytes([x[0], x[1]])); + if cb_base < 8 { + return None; + } + let (sti, kind_base, upx_next) = (u16_at(0)? & 0x0FFF, u16_at(2)?, u16_at(4)?); + let (stk, istd_base, cupx) = ((kind_base & 0x000F) as u8, kind_base >> 4, usize::from(upx_next & 0x000F)); + let len = usize::from(*std.get(cb_base)?); + // A built-in style goes by its built-in name, as in Word, whatever name the file stores + // ("Default" or "Обычный" for Normal). Other names are 8-bit, as Word shows them + // (Windows-1252: names it couldn't store read back with '?'). + let stored = std.get(cb_base + 1..cb_base + 1 + len)?; + let name: String = match BUILT_IN_NAMES.get(usize::from(sti)) { + Some(built_in) => (*built_in).to_owned(), + None => stored.iter().map(|&b| craft_codepage::cp1252(b)).collect(), + }; + let mut upxs: Vec<&[u8]> = Vec::new(); + let mut j = cb_base + 1 + len + 1; + for _ in 0..cupx.min(3) { + j += j % 2; + let Some(n) = u16_at(j).map(usize::from) else { break }; + let Some(upx) = std.get(j + 2..j + 2 + n) else { break }; + upxs.push(upx); + j += 2 + n; + } + // A paragraph style: style index + paragraph sprms, then character sprms. A character style: + // character sprms. + let (papx, chpx) = match stk { + 1 => (upxs.first().and_then(|u| u.get(2..)).unwrap_or_default(), upxs.get(1).copied().unwrap_or_default()), + _ => (&[][..], upxs.first().copied().unwrap_or_default()), + }; + let fact = StyleFacts { base: istd_base, font: last_font(chpx), lid: last_lid(chpx) }; + Some((RawStyle { stk, istd_base, name, papx: translate_pap(papx), chpx: translate_chp(chpx) }, fact)) +} + +/// One font table entry. +struct Font { + name: String, + charset: u8, +} + +/// The fonts of the font table at `word[fc..fc+lcb]`, by index. A truncated entry ends the table. +fn font_table(word: &[u8], fc: u32, lcb: u32) -> Vec { + let Some(table) = (fc as usize).checked_add(lcb as usize).and_then(|end| word.get(fc as usize..end)) else { return Vec::new() }; + // The table starts with its own size, which bounds the entries along with `lcb`. + let size = table.get(0..2).map_or(0, |b| usize::from(u16::from_le_bytes([b[0], b[1]]))); + let table = table.get(..size.min(table.len())).unwrap_or_default(); + let mut fonts = Vec::new(); + let mut at = 2usize; + while let Some(&cb_m1) = table.get(at) { + let next = at + usize::from(cb_m1) + 1; + // The entry must lie inside the table and hold its charset (one too short is corrupt). + let Some(entry) = table.get(at..next) else { break }; + let (Some(&charset), Some(name)) = (entry.get(4), entry.get(6..)) else { break }; + if fonts.len() >= MAX_FONTS { + break; + } + // An 8-bit name; font names are ASCII in every file seen, other bytes read as Latin-1. + let name = name.split(|&b| b == 0).next().unwrap_or_default().iter().map(|&b| char::from(b)).collect(); + fonts.push(Font { name, charset }); + at = next; + } + fonts +} + +/// One FKP run as stored: its FC range, the paragraph style (PAPX runs) and its Word 6 grpprl. +struct Run<'w> { + lo: u32, + hi: u32, + istd: u16, + grpprl: &'w [u8], +} + +/// The page numbers of a bin table with `n` entries ((n+1) u32 FCs, then n u16 page numbers), +/// each once, however many entries name it: a page's runs carry their own FCs, so reading it +/// again would only repeat them. The entries read are capped as for Word 97. +fn distinct_pages(plc: &[u8], n: usize) -> Vec { + let mut pns: Vec = (0..n.min(MAX_BINS)) + .filter_map(|i| plc.get(4 * (n + 1) + 2 * i..4 * (n + 1) + 2 * i + 2).map(|b| usize::from(u16::from_le_bytes([b[0], b[1]])))) + .collect(); + pns.sort_unstable(); + pns.dedup(); + pns +} + +/// Every run of the FKP pages a bin table at `word[fc..fc+lcb]` lists, each page read once and +/// at most [`MAX_RUNS`] runs in all. Unreadable pages are skipped. +fn fkp_runs(word: &[u8], fc: u32, lcb: u32, papx: bool) -> Vec> { + let Some(plc) = (fc as usize).checked_add(lcb as usize).and_then(|end| word.get(fc as usize..end)) else { return Vec::new() }; + let Some(n) = plc.len().checked_sub(4).filter(|r| r % 6 == 0).map(|r| r / 6) else { return Vec::new() }; + let entry = if papx { PAPX_BX } else { 1 }; + let mut runs = Vec::new(); + for pn in distinct_pages(plc, n) { + if runs.len() >= MAX_RUNS { + break; + } + let Some(page) = pn.checked_mul(PAGE).and_then(|at| word.get(at..at.checked_add(PAGE)?)) else { continue }; + let crun = usize::from(page.get(PAGE - 1).copied().unwrap_or(0)); + // The FCs and the entries must fit before the count byte. + if 4 * (crun + 1) + entry * crun >= PAGE { + continue; + } + let fc_at = |k: usize| page.get(4 * k..4 * k + 4).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]])); + for k in 0..crun { + let (Some(lo), Some(hi)) = (fc_at(k), fc_at(k + 1)) else { break }; + let off = usize::from(page.get(4 * (crun + 1) + entry * k).copied().unwrap_or(0)) * 2; + let (istd, grpprl) = match (off, papx) { + (0, _) => (0, &[][..]), + // A CHPX: a byte count, then the grpprl. + (_, false) => (0, page.get(off).and_then(|&cb| page.get(off + 1..off + 1 + usize::from(cb))).unwrap_or_default()), + // A PAPX: a count of 2-byte words, then the style index and the grpprl. + (_, true) => { + let body = page.get(off).and_then(|&cw| page.get(off + 1..off + 1 + 2 * usize::from(cw))).unwrap_or_default(); + let istd = body.get(0..2).map_or(0, |b| u16::from_le_bytes([b[0], b[1]])); + (istd, body.get(2..).unwrap_or_default()) + } + }; + runs.push(Run { lo, hi, istd, grpprl }); + } + } + runs +} + +/// The sprms of a Word 6.0/95 grpprl as (opcode, operand). Reading stops at an opcode whose +/// operand length is not known, since the rest can't be framed, and at a lone trailing 0 (the +/// padding of a PAPX to whole words). +fn sprms(grpprl: &[u8]) -> impl Iterator { + let mut rest = grpprl; + std::iter::from_fn(move || { + let (&op, after) = rest.split_first()?; + let len = match op { + // 0x55-0x5C are the character toggles of [MS-DOC]'s Prm0 isprm table (bold, italic, + // strike, outline, shadow, small caps, caps, hidden), all one byte; 0x55, 0x56, 0x5A + // and 0x5B are also seen in real files. + // 0x0D, 0x0E, 0x1D, 0x25 and 0x2C are one-byte paragraph sprms of [MS-DOC]'s isprm table + // too (numbering level, no line numbers, frame anchoring, wrapping, no hyphenation). + 0x05 | 0x08 | 0x0D | 0x0E | 0x18 | 0x19 | 0x1D | 0x25 | 0x2C | 0x33 | 0x55..=0x5C | 0x5E | 0x62 | 0x68 | 0x75 | 0xB7 | 0xB9 => 1, + // 0x1A/0x1B: a frame's position; 0x6B: kerning in half-points (H1 has 0x0024; Word + // shows kern=36). + 0x10 | 0x11 | 0x13 | 0x15 | 0x16 | 0x1A | 0x1B | 0x26 | 0x27 | 0x28 | 0x29 | 0x2F | 0x50 | 0x5D | 0x60 | 0x61 | 0x63 | 0x6B | 0xB6 + | 0xB8 | 0xBD | 0xC0 => 2, + 0x14 => 4, + // A count byte, then that many bytes (0x0C: a bulleted style's numbering, 52 bytes). + 0x0C | 0x0F | 0x44 | 0xBF => 1 + usize::from(*after.first()?), + // A 2-byte count, then one byte fewer than it says (as Word 97's sprmTDefTable). + 0xBE => 2 + usize::from(u16::from_le_bytes([*after.first()?, *after.get(1)?])).saturating_sub(1), + _ => return None, + }; + let operand = after.get(..len)?; + rest = after.get(len..).unwrap_or_default(); + Some((op, operand)) + }) +} + +/// Append a Word 97 Prl: opcode, then operand. +fn prl(out: &mut Vec, op: u16, operand: &[u8]) { + out.extend_from_slice(&op.to_le_bytes()); + out.extend_from_slice(operand); +} + +/// A Word 6.0/95 CHPX grpprl as the Word 97 sprms it stands for. Sprms not read are left out. +fn translate_chp(grpprl: &[u8]) -> Vec { + let mut out = Vec::new(); + for (op, v) in sprms(grpprl) { + match op { + chp::BOLD => prl(&mut out, w97::C_BOLD, v), + chp::ITALIC => prl(&mut out, w97::C_ITALIC, v), + chp::SMALL_CAPS => prl(&mut out, w97::C_SMALL_CAPS, v), + chp::CAPS => prl(&mut out, w97::C_CAPS, v), + // Only none and single were seen; any other value is left unread. + chp::UNDERLINE if matches!(v, [0] | [1]) => prl(&mut out, w97::C_KUL, v), + chp::COLOR => prl(&mut out, w97::C_ICO, v), + chp::SIZE => prl(&mut out, w97::C_HPS, v), + chp::FONT => prl(&mut out, w97::C_FTC0, v), + _ => {} + } + } + out +} + +/// A Word 6.0/95 Prc grpprl as the Word 97 sprms it stands for: character and paragraph sprms +/// alike (Word 95 records a paragraph change before a fast save as a Prc on the mark's piece). +fn translate_prc(grpprl: &[u8]) -> Vec { + [translate_chp(grpprl), translate_pap(grpprl)].concat() +} + +/// A Word 6.0/95 PAPX grpprl as the Word 97 sprms it stands for, table rows included. Sprms not +/// read are left out. +fn translate_pap(grpprl: &[u8]) -> Vec { + let mut out = Vec::new(); + for (op, v) in sprms(grpprl) { + match op { + // Only 0-3 were seen: another value is left unread. + pap::JC if matches!(v, [0..=3]) => prl(&mut out, w97::P_JC80, v), + pap::KEEP_NEXT => prl(&mut out, w97::P_KEEP_FOLLOW, v), + pap::RIGHT => prl(&mut out, w97::P_DXA_RIGHT80, v), + pap::LEFT => prl(&mut out, w97::P_DXA_LEFT80, v), + pap::FIRST => prl(&mut out, w97::P_DXA_LEFT1_80, v), + // Only "at least" spacing was seen (a positive height, not a multiple): the other + // forms are left unread. + pap::LINE => { + if let [l0, l1, 0, 0] = v + && i16::from_le_bytes([*l0, *l1]) > 0 + { + prl(&mut out, w97::P_DYA_LINE, v); + } + } + pap::IN_TABLE => prl(&mut out, w97::P_IN_TABLE, v), + pap::TTP => prl(&mut out, w97::P_TTP, v), + pap::ROW_JC => prl(&mut out, w97::T_JC, v), + pap::ROW_HEIGHT => prl(&mut out, w97::T_ROW_HEIGHT, v), + pap::DEF_TABLE => def_table(&mut out, v), + _ => {} + } + } + out +} + +/// A Word 6.0/95 table definition as a Word 97 `sprmTDefTable` holding the cell edges (the only +/// part the table reader uses) and one `sprmTMerge` per horizontal merge. +fn def_table(out: &mut Vec, v: &[u8]) { + let Some(&itc) = v.get(2) else { return }; + let itc = usize::from(itc).min(MAX_CELLS); + let Some(edges) = v.get(3..3 + 2 * (itc + 1)) else { return }; + // Word 97 operand: cb (u16: the bytes after it, plus one), the cell count, the edges. + let cb = (1 + edges.len() + 1) as u16; + out.extend_from_slice(&w97::T_DEF_TABLE.to_le_bytes()); + out.extend_from_slice(&cb.to_le_bytes()); + out.push(itc as u8); + out.extend_from_slice(edges); + let cells = v.get(3 + edges.len()..).unwrap_or_default(); + let mut merges: Vec<(usize, usize)> = Vec::new(); + let mut start: Option = None; + for i in 0..=itc { + let rgf = cells.get(10 * i..10 * i + 2).map_or(0, |b| u16::from_le_bytes([b[0], b[1]])); + let continues = i < itc && rgf & 0x0002 != 0 && rgf & 0x0001 == 0; + if continues { + continue; + } + // A merge runs from its first cell through every cell that continues it. + if let Some(a) = start.take() + && i > a + 1 + { + merges.push((a, i)); + } + if i < itc && rgf & 0x0001 != 0 { + start = Some(i); + } + } + // Right to left: the table reader folds each merge's cells into its first one as it goes, so + // a merge further right must be applied before one to its left moves its cells. + for (a, b) in merges.into_iter().rev() { + prl(out, w97::T_MERGE, &[a as u8, b as u8]); + } +} + +/// The code page of text in paragraph style `istd` whose run names `run_font` and `run_lid`: the +/// run's font, else the style's (nearest in its base chain), when that font's charset has a code +/// page; else the run's language, else the style's; else Windows-1252. +fn text_code_page(fonts: &[Font], facts: &[StyleFacts], istd: u16, run_font: Option, run_lid: Option) -> u16 { + let (style_font, style_lid) = StyleFacts::resolve(facts, istd); + run_font.or(style_font).and_then(|f| font_code_page(fonts, f)).or_else(|| run_lid.or(style_lid).map(lid_code_page)).unwrap_or(1252) +} + +/// The language a grpprl names (sprm 0x61), if any: the last one. +fn last_lid(grpprl: &[u8]) -> Option { + sprms(grpprl).filter(|(op, _)| *op == chp::LID).filter_map(|(_, v)| Some(u16::from_le_bytes([*v.first()?, *v.get(1)?]))).last() +} + +/// The byte ranges whose code page is not the document default `default`, cut at every character and paragraph run +/// boundary, each stretch in [`text_code_page`]'s. Sorted by start and not overlapping. +fn code_page_runs(chp: &[Run], pap: &[Run], facts: &[StyleFacts], fonts: &[Font], default: u16) -> Vec { + let sorted = |runs: &[Run]| -> Vec<(u32, u32, usize)> { + let mut v: Vec<(u32, u32, usize)> = runs.iter().enumerate().filter(|(_, r)| r.lo < r.hi).map(|(i, r)| (r.lo, r.hi, i)).collect(); + v.sort_unstable(); + v + }; + let (chp_sorted, pap_sorted) = (sorted(chp), sorted(pap)); + let covering = |v: &[(u32, u32, usize)], fc: u32| -> Option { + let k = v.partition_point(|&(lo, _, _)| lo <= fc).checked_sub(1)?; + v.get(k).filter(|&&(_, hi, _)| fc < hi).map(|&(_, _, i)| i) + }; + let mut cuts: Vec = chp_sorted.iter().chain(pap_sorted.iter()).flat_map(|&(lo, hi, _)| [lo, hi]).collect(); + cuts.sort_unstable(); + cuts.dedup(); + let mut out: Vec = Vec::new(); + for w in cuts.windows(2) { + let (&lo, &hi) = (w.first().unwrap_or(&0), w.get(1).unwrap_or(&0)); + let run = covering(&chp_sorted, lo).and_then(|i| chp.get(i)); + let para = covering(&pap_sorted, lo).and_then(|i| pap.get(i)); + if run.is_none() && para.is_none() { + continue; + } + let run_font = run.and_then(|r| last_font(r.grpprl)); + let run_lid = run.and_then(|r| last_lid(r.grpprl)); + let code_page = text_code_page(fonts, facts, para.map_or(0, |p| p.istd), run_font, run_lid); + if code_page == default { + continue; + } + match out.last_mut() { + Some(last) if last.hi == lo && last.code_page == code_page => last.hi = hi, + _ => out.push(CodePageRun { lo, hi, code_page }), + } + } + out +} + +/// The font a CHPX or Prc grpprl names (sprm 0x5D), if any: the last one, as a later sprm +/// overrides an earlier one when the formatting is applied (a font before an opcode of unknown +/// length stands). +fn last_font(grpprl: &[u8]) -> Option { + sprms(grpprl).filter(|(op, _)| *op == chp::FONT).filter_map(|(_, v)| Some(u16::from_le_bytes([*v.first()?, *v.get(1)?]))).last() +} + +#[cfg(test)] +mod tests { + #[test] + fn a_page_named_by_many_bin_entries_is_read_once() { + // A hostile bin table: a million entries all naming page 7, and one naming page 3. + let n = 1_000_001usize; + let mut plc = vec![0u8; 4 * (n + 1)]; + for i in 0..n { + plc.extend_from_slice(&(if i == 5 { 3u16 } else { 7u16 }).to_le_bytes()); + } + assert_eq!(super::distinct_pages(&plc, n), [3, 7]); + } + + #[test] + fn bin_entries_past_the_cap_are_not_read() { + let n = super::MAX_BINS + 1; + let mut plc = vec![0u8; 4 * (n + 1)]; + for i in 0..n { + plc.extend_from_slice(&(if i == n - 1 { 9u16 } else { 7u16 }).to_le_bytes()); + } + assert_eq!(super::distinct_pages(&plc, n), [7]); + } +} diff --git a/crates/docpath/tests/long_path.rs b/crates/docpath/tests/long_path.rs new file mode 100644 index 000000000..2ecf08ba1 --- /dev/null +++ b/crates/docpath/tests/long_path.rs @@ -0,0 +1,29 @@ +//! Paths long enough that a per-level recursive descent would exhaust the stack. Parsing is +//! iterative — `resolve` and `classify` reach `parse_share` at a fixed depth — so these come +//! back; a refusal is fine, a crash is not. + +use craft_docpath::{classify, resolve}; + +/// Half a megabyte of components under a UNC share: one component per level for a parser that +/// recursed on each separator. +#[test] +fn a_half_megabyte_unc_path_comes_back() { + let mut path = String::from(r"\\?\UNC\host\share"); + for _ in 0..(1 << 18) { + path.push_str(r"\d"); + } + let _ = resolve(&path, None); + let _ = classify(&path); +} + +/// Sixteen thousand nested verbatim prefixes: each level was a candidate recursive call when the +/// UNC prefixes were first reviewed. +#[test] +fn nested_verbatim_prefixes_come_back() { + let mut path = String::new(); + for _ in 0..(1 << 14) { + path.push_str(r"\\?\UNC\"); + } + let _ = resolve(&path, None); + let _ = classify(&path); +} diff --git a/crates/docx/src/read.rs b/crates/docx/src/read.rs index 5ea11dbd8..0f95b4f76 100644 --- a/crates/docx/src/read.rs +++ b/crates/docx/src/read.rs @@ -119,6 +119,10 @@ pub(crate) struct Reader<'p> { pub theme_fills: shape_style::ThemeFills, /// The fill of the group being read (what its members' `a:grpFill` stands for). pub group_fill: Option, + /// The package has no default table style (no styles part, or none marked `w:default`): + /// Word then gives tables no cell margins rather than its Normal Table's 0.08". Set by + /// `read_styles`, which runs before any story with tables is read. + pub no_default_table_style: bool, } /// The package path that internal relationship `id` points at, if it has type `kind` (an `rt` @@ -193,6 +197,7 @@ fn read_opened_in(pkg: &Package, chunks: ChunkState) -> Result<(Document, usize, theme_fills: Default::default(), group_fill: None, chunks, + no_default_table_style: true, }; r.doc.missing_pictures.main = main.clone(); r.pc.major_font = r.doc.settings.major_font.clone(); diff --git a/crates/docx/src/read/story.rs b/crates/docx/src/read/story.rs index 0550557f1..770375365 100644 --- a/crates/docx/src/read/story.rs +++ b/crates/docx/src/read/story.rs @@ -113,6 +113,8 @@ pub struct StoryCtx { pub story_depth: usize, /// Reading a header or footer (not a text box inside one): where Word keeps its watermark. pub in_hf: bool, + /// Reading a table cell's paragraphs (not a text box inside one). + in_cell: bool, } /// Paragraph content under construction. diff --git a/crates/docx/src/read/story/blocks.rs b/crates/docx/src/read/story/blocks.rs index e72f16bf6..38c35a02b 100644 --- a/crates/docx/src/read/story/blocks.rs +++ b/crates/docx/src/read/story/blocks.rs @@ -276,6 +276,10 @@ impl Reader<'_> { if sc.fields.iter().any(|f| f.phase == Phase::Instr) && para.plain_text().is_empty() { para.mark.hidden = Some(true); } + // A cell's paragraphs are marked once their table is read: its style counts. + if !sc.in_cell { + self.doc.styles.mark_runs_as_word_draws_them(&mut para, None); + } para.normalize(); para } @@ -314,15 +318,32 @@ impl Reader<'_> { // ---- tables ---- + /// Word gives a table no cell margins when the file has no default table style and neither the + /// table nor its style sets them; saving may write a default Normal Table, so the zeros are + /// kept as the table's own. shortcut: they stay direct formatting (a table style applied later + /// keeps them), until the model knows the file's default table style. A `w:tblCellMar` that sets + /// only some sides keeps them: only the sides it omits become 0. + fn no_default_margins(&self, props: &mut craft_wordmodel::props::TableProps) { + if self.no_default_table_style + && props.cell_margins.is_none() + && props.style.as_deref().and_then(|id| self.doc.styles.table_style(id)).is_none_or(|s| s.parts.cell_margins.is_none()) + { + props.cell_margins = Some([0.0; 4]); + } + } + fn read_table(&mut self, sc: &mut StoryCtx, t: &El, rels: &Rels, depth: usize) -> Option { let mut table = Table::default(); if let Some(p) = t.child("w:tblPr") { table.props = self.pc.tblpr(p); if let Some(ch) = p.child("w:tblPrChange") { - let old = ch.child("w:tblPr").map(|o| self.pc.tblpr(o)).unwrap_or_default(); + let mut old = ch.child("w:tblPr").map(|o| self.pc.tblpr(o)).unwrap_or_default(); + // Rejecting the change brings back these properties: with the same margins. + self.no_default_margins(&mut old); table.props.fmt_change = PropChange::boxed(self.revision(RevisionKind::Format, ch), old); } } + self.no_default_margins(&mut table.props); if let Some(g) = t.child("w:tblGrid") { table.grid = g .children("w:gridCol") @@ -355,8 +376,10 @@ impl Reader<'_> { continue; } let mut blocks = Blocks::new(); + let outer = std::mem::replace(&mut sc.in_cell, true); self.read_blocks(sc, tc, rels, &mut blocks, depth + 4); self.flush_pending(sc, &mut blocks); + sc.in_cell = outer; row.cells.push(Cell { props, blocks, controls: cell_controls }); } if !row.cells.is_empty() { @@ -370,6 +393,20 @@ impl Reader<'_> { let cols = table.cols().clamp(1, MAX_COLS); table.grid = vec![72.0; cols]; } + // The cells' paragraphs, with the run formatting the table style gives each cell (a + // table inside has marked its own). + let chrs = self.doc.styles.table_cells_chr(&table); + for (ri, row) in table.rows.iter_mut().enumerate() { + for (ci, cell) in row.cells.iter_mut().enumerate() { + let chr = chrs.as_ref().and_then(|c| c.get(ri)?.get(ci)); + for b in &mut cell.blocks { + if let Block::Para(p) = Arc::make_mut(b) { + self.doc.styles.mark_runs_as_word_draws_them(p, chr); + p.normalize(); + } + } + } + } Some(table) } } diff --git a/crates/docx/src/read/styles.rs b/crates/docx/src/read/styles.rs index 080eaff21..e12749a87 100644 --- a/crates/docx/src/read/styles.rs +++ b/crates/docx/src/read/styles.rs @@ -64,6 +64,20 @@ fn builtin_heading_level(name: &str) -> Option { } impl Reader<'_> { + /// Whether a style's kept (last) definition is a default table style. + fn has_default_table_style(&self, root: &El) -> bool { + let mut defaults: Vec = Vec::new(); + for s in root.children("w:style").take(10_000) { + let Some(raw_id) = s.attr("w:styleId").filter(|i| !i.is_empty()) else { continue }; + let id = self.pc.style_id(raw_id); + defaults.retain(|d| *d != id); + if s.attr("w:type") == Some("table") && style_is_default(s) { + defaults.push(id); + } + } + !defaults.is_empty() + } + pub(super) fn read_styles(&mut self, path: &str) -> Result<(), DocxError> { /// Largest `w:latentStyles` kept (Word's has ~380 exceptions, ~40 KB). const MAX_LATENT_STYLES_XML: usize = 1 << 20; @@ -207,6 +221,7 @@ impl Reader<'_> { None => sheet.styles.push(st), } } + self.no_default_table_style = !self.has_default_table_style(&root); // Styles that give only a list level take the list from the style they are based on // (Heading 3: level 3 of Heading 2's outline list). let fixed: Vec> = sheet @@ -316,9 +331,11 @@ impl Reader<'_> { for a in root.children("w:abstractNum").take(10_000) { let Some(id) = a.attr("w:abstractNumId").and_then(u32_of) else { continue }; let mut levels: Vec = (0..9).map(default_level).collect(); + let mut explicit_levels = [false; 9]; for l in a.children("w:lvl") { let Some(i) = numbering_level_index(l.attr("w:ilvl")) else { continue }; levels[i] = self.level(l, i); + explicit_levels[i] = true; } if let Some(link) = a.child_val("w:numStyleLink") { links.push((id, link.to_string())); @@ -328,6 +345,7 @@ impl Reader<'_> { id, name: a.child_val("w:name").map(str::to_string), levels, + explicit_levels: Some(explicit_levels), nsid: a.child_val("w:nsid").filter(|v| hex(v)).map(str::to_ascii_uppercase), tmpl: a.child_val("w:tmpl").filter(|v| hex(v)).map(str::to_ascii_uppercase), multi_level_type: a @@ -361,9 +379,13 @@ impl Reader<'_> { for (aid, style) in links { let num = self.doc.styles.get(&self.pc.style_id(&style)).and_then(|s| s.para.numbering).map(|n| n.num); let src = num.and_then(|n| nums.iter().find(|x: &&Num| x.id == n)).map(|x| x.abstract_id); - let levels = src.filter(|s| *s != aid).and_then(|s| abstracts.iter().find(|a: &&AbstractNum| a.id == s)).map(|a| a.levels.clone()); - if let (Some(levels), Some(a)) = (levels, abstracts.iter_mut().find(|a| a.id == aid)) { + let source = src + .filter(|s| *s != aid) + .and_then(|s| abstracts.iter().find(|a: &&AbstractNum| a.id == s)) + .map(|a| (a.levels.clone(), a.explicit_levels)); + if let (Some((levels, explicit_levels)), Some(a)) = (source, abstracts.iter_mut().find(|a| a.id == aid)) { a.levels = levels; + a.explicit_levels = explicit_levels; } } Arc::make_mut(&mut self.doc.numbering).abstracts = abstracts; diff --git a/crates/docx/tests/it/bidi.rs b/crates/docx/tests/it/bidi.rs index 3df1c04c9..bea7e2131 100644 --- a/crates/docx/tests/it/bidi.rs +++ b/crates/docx/tests/it/bidi.rs @@ -175,3 +175,155 @@ fn rtl_tables_and_sections_round_trip() { assert_eq!(again, vec![true, false]); assert!(r.last_section.rtl && r.last_section.columns.count == 2); } + +/// A package whose body is `body` and whose styles part holds `styles`. +fn docx_with_styles(body: &str, styles: &str) -> Vec { + let ns = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main""#; + let root = r#""#; + let rels = r#""#; + let doc = format!(r#"{body}"#); + let st = format!(r#"{styles}"#); + zip(&[ + ("_rels/.rels", root.as_bytes()), + ("word/_rels/document.xml.rels", rels.as_bytes()), + ("word/document.xml", doc.as_bytes()), + ("word/styles.xml", st.as_bytes()), + ]) +} + +/// Thai text draws in the complex-script formatting only where Word draws it so (measured in Word +/// for Windows; run defaults Angsana New 22 pt, complex script Tahoma 11 pt bold). Without +/// `w:cs` or `w:rtl` on the run, its paragraph style or the run defaults, Thai text measures as +/// Angsana New 22 pt, whatever its language, `w:hint` or character style. +#[test] +fn thai_text_takes_the_complex_script_formatting_only_in_a_complex_script_run() { + let thai = "ทดสอบ ภาษาไทย"; + let defaults = |extra: &str| { + format!( + r#"{extra} + + + + + + +"# + ) + }; + let latin = ("Angsana New", 22.0, false); + let complex = ("Tahoma", 11.0, true); + let run = |rpr: &str| format!(r#"{rpr}{thai}"#); + let styled = |p: &str, rpr: &str| { + format!(r#"{rpr}{thai}"#) + }; + let in_table = |style: &str| { + format!( + r#"{}"#, + run("") + ) + }; + let cs_off = r#""#; + let cases: Vec<(&str, String, String, (&str, f32, bool))> = vec![ + ("plain", defaults(""), run(""), latin), + ("bidi language", defaults(""), run(r#""#), latin), + ("hint", defaults(""), run(r#""#), latin), + ("character style", defaults(""), run(r#""#), latin), + ("character style rtl", defaults(""), run(r#""#), latin), + ("run cs", defaults(""), run(""), complex), + ("run rtl", defaults(""), run(""), complex), + ("paragraph style", defaults(""), styled("CsPara", ""), complex), + ("paragraph style, run cs off", defaults(""), styled("CsPara", r#""#), latin), + ("run defaults", defaults(""), run(""), complex), + // A character style's `w:cs w:val="0"` doesn't turn the paragraph style's or the run + // defaults' off either. + ("paragraph style, character style cs off", defaults(""), styled("CsPara", cs_off), complex), + ("run defaults, character style cs off", defaults(""), run(cs_off), complex), + // A table style's `w:cs` counts, and its sizes are the run's. + ("table style", defaults(""), in_table("CsTable"), complex), + ("table style sizes", defaults(""), in_table("SizeTable"), ("Angsana New", 15.0, false)), + ]; + let check = |d: &Document, name: &str, want: (&str, f32, bool), when: &str| { + let (p, table) = match &*d.body[0] { + craft_wordmodel::Block::Table(t) => { + (t.rows[0].cells[0].blocks[0].as_para().unwrap(), d.styles.table_cells_chr(t).map(|c| c[0][0].clone())) + } + b => (b.as_para().unwrap(), None), + }; + let rc = d.styles.resolve_char_in(p.props.style.as_deref(), table.as_ref(), &p.runs[0].props); + for ch in p.text.chars() { + let drawn = if rc.uses_complex(ch) { rc.complex() } else { rc.clone() }; + assert_eq!((drawn.font.as_str(), drawn.size, drawn.bold), want, "{name} {when}: {ch:?}"); + } + let c = &p.runs[0].props; + assert_eq!((c.font_cs.as_deref(), c.size_cs, c.bold_cs), (None, None, None), "{name} {when}: nothing is pinned"); + }; + for (name, styles, body, want) in &cases { + let d = craft_docx::read(&docx_with_styles(body, styles)).unwrap(); + check(&d, name, *want, "read"); + let again = craft_docx::read(&craft_docx::write(&d).unwrap()).unwrap(); + check(&again, name, *want, "after a save"); + } + // Latin text and right-to-left text are left as they were. + let body = r#"Latin onlyسلام"#; + let d = craft_docx::read(&docx_with_styles(body, &defaults(""))).unwrap(); + for p in paras(&d) { + assert_eq!(p.runs[0].props.cs, None, "{}", p.text); + } + // Latin text follows the same rule (measured): a character style's `w:cs` or `w:rtl` leaves it in the + // run's own formatting, a character style's `w:cs w:val="0"` under a paragraph style's + // `w:cs` in the complex-script formatting. + let latin_in = + |p: &str, rpr: &str| format!(r#"{rpr}Latin"#); + let body = + latin_in("Normal", r#""#) + &latin_in("CsPara", cs_off) + &latin_in("Normal", r#""#); + let d = craft_docx::read(&docx_with_styles(&body, &defaults(""))).unwrap(); + let again = craft_docx::read(&craft_docx::write(&d).unwrap()).unwrap(); + for (d, when) in [(&d, "read"), (&again, "after a save")] { + let drawn: Vec<_> = paras(d) + .iter() + .map(|p| { + let rc = d.styles.resolve_char(p.props.style.as_deref(), &p.runs[0].props); + let c = if rc.uses_complex('L') { rc.complex() } else { rc }; + (c.font, c.size) + }) + .collect(); + assert_eq!(drawn, [("Angsana New".to_string(), 22.0), ("Tahoma".to_string(), 11.0), ("Angsana New".to_string(), 22.0)], "{when}"); + } +} + +/// Measured in Word for Windows: Thai text as a field's result follows the same rule as ordinary +/// text, also when the reader keeps the field as one object with its result. +#[test] +fn a_fields_thai_result_takes_the_complex_script_formatting_only_in_a_complex_script_run() { + let styles = r#" +"#; + let field = |rpr: &str| { + let r = |inner: &str| format!("{rpr}{inner}"); + format!( + "{}{}{}{}{}", + r(r#""#), + r(r#" QUOTE "x" "#), + r(r#""#), + r("ทดสอบ"), + r(r#""#) + ) + }; + let drawn = |d: &Document| { + let p = paras(d)[0]; + let at = p.text.find(craft_wordmodel::para::OBJ).expect("the field is one object"); + let run = p.runs.iter().scan(0, |end, r| { + let start = *end; + *end += r.len; + Some((start, r)) + }); + let (_, run) = run.into_iter().find(|(start, r)| *start <= at && at < start + r.len).unwrap(); + let rc = d.styles.resolve_char(p.props.style.as_deref(), &run.props); + if rc.uses_complex('ท') { (rc.font_cs.clone(), rc.size_cs) } else { (rc.font.clone(), rc.size) } + }; + for (what, rpr, want) in [("nothing", "", ("Angsana New", 10.0)), ("w:cs", "", ("Tahoma", 20.0))] { + let d = craft_docx::read(&docx_with_styles(&field(rpr), styles)).unwrap(); + assert_eq!(drawn(&d), (want.0.to_string(), want.1), "a run with {what}: read"); + let again = craft_docx::read(&craft_docx::write(&d).unwrap()).unwrap(); + assert_eq!(drawn(&again), (want.0.to_string(), want.1), "a run with {what}: after a save"); + } +} diff --git a/crates/docx/tests/it/cell_margin_inheritance.rs b/crates/docx/tests/it/cell_margin_inheritance.rs index f4d8de5a5..83aafc7d0 100644 --- a/crates/docx/tests/it/cell_margin_inheritance.rs +++ b/crates/docx/tests/it/cell_margin_inheritance.rs @@ -28,3 +28,73 @@ fn partial_margin_stays_partial() { assert!(!margins.contains(") -> Vec { + const W: &str = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; + let mut zip = zip::ZipWriter::new(Cursor::new(Vec::new())); + let mut add = |name: &str, xml: &str| { + zip.start_file(name, zip::write::SimpleFileOptions::default()).unwrap(); + zip.write_all(xml.as_bytes()).unwrap(); + }; + add("word/document.xml", &format!(r#"{body}"#)); + if let Some(styles) = styles { + add("word/styles.xml", &format!(r#"{styles}"#)); + add( + "word/_rels/document.xml.rels", + r#""#, + ); + } + zip.finish().unwrap().into_inner() +} + +/// Without a default table style (no styles part, or none marked `w:default`) Word gives a table +/// no cell margins: a cell that sets only its right margin has none on its other sides. +#[test] +fn no_default_table_style_means_no_cell_margins() { + let body = r#""#; + let normal_table = r#""#; + let margins = |styles: Option<&str>| { + let doc = craft_docx::read(&package(body, styles)).unwrap(); + let table = doc.body[0].as_table().unwrap(); + (table.props.cell_margins, table.rows[0].cells[0].props.resolved_margins(table.props.cell_margins.unwrap_or([0.0, 5.4, 0.0, 5.4]))) + }; + assert_eq!(margins(None), (Some([0.0; 4]), [0.0, 0.0, 0.0, 5.4])); + assert_eq!(margins(Some(r#""#)), (Some([0.0; 4]), [0.0, 0.0, 0.0, 5.4])); + // With Word's usual Normal Table the table keeps no margins of its own (the defaults apply). + assert_eq!(margins(Some(normal_table)).0, None); + // A style defined twice is its last definition: a default one redefined as not default leaves none. + let redefined = format!(r#"{normal_table}"#); + assert_eq!(margins(Some(&redefined)).0, Some([0.0; 4])); + // A custom default table style marked `on` is a default too, also after saving and reopening. + let plain = r#""#; + let doc = craft_docx::read(&package(body, Some(plain))).unwrap(); + assert_eq!(doc.body[0].as_table().unwrap().props.cell_margins, None); + let reopened = craft_docx::read(&craft_docx::write(&doc).unwrap()).unwrap(); + assert_eq!(reopened.body[0].as_table().unwrap().props.cell_margins, None); + // A table that sets only some sides keeps them; the sides it omits are 0. + let partial = body.replace("", r#""#); + let doc = craft_docx::read(&package(&partial, None)).unwrap(); + assert_eq!(doc.body[0].as_table().unwrap().props.cell_margins, Some([0.0, 7.2, 0.0, 0.0])); + // Rejecting a tracked change to the table's properties brings back the same zero margins. + let tracked = body.replace( + "", + r#""#, + ); + let doc = craft_docx::read(&package(&tracked, None)).unwrap(); + let change = doc.body[0].as_table().unwrap().props.fmt_change.as_deref().expect("a tracked table change"); + assert_eq!(change.old.cell_margins, Some([0.0; 4])); + // A table whose own style gives margins keeps them. + let styled = body.replace("", r#""#); + let boxed = r#""#; + let doc = craft_docx::read(&package(&styled, Some(boxed))).unwrap(); + assert_eq!(doc.body[0].as_table().unwrap().props.cell_margins, None); + // The zeros survive saving, which writes a default Normal Table. + let doc = craft_docx::read(&package(body, None)).unwrap(); + let bytes = craft_docx::write(&doc).unwrap(); + let mut xml = String::new(); + zip::ZipArchive::new(Cursor::new(bytes.clone())).unwrap().by_name("word/document.xml").unwrap().read_to_string(&mut xml).unwrap(); + assert!(xml.contains(""), "{xml}"); + let reopened = craft_docx::read(&bytes).unwrap(); + assert_eq!(reopened.body[0].as_table().unwrap().props.cell_margins, Some([0.0; 4])); +} diff --git a/crates/docx/tests/it/passthrough.rs b/crates/docx/tests/it/passthrough.rs index 9e5f35a9f..9d3176a76 100644 --- a/crates/docx/tests/it/passthrough.rs +++ b/crates/docx/tests/it/passthrough.rs @@ -335,6 +335,9 @@ fn malformed_numbering_levels_are_skipped_instead_of_becoming_level_zero() { }) .collect(); assert_eq!(paragraphs.len(), 5); + assert!(doc.numbering.level_is_defined(1, 0)); + assert!(!doc.numbering.level_is_defined(1, 2), "effective defaults remain distinguishable from source definitions"); + assert!(doc.numbering.level(1, 2).is_some(), "Word's effective default level remains available"); assert_eq!(paragraphs[0].props.numbering.map(|n| (n.num, n.level)), Some((1, 0)), "an omitted level defaults to zero"); assert_eq!(paragraphs[1].props.numbering.map(|n| (n.num, n.level)), Some((1, 2)), "a valid level is preserved"); for p in ¶graphs[2..] { diff --git a/crates/dwg/README.md b/crates/dwg/README.md index ed52bdcfc..80088a89e 100644 --- a/crates/dwg/README.md +++ b/crates/dwg/README.md @@ -11,7 +11,9 @@ allocation failure inside the decoder. On native applications, call `use_own_wor startup and call `serve_if_asked()` before normal argument handling. Subsequent `decode` and `encode_*` calls then run in a child process with a 30-minute timeout; a crash or stack overflow is contained to that child. The worker has no operating-system memory limit yet. Without a -configured worker, conversions run in the current process. +configured worker, conversions are refused rather than run in the current process: call +`allow_in_process()` only for input that is known to be trusted (tests, or bytes this process +produced itself). Coordinates, once a worker is configured but cannot start, also fail closed. The bridge is not built for `wasm32` or FreeBSD/aarch64. Those targets retain the public API and return a clear unsupported-build error. The `acadrust` 0.6.6 dependency is used unmodified under diff --git a/crates/dwg/src/lib.rs b/crates/dwg/src/lib.rs index 2db7afeae..05c1a673c 100644 --- a/crates/dwg/src/lib.rs +++ b/crates/dwg/src/lib.rs @@ -21,6 +21,11 @@ //! These bound the input- and output-size variants. A small file that declares a huge internal //! table or section can still make the dependency allocate before any size is known, so the app //! runs both directions in a child process ([`worker`]): what that kills is the child. +//! +//! Isolation is not optional for untrusted input. With no worker configured the conversion is +//! refused rather than run here, and a worker that cannot start is an error, so a missing worker +//! never turns into decoding a hostile file in the caller's process. [`worker::allow_in_process`] +//! is the explicit exception, for input the caller knows is trusted. #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] #![forbid(unsafe_code)] @@ -65,7 +70,10 @@ fn mb(n: u64) -> String { #[cfg(not(no_dwg_bridge))] pub mod worker; #[cfg(not(no_dwg_bridge))] -pub use worker::{WORKER_ARG, crash_worker_for_test, decode, encode_as, encode_kept_noted, encode_noted, serve_if_asked, use_own_worker, use_worker}; +pub use worker::{ + WORKER_ARG, allow_in_process, crash_worker_for_test, decode, encode_as, encode_kept_noted, encode_noted, serve_if_asked, use_own_worker, + use_worker, +}; #[cfg(not(no_dwg_bridge))] mod proxy_transport; @@ -1569,6 +1577,8 @@ pub fn serve_if_asked() -> Option { #[cfg(no_dwg_bridge)] pub fn use_own_worker() {} #[cfg(no_dwg_bridge)] +pub fn allow_in_process() {} +#[cfg(no_dwg_bridge)] pub fn decode(bytes: &[u8], limits: Limits) -> Result<(Vec, Vec), String> { dwg_to_dxf_noted(bytes, limits) } diff --git a/crates/dwg/src/worker.rs b/crates/dwg/src/worker.rs index 0c273257b..1d16f5701 100644 --- a/crates/dwg/src/worker.rs +++ b/crates/dwg/src/worker.rs @@ -6,9 +6,11 @@ //! that reads one request from standard input and writes one reply to standard output. A file that //! kills the worker then costs that one open or save, and every other open drawing stays safe. //! -//! Without a worker (libraries, tests, a build that can't start one) [`decode`] and [`encode_as`] -//! run in the calling process, as they always did. There's no memory limit on the worker yet: that -//! takes per-platform system calls, which `unsafe_code = "forbid"` keeps out of this crate. +//! Without a worker [`decode`] and [`encode_as`] refuse rather than run the conversion in the +//! calling process, because the third-party code is the part that can end a process. +//! [`allow_in_process`] is the explicit exception, for input the caller knows is trusted (tests, or +//! bytes this process produced itself). There's no memory limit on the worker yet: that takes +//! per-platform system calls, which `unsafe_code = "forbid"` keeps out of this crate. //! //! A reply comes from a process that just handled a possibly hostile file, so it is parsed as //! untrusted input. @@ -18,6 +20,7 @@ use std::io::{Read, Write}; use std::path::PathBuf; use std::process::{Command, Stdio}; use std::sync::Mutex; +use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::{RecvTimeoutError, channel}; use std::time::Duration; @@ -38,6 +41,9 @@ const MAX_NOTES_BYTES: u64 = 64 << 20; const MAX_DWG_REPLY: u64 = 2 << 30; /// Most bytes one request holds: the largest input, whichever the direction, and its header. const MAX_REQUEST: u64 = (2 << 30) + 256; +/// What a conversion says when it has no worker and is not allowed to run here. +const NO_WORKER: &str = "DWG: no worker is configured, so this file was not converted; \ + call use_worker or use_own_worker at startup, or allow_in_process for input that is trusted"; struct Worker { program: PathBuf, @@ -45,6 +51,8 @@ struct Worker { } static WORKER: Mutex> = Mutex::new(None); +/// Whether this process may run the conversion itself, set only by [`allow_in_process`]. +static IN_PROCESS: AtomicBool = AtomicBool::new(false); /// Start `program args` as the worker from now on. `program` must call [`serve_if_asked`]. pub fn use_worker(program: PathBuf, args: Vec) { @@ -62,12 +70,27 @@ pub fn use_own_worker() { } } +/// Let [`decode`] and [`encode_as`] run the conversion in this process when no worker is set. +/// +/// This opts out of the isolation the crate exists for: the third-party code allocates from sizes +/// a hostile file declares, and a failed allocation ends the process. Call it for input the caller +/// knows is trusted (tests, or bytes this process produced), never for a file that arrived from +/// outside. +pub fn allow_in_process() { + IN_PROCESS.store(true, Ordering::Relaxed); +} + +fn in_process_allowed() -> bool { + IN_PROCESS.load(Ordering::Relaxed) +} + /// Serve one request and return the exit code if this process was started as a worker. pub fn serve_if_asked() -> Option { (std::env::args_os().nth(1)? == WORKER_ARG).then(|| serve(std::io::stdin().lock(), std::io::stdout().lock())) } -/// [`super::dwg_to_dxf_noted`] in the worker, when there is one. +/// [`super::dwg_to_dxf_noted`] in the worker. With no worker configured this is refused unless +/// [`allow_in_process`] was called; a configured worker that cannot start is an error. pub fn decode(bytes: &[u8], limits: Limits) -> Result<(Vec, Vec), String> { // Before starting a process, and so the same limit holds with or without one. if bytes.len() > limits.max_dwg_bytes { @@ -76,17 +99,19 @@ pub fn decode(bytes: &[u8], limits: Limits) -> Result<(Vec, Vec), St let reply_limit = (limits.max_dxf_bytes as u64).saturating_add(MAX_NOTES_BYTES); match call(&request(DECODE, limits, ""), bytes, reply_limit, "reading this file") { Some(reply) => reply?, - None => super::dwg_to_dxf_noted(bytes, limits), + None if in_process_allowed() => super::dwg_to_dxf_noted(bytes, limits), + None => Err(NO_WORKER.to_string()), } } -/// [`super::dxf_to_dwg_as`] in the worker, when there is one. +/// [`super::dxf_to_dwg_as`] in the worker. Refused without a worker unless [`allow_in_process`] +/// was called. pub fn encode_as(dxf: &[u8], version: &str) -> Result, String> { encode_noted(dxf, version).map(|(dwg, _)| dwg) } -/// [`super::dxf_to_dwg_noted`] in the worker, when there is one. What the writer left out comes -/// back as notes of `TYPEcount`. +/// [`super::dxf_to_dwg_noted`] in the worker. What the writer left out comes back as notes of +/// `TYPEcount`. pub fn encode_noted(dxf: &[u8], version: &str) -> Result<(Vec, super::Unwritten), String> { encode_kept_noted(dxf, version, None) } @@ -103,7 +128,8 @@ pub fn encode_kept_noted(dxf: &[u8], version: &str, kept: Option<&str>) -> Resul push_str(&mut header, kept.unwrap_or("")); match call(&header, dxf, MAX_DWG_REPLY, "writing this file") { Some(reply) => reply?.map(|(dwg, notes)| (dwg, unwritten_from(¬es))), - None => encode_here(dxf, version, kept), + None if in_process_allowed() => encode_here(dxf, version, kept), + None => Err(NO_WORKER.to_string()), } } @@ -147,8 +173,9 @@ fn push_str(r: &mut Vec, text: &str) { type Reply = Result<(Vec, Vec), String>; -/// Run one request in a worker. `None`: none could be started, so the caller does the work. -/// `Err`: the worker went wrong, and `doing` says at what, for the user. +/// Run one request in a worker. `None`: no worker is configured, so the caller decides what to do +/// (convert here only with [`allow_in_process`], or refuse). `Err`: a configured worker could not +/// start or went wrong, and `doing` says at what, for the user. fn call(header: &[u8], payload: &[u8], reply_limit: u64, doing: &str) -> Option> { let mut command = { let worker = WORKER.lock().ok()?; @@ -167,14 +194,15 @@ fn call(header: &[u8], payload: &[u8], reply_limit: u64, doing: &str) -> Option< let mut child = match command.spawn() { Ok(c) => c, Err(e) => { + // A worker is configured but cannot run: never fall back to decoding here. log::warn!("DWG worker: can't start it: {e}"); - return None; + return Some(Err(format!("the DWG worker could not start ({e}); nothing else is affected"))); } }; let (Some(mut to_child), Some(from_child)) = (child.stdin.take(), child.stdout.take()) else { let _ = child.kill(); let _ = child.wait(); - return None; + return Some(Err("the DWG worker could not start (no pipes); nothing else is affected".to_string())); }; let received = std::thread::scope(|scope| { // Written beside the read: a worker that replies before it has read everything, or dies, @@ -367,6 +395,25 @@ mod tests { } } + /// A conversion is refused rather than run here, and a worker that cannot start is an error: + /// neither case may turn into decoding a (possibly hostile) file in this process. + #[test] + fn a_conversion_is_refused_without_a_worker_and_never_falls_back_to_this_process() { + // No worker is configured and nothing opted in. + assert_eq!(decode(b"AC1032", Limits::DEFAULT).unwrap_err(), NO_WORKER); + assert_eq!(encode_as(b"not a dxf", "AC1032").unwrap_err(), NO_WORKER); + // Trusted input may run here, and the reader still reports a file it cannot read. + allow_in_process(); + assert!(in_process_allowed()); + assert!(decode(b"AC1032", Limits::DEFAULT).is_err()); + assert!(encode_as(b"not a dxf", "AC1032").is_err()); + // A configured worker that cannot start is an error even after the opt-in: the conversion + // never runs here instead. + use_worker(PathBuf::from("/nonexistent/craft-dwg-worker"), Vec::new()); + assert!(decode(b"AC1032", Limits::DEFAULT).unwrap_err().contains("could not start")); + assert!(encode_as(b"not a dxf", "AC1032").unwrap_err().contains("could not start")); + } + #[test] fn serving_reports_a_decode_failure_as_a_reply() { let mut request = self::request(DECODE, Limits::DEFAULT, ""); diff --git a/crates/fig/src/archive.rs b/crates/fig/src/archive.rs index e408ddbe7..e083aa1dc 100644 --- a/crates/fig/src/archive.rs +++ b/crates/fig/src/archive.rs @@ -12,8 +12,10 @@ use crate::{Error, corrupt}; const MAX_META: u64 = 1 << 20; /// Largest file [`write_archive`] writes (the canvas and the images together). const MAX_FILE: u64 = 2 * 1024 * 1024 * 1024; -/// Most entries an archive may declare; checked before the archive is indexed. -pub const MAX_ENTRIES: u64 = 1_000_000; +/// Most entries an archive may declare; checked before the archive is indexed. The archive holds one +/// entry per image, so 100 000 leaves room for far more than a `.fig` file carries, and a header +/// count can no longer make the index alone cost a hundred megabytes. +pub const MAX_ENTRIES: u64 = 100_000; /// What one open may read from `images/`. #[derive(Clone, Copy, Debug, PartialEq, Eq)] diff --git a/crates/gif/src/write.rs b/crates/gif/src/write.rs index bc8a4be97..259aa78f1 100644 --- a/crates/gif/src/write.rs +++ b/crates/gif/src/write.rs @@ -194,6 +194,45 @@ pub fn animation(width: u32, height: u32, frames: &[RgbaFrame<'_>], options: Opt writer.finish() } +/// Encode a single pre-quantized indexed-color frame as a GIF89a image. +/// +/// `palette` holds 1 to 256 RGB triplets; `indices` is `width * height` row-major palette indices. +/// When `interlaced` is true, rows are reordered into GIF's 4-pass interlaced order before writing. +pub fn encode_indexed( + width: u32, + height: u32, + palette: &[[u8; 3]], + indices: &[u8], + transparent: Option, + interlaced: bool, +) -> Result, Error> { + let (w, h) = dimensions(width, height)?; + let expected = usize::from(w) * usize::from(h); + if indices.len() != expected { + return Err(Error::FrameLength { index: 0, actual: indices.len(), expected }); + } + if palette.is_empty() || palette.len() > 256 { + return Err(Error::Encode("GIF palette must hold 1 to 256 entries".into())); + } + let flat_palette: Vec = palette.iter().flat_map(|c| *c).collect(); + let buffer = if interlaced { std::borrow::Cow::Owned(interlace(indices, usize::from(w))) } else { std::borrow::Cow::Borrowed(indices) }; + let mut enc = gif::Encoder::new(Vec::new(), w, h, &flat_palette).map_err(Error::from)?; + let frame = gif::Frame { width: w, height: h, buffer, transparent, interlaced, ..gif::Frame::default() }; + enc.write_frame(&frame).map_err(Error::from)?; + enc.into_inner().map_err(Error::from) +} + +/// The rows of a `width`-wide image in GIF's interlaced order: every 8th row from 0, every 8th +/// from 4, every 4th from 2, then every 2nd from 1. +fn interlace(px: &[u8], width: usize) -> Vec { + let rows: Vec<&[u8]> = px.chunks(width.max(1)).collect(); + [(0, 8), (4, 8), (2, 4), (1, 2)] + .into_iter() + .flat_map(|(start, step)| rows.iter().skip(start).step_by(step)) + .flat_map(|r| r.iter().copied()) + .collect() +} + #[cfg(test)] mod tests { #![allow(clippy::unwrap_used)] @@ -288,4 +327,17 @@ mod tests { assert_eq!(decoded.frames[0].rgba, red); assert_eq!(decoded.frames[1].rgba, blue); } + + #[test] + fn encode_indexed_validates_and_writes_gif() { + let palette = [[255, 0, 0], [0, 255, 0]]; + let indices = [0u8, 1, 1, 0, 0, 1, 1, 0, 0]; + let bytes = super::encode_indexed(3, 3, &palette, &indices, Some(1), true).unwrap(); + let info = crate::scan(&bytes, 4).unwrap(); + assert_eq!((info.width, info.height), (3, 3)); + assert_eq!(info.frames.len(), 1); + assert_eq!(info.frames[0].transparent, Some(1)); + assert!(super::encode_indexed(2, 2, &[], &[0; 4], None, false).is_err()); + assert!(super::encode_indexed(2, 2, &palette, &[0; 3], None, false).is_err()); + } } diff --git a/crates/hdr/src/lib.rs b/crates/hdr/src/lib.rs index 9d7d3272c..d46232306 100644 --- a/crates/hdr/src/lib.rs +++ b/crates/hdr/src/lib.rs @@ -11,7 +11,9 @@ //! //! Input is treated as hostile: the header length and the picture size are capped by [`Limits`], //! and every scan line is validated before the float picture is allocated, so a short file cannot -//! ask for a large allocation. +//! ask for a large allocation. Whatever the limits, no buffer is requested past the target's +//! largest allocation (`isize::MAX` bytes), so raised limits on a 32-bit target such as `wasm32` +//! give [`Error::LimitExceeded`] instead of a panic. #[cfg(test)] mod tests; @@ -42,6 +44,7 @@ pub type Result = std::result::Result; #[non_exhaustive] pub struct Limits { /// Most pixels (width × height) a picture may have; each takes 12 bytes decoded. Default 2^26. + /// A picture over the target's largest allocation is refused at any setting. pub max_pixels: u64, /// Longest header read, in bytes (real ones are a few hundred). Default 64 KiB. pub max_header: usize, @@ -62,6 +65,27 @@ pub struct Picture { pub rgb: Vec, } +/// The largest allocation in bytes that a `Vec` or `String` can make on the target. +const MAX_ALLOC_BYTES: u64 = isize::MAX as u64; + +/// Bytes taken by `count` elements of `element_bytes` each, or `None` when the count overflows `u64`. +fn checked_byte_len(count: u64, element_bytes: usize) -> Option { + count.checked_mul(element_bytes as u64) +} + +/// `count` as a length when `count` elements of `element_bytes` each fit in `max_alloc_bytes`. +fn alloc_len(count: u64, element_bytes: usize, max_alloc_bytes: u64) -> Option { + checked_byte_len(count, element_bytes).filter(|&bytes| bytes <= max_alloc_bytes).and_then(|_| usize::try_from(count).ok()) +} + +/// Bytes of `String::from_utf8_lossy(bytes)`: each invalid sequence becomes one U+FFFD (3 bytes). +fn lossy_len(bytes: &[u8]) -> Option { + bytes.utf8_chunks().try_fold(0u64, |n, chunk| { + let invalid = if chunk.invalid().is_empty() { 0 } else { '\u{FFFD}'.len_utf8() }; + n.checked_add(chunk.valid().len() as u64)?.checked_add(invalid as u64) + }) +} + /// Whether `bytes` starts like a Radiance picture. pub fn sniff(bytes: &[u8]) -> bool { bytes.starts_with(b"#?RADIANCE") || bytes.starts_with(b"#?RGBE") @@ -158,6 +182,11 @@ fn scanline(b: &[u8], pos: &mut usize, line: &mut [[u8; 4]]) -> Result<()> { /// Decodes a Radiance picture to linear float RGB, top row first. pub fn decode(b: &[u8], limits: &Limits) -> Result { + decode_within(b, limits, MAX_ALLOC_BYTES) +} + +/// [`decode`] with `max_alloc_bytes` as the largest allocation the target allows. +fn decode_within(b: &[u8], limits: &Limits, max_alloc_bytes: u64) -> Result { if !sniff(b) { return Err(Error::NotHdr); } @@ -166,7 +195,11 @@ pub fn decode(b: &[u8], limits: &Limits) -> Result { let line = |pos: &mut usize| -> Result { let rest = b.get(*pos..).ok_or(Error::Malformed("truncated header"))?; let end = rest.iter().take(limits.max_header).position(|&c| c == b'\n').ok_or(Error::Malformed("truncated header"))?; - let s = String::from_utf8_lossy(rest.get(..end).unwrap_or_default()).trim_end_matches('\r').to_string(); + let raw = rest.get(..end).unwrap_or_default(); + if lossy_len(raw).is_none_or(|n| n > max_alloc_bytes) { + return Err(Error::LimitExceeded("header length")); + } + let s = String::from_utf8_lossy(raw).trim_end_matches('\r').to_string(); *pos += end + 1; Ok(s) }; @@ -188,8 +221,9 @@ pub fn decode(b: &[u8], limits: &Limits) -> Result { } let res = line(&mut pos)?; let bad = Error::Malformed("bad resolution line"); - let t: Vec<&str> = res.split_whitespace().collect(); - let &[a1, n1, a2, n2] = t.as_slice().first_chunk::<4>().ok_or(bad.clone())?; + // Tokens after the fourth are ignored, so they are not collected. + let mut t = res.split_whitespace(); + let (Some(a1), Some(n1), Some(a2), Some(n2)) = (t.next(), t.next(), t.next(), t.next()) else { return Err(bad) }; let ((x1, p1), (x2, p2)) = (axis(a1).ok_or(bad.clone())?, axis(a2).ok_or(bad.clone())?); if x1 == x2 { return Err(Error::Malformed("resolution line names one axis twice")); @@ -205,6 +239,10 @@ pub fn decode(b: &[u8], limits: &Limits) -> Result { if pixels > limits.max_pixels { return Err(Error::LimitExceeded("pixel count")); } + // One scan line of RGBE pixels and the float picture, each within the target's largest allocation. + let scan_len = alloc_len(u64::from(if x1 { height } else { width }), size_of::<[u8; 4]>(), max_alloc_bytes); + let rgb_len = pixels.checked_mul(3).and_then(|floats| alloc_len(floats, size_of::(), max_alloc_bytes)); + let (Some(scan_len), Some(total)) = (scan_len, rgb_len) else { return Err(Error::LimitExceeded("pixel count")) }; // A scan line takes at least a few bits of file, so a header cannot ask for more than the data has. if (b.len().saturating_sub(pos) as u64).saturating_mul(4096) < pixels { return Err(Error::Malformed("truncated pixel data")); @@ -212,12 +250,11 @@ pub fn decode(b: &[u8], limits: &Limits) -> Result { // Validate every encoded scan line before allocating the float picture: a short file could // otherwise ask for the whole limit of zeroed pixels and only then fail on its first line. let pixel_data = pos; - let mut scan = vec![[0u8; 4]; n2]; + let mut scan = vec![[0u8; 4]; scan_len]; for _ in 0..n1 { scanline(b, &mut pos, &mut scan)?; } - let total = w.checked_mul(h).and_then(|n| n.checked_mul(3)).ok_or(Error::LimitExceeded("pixel count"))?; let mut rgb = vec![0f32; total]; pos = pixel_data; for i in 0..n1 { diff --git a/crates/hdr/src/tests.rs b/crates/hdr/src/tests.rs index 7e40454b1..576a8db3f 100644 --- a/crates/hdr/src/tests.rs +++ b/crates/hdr/src/tests.rs @@ -142,3 +142,57 @@ fn limits_are_checked_before_allocating() { assert_eq!(decode(&long, &Limits { max_header: 8, ..Limits::default() }), Err(Error::Malformed("truncated header"))); assert_eq!((Limits::default().max_pixels, Limits::default().max_header), (1 << 26, 64 << 10)); } + +/// `isize::MAX` on a 32-bit target such as `wasm32`. +const BOUND_32: u64 = i32::MAX as u64; + +#[test] +fn buffer_lengths_respect_target_isize_bound() { + // A scan line of RGBE pixels, 4 bytes each. + assert_eq!(alloc_len(536_870_911, 4, BOUND_32), Some(536_870_911)); + assert_eq!(alloc_len(536_870_912, 4, BOUND_32), None); + assert_eq!(alloc_len(u64::from(u32::MAX), 4, BOUND_32), None); + // The float picture: 3 floats of 4 bytes a pixel. + assert_eq!(alloc_len(178_956_970 * 3, 4, BOUND_32), Some(178_956_970 * 3)); + assert_eq!(alloc_len(178_956_971 * 3, 4, BOUND_32), None); + // The default pixel limit fits a 32-bit target. + assert_eq!(alloc_len(Limits::default().max_pixels * 3, 4, BOUND_32), Some(3 << 26)); + assert_eq!(checked_byte_len(u64::MAX, 4), None); + assert_eq!(alloc_len(u64::MAX, 1, MAX_ALLOC_BYTES), None); +} + +#[test] +fn raised_pixel_limit_stops_at_the_target_bound() { + let open = Limits { max_pixels: u64::MAX, ..Limits::default() }; + // 2 × 2 needs an 8-byte scan line and a 48-byte float picture. + let ok = file("-Y 2 +X 2", &[0; 16]); + assert!(decode_within(&ok, &open, 48).is_ok()); + assert_eq!(decode_within(&ok, &open, 47), Err(Error::LimitExceeded("pixel count"))); + // A 1 × 536,870,912 picture is past the 32-bit bound in both buffers; refused before + // anything is allocated, ahead of the scan-line validation. + let wide = file("-Y 1 +X 536870912", &[0; 131_072]); + assert_eq!(decode_within(&wide, &open, BOUND_32), Err(Error::LimitExceeded("pixel count"))); + // 178,956,971 one-pixel scan lines fit the scan buffer but not the float picture. + let tall = file("-Y 178956971 +X 1", &[0; 43_692]); + assert_eq!(decode_within(&tall, &open, BOUND_32), Err(Error::LimitExceeded("pixel count"))); +} + +#[test] +fn header_lines_are_measured_after_lossy_conversion() { + // `#` and ten bytes that are not UTF-8: 11 bytes in the file, 31 as text. + let mut f = b"#?RADIANCE\n#".to_vec(); + f.extend_from_slice(&[0xff; 10]); + f.extend_from_slice(b"\n\n-Y 1 +X 1\n\0\0\0\0"); + assert_eq!(lossy_len(&f[11..22]), Some(31)); + assert!(decode_within(&f, &Limits::default(), 31).is_ok()); + assert_eq!(decode_within(&f, &Limits::default(), 30), Err(Error::LimitExceeded("header length"))); + assert_eq!(lossy_len(b"ok \xe2\x82"), Some(6)); + assert_eq!(lossy_len(b""), Some(0)); +} + +#[test] +fn resolution_tokens_after_the_fourth_are_ignored() { + let f = dec(&file("-Y 1 +X 1 a b c d e f", &[0; 4])).unwrap(); + assert_eq!((f.width, f.height), (1, 1)); + assert_eq!(dec(&file("-Y 1 +X", &[0; 4])), Err(Error::Malformed("bad resolution line"))); +} diff --git a/crates/image-codecs/Cargo.toml b/crates/image-codecs/Cargo.toml index b893c347a..2dc9426ac 100644 --- a/crates/image-codecs/Cargo.toml +++ b/crates/image-codecs/Cargo.toml @@ -13,7 +13,7 @@ workspace = true [features] corpus = [] default = [] -avif = ["image/avif"] +avif = ["image/avif", "dep:ravif", "dep:rav1e", "dep:avif-serialize"] heif = ["dep:craft-heif"] [dependencies] @@ -33,6 +33,10 @@ craft-jpeg = { workspace = true } craft-metadata = { workspace = true } zune-jpegxl = { version = "=0.5.2", default-features = false, features = ["std"] } craft-heif = { workspace = true, optional = true } +ravif = { version = "0.13", default-features = false, optional = true } +rav1e = { version = "0.8", default-features = false, optional = true } +avif-serialize = { version = "0.8", optional = true } +hayro-jpeg2000 = { version = "=0.3.5", default-features = false, features = ["std", "simd"] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] rayon = { workspace = true } diff --git a/crates/image-codecs/src/codecs.rs b/crates/image-codecs/src/codecs.rs index 4c4c22388..0288ae22b 100644 --- a/crates/image-codecs/src/codecs.rs +++ b/crates/image-codecs/src/codecs.rs @@ -1,3 +1,4 @@ +pub(crate) mod avif; pub(crate) mod dds; pub(crate) mod dds_bc; pub(crate) mod deep_exr; @@ -10,6 +11,7 @@ pub(crate) mod gif_frames; pub(crate) mod gif_palette; pub(crate) mod heif; pub(crate) mod iff; +pub(crate) mod jp2; pub(crate) mod jpeg; pub(crate) mod jxl; pub(crate) mod pcx; diff --git a/crates/image-codecs/src/codecs/avif.rs b/crates/image-codecs/src/codecs/avif.rs new file mode 100644 index 000000000..d27112fa1 --- /dev/null +++ b/crates/image-codecs/src/codecs/avif.rs @@ -0,0 +1,211 @@ +//! AVIF encoding helpers (8/16-bit SDR via `ravif` and 10-bit 4:4:4 BT.2020 HDR via `rav1e` + `avif-serialize`). + +use crate::{CodecError, Format, Image}; + +/// Transfer function for [`encode_avif_hdr`]. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum HdrTransfer { + Pq, + Hlg, +} + +/// Absolute luminance of SDR white in an HDR file (ITU-R BT.2408 reference white), cd/m^2. +pub const HDR_REFERENCE_WHITE_NITS: f32 = 203.0; + +#[cfg(all(feature = "avif", not(target_arch = "wasm32")))] +fn pq_encode(nits: f32) -> f32 { + const M1: f32 = 2610.0 / 16384.0; + const M2: f32 = 2523.0 / 4096.0 * 128.0; + const C1: f32 = 3424.0 / 4096.0; + const C2: f32 = 2413.0 / 4096.0 * 32.0; + const C3: f32 = 2392.0 / 4096.0 * 32.0; + let y = (nits / 10_000.0).clamp(0.0, 1.0).powf(M1); + ((C1 + C2 * y) / (1.0 + C3 * y)).powf(M2) +} + +#[cfg(all(feature = "avif", not(target_arch = "wasm32")))] +fn hlg_signal(rgb: [f32; 3]) -> [f32; 3] { + const A: f32 = 0.17883277; + const B: f32 = 0.28466892; + const C: f32 = 0.559_910_7; + rgb.map(|v| { + let s = if v.is_finite() { v.clamp(0.0, 12.0) / 12.0 } else { 0.0 }; + if s <= 1.0 / 12.0 { (3.0 * s).sqrt() } else { A * (12.0 * s - B).ln() + C } + }) +} + +/// Encode an 8-bit or 16-bit SDR `Image` as AVIF (`4:4:4`, 10-bit AV1) with `quality` (`1..=100`) +/// and `speed` (`1..=10`). +pub fn encode_avif_with_speed(img: &Image, quality: u8, speed: u8) -> Result, CodecError> { + #[cfg(all(feature = "avif", not(target_arch = "wasm32")))] + { + let mut enc = + ravif::Encoder::new().with_quality(f32::from(quality.clamp(1, 100))).with_speed(speed.clamp(1, 10)).with_bit_depth(ravif::BitDepth::Ten); + let alpha_mode = if img.layout().has_alpha() { ravif::AlphaColorMode::UnassociatedClean } else { ravif::AlphaColorMode::UnassociatedDirty }; + enc = enc.with_alpha_color_mode(alpha_mode); + let w = img.width() as usize; + let h = img.height() as usize; + if let Some(s16) = img.to_u16_samples() { + let ch = img.layout().channels(); + let mut planes = Vec::with_capacity(w * h); + let mut a_plane = if img.layout().has_alpha() { Some(Vec::with_capacity(w * h)) } else { None }; + for px in s16.chunks_exact(ch) { + let (r, g, b, a) = match ch { + 1 => (px[0], px[0], px[0], u16::MAX), + 2 => (px[0], px[0], px[0], px[1]), + 3 => (px[0], px[1], px[2], u16::MAX), + _ => (px[0], px[1], px[2], px[3]), + }; + let (rf, gf, bf) = (f32::from(r) / 65535.0, f32::from(g) / 65535.0, f32::from(b) / 65535.0); + let y = 0.299 * rf + 0.587 * gf + 0.114 * bf; + let cb = (bf - y) * (0.5 / (1.0 - 0.114)) + 0.5; + let cr = (rf - y) * (0.5 / (1.0 - 0.299)) + 0.5; + let q10 = |v: f32| (v * 1023.0).round().clamp(0.0, 1023.0) as u16; + planes.push([q10(y), q10(cb), q10(cr)]); + if let Some(ap) = &mut a_plane { + ap.push(q10(f32::from(a) / 65535.0)); + } + } + let alpha = a_plane.map(Vec::into_iter); + let res = enc + .encode_raw_planes_10_bit(w, h, planes, alpha, ravif::PixelRange::Full, ravif::MatrixCoefficients::BT601) + .map_err(|e| CodecError::encode(Format::Avif, e))?; + return Ok(res.avif_file); + } + let rgba = img.to_rgba8(); + let px: Vec = rgba.as_chunks::<4>().0.iter().map(|c| ravif::RGBA8::new(c[0], c[1], c[2], c[3])).collect(); + let res = enc.encode_rgba(ravif::Img::new(&px[..], w, h)).map_err(|e| CodecError::encode(Format::Avif, e))?; + Ok(res.avif_file) + } + #[cfg(not(all(feature = "avif", not(target_arch = "wasm32"))))] + { + let _ = (img, quality, speed); + Err(CodecError::unsupported(Format::Avif, "AVIF encoding is not available in this build")) + } +} + +/// Encode linear BT.2020 HDR `rgb` (`width * height * 3` floats, `1.0` = `203` cd/m^2) as +/// 10-bit 4:4:4 BT.2020 PQ or HLG AVIF. +pub fn encode_avif_hdr( + width: u32, + height: u32, + rgb: &[f32], + quality: u8, + speed: u8, + exif: Option<&[u8]>, + transfer: HdrTransfer, +) -> Result, CodecError> { + let (w, h) = (width as usize, height as usize); + let n = w.checked_mul(h).ok_or_else(|| CodecError::encode(Format::Avif, "image too large"))?; + if n == 0 || n.checked_mul(3).is_none_or(|need| rgb.len() < need) { + return Err(CodecError::encode(Format::Avif, "HDR AVIF: sample buffer too short")); + } + #[cfg(all(feature = "avif", not(target_arch = "wasm32")))] + { + use rav1e::prelude::*; + const K: [f32; 3] = [0.2627, 0.6780, 0.0593]; + let mut max_nits = 0f32; + let mut sum_nits = 0f64; + let planes: Vec<[u16; 3]> = rgb + .as_chunks::<3>() + .0 + .iter() + .take(n) + .map(|c| { + let nits = [c[0], c[1], c[2]].map(|v| (if v.is_finite() { v.max(0.0) } else { 0.0 }) * HDR_REFERENCE_WHITE_NITS); + let y_nits = K[0] * nits[0] + K[1] * nits[1] + K[2] * nits[2]; + max_nits = max_nits.max(nits[0].max(nits[1]).max(nits[2])); + sum_nits += f64::from(y_nits); + let [r, g, b] = match transfer { + HdrTransfer::Pq => nits.map(pq_encode), + HdrTransfer::Hlg => hlg_signal([c[0], c[1], c[2]]), + }; + let y = K[0] * r + K[1] * g + K[2] * b; + let cb = (b - y) / (2.0 * (1.0 - K[2])) + 0.5; + let cr = (r - y) / (2.0 * (1.0 - K[0])) + 0.5; + [y, cb, cr].map(|v| (v * 1023.0).round().clamp(0.0, 1023.0) as u16) + }) + .collect(); + let q = f32::from(quality.clamp(1, 100)) / 100.0; + let x = if q >= 0.82 { + (1.0 - q) * 2.6 + } else if q > 0.25 { + 1.0 - 0.125 - q * 0.5 + } else { + 1.0 - q + }; + let quantizer = (x * 255.0).round() as usize; + let mut ec = EncoderConfig::with_speed_preset(speed.clamp(1, 10)); + ec.width = w; + ec.height = h; + ec.time_base = Rational::new(1, 1); + ec.bit_depth = 10; + ec.chroma_sampling = ChromaSampling::Cs444; + ec.pixel_range = PixelRange::Full; + ec.color_description = Some(ColorDescription { + color_primaries: ColorPrimaries::BT2020, + transfer_characteristics: match transfer { + HdrTransfer::Pq => TransferCharacteristics::SMPTE2084, + HdrTransfer::Hlg => TransferCharacteristics::HLG, + }, + matrix_coefficients: MatrixCoefficients::BT2020NCL, + }); + let cll = (max_nits.ceil().clamp(1.0, 10_000.0) as u16, (sum_nits / n as f64).ceil().clamp(1.0, 10_000.0) as u16); + ec.content_light = + (transfer == HdrTransfer::Pq).then_some(ContentLight { max_content_light_level: cll.0, max_frame_average_light_level: cll.1 }); + ec.still_picture = true; + ec.quantizer = quantizer; + ec.min_quantizer = quantizer.min(255) as u8; + ec.speed_settings.multiref = false; + ec.speed_settings.rdo_lookahead_frames = 1; + ec.speed_settings.scene_detection_mode = SceneDetectionSpeed::None; + let mut ctx: Context = Config::new().with_encoder_config(ec).new_context().map_err(|e| CodecError::encode(Format::Avif, e))?; + let mut frame = ctx.new_frame(); + { + let mut it = planes.iter(); + let mut fp = frame.planes.iter_mut(); + let (Some(py), Some(pu), Some(pv)) = (fp.next(), fp.next(), fp.next()) else { + return Err(CodecError::encode(Format::Avif, "AV1: frame has no planes")); + }; + let (mut sy, mut su, mut sv) = (py.mut_slice(Default::default()), pu.mut_slice(Default::default()), pv.mut_slice(Default::default())); + for ((ry, ru), rv) in sy.rows_iter_mut().zip(su.rows_iter_mut()).zip(sv.rows_iter_mut()).take(h) { + for ((y, u), v) in ry.iter_mut().zip(ru.iter_mut()).zip(rv.iter_mut()).take(w) { + let Some(p) = it.next() else { break }; + (*y, *u, *v) = (p[0], p[1], p[2]); + } + } + } + ctx.send_frame(frame).map_err(|e| CodecError::encode(Format::Avif, e))?; + ctx.flush(); + let mut av1 = Vec::new(); + loop { + match ctx.receive_packet() { + Ok(mut p) if p.frame_type == FrameType::KEY => av1.append(&mut p.data), + Ok(_) => {} + Err(EncoderStatus::Encoded | EncoderStatus::LimitReached) => break, + Err(e) => return Err(CodecError::encode(Format::Avif, e)), + } + } + use avif_serialize::constants as c; + let mut a = avif_serialize::Aviffy::new(); + a.set_matrix_coefficients(c::MatrixCoefficients::Bt2020Ncl) + .set_transfer_characteristics(match transfer { + HdrTransfer::Pq => c::TransferCharacteristics::Smpte2084, + HdrTransfer::Hlg => c::TransferCharacteristics::Hlg, + }) + .set_color_primaries(c::ColorPrimaries::Bt2020) + .set_full_color_range(true); + if transfer == HdrTransfer::Pq { + a.set_content_light_level(cll.0, cll.1); + } + if let Some(exif) = exif { + a.set_exif(exif.to_vec()); + } + Ok(a.to_vec(&av1, None, width, height, 10)) + } + #[cfg(not(all(feature = "avif", not(target_arch = "wasm32"))))] + { + let _ = (width, height, rgb, quality, speed, exif, transfer); + Err(CodecError::unsupported(Format::Avif, "AVIF encoding is not available in this build")) + } +} diff --git a/crates/image-codecs/src/codecs/jp2.rs b/crates/image-codecs/src/codecs/jp2.rs new file mode 100644 index 000000000..763ac9c9c --- /dev/null +++ b/crates/image-codecs/src/codecs/jp2.rs @@ -0,0 +1,242 @@ +//! JPEG 2000 (JP2 container and raw J2K codestream) decoding via `hayro-jpeg2000`. +//! +//! Decodes within `Limits`, rejecting unsupported JPX multi-layer/composition boxes and +//! checking declared SIZ/tile/palette/channel-definition bounds before pixel allocation. + +use hayro_jpeg2000::{ColorSpace, DecodeSettings, DecoderContext, Image as Jp2Image}; + +use crate::Format; +use crate::error::CodecError; +use crate::image::{ChannelLayout, Image, SampleType}; +use crate::options::Limits; + +const F: Format = Format::Jp2; +const JP2_SIGNATURE: &[u8] = b"\0\0\0\x0cjP \r\n\x87\n"; +const MAX_PROFILE: usize = 4 * 1024 * 1024; +const MAX_TILES: u64 = 65_535; +const TILE_COMPONENT_BYTES: u64 = 256; + +/// Boxes at one JP2 nesting level. Invalid lengths stop the walk at that level. +fn boxes(b: &[u8]) -> impl Iterator { + let mut at = 0usize; + std::iter::from_fn(move || { + let header = b.get(at..at.checked_add(8)?)?; + let size = u32::from_be_bytes(header.get(..4)?.try_into().ok()?); + let (length, head) = match size { + 0 => (b.len().checked_sub(at)?, 8usize), + 1 => (usize::try_from(u64::from_be_bytes(b.get(at.checked_add(8)?..at.checked_add(16)?)?.try_into().ok()?)).ok()?, 16usize), + n => (n as usize, 8usize), + }; + if length < head { + return None; + } + let end = at.checked_add(length)?; + let data = b.get(at.checked_add(head)?..end)?; + let ty = header.get(4..8)?; + at = end; + Some((ty, data)) + }) +} + +fn unsupported_jpx_content(bytes: &[u8]) -> bool { + fn association_has_content(data: &[u8], depth: usize) -> bool { + if depth >= 16 { + return true; + } + boxes(data).any(|(ty, child)| { + matches!(ty, b"jp2c" | b"jpch" | b"jplh" | b"comp" | b"ftbl") || (ty == b"asoc" && association_has_content(child, depth + 1)) + }) + } + let mut codestreams = 0u8; + for (ty, data) in boxes(bytes) { + match ty { + b"jp2c" => { + codestreams = codestreams.saturating_add(1); + if codestreams > 1 { + return true; + } + } + b"jpch" | b"jplh" | b"comp" | b"ftbl" => return true, + b"asoc" if association_has_content(data, 0) => return true, + _ => {} + } + } + false +} + +fn codestream_size(bytes: &[u8]) -> Option<(u64, u32, u32, u64)> { + let stream = if bytes.starts_with(b"\xff\x4f") { bytes } else { boxes(bytes).find_map(|(ty, data)| (ty == b"jp2c").then_some(data))? }; + if stream.get(..4)? != b"\xff\x4f\xff\x51" { + return None; + } + let length = u16::from_be_bytes(stream.get(4..6)?.try_into().ok()?) as usize; + let count = u16::from_be_bytes(stream.get(40..42)?.try_into().ok()?) as usize; + if count == 0 || length != 38usize.checked_add(count.checked_mul(3)?)? || stream.get(4..4usize.checked_add(length)?)?.len() != length { + return None; + } + let be32 = |range: std::ops::Range| stream.get(range).and_then(|v| v.try_into().ok()).map(u32::from_be_bytes); + let width = be32(8..12)?.checked_sub(be32(16..20)?)?; + let height = be32(12..16)?.checked_sub(be32(20..24)?)?; + let tiles = |grid: u32, offset: u32, tile: u32| match (grid.checked_sub(offset), tile) { + (Some(span), 1..) => Some(u64::from(span).div_ceil(u64::from(tile))), + _ => None, + }; + let across = tiles(be32(8..12)?, be32(32..36)?, be32(24..28)?)?; + let down = tiles(be32(12..16)?, be32(36..40)?, be32(28..32)?)?; + Some((count as u64, width, height, across.checked_mul(down)?)) +} + +fn single_header_boxes(bytes: &[u8]) -> bool { + let mut headers = boxes(bytes).filter(|(ty, _)| *ty == b"jp2h"); + let (Some((_, header)), None) = (headers.next(), headers.next()) else { + return false; + }; + [b"pclr", b"cmap", b"cdef"].iter().all(|kind| boxes(header).filter(|(ty, _)| ty == kind).count() <= 1) +} + +fn channel_definitions(bytes: &[u8]) -> Option { + if bytes.starts_with(b"\xff\x4f") { + return None; + } + let header = boxes(bytes).find_map(|(ty, data)| (ty == b"jp2h").then_some(data))?; + let cdef = boxes(header).find_map(|(ty, data)| (ty == b"cdef").then_some(data))?; + let count = cdef.get(..2).and_then(|n| n.try_into().ok()).map_or(0, |n: [u8; 2]| usize::from(u16::from_be_bytes(n))); + let Some(entries) = cdef.get(2..).filter(|rest| count > 0 && rest.len() == count * 6) else { + return Some(0); + }; + let mut defs: Vec<[u16; 3]> = + entries.as_chunks::<6>().0.iter().map(|&[a, b, c, d, e, f]| [[a, b], [c, d], [e, f]].map(u16::from_be_bytes)).collect(); + defs.sort_unstable(); + let opacity = defs.last().is_some_and(|&[_, kind, association]| kind == 1 && association == 0); + let colors = defs.len() - usize::from(opacity); + let mut seen = vec![false; colors]; + let layout_ok = defs.iter().enumerate().all(|(i, &[channel, kind, association])| { + usize::from(channel) == i + && (i >= colors + || (kind == 0 + && usize::from(association).checked_sub(1).and_then(|slot| seen.get_mut(slot)).is_some_and(|s| !std::mem::replace(s, true)))) + }); + Some(if layout_ok { defs.len() as u64 } else { 0 }) +} + +fn palette_columns(bytes: &[u8], components: u64) -> Result, ()> { + if bytes.starts_with(b"\xff\x4f") { + return Ok(None); + } + let Some(header) = boxes(bytes).find_map(|(ty, data)| (ty == b"jp2h").then_some(data)) else { + return Ok(None); + }; + let Some(palette) = boxes(header).find_map(|(ty, data)| (ty == b"pclr").then_some(data)) else { + return Ok(None); + }; + let columns = palette.get(2).copied().ok_or(())?; + let depths = palette.get(3..3 + usize::from(columns)).ok_or(())?; + if columns == 0 || depths.iter().any(|&d| d & 0x80 != 0 || (d & 0x7f) + 1 > 31) { + return Err(()); + } + let Some(mapping) = boxes(header).find_map(|(ty, data)| (ty == b"cmap").then_some(data)) else { + return Ok(Some(u64::from(columns))); + }; + if mapping.len() != usize::from(columns) * 4 { + return Err(()); + } + for &[c0, c1, kind, column] in mapping.as_chunks::<4>().0 { + let valid = u64::from(u16::from_be_bytes([c0, c1])) < components + && match kind { + 0 => true, + 1 => column < columns, + _ => false, + }; + if !valid { + return Err(()); + } + } + Ok(Some(u64::from(columns))) +} + +pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { + let malformed = |msg: &str| CodecError::malformed(F, msg); + if bytes.starts_with(JP2_SIGNATURE) && unsupported_jpx_content(bytes) { + return Err(CodecError::unsupported(F, "unsupported color space or JPX content")); + } + if bytes.starts_with(JP2_SIGNATURE) && !single_header_boxes(bytes) { + return Err(malformed("invalid JP2 header boxes")); + } + let (codestream_channels, declared_w, declared_h, tile_count) = codestream_size(bytes).ok_or_else(|| malformed("invalid codestream SIZ"))?; + if tile_count == 0 || tile_count > MAX_TILES { + return Err(malformed("invalid tile count")); + } + let max_alloc = limits.max_alloc; + let declared_pixels = u64::from(declared_w).checked_mul(u64::from(declared_h)); + if declared_w == 0 + || declared_h == 0 + || declared_w > limits.max_width + || declared_h > limits.max_height + || declared_pixels.is_none_or(|n| n > limits.max_pixels) + || declared_pixels.and_then(|n| n.checked_mul(codestream_channels)).and_then(|n| n.checked_mul(4)).is_none_or(|n| n > max_alloc) + || tile_count.checked_mul(codestream_channels).and_then(|n| n.checked_mul(TILE_COMPONENT_BYTES)).is_none_or(|n| n > max_alloc) + { + return Err(CodecError::LimitExceeded(format!("dimensions {declared_w}x{declared_h} exceed decode limits"))); + } + let header = boxes(bytes).find_map(|(ty, data)| (ty == b"jp2h").then_some(data)).unwrap_or_default(); + if boxes(header).any(|(ty, data)| ty == b"colr" && data.len() > MAX_PROFILE + 3) { + return Err(CodecError::unsupported(F, "ICC profile exceeds 4 MiB limit")); + } + let palette = palette_columns(bytes, codestream_channels).map_err(|()| malformed("invalid JP2 palette"))?; + let palette_rows = boxes(header).find_map(|(ty, data)| (ty == b"pclr").then(|| data.get(..2)).flatten()); + if let (Some(columns), Some(&[r0, r1])) = (palette, palette_rows) + && u64::from(u16::from_be_bytes([r0, r1])) * (columns * 8 + 24) > max_alloc + { + return Err(CodecError::LimitExceeded("JP2 palette exceeds max_alloc".into())); + } + let image = Jp2Image::new(bytes, &DecodeSettings::default()).map_err(|e| malformed(&format!("{e:?}")))?; + let (w, h) = (image.width(), image.height()); + let color = image.color_space(); + let (channels, icc) = match color { + ColorSpace::Gray => (1u8, None), + ColorSpace::RGB => (3u8, None), + ColorSpace::CMYK => (4u8, None), + ColorSpace::Icc { num_channels: ch @ (1 | 3 | 4), profile } if profile.len() <= MAX_PROFILE => (*ch, Some(profile.to_vec())), + _ => return Err(CodecError::unsupported(F, "unsupported JPEG 2000 color space")), + }; + let has_alpha = image.has_alpha(); + let stride = u64::from(channels) + u64::from(has_alpha); + if palette.is_some_and(|columns| columns != stride) { + return Err(malformed("JP2 palette columns do not match output channels")); + } + if channel_definitions(bytes).is_some_and(|count| count != stride || count != palette.unwrap_or(codestream_channels)) { + return Err(malformed("JP2 channel definitions do not match output channels")); + } + let layout = match (channels, has_alpha) { + (1, false) => ChannelLayout::Gray, + (1, true) => ChannelLayout::GrayA, + (3, false) => ChannelLayout::Rgb, + (3, true) => ChannelLayout::Rgba, + (4, false) => ChannelLayout::Cmyk, + (4, true) => ChannelLayout::CmykA, + _ => return Err(CodecError::unsupported(F, "unsupported channel count")), + }; + limits.check(w, h, layout, SampleType::U8)?; + let plane_channels = codestream_channels.max(stride).max(palette.unwrap_or(0)); + let pixels = u64::from(w).checked_mul(u64::from(h)).ok_or_else(|| CodecError::LimitExceeded("pixel count overflow".into()))?; + let size = pixels.checked_mul(stride).ok_or_else(|| CodecError::LimitExceeded("sample count overflow".into()))?; + let plane_bytes = + pixels.checked_mul(plane_channels).and_then(|n| n.checked_mul(4)).ok_or_else(|| CodecError::LimitExceeded("plane overflow".into()))?; + let scratch_bytes = plane_bytes + .checked_mul(2) + .and_then(|n| n.checked_add(pixels.checked_mul(8)?)) + .ok_or_else(|| CodecError::LimitExceeded("scratch overflow".into()))?; + let working = size + .checked_add(plane_bytes) + .and_then(|n| n.checked_add(scratch_bytes)) + .ok_or_else(|| CodecError::LimitExceeded("memory overflow".into()))?; + if working > max_alloc { + return Err(CodecError::LimitExceeded(format!("working set {working} exceeds max_alloc {max_alloc}"))); + } + let len = usize::try_from(size).map_err(|_| CodecError::LimitExceeded("buffer does not fit in memory".into()))?; + let mut samples = vec![0u8; len]; + image.decode_into(&mut samples, &mut DecoderContext::default()).map_err(|e| malformed(&format!("{e:?}")))?; + let mut out = Image::from_raw(w, h, layout, SampleType::U8, samples)?; + out.icc = icc; + Ok(out) +} diff --git a/crates/image-codecs/src/codecs/jpeg.rs b/crates/image-codecs/src/codecs/jpeg.rs index 173b4b740..bcd86ad5a 100644 --- a/crates/image-codecs/src/codecs/jpeg.rs +++ b/crates/image-codecs/src/codecs/jpeg.rs @@ -479,6 +479,9 @@ pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result "Cineon", Format::Dicom => "DICOM", Format::Iff => "Amiga IFF", + Format::Jp2 => "JPEG 2000", } } @@ -201,6 +206,7 @@ impl Format { Format::Cineon => &["cin"], Format::Dicom => &["dcm", "dicom", "dc3"], Format::Iff => &["iff", "lbm", "ilbm"], + Format::Jp2 => &["jp2", "j2k", "j2c", "jpf", "jpx"], } } @@ -229,6 +235,7 @@ impl Format { Format::Cineon => "image/x-cin", Format::Dicom => "application/dicom", Format::Iff => "image/x-iff", + Format::Jp2 => "image/jp2", } } @@ -303,6 +310,7 @@ pub fn caps(format: Format) -> FormatCaps { Format::Cineon => FormatCaps { depths: &[S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, Format::Dicom => FormatCaps { read: true, write: false, depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, Format::Iff => FormatCaps { depths: &[S::U8], layouts: &[L::Rgb, L::Rgba], alpha: true, ..base }, + Format::Jp2 => FormatCaps { read: true, write: false, layouts: &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L::Cmyk, L::CmykA], icc: true, ..base }, } } @@ -320,6 +328,10 @@ pub fn detect(bytes: &[u8]) -> Option { if b.starts_with(&[0xFF, 0x0A]) || b.starts_with(b"\0\0\0\x0cJXL \r\n\x87\n") { return Some(Format::Jxl); } + // JPEG 2000 container signature box or raw J2K codestream (SOC + SIZ). + if b.starts_with(b"\0\0\0\x0cjP \r\n\x87\n") || b.starts_with(b"\xff\x4f\xff\x51") { + return Some(Format::Jp2); + } if b.starts_with(b"II*\0") || b.starts_with(b"MM\0*") || b.starts_with(b"II+\0") || b.starts_with(b"MM\0+") { return Some(Format::Tiff); } diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index b4358bba1..61109faa1 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -33,6 +33,7 @@ pub mod web; pub use crate::allocation::probe_allocation_size; pub use crate::cicp::Cicp; pub use crate::cicp::ContentLightLevel; +pub use crate::codecs::avif::{HDR_REFERENCE_WHITE_NITS, HdrTransfer, encode_avif_hdr, encode_avif_with_speed}; pub use crate::codecs::exr_cryptomatte::{cryptomatte_id, cryptomatte_key, cryptomatte_preview_color}; pub use crate::codecs::gif_frames::{AnimationFrame, MAX_GIF_FRAMES}; pub use crate::codecs::png::{ @@ -61,7 +62,7 @@ pub use crate::resolution::{exif_resolution, export_exif, export_xmp, photoshop_ pub use crate::source_settings::{WebpEncoding, jpeg_quality, webp_encoding}; pub use half::f16; -use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jpeg, jxl, pcx, png, pnm, tiff, via_image, webp, xpm}; +use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jp2, jpeg, jxl, pcx, png, pnm, tiff, via_image, webp, xpm}; /// Detect the format and decode with default [`Limits`]. pub fn decode(bytes: &[u8]) -> Result { @@ -102,6 +103,7 @@ pub fn decode_as_with(format: Format, bytes: &[u8], opts: &DecodeOptions) -> Res Format::Iff => iff::decode(bytes, l), Format::OpenExr => exr::decode(bytes, l), Format::Heif => heif::decode(bytes, l, opts.keep_orientation), + Format::Jp2 => jp2::decode(bytes, l), Format::Jxl => Err(CodecError::unsupported(format, "JPEG XL reading is not yet available; export is supported")), Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => via_image::decode(format, bytes, l), }?; @@ -446,6 +448,7 @@ pub fn encode(image: &Image, format: Format, opts: &EncodeOptions) -> Result iff::encode(image, plan), Format::OpenExr => exr::encode(image, plan, opts), Format::Heif => Err(CodecError::unsupported(format, "encoding is not available in this build")), + Format::Jp2 => Err(CodecError::unsupported(format, "JPEG 2000 is import-only; save as PNG or TIFF instead")), Format::Jxl => jxl::encode(image, plan), Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => { via_image::encode(format, image, plan, opts) diff --git a/crates/image-codecs/src/options.rs b/crates/image-codecs/src/options.rs index 9674b4edf..ed96b8733 100644 --- a/crates/image-codecs/src/options.rs +++ b/crates/image-codecs/src/options.rs @@ -299,6 +299,9 @@ pub struct EncodeOptions { pub jpeg_chroma_subsampling: bool, /// JPEG scan layout (baseline, baseline optimized or progressive). pub jpeg_format: JpegFormat, + /// Optional number of progressive JPEG scans (typically 3..=5). Ignored unless + /// `jpeg_format` is [`JpegFormat::Progressive`]. + pub jpeg_progressive_scans: Option, pub png_compression: PngCompression, /// Write Adam7-interlaced PNG. pub png_interlaced: bool, @@ -336,6 +339,7 @@ impl Default for EncodeOptions { jpeg_quality: 90, jpeg_chroma_subsampling: true, jpeg_format: JpegFormat::Baseline, + jpeg_progressive_scans: None, png_compression: PngCompression::Default, png_interlaced: false, png_stereo: None, diff --git a/crates/image-codecs/src/web.rs b/crates/image-codecs/src/web.rs index dfd0cd9b9..098462ad7 100644 --- a/crates/image-codecs/src/web.rs +++ b/crates/image-codecs/src/web.rs @@ -286,7 +286,8 @@ pub fn decode_wbmp(bytes: &[u8]) -> Result<(u32, u32, Vec), CodecError> { /// JPEG from 8-bit RGB (`rgb.len() == 3·w·h`): quality 1..=100, optional progressive scans and /// optimised Huffman tables (Save for Web's "Optimized"), optional ICC profile, density, EXIF and -/// XMP. EXIF that doesn't fit in one segment is left out (see [`jpeg_exif_fits`]). +/// XMP. EXIF that doesn't fit in one segment is left out (see [`jpeg_exif_fits`]); XMP that +/// doesn't fit loses its layered-document properties, else is left out too (see [`jpeg_xmp_fits`]). #[allow(clippy::too_many_arguments)] pub fn encode_jpeg_rgb8( width: u32, @@ -326,11 +327,10 @@ pub fn encode_jpeg_rgb8( if let Some(seg) = exif.and_then(|x| crate::codecs::jpeg::exif_segment(x, ppi)) { enc.add_app_segment(1, &seg).map_err(e)?; } - if let Some(x) = xmp { - // Its `tiff:` resolution, if any, says what the JFIF density says (#1691). - let x = crate::resolution::export_xmp(x, ppi); - let mut seg = b"http://ns.adobe.com/xap/1.0/\0".to_vec(); - seg.extend_from_slice(x.as_bytes()); + // Its `tiff:` resolution, if any, says what the JFIF density says. A packet past one segment + // loses its layered-document properties, else is left out, as the other JPEG paths do; see + // [`jpeg_xmp_fits`]. + if let Some(seg) = xmp.and_then(|x| crate::codecs::jpeg::xmp_segment(x, ppi)) { enc.add_app_segment(1, &seg).map_err(e)?; } if let Some(icc) = icc { @@ -345,6 +345,12 @@ pub fn jpeg_exif_fits(exif: &[u8], dpi: Option) -> bool { crate::codecs::jpeg::exif_segment(exif, dpi.filter(|d| *d >= 1.0).map(|d| (d, d))).is_some() } +/// Whether [`encode_jpeg_rgb8`] writes `xmp` at `dpi`: it must fit in one APP1 segment, if need +/// be without its layered-document properties. +pub fn jpeg_xmp_fits(xmp: &str, dpi: Option) -> bool { + crate::codecs::jpeg::xmp_segment(xmp, dpi.filter(|d| *d >= 1.0).map(|d| (d, d))).is_some() +} + #[cfg(test)] mod tests { use super::*; @@ -355,6 +361,27 @@ mod tests { (w, h, img.convert(crate::ChannelLayout::Rgba, crate::SampleType::U8).data().to_vec()) } + /// XMP past one APP1 segment loses its layered-document properties, and a packet still too + /// large is left out; the JPEG is written either way. + #[test] + fn web_jpeg_oversized_xmp_is_trimmed_or_left_out() { + let rgb = vec![100u8; 8 * 8 * 3]; + let ancestors = format!( + "{}", + "id".repeat(5_000) + ); + assert!(ancestors.len() > 70_000); + assert!(jpeg_xmp_fits(&ancestors, Some(72.0)), "trimmed, it fits"); + let out = encode_jpeg_rgb8(8, 8, &rgb, 80, false, false, None, Some(72.0), None, Some(&ancestors)).unwrap(); + let xmp = crate::decode(&out).unwrap().meta.xmp.unwrap_or_default(); + assert!(xmp.contains("dc:title=\"kept\"") && !xmp.contains("DocumentAncestors"), "{xmp}"); + let big = format!("", "x".repeat(70_000)); + assert!(!jpeg_xmp_fits(&big, Some(72.0))); + let out = encode_jpeg_rgb8(8, 8, &rgb, 80, false, false, None, Some(72.0), None, Some(&big)).unwrap(); + let img = crate::decode(&out).unwrap(); + assert_eq!((img.dimensions(), img.meta.xmp), ((8, 8), None), "written without the XMP"); + } + fn pattern(w: usize, h: usize, n: usize) -> Vec { (0..w * h).map(|i| (((i % w) * 7 + (i / w) * 13 + (i * i) % 5) % n) as u8).collect() } diff --git a/crates/kra/README.md b/crates/kra/README.md index 363455bdf..582a0117d 100644 --- a/crates/kra/README.md +++ b/crates/kra/README.md @@ -31,6 +31,7 @@ here. There is no writer. | `` nodes | 8192 (`MAX_NODES`) | | `maindoc.xml` | 32 MiB, 512 levels of nesting, no internal DTD subset | | A pixel file, inflated | 1 GiB (`MAX_LAYER_FILE`) | +| All entries read from one archive | 4 GiB (`MAX_TOTAL`) | | ICC profile | 16 MiB | | Tile | 1024 × 1024 px, 64 bytes per pixel, header lines of 256 bytes | diff --git a/crates/kra/src/lib.rs b/crates/kra/src/lib.rs index f59bd95cd..e2dfb1f64 100644 --- a/crates/kra/src/lib.rs +++ b/crates/kra/src/lib.rs @@ -56,6 +56,10 @@ pub const MAX_MAINDOC: usize = 32 << 20; pub const MAX_LAYER_FILE: usize = 1 << 30; /// Most entries the archive may declare; checked before the archive is indexed. pub const MAX_ENTRIES: u64 = 100_000; +/// Most bytes all the entries read from one archive may expand to together. Each pixel file may be +/// [`MAX_LAYER_FILE`] on its own, so without a total a document of many layers could spend far more +/// than one open should. +pub const MAX_TOTAL: u64 = 4 << 30; /// Largest ICC profile. pub const MAX_ICC: usize = 16 << 20; /// Deepest element nesting of `maindoc.xml`. @@ -287,8 +291,13 @@ pub struct Archive<'a> { impl<'a> Archive<'a> { /// Opens the archive, checks its `mimetype` and reads `maindoc.xml`. pub fn open(bytes: &'a [u8]) -> Result> { - let mut zip = - craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: MAX_ENTRIES, max_total_bytes: u64::MAX }).map_err(zip_error)?; + Self::open_with(bytes, craft_zip::Limits { max_entries: MAX_ENTRIES, max_total_bytes: MAX_TOTAL }) + } + + /// [`Archive::open`] with explicit zip limits, so a test can set a small document-wide total + /// ([`MAX_TOTAL`] is what [`Archive::open`] passes). + pub(crate) fn open_with(bytes: &'a [u8], limits: craft_zip::Limits) -> Result> { + let mut zip = craft_zip::Archive::open(bytes, limits).map_err(zip_error)?; // `zip` keeps one entry of a repeated name; which one the writer meant is not known. if zip.has_duplicate_names() { return Err(invalid("two entries of the archive share a name")); diff --git a/crates/kra/src/tests.rs b/crates/kra/src/tests.rs index 31656e4e1..b6dfd4b72 100644 --- a/crates/kra/src/tests.rs +++ b/crates/kra/src/tests.rs @@ -386,6 +386,23 @@ fn an_archive_declaring_too_many_entries_is_refused_before_it_is_indexed() { ); } +/// Every entry read is charged to one document-wide total: each pixel file may be +/// [`MAX_LAYER_FILE`] on its own, but a document with many of them still has one budget. +#[test] +fn pixel_files_share_one_document_wide_budget() { + let doc = maindoc("RGBA", ""); + // `layer_file("x")` reads `/layers/x`. + let (first, second) = ("img/layers/x", "img/layers/y"); + let files: Vec<(&str, Vec)> = vec![(first, vec![3; 64]), (second, vec![4; 64])]; + let bytes = archive(&doc, &files); + // The mimetype and maindoc reads are charged too: room for one 64-byte pixel file and 16 bytes. + let total = doc.len() as u64 + MIMETYPE.len() as u64 + 64 + 16; + let limits = craft_zip::Limits { max_entries: MAX_ENTRIES, max_total_bytes: total }; + let mut archive = Archive::open_with(&bytes, limits).unwrap(); + assert_eq!(archive.layer_file("x").unwrap().unwrap(), vec![3; 64]); + assert!(matches!(archive.layer_file("y"), Err(Error::Invalid(_))), "the second pixel file is past the total"); +} + /// Two entries of one name: `zip` keeps one, and which one the writer meant is not known. #[test] fn two_entries_of_one_name_are_refused() { diff --git a/crates/media/README.md b/crates/media/README.md index 8eca6ecb5..b7ad5be7a 100644 --- a/crates/media/README.md +++ b/crates/media/README.md @@ -243,6 +243,10 @@ let bytes = w.finish_faststart()?.into_inner(); // or finish() for moov-at-end `ipcm`/`fpcm` + `pcmC` in MP4. A PCM `write_sample` call can hold any whole number of frames, and the track timescale must equal the sample rate. - Edit lists: set `TrackConfig::edits` explicitly, or use `media_start` to get a single edit. +- Audio pre-roll: `TrackConfig::roll_distance` puts every sample in a `roll` sample group (`sgpd` + + `sbgp`). Set -1 for AAC with `media_start` at the priming: Apple's decoders apply that edit only + when the track has the group. +- A file starts at the writer's position: chunk offsets count from there. - Timecode: `add_timecode_track(video, TimecodeConfig, start_frame)` adds a `tmcd` track (with `gmhd`) and a `tref/tmcd` link from the video track. - `WriterOptions::metadata` writes `©xxx` text atoms to `udta`. diff --git a/crates/media/src/isobmff/mux.rs b/crates/media/src/isobmff/mux.rs index e38186e9f..bcadfbaeb 100644 --- a/crates/media/src/isobmff/mux.rs +++ b/crates/media/src/isobmff/mux.rs @@ -51,11 +51,17 @@ pub struct TrackConfig { pub media_start: Option, /// `hdlr` name; a sensible default is used when `None`. pub handler_name: Option, + /// Audio pre-roll: puts every sample in a `roll` sample group (`sgpd` + `sbgp`, ISO/IEC + /// 14496-12 §10.1) with this roll distance, the number of samples a decoder must decode before + /// (negative) a sample for its output to be correct; -1 for AAC. Apple's decoders take an AAC + /// track's priming from the edit list only when the track has this group. Written by + /// [`Mp4Writer`]; [`FragmentedWriter`] ignores it. + pub roll_distance: Option, } impl TrackConfig { pub fn new(entry: SampleEntry, timescale: u32) -> Self { - TrackConfig { entry, timescale, language: "und".into(), edits: Vec::new(), media_start: None, handler_name: None } + TrackConfig { entry, timescale, language: "und".into(), edits: Vec::new(), media_start: None, handler_name: None, roll_distance: None } } } @@ -543,6 +549,23 @@ fn write_stbl(b: &mut BoxBuf, t: &WTrack, qt: bool, shift: u64, force_co64: bool } } b.end(m); + if let Some(distance) = t.cfg.roll_distance + && sample_count > 0 + { + // One description, which every sample maps to. + let m = b.start_full(b"sgpd", 1, 0); + b.bytes(b"roll"); + b.u32(2); + b.u32(1); + b.i16(distance); + b.end(m); + let m = b.start_full(b"sbgp", 0, 0); + b.bytes(b"roll"); + b.u32(1); + b.u32(sample_count); + b.u32(1); + b.end(m); + } b.end(stbl); Ok(()) } @@ -592,12 +615,21 @@ fn validate_track(entry: &SampleEntry, timescale: u32) -> Result> { Ok(None) } +/// The position after `bytes` more. A writer that takes bytes past the last position a file can +/// have is an error here, not an overflow. +fn advance(pos: u64, bytes: usize) -> Result { + pos.checked_add(bytes as u64).ok_or_else(|| Error::Mux("the file would end past the last position".into())) +} + /// Progressive MP4/MOV writer: `ftyp`, then one `mdat` streamed to `W`, then `moov` at [`finish`](Self::finish) /// (or moved in front of `mdat` by [`finish_faststart`](Self::finish_faststart)). pub struct Mp4Writer { w: W, opts: WriterOptions, tracks: Vec, + /// The writer's position when the file was begun: chunk offsets count from here, the first + /// byte of the file, wherever in the writer that is. + start: u64, /// Offset of the placeholder box (8 bytes) preceding the mdat header. pad_pos: u64, mdat_pos: u64, @@ -608,18 +640,22 @@ pub struct Mp4Writer { } impl Mp4Writer { - /// Start a file: writes `ftyp`, a placeholder box and the `mdat` header. + /// Start a file at the writer's current position: writes `ftyp`, a placeholder box and the + /// `mdat` header. pub fn new(mut w: W, opts: WriterOptions) -> Result { let start = w.stream_position()?; let mut b = BoxBuf::new(); write_ftyp(&mut b, opts.brand); - let pad_pos = start + b.len() as u64; + let pad = b.len() as u64; b.leaf(if opts.brand == Brand::Mov { b"wide" } else { b"free" }, &[]); - let mdat_pos = start + b.len() as u64; + let mdat = b.len() as u64; b.leaf(b"mdat", &[]); + // A writer positioned at the very end of what a position can count has no room for the + // header; the positions below are sums that would overflow. + let pos = start.checked_add(b.len() as u64).ok_or_else(|| Error::Mux("the writer's position leaves no room for a file".into()))?; + let (pad_pos, mdat_pos) = (start + pad, start + mdat); w.write_all(&b.buf)?; - let pos = start + b.len() as u64; - Ok(Mp4Writer { w, opts, tracks: Vec::new(), pad_pos, mdat_pos, pos, last_track: None, chapters: Vec::new() }) + Ok(Mp4Writer { w, opts, tracks: Vec::new(), start, pad_pos, mdat_pos, pos, last_track: None, chapters: Vec::new() }) } /// Add a track; returns its index (track ids are index + 1). @@ -708,8 +744,8 @@ impl Mp4Writer { return Err(too_many_samples()); } self.w.write_all(s.data)?; - let offset = self.pos; - self.pos += s.data.len() as u64; + let offset = self.pos - self.start; + self.pos = advance(self.pos, s.data.len())?; let contiguous = self.last_track == Some(track); self.last_track = Some(track); let tr = &mut self.tracks[track]; @@ -749,8 +785,8 @@ impl Mp4Writer { }; let d = rescale(vd, vts, tts).max(tc_dur as u64).min(u32::MAX as u64) as u32; self.w.write_all(&start.to_be_bytes())?; - let off = self.pos; - self.pos += 4; + let off = self.pos - self.start; + self.pos = advance(self.pos, 4)?; self.last_track = None; let t = &mut self.tracks[i].t; t.stts.push((1, d)); @@ -807,13 +843,13 @@ impl Mp4Writer { } let id = self.tracks.len() as u32 + 1; let mut t = Tables::new(); - let off = self.pos; + let off = self.pos - self.start; for (i, c) in self.chapters.iter().enumerate() { let end = self.chapters.get(i + 1).map_or(duration, |n| n.start); let d = u32::try_from(end.saturating_sub(c.start)).map_err(|_| Error::Mux("chapter longer than 2^32 movie ticks".into()))?; let data = text_sample(&c.title); self.w.write_all(&data)?; - self.pos += data.len() as u64; + self.pos = advance(self.pos, data.len())?; Tables::push_run(&mut t.stts, 1, d); Tables::push_run(&mut t.ctts, 1, 0); t.sizes.push(data.len() as u32); @@ -1341,4 +1377,105 @@ mod tests { } assert_eq!(t.sync_sample_before(5), 3); } + + /// `roll_distance` puts every sample in one `roll` group; without it, or without samples, + /// the track has no sample group. + #[test] + fn roll_distance_is_a_sample_group_over_every_sample() { + let file = |roll: Option, samples: u32| { + let mut w = Mp4Writer::new(Cursor::new(Vec::new()), WriterOptions::new(Brand::Mp4)).unwrap(); + let mut cfg = TrackConfig::new(SampleEntry::aac(vec![0x11, 0x90], 2, 48_000), 48_000); + cfg.roll_distance = roll; + let t = w.add_track(cfg).unwrap(); + for _ in 0..samples { + w.write_sample(t, WriteSample { data: &[0; 8], duration: 1024, composition_offset: 0, is_sync: true }).unwrap(); + } + w.finish().unwrap().into_inner() + }; + let out = file(Some(-1), 5); + let f = crate::isobmff::open(out.as_slice()).unwrap(); + let groups = &f.tracks[0].sample_groups; + assert_eq!(groups.len(), 1); + assert_eq!((groups[0].grouping_type, groups[0].runs.as_slice()), (crate::isobmff::FourCc(*b"roll"), [(5, 1)].as_slice())); + assert_eq!(groups[0].descriptions, [(-1i16).to_be_bytes().to_vec()]); + assert_eq!(f.tracks[0].samples.len(), 5); + for out in [file(None, 5), file(Some(-1), 0)] { + assert!(crate::isobmff::open(out.as_slice()).unwrap().tracks[0].sample_groups.is_empty()); + } + // The boxes as ISO/IEC 14496-12 §8.9 has them (and ffmpeg writes them): `sgpd` version 1 + // with a default description length of 2, one description; `sbgp` version 0, one run. + let sgpd = [&26u32.to_be_bytes()[..], b"sgpd", &[1, 0, 0, 0], b"roll", &[0, 0, 0, 2, 0, 0, 0, 1, 0xFF, 0xFF]].concat(); + let sbgp = [&28u32.to_be_bytes()[..], b"sbgp", &[0, 0, 0, 0], b"roll", &[0, 0, 0, 1, 0, 0, 0, 5, 0, 0, 0, 1]].concat(); + let both = [sgpd, sbgp].concat(); + assert!(out.windows(both.len()).any(|w| w == both)); + } + + /// A writer positioned where the header's end would pass `u64::MAX` is refused, not overflowed. + #[test] + fn a_writer_at_the_end_of_the_position_range_is_refused() { + for back in [0, 1, 10, 27] { + let mut bytes = [0u8; 8]; + let mut sink = Cursor::new(&mut bytes[..]); + sink.set_position(u64::MAX - back); + assert!(matches!(Mp4Writer::new(sink, WriterOptions::new(Brand::Mp4)), Err(Error::Mux(_))), "{back} before the end"); + } + } + + /// A writer that keeps taking bytes where positions end makes writing fail, not overflow. + #[test] + fn samples_past_the_last_position_are_refused() { + struct Endless(u64); + impl Write for Endless { + fn write(&mut self, data: &[u8]) -> std::io::Result { + self.0 = self.0.wrapping_add(data.len() as u64); + Ok(data.len()) + } + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } + } + impl Seek for Endless { + fn seek(&mut self, _: SeekFrom) -> std::io::Result { + Ok(self.0) + } + } + let mut w = Mp4Writer::new(Endless(u64::MAX - 100), WriterOptions::new(Brand::Mp4)).unwrap(); + let v = w.add_track(TrackConfig::new(avc_entry(), 1000)).unwrap(); + let sample = WriteSample { data: &[0; 40], duration: 40, composition_offset: 0, is_sync: true }; + w.write_sample(v, sample).unwrap(); + assert!(matches!(w.write_sample(v, sample), Err(Error::Mux(_)))); + } + + /// A file begun in the middle of a writer is a file from there: its chunk offsets count from + /// its own first byte, with `moov` at the end or in front, a timecode track and chapters. + #[test] + fn a_file_begun_mid_writer_has_offsets_from_its_start() { + let file = |prefix: usize, faststart: bool| { + let mut sink = Cursor::new(vec![0xAA; prefix]); + sink.set_position(prefix as u64); + let mut opts = WriterOptions::new(Brand::Mov); + opts.chapters = vec![Chapter { start: 0, title: "one".into() }]; + let mut w = Mp4Writer::new(sink, opts).unwrap(); + let v = w.add_track(TrackConfig::new(avc_entry(), 1000)).unwrap(); + let tc = TimecodeConfig { timescale: 1000, frame_duration: 40, frames_per_second: 25, ..Default::default() }; + w.add_timecode_track(v, tc, 7).unwrap(); + for i in 0..4u8 { + w.write_sample(v, WriteSample { data: &[i + 1; 10], duration: 40, composition_offset: 0, is_sync: true }).unwrap(); + } + let out = if faststart { w.finish_faststart() } else { w.finish() }.unwrap(); + assert_eq!(out.position(), out.get_ref().len() as u64); + out.into_inner() + }; + for faststart in [false, true] { + let (plain, offset) = (file(0, faststart), file(100, faststart)); + assert!(offset[..100].iter().all(|&b| b == 0xAA)); + assert!(offset[100..] == plain, "faststart {faststart}"); + let f = crate::isobmff::open(plain.as_slice()).unwrap(); + assert_eq!(f.tracks.len(), 3); + for i in 0..4 { + assert_eq!(f.read_sample(plain.as_slice(), 0, i).unwrap(), vec![i as u8 + 1; 10]); + } + assert_eq!(f.read_sample(plain.as_slice(), 1, 0).unwrap(), 7u32.to_be_bytes()); + } + } } diff --git a/crates/ora/README.md b/crates/ora/README.md index 70cdbd932..488268b3c 100644 --- a/crates/ora/README.md +++ b/crates/ora/README.md @@ -29,6 +29,7 @@ means. `SVG_COMPOSITE_OPS` lists the names the specification defines. | Elements under the root stack | 8192 (`MAX_NODES`) | | `stack.xml` | 16 MiB, 512 levels of nesting, no document type declaration | | An entry | the caller's limit; `MAX_ENTRY` (512 MiB) is the suggested one for a layer image | +| All entries read from one archive | 4 GiB (`MAX_TOTAL`) | ## ZIP diff --git a/crates/ora/src/lib.rs b/crates/ora/src/lib.rs index 69d494d0d..f39d13f06 100644 --- a/crates/ora/src/lib.rs +++ b/crates/ora/src/lib.rs @@ -51,6 +51,10 @@ pub const MAX_STACK_XML: usize = 16 << 20; pub const MAX_ENTRY: usize = 512 << 20; /// Most entries the archive may declare; checked before the archive is indexed. pub const MAX_ENTRIES: u64 = 100_000; +/// Most bytes all the entries read from one archive may expand to together. Each layer image may +/// be [`MAX_ENTRY`] on its own, so without a total a document of many layers could spend far more +/// than one open should. +pub const MAX_TOTAL: u64 = 4 << 30; /// Deepest element nesting of `stack.xml`. pub const MAX_XML_DEPTH: usize = 512; @@ -363,8 +367,13 @@ pub struct Archive<'a> { impl<'a> Archive<'a> { /// Opens the archive, checks its `mimetype` and reads `stack.xml`. pub fn open(bytes: &'a [u8]) -> Result> { - let mut zip = - craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: MAX_ENTRIES, max_total_bytes: u64::MAX }).map_err(zip_error)?; + Self::open_with_total(bytes, MAX_TOTAL) + } + + /// [`Archive::open`], allowing the entries read from the archive to expand to + /// `max_total_bytes` together ([`MAX_TOTAL`] is the default). + pub(crate) fn open_with_total(bytes: &'a [u8], max_total_bytes: u64) -> Result> { + let mut zip = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: MAX_ENTRIES, max_total_bytes }).map_err(zip_error)?; // `zip` keeps one entry of a repeated name; which one the writer meant is not known. if zip.has_duplicate_names() { return Err(invalid("two entries of the archive share a name")); diff --git a/crates/ora/src/tests.rs b/crates/ora/src/tests.rs index cfaac7c41..f8773f79c 100644 --- a/crates/ora/src/tests.rs +++ b/crates/ora/src/tests.rs @@ -315,6 +315,19 @@ fn an_archive_declaring_too_many_entries_is_refused_before_it_is_indexed() { ); } +/// Every entry read is charged to one document-wide total, so an archive of many individually +/// allowed entries cannot spend an unbounded amount of memory in one open. +#[test] +fn entries_are_charged_to_one_document_wide_total() { + let stack = ""; + let bytes = archive(stack, &[("a.png", vec![1; 100]), ("b.png", vec![2; 100])]); + // Room for `stack.xml` and one image, not two. + let mut a = Archive::open_with_total(&bytes, stack.len() as u64 + 120).unwrap(); + assert_eq!(a.entry("a.png", MAX_ENTRY).unwrap().unwrap(), vec![1; 100]); + let err = a.entry("b.png", MAX_ENTRY).unwrap_err(); + assert!(matches!(&err, Error::Invalid(m) if m.contains("b.png") && m.contains("may expand to")), "{err:?}"); +} + /// Two entries of one name: `zip` keeps one, and which one the writer meant is not known. #[test] fn two_entries_of_one_name_are_refused() { diff --git a/crates/pdf/Cargo.toml b/crates/pdf/Cargo.toml index ff94c6d8e..266297f42 100644 --- a/crates/pdf/Cargo.toml +++ b/crates/pdf/Cargo.toml @@ -17,6 +17,7 @@ interpret = ["read", "dep:hayro-interpret"] paths = ["interpret", "dep:kurbo"] raster = ["interpret", "dep:hayro", "dep:bytemuck"] write = ["dep:krilla", "dep:thiserror"] +write-svg = ["write", "dep:craft-svg", "dep:krilla-svg", "dep:usvg", "craft-svg/tree-text"] write-pdf = ["write", "krilla/pdf"] write-raster = ["dep:flate2", "dep:roxmltree"] write-simple-text = ["write", "krilla/simple-text"] @@ -30,6 +31,9 @@ hayro-interpret = { workspace = true, optional = true } hayro = { workspace = true, optional = true } bytemuck = { workspace = true, optional = true } krilla = { workspace = true, optional = true } +craft-svg = { workspace = true, optional = true } +krilla-svg = { version = "=0.8.1", optional = true } +usvg = { version = "=0.47.0", features = ["text"], optional = true } thiserror = { workspace = true, optional = true } flate2 = { version = "1.1", default-features = false, features = ["zlib-rs"], optional = true } roxmltree = { version = "0.21", optional = true } diff --git a/crates/pdf/README.md b/crates/pdf/README.md index 18d3d25a9..210b79849 100644 --- a/crates/pdf/README.md +++ b/crates/pdf/README.md @@ -10,6 +10,7 @@ No feature is enabled by default. | `interpret` | `read` plus an app-owned Device interpreter, fonts and CMaps | | `raster` | `interpret` plus premultiplied RGBA8 rendering | | `write` | Guarded Krilla authoring; no shared reader dependency | +| `write-svg` | `write` plus bounded SVG-to-PDF vector drawing | | `write-pdf` | `write` plus Krilla's embedded-PDF support | | `write-simple-text` | `write` plus Krilla's optional text shaper and `draw_text` | | `filters` | The `/Filter` chain for editing and writing PDF files: decode and encode, with limits and accounting. No reader or writer dependency | @@ -201,9 +202,12 @@ PDF files. The reader decrypts on its own and does not use it. It covers revisio block is ignored and bad padding is left in. - `permissions()` gives the `/P` flags; for R5/R6 a permission counts only when `/Perms` grants it too. Owners have every permission. -- `create(&NewEncryption, id0)` builds the `/Encrypt` values for RC4-40, RC4-128, AES-128 or - AES-256. The caller supplies 32 bytes of entropy (`seed`), so the module needs no OS access - and builds for the web. `file_id` derives a file identifier from the same seed. +- `create(&NewEncryption, id0)` builds the `/Encrypt` values for AES-128 or AES-256. It refuses + the legacy RC4-40 and RC4-128 schemes, because RC4 is broken (RFC 7465); `create_legacy` + writes those, for a file that must stay readable by an old reader. `Document::set_encryption` + refuses them too, with `set_encryption_legacy` as the explicit way in. The caller supplies 32 + bytes of entropy (`seed`), so the module needs no OS access and builds for the web. `file_id` + derives a file identifier from the same seed. - `EncryptDict` holds the dictionary's values. The caller reads them from, and writes them to, its own object model; the module does not parse PDF syntax. Keys and prepared passwords are zeroized when dropped. @@ -240,6 +244,9 @@ serialization uses the same backend version as before extraction. It provides: refuses it. Apps build URI links with it instead of `backend::action::LinkAction`, so a link target from someone else's document becomes a PDF link only when it is an `http`/`https` URL with a host or a `mailto` address. A refused target writes no link annotation. +- `write::svg` (feature `write-svg`): bounded SVG/SVGZ text preflight, data-only image + resolution, and vector drawing through krilla-svg. Callers provide font and fallback options; + the adapter enforces input, nesting, embedded-image, destination-area and filter budgets. - `safe_url` (crate root, any feature): the same rule, for apps that open links themselves, such as a slide show. diff --git a/crates/pdf/examples/cos_protect.rs b/crates/pdf/examples/cos_protect.rs index c40bccdce..b12c700cf 100644 --- a/crates/pdf/examples/cos_protect.rs +++ b/crates/pdf/examples/cos_protect.rs @@ -22,7 +22,8 @@ fn main() { _ => craft_pdf::cos::Algorithm::Aes256, }; let mut doc = craft_pdf::cos::Document::open(std::sync::Arc::new(std::fs::read(input).unwrap())).unwrap(); - doc.set_encryption(&craft_pdf::cos::NewEncryption { + // `rc4-40` and `rc4-128` are the legacy schemes, which `set_encryption` refuses. + doc.set_encryption_legacy(&craft_pdf::cos::NewEncryption { algorithm, user_password: user, owner_password: owner, diff --git a/crates/pdf/src/cos/document/open.rs b/crates/pdf/src/cos/document/open.rs index 46a6423a9..4991412ce 100644 --- a/crates/pdf/src/cos/document/open.rs +++ b/crates/pdf/src/cos/document/open.rs @@ -414,7 +414,20 @@ impl Document { /// Protect the document with a password (§7.6.4). Takes effect on the next save, which is /// always a full rewrite. Returns the handler (authenticated as owner). + /// + /// The legacy RC4 schemes are refused (RC4 is broken, `RFC 7465`): [`Self::set_encryption_legacy`] + /// writes them for a file an old reader must open. pub fn set_encryption(&mut self, params: &crate::crypt::NewEncryption) -> Result<(), CosError> { + self.set_encryption_with(params, false) + } + + /// [`Self::set_encryption`] with the legacy RC4-40 and RC4-128 schemes allowed. Pass it only + /// for a file that must stay readable by a reader without AES. + pub fn set_encryption_legacy(&mut self, params: &crate::crypt::NewEncryption) -> Result<(), CosError> { + self.set_encryption_with(params, true) + } + + fn set_encryption_with(&mut self, params: &crate::crypt::NewEncryption, legacy: bool) -> Result<(), CosError> { // The file identifier is part of the key; make sure it exists and keep it. let id = self.trailer.get(b"ID").map(|id| self.resolve(id)); let id0 = match id.as_deref() { @@ -426,7 +439,8 @@ impl Document { id } }; - let h = crate::crypt::create(params, &id0).map_err(|e| CosError::Security(e.to_string()))?; + let built = if legacy { crate::crypt::create_legacy(params, &id0) } else { crate::crypt::create(params, &id0) }; + let h = built.map_err(|e| CosError::Security(e.to_string()))?; let d = h.dict(); let s = |b: &[u8]| Object::String(crate::cos::PdfString { bytes: b.to_vec(), hex: true }); let mut e = Dict::new(); diff --git a/crates/pdf/src/crypt.rs b/crates/pdf/src/crypt.rs index 069627db9..c516fa86e 100644 --- a/crates/pdf/src/crypt.rs +++ b/crates/pdf/src/crypt.rs @@ -283,7 +283,7 @@ impl SecurityHandler { /// different key is refused too. pub fn authenticate_owner(&self, id0: &[u8], password: &str) -> Result { let owner = Self::open(self.dict.clone(), id0, Some(password))?; - if owner.auth != Auth::Owner || owner.key != self.key { + if owner.auth != Auth::Owner || !same(owner.key.as_slice(), self.key.as_slice()) { return Err(CryptError::WrongPassword); } Ok(owner) @@ -539,6 +539,12 @@ fn u_value_r4(d: &EncryptDict, id0: &[u8], key: &[u8]) -> Vec { x } +/// Whether `a` and `b` are equal, in time that depends only on their lengths: password-derived +/// digests, keys and verifiers are compared without stopping at the first differing byte. +fn same(a: &[u8], b: &[u8]) -> bool { + a.len() == b.len() && a.iter().zip(b).fold(0u8, |acc, (x, y)| acc | (x ^ y)) == 0 +} + /// Algorithm 6: authenticate a user password; returns the file key. /// /// Some producers ignore `/EncryptMetadata false` when deriving the key (they omit the @@ -549,7 +555,7 @@ fn user_key_r4(d: &EncryptDict, id0: &[u8], pw: &[u8]) -> Option= n && u[..n] == d.u[..n]).then_some(key) + (d.u.len() >= n && same(&u[..n], &d.u[..n])).then_some(key) }; check(d).or_else(|| (d.r >= 4 && !d.encrypt_metadata).then(|| check(&EncryptDict { encrypt_metadata: true, ..d.clone() })).flatten()) } @@ -636,7 +642,8 @@ fn key_r6(d: &EncryptDict, pw: &[u8], owner: bool) -> Result Result bool { + matches!(self, Algorithm::Rc4_40 | Algorithm::Rc4_128) + } +} + /// A password as [`create`] prepares it: for R2–R4 its PDFDocEncoding (characters it lacks /// dropped), at most 32 bytes; for R6 its SASLprep (RFC 4013: soft hyphens and the like removed, /// other spaces made plain, then NFKC; the password as given if SASLprep refuses it), in UTF-8, @@ -725,7 +740,23 @@ pub fn file_id(seed: &[u8; 32]) -> Vec { } /// Build the `/Encrypt` values and an authenticated (owner) handler for a new encryption. +/// +/// The legacy RC4 schemes are refused, because RC4 is broken (`RFC 7465`) and a file written with +/// it protects nothing: [`create_legacy`] is the explicit way to write them. pub fn create(params: &NewEncryption, id0: &[u8]) -> Result { + if params.algorithm.is_legacy() { + return Err(CryptError::Unsupported("RC4 encryption is legacy (RFC 7465); use create_legacy for a file an old reader must open".into())); + } + create_any(params, id0) +} + +/// [`create`] without the RC4 gate: writes RC4-40 and RC4-128 too. For a file that must stay +/// readable by a reader without AES, and for tests that read and write every algorithm. +pub fn create_legacy(params: &NewEncryption, id0: &[u8]) -> Result { + create_any(params, id0) +} + +fn create_any(params: &NewEncryption, id0: &[u8]) -> Result { // R2 reserves bits 7-32; later revisions reserve bits 7-8 and 13-32. // Reserved bits must be 1, and bits 1-2 must be 0 (§7.6.4.2). let reserved = if params.algorithm == Algorithm::Rc4_40 { !0x3F } else { !0xFFF | 0xC0 }; diff --git a/crates/pdf/src/crypt/tests.rs b/crates/pdf/src/crypt/tests.rs index 04bb7c61e..08878c8be 100644 --- a/crates/pdf/src/crypt/tests.rs +++ b/crates/pdf/src/crypt/tests.rs @@ -52,13 +52,45 @@ fn aes_padding_is_added_and_tolerantly_removed() { const ID: &[u8] = b"0123456789abcdef"; fn new(alg: Algorithm, user: &str, owner: &str, perms: i32) -> SecurityHandler { - create( + // These tests read and write every algorithm, RC4 included. + create_legacy( &NewEncryption { algorithm: alg, user_password: user, owner_password: owner, permissions: perms, encrypt_metadata: true, seed: [42; 32] }, ID, ) .unwrap() } +/// New files are not written with RC4: `create` refuses it, and the explicitly named +/// `create_legacy` is the only way to write it. +#[test] +fn create_refuses_rc4_unless_asked() { + let params = |algorithm| NewEncryption { + algorithm, + user_password: "user", + owner_password: "owner", + permissions: -1, + encrypt_metadata: true, + seed: [42; 32], + }; + for (alg, revision) in [(Algorithm::Rc4_40, 2), (Algorithm::Rc4_128, 3)] { + assert!(matches!(create(¶ms(alg), ID), Err(CryptError::Unsupported(_))), "{alg:?}"); + assert_eq!(create_legacy(¶ms(alg), ID).unwrap().dict().r, revision, "{alg:?}"); + } + for alg in [Algorithm::Aes128, Algorithm::Aes256] { + assert!(create(¶ms(alg), ID).is_ok(), "AES is not legacy: {alg:?}"); + } +} + +/// Every verifier check goes through this comparison: it reads both sides in full and a value +/// that merely starts the same never matches. +#[test] +fn verifier_comparison_reads_every_byte() { + assert!(super::same(b"", b"")); + assert!(super::same(b"0123456789abcdef", b"0123456789abcdef")); + assert!(!super::same(b"0123456789abcdef", b"0123456789abcdee"), "difference in the last byte"); + assert!(!super::same(b"0123456789abcdef", b"0123456789abcde"), "shorter value"); +} + const ALL: [Algorithm; 4] = [Algorithm::Rc4_40, Algorithm::Rc4_128, Algorithm::Aes128, Algorithm::Aes256]; #[test] diff --git a/crates/pdf/src/write.rs b/crates/pdf/src/write.rs index 8114327d4..924910e25 100644 --- a/crates/pdf/src/write.rs +++ b/crates/pdf/src/write.rs @@ -15,6 +15,8 @@ const _: bool = backend::CRAFT_PRINT_COLOR; const _: bool = backend::CRAFT_OPTIONAL_CONTENT; pub mod print; +#[cfg(feature = "write-svg")] +pub mod svg; use backend::configure::{Archival, Configuration, ConfigurationBuilder, ConfigurationError, PdfVersion}; use backend::text::{Font, Tag}; diff --git a/crates/pdf/src/write/svg.rs b/crates/pdf/src/write/svg.rs new file mode 100644 index 000000000..eb6542c0a --- /dev/null +++ b/crates/pdf/src/write/svg.rs @@ -0,0 +1,176 @@ +//! Bounded SVG parsing and vector drawing on a PDF surface. +//! +//! This adapter owns the usvg version required by krilla-svg. Callers pass their font and +//! fallback options, while this module checks input, embedded SVGs, and output/filter budgets +//! before converting SVG to PDF drawing operations. + +use std::sync::Arc; +use std::sync::atomic::{AtomicUsize, Ordering}; + +use krilla_svg::SurfaceExt; + +pub use krilla_svg::SvgSettings; +pub use usvg; + +/// Resource limits for parsing SVG and drawing it to PDF. +#[derive(Clone, Copy, Debug)] +pub struct Limits { + /// Maximum decoded bytes in one SVG document. + pub input_bytes: usize, + /// Maximum SVG element and reference depth. + pub depth: usize, + /// Maximum XML nodes in one document. + pub xml_nodes: u32, + /// Maximum total decoded bytes of embedded SVGs. + pub embedded_svg_bytes: usize, + /// Maximum number of embedded SVG documents nested within one another. + pub embedded_svg_depth: usize, + /// Maximum encoded bytes retained for embedded raster images. + pub embedded_image_bytes: usize, + /// Maximum PDF surface area, measured in 96 dpi pixels. + pub vector_pixels: u64, + /// Maximum raster pixels used by filters after `filter_scale` is applied. + pub filter_pixels: u64, +} + +impl Default for Limits { + fn default() -> Self { + Self { + input_bytes: 16 * 1024 * 1024, + depth: 64, + xml_nodes: 100_000, + embedded_svg_bytes: 32 * 1024 * 1024, + embedded_svg_depth: 4, + embedded_image_bytes: 32 * 1024 * 1024, + vector_pixels: 256_000_000, + filter_pixels: 256_000_000, + } + } +} + +/// Why an SVG could not be parsed or drawn within its limits. +#[derive(Debug, thiserror::Error)] +pub enum Error { + /// The bounded SVG text/XML preflight refused the input. + #[error("SVG preflight failed: {0}")] + Preflight(String), + /// usvg could not construct the SVG tree. + #[error("SVG parsing failed: {0}")] + Parse(String), + /// The requested PDF area or filter raster exceeds its budget. + #[error("SVG drawing exceeds the PDF area or filter budget")] + DrawLimit, + /// krilla-svg refused to draw the parsed tree. + #[error("krilla-svg could not draw the parsed tree")] + Draw, +} + +/// An SVG tree parsed within [`Limits`], ready to draw as PDF vectors. +pub struct Document { + tree: usvg::Tree, + limits: Limits, +} + +impl Document { + /// Parse SVG/SVGZ bytes after bounded shared preflight. `options` controls the caller's font + /// database, family selection, and fallback policy; its file-reading and image resolvers are + /// replaced with a data-only resolver governed by `limits`. + pub fn parse<'a>(bytes: &[u8], limits: Limits, mut options: usvg::Options<'a>) -> Result { + let text = bounded_text(bytes, limits.input_bytes, limits).map_err(|e| Error::Preflight(e.to_string()))?; + options.image_href_resolver = image_resolver(limits); + let tree = usvg::Tree::from_str(&text, &options).map_err(|e| Error::Parse(e.to_string()))?; + Ok(Self { tree, limits }) + } + + /// Draw the parsed tree as PDF vectors. The limits account for the destination size and the + /// renderer's filter scale before krilla-svg allocates filter rasters. + pub fn draw(&self, surface: &mut krilla::surface::Surface<'_>, size: krilla::geom::Size, settings: SvgSettings) -> Result<(), Error> { + let width = f64::from(size.width()); + let height = f64::from(size.height()); + let scale = f64::from(settings.filter_scale); + if !width.is_finite() || !height.is_finite() || !scale.is_finite() || width <= 0.0 || height <= 0.0 || scale <= 0.0 { + return Err(Error::DrawLimit); + } + let area = width * height * (96.0_f64 / 72.0).powi(2); + let filter_area = area * scale * scale; + if !area.is_finite() || !filter_area.is_finite() || area > self.limits.vector_pixels as f64 || filter_area > self.limits.filter_pixels as f64 + { + return Err(Error::DrawLimit); + } + surface.draw_svg(&self.tree, size, settings).ok_or(Error::Draw) + } +} + +#[derive(Default)] +struct Usage { + svg_bytes: AtomicUsize, + svg_depth: AtomicUsize, + image_bytes: AtomicUsize, +} + +fn shared_limits(bytes: usize, limits: Limits) -> craft_svg::tree::Limits { + craft_svg::tree::Limits { + bytes, + depth: limits.depth, + tree_depth: limits.depth, + refuse_entity_markup: true, + xml_nodes: limits.xml_nodes, + embedded_bytes: limits.embedded_svg_bytes, + embedded_depth: limits.embedded_svg_depth, + ..Default::default() + } +} + +fn bounded_text(bytes: &[u8], byte_limit: usize, limits: Limits) -> Result, craft_svg::tree::Error> { + craft_svg::tree::text(bytes, &shared_limits(byte_limit, limits)) +} + +fn reserve(counter: &AtomicUsize, amount: usize, limit: usize) -> bool { + counter.try_update(Ordering::Relaxed, Ordering::Relaxed, |used| used.checked_add(amount).filter(|next| *next <= limit)).is_ok() +} + +fn embedded_svg(usage: &Usage, limits: Limits, bytes: &[u8], options: &usvg::Options<'_>) -> Option { + let used = usage.svg_bytes.load(Ordering::Relaxed); + let remaining = limits.embedded_svg_bytes.saturating_sub(used); + let text = bounded_text(bytes, remaining.min(limits.input_bytes), limits).ok()?; + if !reserve(&usage.svg_bytes, text.len(), limits.embedded_svg_bytes) { + return None; + } + let depth = usage.svg_depth.fetch_add(1, Ordering::Relaxed); + if depth >= limits.embedded_svg_depth { + usage.svg_depth.fetch_sub(1, Ordering::Relaxed); + return None; + } + let parsed = usvg::Tree::from_str(&text, options).ok(); + usage.svg_depth.fetch_sub(1, Ordering::Relaxed); + parsed.map(usvg::ImageKind::SVG) +} + +fn image_resolver(limits: Limits) -> usvg::ImageHrefResolver<'static> { + let usage = Arc::new(Usage::default()); + let default_data = usvg::ImageHrefResolver::default_data_resolver(); + usvg::ImageHrefResolver { + resolve_data: Box::new(move |mime, bytes, options| { + if mime == "image/svg+xml" || (mime == "text/plain" && (craft_svg::sniff::is_svg(&bytes) || craft_svg::sniff::is_svgz(&bytes))) { + return embedded_svg(&usage, limits, &bytes, options); + } + if !reserve(&usage.image_bytes, bytes.len(), limits.embedded_image_bytes) { + return None; + } + default_data(mime, bytes, options) + }), + resolve_string: Box::new(|_, _| None), + } +} + +#[cfg(test)] +mod tests { + use super::{Limits, shared_limits}; + + #[test] + fn shared_depth_limit_also_bounds_svg_references() { + let limits = shared_limits(1024, Limits { depth: 7, ..Limits::default() }); + assert_eq!(limits.depth, 7); + assert_eq!(limits.tree_depth, 7); + } +} diff --git a/crates/pdf/tests/cos/bounded_writer.rs b/crates/pdf/tests/cos/bounded_writer.rs index b8fe79370..5677ca542 100644 --- a/crates/pdf/tests/cos/bounded_writer.rs +++ b/crates/pdf/tests/cos/bounded_writer.rs @@ -139,7 +139,8 @@ fn full_rewrites_preserve_graph_across_multiple_object_streams_and_evictions() { } fn protect(doc: &mut Document, algorithm: Algorithm) { - doc.set_encryption(&NewEncryption { + // Every algorithm, RC4 included, has to survive a rewrite. + doc.set_encryption_legacy(&NewEncryption { algorithm, user_password: "user", owner_password: "owner", diff --git a/crates/pdf/tests/cos/encryption.rs b/crates/pdf/tests/cos/encryption.rs index 923b04e39..2ac53cb69 100644 --- a/crates/pdf/tests/cos/encryption.rs +++ b/crates/pdf/tests/cos/encryption.rs @@ -39,9 +39,11 @@ fn fixture_with(extra: &str) -> Vec { out } +/// Every algorithm, RC4-40 and RC4-128 included: those two are the legacy schemes, which +/// `set_encryption` refuses. fn protect(alg: Algorithm, user: &str, owner: &str, permissions: i32) -> Vec { let mut doc = Document::open(Arc::new(fixture())).unwrap(); - doc.set_encryption(&NewEncryption { + doc.set_encryption_legacy(&NewEncryption { algorithm: alg, user_password: user, owner_password: owner, @@ -287,7 +289,7 @@ fn signature_contents_stay_unencrypted() { c.set(b"Test".to_vec(), Object::Array(vec![Object::Ref(sig), Object::Ref(untyped), Object::Ref(note)])) }) .unwrap(); - doc.set_encryption(&NewEncryption { + doc.set_encryption_legacy(&NewEncryption { algorithm: alg, user_password: "", owner_password: "owner", @@ -423,7 +425,7 @@ fn an_earlier_revision_opens_with_the_documents_own_credentials() { // Another file, encrypted with other keys: the document's key is no use, and it says so. let other = { let mut doc = Document::open(Arc::new(fixture())).unwrap(); - doc.set_encryption(&NewEncryption { + doc.set_encryption_legacy(&NewEncryption { algorithm: alg, user_password: "user", owner_password: "owner", @@ -444,7 +446,7 @@ fn an_earlier_revision_opens_with_the_documents_own_credentials() { /// instead of as an indirect object. fn protect_inline(alg: Algorithm, user: &str, owner: &str) -> Vec { let mut doc = Document::open(Arc::new(fixture())).unwrap(); - doc.set_encryption(&NewEncryption { + doc.set_encryption_legacy(&NewEncryption { algorithm: alg, user_password: user, owner_password: owner, diff --git a/crates/pdf/tests/cos/file_identifier.rs b/crates/pdf/tests/cos/file_identifier.rs index 551db4ebd..5ca0f0f54 100644 --- a/crates/pdf/tests/cos/file_identifier.rs +++ b/crates/pdf/tests/cos/file_identifier.rs @@ -122,7 +122,8 @@ fn caller_entropy_makes_the_new_identifier_reproducible() { fn an_encrypted_rewrite_keeps_the_first_identifier_string_its_key_depends_on() { for algorithm in [Algorithm::Rc4_128, Algorithm::Aes128, Algorithm::Aes256] { let mut doc = reopen(source(true)); - doc.set_encryption(&NewEncryption { + // The loop covers RC4-128, which `set_encryption` refuses. + doc.set_encryption_legacy(&NewEncryption { algorithm, user_password: "user", owner_password: "owner", @@ -160,7 +161,7 @@ fn unusable_ids() -> Vec<(&'static str, Option)> { /// An encrypted file whose key is derived from the empty string, with its trailer `/ID` replaced by `id`. fn protected_with_id(algorithm: Algorithm, id: Option) -> Vec { let mut doc = reopen(source_with(Some("/ID [<> <>]"))); - doc.set_encryption(&NewEncryption { + doc.set_encryption_legacy(&NewEncryption { algorithm, user_password: "user", owner_password: "owner", diff --git a/crates/pdf/tests/crypt/permissions_revision.rs b/crates/pdf/tests/crypt/permissions_revision.rs index 06a5a5b98..a58868637 100644 --- a/crates/pdf/tests/crypt/permissions_revision.rs +++ b/crates/pdf/tests/crypt/permissions_revision.rs @@ -3,7 +3,7 @@ //! //! Bit n has the mask `1 << (n - 1)`. Readers use bits 3 to 6 and 9 to 12 and ignore the rest. -use craft_pdf::crypt::{Algorithm, Auth, NewEncryption, Permissions, SecurityHandler, create}; +use craft_pdf::crypt::{Algorithm, Auth, NewEncryption, Permissions, SecurityHandler, create_legacy}; const ID: &[u8] = b"0123456789abcdef"; const T: bool = true; @@ -18,7 +18,7 @@ fn cells(p: &Permissions) -> [bool; 8] { /// Creates a file with `flags` at revision `revision`, opens it with the user password, and /// returns the user's permissions. fn user_view(alg: Algorithm, revision: i64, flags: i32) -> Permissions { - let made = create( + let made = create_legacy( &NewEncryption { algorithm: alg, user_password: "user", owner_password: "owner", permissions: flags, encrypt_metadata: true, seed: [42; 32] }, ID, ) @@ -56,7 +56,7 @@ fn revision_three_rows_hold_through_a_created_and_reopened_file() { #[test] fn owner_access_gives_every_flag() { - let made = create( + let made = create_legacy( &NewEncryption { algorithm: Algorithm::Rc4_128, user_password: "user", diff --git a/crates/pdf/tests/crypt/proptests.rs b/crates/pdf/tests/crypt/proptests.rs index a81808ab9..7b9ede4d3 100644 --- a/crates/pdf/tests/crypt/proptests.rs +++ b/crates/pdf/tests/crypt/proptests.rs @@ -19,7 +19,7 @@ use craft_pdf::crypt::{ Algorithm, Auth, CryptError, Method, NewEncryption, PADDING, SecurityHandler, StreamKind, aes_cbc_decrypt, aes_cbc_encrypt, create, - effective_password, pdfdoc_byte, rc4, + create_legacy, effective_password, pdfdoc_byte, rc4, }; use proptest::prelude::*; @@ -92,8 +92,12 @@ fn expected_auth(algorithm: Algorithm, user_target: &[u8], owner_target: &[u8], } fn make(algorithm: Algorithm, user: &str, owner: &str) -> SecurityHandler { - create(&NewEncryption { algorithm, user_password: user, owner_password: owner, permissions: -1, encrypt_metadata: true, seed: [0x5e; 32] }, ID) - .unwrap() + // These properties read and write every algorithm, RC4 included. + create_legacy( + &NewEncryption { algorithm, user_password: user, owner_password: owner, permissions: -1, encrypt_metadata: true, seed: [0x5e; 32] }, + ID, + ) + .unwrap() } proptest! { @@ -457,7 +461,7 @@ proptest! { } // Revisions below 4 always encrypt metadata, whatever the caller asked for. - let legacy = create(&NewEncryption { algorithm: Algorithm::Rc4_128, user_password: "user", owner_password: "owner", permissions: -1, encrypt_metadata: false, seed: [0x5e; 32] }, ID).unwrap(); + let legacy = create_legacy(&NewEncryption { algorithm: Algorithm::Rc4_128, user_password: "user", owner_password: "owner", permissions: -1, encrypt_metadata: false, seed: [0x5e; 32] }, ID).unwrap(); prop_assert!(legacy.dict().encrypt_metadata && legacy.encrypts_metadata(), "V<4 always encrypts metadata, as create() documents"); } diff --git a/crates/pkcs7/src/pkcs12.rs b/crates/pkcs7/src/pkcs12.rs index f14b2297e..ee245f380 100644 --- a/crates/pkcs7/src/pkcs12.rs +++ b/crates/pkcs7/src/pkcs12.rs @@ -184,6 +184,12 @@ impl Cipher { } } +/// Whether `a` and `b` are equal, in time that depends only on their lengths: the MAC is derived +/// from the password, so it is compared without stopping at the first differing byte. +fn same(a: &[u8], b: &[u8]) -> bool { + a.len() == b.len() && a.iter().zip(b).fold(0u8, |acc, (x, y)| acc | (x ^ y)) == 0 +} + fn hmac(alg: DigestAlg, key: &[u8], data: &[u8]) -> Result, Error> { fn run(key: &[u8], data: &[u8]) -> Result, Error> where @@ -411,7 +417,8 @@ pub fn open(bytes: &[u8], password: &str) -> Result { let key_len = alg.digest(&[]).len(); let iterations = budget.spend(alg, iterations, key_len)?; let key = pkcs12_kdf(alg, &bmp(password), &salt, 3, iterations, key_len); - if hmac(alg, &key, &content)? != *expected { + let computed = hmac(alg, &key, &content)?; + if !same(&computed, &expected) { return Err(Error::WrongPassword); } } diff --git a/crates/prepress/src/lib.rs b/crates/prepress/src/lib.rs index 3fa4f472e..1afc7cf6f 100644 --- a/crates/prepress/src/lib.rs +++ b/crates/prepress/src/lib.rs @@ -12,6 +12,8 @@ pub mod flatten; pub mod ink; pub mod marks; pub mod output; +#[cfg(feature = "flatten")] +pub mod pdfx1a; pub mod plates; pub use cms::{CmsConversion, ProfileError}; diff --git a/crates/prepress/src/pdfx1a.rs b/crates/prepress/src/pdfx1a.rs new file mode 100644 index 000000000..92110166b --- /dev/null +++ b/crates/prepress/src/pdfx1a.rs @@ -0,0 +1,913 @@ +//! PDF/X-1a:2003: the PDF/X-4 print file flattened by craft-prepress (no transparency left; RGB +//! and ICC color converted into the output intent's CMYK; inks, overprint and live text kept), +//! written by krilla as PDF 1.4, given the PDF/X-1a identification and output intent, then +//! checked. Apps use [`flatten_x1a`] to flatten and finalize in one step, and [`check_pdfx1a`] +//! to inspect an existing file. + +use std::collections::{HashMap, HashSet}; + +use crate::flatten::pdf::ReadOptions; +use crate::flatten::{FlattenDocumentOptions, FlattenOptions, FlattenTotals, flatten_document}; +use craft_pdf::syntax::content::UntypedIter; +use craft_pdf::syntax::object::{Array, Dict, Name, Object, ObjectIdentifier, Stream}; +use craft_pdf::syntax::page::Resources; +use craft_pdf::write::backend::configure::PdfVersion; + +const MAX_DEPTH: usize = 16; + +/// `GTS_PDFXVersion` of PDF/X-1a:2003 (ISO 15930-4): the base level, PDF/X-1:2003. +const VERSION: &str = "PDF/X-1:2003"; + +/// `GTS_PDFXConformance` of PDF/X-1a:2003 (ISO 15930-4): with [`VERSION`], it names the +/// conformance level. +const CONFORMANCE: &str = "PDF/X-1a:2003"; + +/// `/RegistryName` of the ICC's registry of characterized printing conditions. An output intent +/// naming one of them needs no `/DestOutputProfile` in PDF/X. +const ICC_REGISTRY: &str = "http://www.color.org"; +const REGISTERED_CONDITIONS: &[&str] = &["FOGRA39"]; +/// Largest classic cross-reference size accepted by the public finalizer. The rewritten table +/// emits one entry per object number, so this is also an output-allocation bound. +const MAX_XREF_SIZE: usize = 1 << 20; + +/// The graphics placed on one output page, to name in errors: the page's name and the graphics' +/// names. +#[derive(Clone, Debug, Default)] +pub struct PageGraphics { + pub label: String, + pub graphics: Vec, +} + +/// The PDF/X-1a file for `pdf`, the PDF/X-4 print file: flattened at `dpi`, written at PDF 1.4, +/// identified, checked. `pages` names each output page and its graphics for errors. Fails when +/// the flattener fails or leaves an image out, when the output intent's profile can't go into a +/// PDF 1.4 file, or when the result still has transparency or color outside DeviceCMYK, +/// DeviceGray, Separation and DeviceN; other check findings become warnings. +pub fn flatten_x1a(pdf: &[u8], dpi: f32, pages: &[PageGraphics]) -> Result<(Vec, Vec), String> { + let dpi = if dpi.is_finite() { dpi.clamp(36.0, 2400.0) } else { 300.0 }; + let opts = FlattenDocumentOptions { + read: ReadOptions { raster_dpi: dpi, ..ReadOptions::default() }, + flatten: FlattenOptions { raster_dpi: dpi, ..FlattenOptions::default() }, + producer: "DesignCraft".into(), + version: PdfVersion::Pdf14, + pdfx_version: Some(VERSION.into()), + ..FlattenDocumentOptions::default() + }; + let (bytes, report) = flatten_document(pdf, &opts).map_err(|e| format!("PDF/X-1a: the transparency flattener failed: {e}"))?; + let unread: Vec = report + .pages + .iter() + .filter(|(_, r)| r.read.unreadable_images > 0) + .map(|(page, r)| { + let n = r.read.unreadable_images; + let images = if n == 1 { "1 image".to_string() } else { format!("{n} images") }; + let fallback = PageGraphics { label: page.saturating_add(1).to_string(), graphics: Vec::new() }; + let at = pages.get(*page).unwrap_or(&fallback); + let names: Vec = at.graphics.iter().take(5).map(|g| format!("`{g}`")).collect(); + let more = if at.graphics.len() > 5 { ", …" } else { "" }; + let placed = if names.is_empty() { String::new() } else { format!(" (graphics on it: {}{more})", names.join(", ")) }; + format!("{images} on page {}{placed}", at.label) + }) + .collect(); + if !unread.is_empty() { + return Err(format!( + "PDF/X-1a: the transparency flattener couldn't read {}, and a PDF/X file can't leave images out: replace the graphic, or export without PDF/X-1a", + unread.join("; ") + )); + } + let mut warnings = flatten_warnings(&report.totals, dpi); + let bytes = finish_x1a(&bytes)?; + let (fatal, other) = checks(&bytes); + if !fatal.is_empty() { + return Err(format!("PDF/X-1a: after flattening, {}", fatal.join("; "))); + } + warnings.extend(other.into_iter().map(|issue| format!("PDF/X-1a: {issue}"))); + Ok((bytes, warnings)) +} + +/// What the flattener's `t` (at `dpi`) tells the user: rasterized transparency, and spot inks past +/// the ink set's capacity, whose tints the flattener drops. +pub fn flatten_warnings(t: &FlattenTotals, dpi: f32) -> Vec { + let mut out = Vec::new(); + if t.complex_regions > 0 || t.complex_groups > 0 { + let at = t.lowest_dpi.map(|d| format!(", some below {dpi:.0} ppi (lowest {d:.0} ppi)")).unwrap_or_default(); + out.push(format!("PDF/X-1a: transparency flattened: {} region(s) rasterized at {dpi:.0} ppi{at}", t.complex_regions + t.complex_groups)); + } + if t.dropped_inks > 0 { + out.push(format!( + "PDF/X-1a: the document has more spot inks than the flattener can separate: {} tint(s) in the inks past its limit were dropped and won't print", + t.dropped_inks + )); + } + out +} + +/// PDF/X-1a:2003 checks on an exported file (empty = passes them): PDF 1.4 or lower, the +/// identification (`GTS_PDFXVersion` and `GTS_PDFXConformance`), a CMYK `GTS_PDFX` output intent +/// whose profile, when it has one, is ICC v2 (and without one, names a registered printing +/// condition), `/Trapped` True or False, trim boxes, no encryption or interpolation; no +/// transparency (soft masks, transparency groups, constant opacity below 1, blend modes other +/// than Normal); only DeviceCMYK, DeviceGray, Separation and DeviceN (and Indexed and Pattern +/// spaces over them), a spot's alternate space process; every font embedded. +pub fn check_pdfx1a(pdf: &[u8]) -> Vec { + let (mut issues, other) = checks(pdf); + issues.extend(other); + issues +} + +/// [`check_pdfx1a`]'s findings: transparency and color spaces first, then the rest. +fn checks(pdf: &[u8]) -> (Vec, Vec) { + let mut issues = Vec::new(); + let file = craft_pdf::syntax::Pdf::new(pdf.to_vec()).ok(); + if !pdf.starts_with(b"%PDF-1.") || pdf.get(7).is_none_or(|v| *v > b'4') { + issues.push("the PDF version must be 1.4 or lower".into()); + } + if file.as_ref().is_none_or(|f| pdfx_intents(f).is_empty()) { + issues.push("no PDF/X output intent".into()); + } + let info = |key: &[u8]| file.as_ref().and_then(|f| info_text(f, key)); + if info(b"GTS_PDFXVersion").as_deref() != Some(VERSION) { + issues.push(format!("the document information doesn't name {VERSION}")); + } + if info(b"GTS_PDFXConformance").as_deref() != Some(CONFORMANCE) { + issues.push(format!("the document information doesn't name the conformance level {CONFORMANCE}")); + } + let trapped = file.as_ref().and_then(|f| f.info()).and_then(|info| info.get::>(b"Trapped")); + if !trapped.as_ref().is_some_and(|value| matches!(value.as_ref(), b"False" | b"True")) { + issues.push("the document information needs /Trapped True or False".into()); + } + if file.as_ref().is_some_and(|f| f.is_encrypted()) { + issues.push("the file is encrypted".into()); + } + if let Some(file) = file.as_ref() { + for (index, page) in file.pages().iter().enumerate() { + let raw = page.raw(); + let trim = raw.contains_key(b"TrimBox"); + let art = raw.contains_key(b"ArtBox"); + if trim == art { + issues.push(format!("page {} needs either a trim box or an art box, but not both", index + 1)); + } + } + } + let (content, fonts, scan_issues) = content(file.as_ref()); + issues.extend(fonts); + issues.extend(scan_issues); + (content, issues) +} + +/// Transparency and color spaces, then the output intent and fonts, walked through every page's +/// content and what it uses. +fn content(file: Option<&craft_pdf::syntax::Pdf>) -> (Vec, Vec, Vec) { + let Some(file) = file else { + return (vec!["the file couldn't be read to check its transparency and color spaces".into()], Vec::new(), Vec::new()); + }; + let mut fonts = intent_issues(file); + let mut found: Vec<(String, Vec)> = Vec::new(); + let mut font_found: Vec<(String, Vec)> = Vec::new(); + let mut scan_found: Vec<(String, Vec)> = Vec::new(); + for (i, page) in file.pages().iter().enumerate() { + let mut scan = Scan::default(); + let res = page.resources(); + if let Some(group) = page.raw().get::>(b"Group") { + scan.group(&group); + } + if let Some(content) = page.page_stream() { + scan.content(content, res, 0); + } + for (list, items) in [(&mut found, scan.found), (&mut font_found, scan.font_issues), (&mut scan_found, scan.other_issues)] { + for what in items { + match list.iter_mut().find(|(w, _)| *w == what) { + Some((_, pages)) => pages.push(i + 1), + None => list.push((what, vec![i + 1])), + } + } + } + } + let on_pages = |(what, pages): (String, Vec)| { + let list: Vec = pages.iter().take(10).map(|p| p.to_string()).collect(); + let more = if pages.len() > 10 { ", …" } else { "" }; + let s = if pages.len() == 1 { "" } else { "s" }; + format!("{what} on page{s} {}{more}", list.join(", ")) + }; + fonts.extend(font_found.into_iter().map(on_pages)); + (found.into_iter().map(on_pages).collect(), fonts, scan_found.into_iter().map(on_pages).collect()) +} + +/// A `GTS_PDFX` output intent, as PDF/X-1a judges it. +struct Intent { + /// Its object number, when it is an indirect object. + obj: Option, + /// `/OutputCondition`, else `/OutputConditionIdentifier`, for messages. + name: String, + /// An `/OutputConditionIdentifier` other than `Custom` in the ICC registry. + registered: bool, + /// Whether it has a `/DestOutputProfile`, and the profile's object number. + has_profile: bool, + profile_obj: Option, + /// The profile's `/N` and decoded bytes. + channels: Option, + profile: Option>, +} + +impl Intent { + /// The profile's ICC major version (byte 8 of its header). + fn major(&self) -> Option { + self.profile.as_deref()?.get(8).copied() + } + + /// "ICC v4", or what keeps the version from being read. + fn version(&self) -> String { + self.major().map_or_else(|| "unreadable".to_string(), |v| format!("ICC v{v}")) + } +} + +/// The catalog's `GTS_PDFX` output intents. +fn pdfx_intents(file: &craft_pdf::syntax::Pdf) -> Vec { + let number = |n: i32| usize::try_from(n).ok(); + raw_pdfx_intents(file) + .into_iter() + .map(|oi| { + let text = |key: &[u8]| { + oi.get::>(key) + .map(|s| String::from_utf8_lossy(s.as_bytes()).trim().to_string()) + .filter(|s| !s.is_empty()) + }; + let identifier = text(b"OutputConditionIdentifier"); + let registry = text(b"RegistryName"); + let profile = oi.get::>(b"DestOutputProfile"); + Intent { + obj: oi.obj_id().and_then(|id| number(id.obj_number)), + name: text(b"OutputCondition").or_else(|| identifier.clone()).unwrap_or_else(|| "?".into()), + registered: registry.is_some_and(|r| r.trim_end_matches('/') == ICC_REGISTRY) + && identifier.as_deref().is_some_and(|identifier| REGISTERED_CONDITIONS.contains(&identifier)), + has_profile: oi.contains_key(b"DestOutputProfile"), + profile_obj: oi.get_ref(b"DestOutputProfile").and_then(|r| number(r.obj_number)), + channels: profile.as_ref().and_then(|p| p.dict().get::(b"N")), + profile: profile.and_then(|p| p.decoded().ok()).map(|b| b.into_owned()), + } + }) + .collect() +} + +fn raw_pdfx_intents<'a>(file: &'a craft_pdf::syntax::Pdf) -> Vec> { + let xref = file.xref(); + let Some(catalog) = xref.get::>(xref.root_id()) else { return Vec::new() }; + let Some(intents) = catalog.get::>(b"OutputIntents") else { return Vec::new() }; + intents.iter::>().filter(|oi| oi.get::>(b"S").is_some_and(|s| s.as_ref() == b"GTS_PDFX")).collect() +} + +fn info_text(file: &craft_pdf::syntax::Pdf, key: &[u8]) -> Option { + file.info()?.get::>(key).map(|s| String::from_utf8_lossy(s.as_bytes()).trim().to_string()) +} + +/// What PDF/X-1a finds wrong with the file's output intent: a CMYK profile, ICC v2 (PDF 1.4 +/// allows no later version), or none for a registered printing condition. +fn intent_issues(file: &craft_pdf::syntax::Pdf) -> Vec { + let Some(oi) = pdfx_intents(file).into_iter().next() else { return Vec::new() }; + let mut issues = Vec::new(); + if oi.has_profile { + if oi.channels != Some(4) { + issues.push("the output intent has no CMYK destination profile".into()); + } + if oi.major() != Some(2) { + issues.push(format!("the output intent's profile is {}, and PDF/X-1a (PDF 1.4) allows only ICC v2 profiles", oi.version())); + } + } else if !oi.registered { + issues + .push(format!("the output intent has no destination profile and doesn't name a printing condition in the ICC registry ({ICC_REGISTRY})")); + } + issues +} + +/// krilla's PDF 1.4 file with what PDF/X-1a needs and krilla doesn't write, rewritten as one +/// revision: `GTS_PDFXConformance` in the document information and the XMP, and the output intent +/// of a registered printing condition without `/DestOutputProfile` (its profile is left out of +/// the file). An unregistered condition keeps its profile, which must then be ICC v2. +pub fn finish_x1a(pdf: &[u8]) -> Result, String> { + let unreadable = || "the flattened file couldn't be read to add the PDF/X-1a identification".to_string(); + let layout = Layout::read(pdf).ok_or_else(unreadable)?; + let file = craft_pdf::syntax::Pdf::new(pdf.to_vec()).map_err(|_| unreadable())?; + let mut bodies: HashMap> = HashMap::new(); + let info = file.info().and_then(|info| info.obj_id()).and_then(|id| usize::try_from(id.obj_number).ok()).ok_or_else(unreadable)?; + let body = layout.body(pdf, info).ok_or_else(unreadable)?; + let (head, close) = body.split_at_checked(rfind(body, b">>").ok_or_else(unreadable)?).ok_or_else(unreadable)?; + bodies.insert(info, [head, format!("/GTS_PDFXConformance({CONFORMANCE})").as_bytes(), close].concat()); + let xref = file.xref(); + if let Some(meta) = xref.get::>(xref.root_id()).and_then(|c| c.get_ref(b"Metadata")).and_then(|r| usize::try_from(r.obj_number).ok()) + && let Some(xmp) = layout.body(pdf, meta).and_then(xmp_with_conformance) + { + bodies.insert(meta, xmp); + } + let mut stripped: Vec = Vec::new(); + for oi in pdfx_intents(&file) { + if !oi.has_profile { + continue; + } + if !oi.registered { + if oi.major() != Some(2) { + return Err(format!( + "the output intent's profile `{}` is {}, and PDF/X-1a (PDF 1.4) allows only ICC v2 profiles: choose a CMYK working space with an ICC v2 profile, or Coated FOGRA39", + oi.name, + oi.version() + )); + } + continue; + } + let (Some(obj), Some(profile)) = (oi.obj, oi.profile_obj) else { return Err(unreadable()) }; + let body = match bodies.get(&obj) { + Some(b) => b.clone(), + None => layout.body(pdf, obj).ok_or_else(unreadable)?.to_vec(), + }; + let key = format!("/DestOutputProfile {profile} 0 R"); + let at = find(&body, key.as_bytes(), 0).ok_or_else(unreadable)?; + let (before, rest) = body.split_at_checked(at).ok_or_else(unreadable)?; + let after = rest.get(key.len()..).ok_or_else(unreadable)?; + bodies.insert(obj, [before, after].concat()); + stripped.push(profile); + } + // A profile only the stripped intents named goes too. + let dropped: HashSet = stripped.iter().copied().filter(|p| references(pdf, *p) == stripped.iter().filter(|q| *q == p).count()).collect(); + layout.write(pdf, &bodies, &dropped).ok_or_else(unreadable) +} + +/// How often `n 0 R` appears in `pdf` (a stream that happens to hold those bytes counts too, which +/// only ever keeps an object). +fn references(pdf: &[u8], n: usize) -> usize { + let needle = format!("{n} 0 R"); + let mut count = 0; + let mut from = 0; + while let Some(at) = find(pdf, needle.as_bytes(), from) { + let before = at.checked_sub(1).and_then(|i| pdf.get(i)).is_none_or(|c| !c.is_ascii_digit()); + let after = pdf.get(at + needle.len()).is_none_or(|c| !c.is_ascii_alphanumeric()); + if before && after { + count += 1; + } + from = at + needle.len(); + } + count +} + +/// The body of an uncompressed XMP metadata stream object with the Adobe `pdfx` properties of +/// the identification (`GTS_PDFXVersion`, `GTS_PDFXConformance`) added and its `/Length` updated; +/// `None` when the stream is filtered or isn't XMP. +fn xmp_with_conformance(body: &[u8]) -> Option> { + let s0 = find(body, b"stream", 0)?; + let dict = String::from_utf8_lossy(body.get(..s0)?).to_string(); + let s0 = s0 + b"stream".len(); + let s0 = s0 + body.get(s0..)?.iter().take_while(|c| **c == b'\r' || **c == b'\n').count(); + let s1 = rfind(body, b"endstream")?; + let xml = String::from_utf8_lossy(body.get(s0..s1)?).trim_end().to_string(); + if dict.contains("/Filter") || !xml.contains("") { + return None; + } + let ids = format!( + "{VERSION}\ + {CONFORMANCE}" + ); + let xml = xml.replacen("", &format!("{ids}"), 1); + let i = dict.find("/Length")?; + let rest = dict.get(i + 7..)?.trim_start(); + let end = rest.find(|c: char| !c.is_ascii_digit()).unwrap_or(rest.len()); + let dict = format!("{}/Length {}{}", dict.get(..i)?, xml.len(), rest.get(end..)?); + Some(format!("\n{}\nstream\n{xml}\nendstream\n", dict.trim()).into_bytes()) +} + +fn find(bytes: &[u8], needle: &[u8], from: usize) -> Option { + (from <= bytes.len()).then(|| bytes.get(from..)?.windows(needle.len()).position(|w| w == needle).map(|at| at + from))? +} + +fn rfind(bytes: &[u8], needle: &[u8]) -> Option { + bytes.windows(needle.len()).rposition(|w| w == needle) +} + +fn int_after(text: &str, key: &str) -> Option { + let at = text.find(key)? + key.len(); + text.get(at..)?.trim_start().split(|c: char| !c.is_ascii_digit()).next()?.parse().ok() +} + +/// Where the objects of a single-revision file with a classic cross-reference table are. +struct Layout { + /// (object number, start, end), by start: from `n 0 obj` up to the next object or the table. + objects: Vec<(usize, usize, usize)>, + /// Where the cross-reference table starts. + table: usize, + /// The trailer dictionary. + trailer: String, + size: usize, +} + +impl Layout { + fn read(pdf: &[u8]) -> Option { + let sx = rfind(pdf, b"startxref")?; + let table: usize = std::str::from_utf8(pdf.get(sx + b"startxref".len()..)?).ok()?.split_whitespace().next()?.parse().ok()?; + let section = pdf.get(table..sx)?; + let t = find(section, b"trailer", 0)?; + let trailer = String::from_utf8_lossy(section.get(t + b"trailer".len()..)?).trim().to_string(); + if trailer.contains("/Prev") || trailer.contains("/XRefStm") { + return None; + } + let size = int_after(&trailer, "/Size")?; + if size > MAX_XREF_SIZE { + return None; + } + let mut tokens = std::str::from_utf8(section.get(..t)?).ok()?.split_whitespace(); + if tokens.next()? != "xref" { + return None; + } + let mut starts: Vec<(usize, usize)> = Vec::new(); + let mut seen = HashSet::new(); + while let Some(first) = tokens.next() { + let first: usize = first.parse().ok()?; + let count: usize = tokens.next()?.parse().ok()?; + for i in 0..count { + let (offset, generation, kind) = (tokens.next()?, tokens.next()?, tokens.next()?); + if kind != "n" { + continue; + } + let n = first.checked_add(i)?; + let offset: usize = offset.parse().ok()?; + if generation != "00000" || offset >= table || n >= size || !seen.insert(n) { + return None; + } + starts.push((n, offset)); + } + } + // Free entries past the objects would only make the rewritten table longer. + let max = starts.iter().map(|(n, _)| *n).max().unwrap_or(0); + if size > max.saturating_mul(2).saturating_add(16) { + return None; + } + starts.sort_by_key(|(_, offset)| *offset); + let mut objects = Vec::with_capacity(starts.len()); + for (i, &(n, start)) in starts.iter().enumerate() { + let end = starts.get(i + 1).map_or(table, |(_, s)| *s); + if !pdf.get(start..end)?.starts_with(format!("{n} 0 obj").as_bytes()) { + return None; + } + objects.push((n, start, end)); + } + Some(Layout { objects, table, trailer, size }) + } + + /// Object `n` between `n 0 obj` and `endobj`. + fn body<'p>(&self, pdf: &'p [u8], n: usize) -> Option<&'p [u8]> { + let &(_, start, end) = self.objects.iter().find(|(m, _, _)| *m == n)?; + let obj = pdf.get(start..end)?; + obj.get(format!("{n} 0 obj").len()..rfind(obj, b"endobj")?) + } + + /// The file with the objects in `bodies` replaced and those in `dropped` left out, and a new + /// cross-reference table. + fn write(&self, pdf: &[u8], bodies: &HashMap>, dropped: &HashSet) -> Option> { + let first = self.objects.first().map_or(self.table, |o| o.1); + let mut out = pdf.get(..first)?.to_vec(); + let mut offsets = HashMap::new(); + for &(n, start, end) in &self.objects { + if dropped.contains(&n) { + continue; + } + offsets.insert(n, out.len()); + match bodies.get(&n) { + Some(body) => { + out.extend_from_slice(format!("{n} 0 obj").as_bytes()); + out.extend_from_slice(body); + out.extend_from_slice(b"endobj\n"); + } + None => out.extend_from_slice(pdf.get(start..end)?), + } + } + if !out.ends_with(b"\n") { + out.push(b'\n'); + } + let table = out.len(); + // The free entries, each pointing at the next. + let free: Vec = (1..self.size).filter(|n| !offsets.contains_key(n)).collect(); + let next: HashMap = free.iter().copied().zip(free.iter().skip(1).copied().chain([0])).collect(); + out.extend_from_slice(format!("xref\n0 {}\n{:010} 65535 f\r\n", self.size, free.first().copied().unwrap_or(0)).as_bytes()); + for n in 1..self.size { + let next = next.get(&n).copied().unwrap_or(0); + let entry = match offsets.get(&n) { + Some(offset) => format!("{offset:010} 00000 n\r\n"), + // A dropped object's number could be used again, at the next generation. + None if dropped.contains(&n) => format!("{next:010} 00001 f\r\n"), + None => format!("{next:010} 65535 f\r\n"), + }; + out.extend_from_slice(entry.as_bytes()); + } + out.extend_from_slice(format!("trailer\n{}\nstartxref\n{table}\n%%EOF\n", self.trailer).as_bytes()); + Some(out) + } +} + +/// Why a color space isn't allowed in PDF/X-1a, or `None` when it is. +fn disallowed(cs: &Object<'_>, res: &Resources<'_>, depth: usize) -> Option { + if depth > 4 { + return Some("a color space nested too deeply".into()); + } + match cs { + Object::Name(n) => match n.as_ref() { + b"DeviceCMYK" | b"CMYK" | b"DeviceGray" | b"G" | b"Pattern" => None, + b"DeviceRGB" | b"RGB" => Some("DeviceRGB color".into()), + b"Indexed" | b"I" => None, + other => match res.get_color_space(&Name::new_unescaped(other)) { + Some(named) if !matches!(named, Object::Name(_)) => disallowed(&named, res, depth + 1), + Some(Object::Name(m)) if m.as_ref() != other => disallowed(&Object::Name(m), res, depth + 1), + _ => Some(format!("the color space /{}", String::from_utf8_lossy(other))), + }, + }, + Object::Array(a) => { + let mut items = a.iter::>(); + let family = match items.next() { + Some(Object::Name(n)) => n, + _ => return Some("an unreadable color space".into()), + }; + match family.as_ref() { + b"ICCBased" => Some("ICC-based color".into()), + b"CalRGB" | b"CalGray" => Some("CIE-based (CalRGB/CalGray) color".into()), + b"Lab" => Some("Lab color".into()), + b"DeviceRGB" | b"RGB" => Some("DeviceRGB color".into()), + b"DeviceCMYK" | b"CMYK" | b"DeviceGray" | b"G" => None, + b"Indexed" | b"I" => items.next().and_then(|base| disallowed(&base, res, depth + 1)), + b"Pattern" => items.next().and_then(|base| disallowed(&base, res, depth + 1)), + b"Separation" | b"DeviceN" => { + let _names = items.next(); + let alt = items.next()?; + if !is_process_space(&alt, res, depth + 1) { + return Some("a spot color whose alternate space isn't a CMYK or gray process space".into()); + } + let _tint = items.next(); + // NChannel attributes are PDF 1.6, past the 1.4 that PDF/X-1a:2003 allows. + let attrs = items.next().and_then(|o| match o { + Object::Dict(d) => Some(d), + _ => None, + }); + attrs + .filter(|a| a.get::>(b"Subtype").is_some_and(|n| n.as_ref() == b"NChannel") || a.contains_key(b"Process")) + .map(|_| "a DeviceN space with NChannel attributes (PDF 1.6)".into()) + } + other => Some(format!("the color space /{}", String::from_utf8_lossy(other))), + } + } + _ => Some("an unreadable color space".into()), + } +} + +fn is_process_space(cs: &Object<'_>, res: &Resources<'_>, depth: usize) -> bool { + if depth > 4 { + return false; + } + match cs { + Object::Name(name) => match name.as_ref() { + b"DeviceCMYK" | b"CMYK" | b"DeviceGray" | b"G" => true, + other => match res.get_color_space(&Name::new_unescaped(other)) { + Some(Object::Name(alias)) if alias.as_ref() != other => is_process_space(&Object::Name(alias), res, depth + 1), + Some(named) if !matches!(named, Object::Name(_)) => is_process_space(&named, res, depth + 1), + _ => false, + }, + }, + Object::Array(array) => { + array.iter::>().next().is_some_and(|family| matches!(family.as_ref(), b"DeviceCMYK" | b"CMYK" | b"DeviceGray" | b"G")) + } + _ => false, + } +} + +/// What one page has that PDF/X-1a doesn't allow, in the order found. +#[derive(Default)] +struct Scan { + /// What was found, in the order found; `known` is the same as a set. + found: Vec, + known: HashSet, + font_issues: Vec, + other_issues: Vec, + seen: HashSet, + fonts: HashSet, +} + +impl Scan { + fn add(&mut self, what: String) { + if self.known.insert(what.clone()) { + self.found.push(what); + } + } + + fn space(&mut self, cs: &Object<'_>, res: &Resources<'_>) { + if let Some(why) = disallowed(cs, res, 0) { + self.add(why); + } + } + + fn content(&mut self, data: &[u8], res: &Resources<'_>, depth: usize) { + if depth > MAX_DEPTH { + self.add("content was not fully checked because nested resources exceed the scan limit".into()); + return; + } + let mut ops = UntypedIter::new(data); + while let Some(op) = ops.next() { + let operator: &[u8] = op.operator; + let last = op.operands().last().cloned(); + let name = || last.clone().and_then(Object::into_name); + match operator { + b"rg" | b"RG" => self.add("DeviceRGB color".into()), + b"cs" | b"CS" => { + if let Some(n) = name() { + self.space(&Object::Name(n), res); + } + } + b"sh" => { + if let Some(sh) = name().and_then(|n| res.get_shading(&n)) { + self.shading(sh, res); + } + } + b"scn" | b"SCN" => { + if let Some(p) = name().and_then(|n| res.get_pattern(&n)) { + self.pattern(p, res, depth); + } + } + b"gs" => { + if let Some(gs) = name().and_then(|n| res.get_ext_g_state(&n)) { + self.ext_g_state(&gs, res, depth); + } + } + b"Tf" => { + let font = op.operands().next().cloned().and_then(Object::into_name).and_then(|n| res.get_font(&n)); + if let Some(font) = font { + self.font(&font, res, depth); + } + } + b"Do" => { + if let Some(xo) = name().and_then(|n| res.get_x_object(&n)) { + match xo.dict().get::>(b"Subtype").as_deref() { + Some(b"Image") => self.image(&xo, res), + Some(b"Form") => self.form(&xo, res, depth), + _ => {} + } + } + } + b"BI" | b"EI" => { + if let Some(Object::Stream(img)) = last { + let dict = img.dict(); + if let Some(cs) = dict.get::>(b"CS").or_else(|| dict.get::>(b"ColorSpace")) { + self.space(&cs, res); + } + self.interpolation(dict); + } + } + _ => {} + } + } + } + + fn ext_g_state(&mut self, gs: &Dict<'_>, res: &Resources<'_>, depth: usize) { + match gs.get::>(b"SMask") { + None => {} + Some(Object::Name(n)) if n.as_ref() == b"None" => {} + Some(mask) => { + self.add("a soft mask (transparency)".into()); + if let Some(form) = match mask { + Object::Dict(d) => d.get::>(b"G"), + _ => None, + } { + self.form(&form, res, depth); + } + } + } + for key in [&b"CA"[..], b"ca"] { + if gs.get::(key).is_some_and(|a| a < 1.0) { + self.add("constant opacity below 100% (transparency)".into()); + } + } + let normal = |n: &[u8]| matches!(n, b"Normal" | b"Compatible"); + match gs.get::>(b"BM") { + Some(Object::Name(n)) if !normal(n.as_ref()) => { + self.add(format!("the blend mode /{} (transparency)", String::from_utf8_lossy(n.as_ref()))) + } + Some(Object::Array(a)) if a.iter::>().any(|n| !normal(n.as_ref())) => { + self.add("a blend mode other than Normal (transparency)".into()) + } + _ => {} + } + } + + fn image(&mut self, img: &Stream<'_>, res: &Resources<'_>) { + if !self.seen.insert(img.obj_id()) { + return; + } + let d = img.dict(); + self.interpolation(d); + if d.get::>(b"SMask").is_some() { + self.add(format!("an image with a soft mask (object {}, transparency)", img.obj_id().obj_number)); + } + if d.get::(b"SMaskInData").is_some_and(|v| v != 0) { + self.add(format!("an image with transparency in its data (object {})", img.obj_id().obj_number)); + } + if d.get::(b"ImageMask") == Some(true) { + return; + } + match d.get::>(b"ColorSpace") { + Some(cs) => { + if let Some(why) = disallowed(&cs, res, 0) { + let (w, h) = (d.get::(b"Width").unwrap_or(0), d.get::(b"Height").unwrap_or(0)); + self.add(format!("{why} in an image (object {}, {w} × {h} px)", img.obj_id().obj_number)); + } + } + // JPX images may carry their own space; X-1a doesn't allow them. + None => self.add(format!("an image without a color space (object {})", img.obj_id().obj_number)), + } + } + + fn interpolation(&mut self, dict: &Dict<'_>) { + if dict.get::(b"Interpolate") == Some(true) { + let message = "an image asks for interpolation".to_string(); + if !self.other_issues.contains(&message) { + self.other_issues.push(message); + } + } + } + + fn form(&mut self, form: &Stream<'_>, parent: &Resources<'_>, depth: usize) { + if depth >= MAX_DEPTH { + self.add("content was not fully checked because nested forms exceed the scan limit".into()); + return; + } + if !self.seen.insert(form.obj_id()) { + return; + } + let res = Resources::from_parent(form.dict().get::>(b"Resources").unwrap_or_default(), parent.clone()); + if let Some(group) = form.dict().get::>(b"Group") { + self.group(&group); + } + match form.decoded() { + Ok(data) => self.content(&data, &res, depth + 1), + Err(_) => self.add(format!("a form XObject could not be decoded (object {})", form.obj_id().obj_number)), + } + } + + fn group(&mut self, group: &Dict<'_>) { + if group.get::>(b"S").is_some_and(|s| s.as_ref() == b"Transparency") { + self.add("a transparency group".into()); + } + } + + fn shading(&mut self, sh: Object<'_>, res: &Resources<'_>) { + let dict = match sh { + Object::Dict(d) => d, + Object::Stream(s) => s.dict().clone(), + _ => return, + }; + if let Some(cs) = dict.get::>(b"ColorSpace") { + self.space(&cs, res); + } + } + + fn pattern(&mut self, p: Object<'_>, res: &Resources<'_>, depth: usize) { + match p { + Object::Dict(d) => { + if let Some(sh) = d.get::>(b"Shading") { + self.shading(sh, res); + } + if let Some(gs) = d.get::>(b"ExtGState") { + self.ext_g_state(&gs, res, depth); + } + } + Object::Stream(s) => self.form(&s, res, depth), + _ => {} + } + } + + /// Embedded: a simple font's descriptor, or a Type 0 font's descendant's, has a font file. A + /// Type 3 font's glyphs are content streams in the file, scanned like the page's. + fn font(&mut self, font: &Dict<'_>, parent: &Resources<'_>, depth: usize) { + if let Some(id) = font.obj_id() + && !self.fonts.insert(id) + { + return; + } + let name = font.get::>(b"BaseFont").map(|n| String::from_utf8_lossy(n.as_ref()).to_string()).unwrap_or_else(|| "?".into()); + let subtype = font.get::>(b"Subtype"); + let descriptor = match subtype.as_deref() { + Some(b"Type3") => return self.type3(font, parent, depth), + Some(b"Type0") => { + font.get::>(b"DescendantFonts").and_then(|a| a.iter::>().next()).and_then(|d| d.get::>(b"FontDescriptor")) + } + _ => font.get::>(b"FontDescriptor"), + }; + let embedded = descriptor.is_some_and(|d| [&b"FontFile"[..], b"FontFile2", b"FontFile3"].iter().any(|k| d.get::>(k).is_some())); + if !embedded { + self.font_issues.push(format!("the font {name} isn't embedded")); + } + } + + /// A Type 3 font's glyph procedures, with the font's resources over `parent`'s. + fn type3(&mut self, font: &Dict<'_>, parent: &Resources<'_>, depth: usize) { + if depth >= MAX_DEPTH { + self.add("content was not fully checked because nested Type 3 glyphs exceed the scan limit".into()); + return; + } + let res = Resources::from_parent(font.get::>(b"Resources").unwrap_or_default(), parent.clone()); + let Some(procs) = font.get::>(b"CharProcs") else { return }; + for key in procs.keys() { + if let Some(glyph) = procs.get::>(key.as_ref()) + && self.seen.insert(glyph.obj_id()) + { + match glyph.decoded() { + Ok(data) => self.content(&data, &res, depth + 1), + Err(_) => self.add(format!("a Type 3 glyph stream could not be decoded (object {})", glyph.obj_id().obj_number)), + } + } + } + } +} + +#[cfg(test)] +mod review_regressions { + use super::{Layout, check_pdfx1a, finish_x1a}; + fn pdf(page_boxes: &str, info: &str, content: &[u8], extra: &[(&str, &str)]) -> Vec { + let mut objects = vec![ + b"<< /Type /Catalog /Pages 2 0 R /OutputIntents [3 0 R] >>".to_vec(), + b"<< /Type /Pages /Kids [4 0 R] /Count 1 >>".to_vec(), + b"<< /Type /OutputIntent /S /GTS_PDFX /OutputConditionIdentifier (FOGRA39) /RegistryName (http://www.color.org) >>".to_vec(), + format!("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 100 100] {page_boxes} /Resources <<>> /Contents 5 0 R >>").into_bytes(), + [format!("<< /Length {} >>\nstream\n", content.len()).as_bytes(), content, b"\nendstream"].concat(), + format!("<< {info} >>").into_bytes(), + ]; + for (key, body) in extra { + objects.push(format!("<< {key} {body} >>").into_bytes()); + } + let mut out = b"%PDF-1.4\n% parser regression fixture\n".to_vec(); + let mut offsets = Vec::new(); + for (index, body) in objects.iter().enumerate() { + offsets.push(out.len()); + out.extend_from_slice(format!("{} 0 obj\n", index + 1).as_bytes()); + out.extend_from_slice(body); + out.extend_from_slice(b"\nendobj\n"); + } + let xref = out.len(); + out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", objects.len() + 1).as_bytes()); + for offset in offsets { + out.extend_from_slice(format!("{offset:010} 00000 n \n").as_bytes()); + } + out.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R /Info 6 0 R >>\nstartxref\n{xref}\n%%EOF\n", objects.len() + 1).as_bytes()); + out + } + + fn compliant_info() -> &'static str { + "/GTS_PDFXVersion (PDF/X-1:2003) /GTS_PDFXConformance (PDF/X-1a:2003) /Trapped /False" + } + + #[test] + fn parsed_info_values_are_required_even_when_comment_contains_expected_values() { + let bytes = pdf( + "/TrimBox [0 0 100 100]", + "/GTS_PDFXVersion (wrong) /GTS_PDFXConformance (wrong) /Trapped /False", + b"% /GTS_PDFXVersion (PDF/X-1:2003) /GTS_PDFXConformance (PDF/X-1a:2003)\n", + &[], + ); + let issues = check_pdfx1a(&bytes); + assert!(issues.iter().any(|issue| issue.contains("doesn't name PDF/X-1:2003")), "{issues:?}"); + assert!(issues.iter().any(|issue| issue.contains("doesn't name the conformance level")), "{issues:?}"); + } + + #[test] + fn either_trim_or_art_box_is_accepted_but_not_both() { + let art_only = pdf("/ArtBox [0 0 100 100]", compliant_info(), b"", &[]); + let both = pdf("/TrimBox [0 0 100 100] /ArtBox [0 0 100 100]", compliant_info(), b"", &[]); + assert!(!check_pdfx1a(&art_only).iter().any(|issue| issue.contains("either a trim box or an art box"))); + assert!(check_pdfx1a(&both).iter().any(|issue| issue.contains("either a trim box or an art box"))); + } + + #[test] + fn an_unknown_registry_identifier_cannot_replace_a_destination_profile() { + let mut bytes = pdf("/TrimBox [0 0 100 100]", compliant_info(), b"", &[]); + let known = b"FOGRA39"; + let at = bytes.windows(known.len()).position(|window| window == known); + assert!(at.is_some()); + if let Some(at) = at { + bytes[at..at + known.len()].copy_from_slice(b"NOTREAL"); + } + let issues = check_pdfx1a(&bytes); + assert!(issues.iter().any(|issue| issue.contains("doesn't name a printing condition in the ICC registry")), "{issues:?}"); + } + + #[test] + fn finalizer_rejects_unbounded_xref_size_before_allocating_the_table() { + let mut bytes = pdf("/TrimBox [0 0 100 100]", compliant_info(), b"", &[]); + let size = b"/Size 7"; + let at = bytes.windows(size.len()).position(|window| window == size); + assert!(at.is_some()); + if let Some(at) = at { + bytes.splice(at..at + size.len(), b"/Size 1048577".iter().copied()); + } + assert!(Layout::read(&bytes).is_none()); + assert!(finish_x1a(&bytes).is_err()); + } + + #[test] + fn trapped_must_be_a_pdf_name_with_a_boolean_name_value() { + for value in ["(False)", "true", "(True)"] { + let info = format!("/GTS_PDFXVersion (PDF/X-1:2003) /GTS_PDFXConformance (PDF/X-1a:2003) /Trapped {value}"); + let bytes = pdf("/TrimBox [0 0 100 100]", &info, b"", &[]); + assert!(check_pdfx1a(&bytes).iter().any(|issue| issue.contains("needs /Trapped True or False")), "{value}"); + } + } +} diff --git a/crates/prepress/tests/flatten_document.rs b/crates/prepress/tests/flatten_document.rs index 165a80e9a..a24f5dfea 100644 --- a/crates/prepress/tests/flatten_document.rs +++ b/crates/prepress/tests/flatten_document.rs @@ -116,7 +116,6 @@ fn three_pages(font_bytes: &[u8]) -> Vec { s.pop(); text(s, &font, &face, (20.0, 110.0), 12.0, "World"); }); - drop(page); writer.finish().unwrap() } diff --git a/crates/print/Cargo.toml b/crates/print/Cargo.toml index 36b8813f2..fca9d4a64 100644 --- a/crates/print/Cargo.toml +++ b/crates/print/Cargo.toml @@ -13,14 +13,14 @@ description = "System PDF print spooler for the Craft apps: CUPS and Windows" # downstream GDI values a different Rust type identity even when the code is unchanged. # PDF loader updates below are independent of this public transport API. craft-winprint-gdi = { git = "https://github.com/storytold/winprint.rs", rev = "64e7afd4d1483512385012c340ecaaaf0f3beb7f" } +# Says why the printer list is empty when lpstat can't run (e.g. a Flatpak without CUPS tools), +# and why a job went on with the paper the request names while the driver reported only one side +# of it (storytold/craft-libs#7230). +log = "0.4" [lints] workspace = true -[target.'cfg(unix)'.dependencies] -# Says why the printer list is empty when lpstat can't run (e.g. a Flatpak without CUPS tools). -log = "0.4" - [target.'cfg(windows)'.dependencies] # Printing without starting anything (storytold/pdfcraft#1107): the printer list (EnumPrinters) # and the job (Windows.Data.Pdf drawn with Direct2D into the XPS print API, options as a diff --git a/crates/print/src/sheets.rs b/crates/print/src/sheets.rs index 0f013478b..fa95d11c4 100644 --- a/crates/print/src/sheets.rs +++ b/crates/print/src/sheets.rs @@ -1136,6 +1136,21 @@ pub(crate) fn plan(sheets: &[Sheet], paper: (f64, f64), fit: Fit) -> Result= 1.0 && close((w, h), paper) { None } else { + // The matrix is written with at most four decimals and a PDF real has no + // exponent, so a scale that rounds away prints nothing at all and one that + // rounds up prints the wrong size. Refuse both, the way a nondefault + // UserUnit is refused: an error, never a job that spools a blank page. + let written = number(scale).parse::().unwrap_or(0.0); + if written <= 0.0 || (written - scale).abs() > scale * 0.01 { + return Err(Error::Spool(format!( + "shrink to fit cannot place sheet {} ({:.2} x {:.2} in) on the {:.2} x {:.2} in paper; nothing was sent", + i + 1, + w / 72.0, + h / 72.0, + pw / 72.0, + ph / 72.0 + ))); + } let [x0, y0, x1, y1] = sheet.bounds; let (cw, ch) = (x1 - x0, y1 - y0); // The paper in the page's unrotated space. diff --git a/crates/print/src/tests/windows_native.rs b/crates/print/src/tests/windows_native.rs index 7882a0578..12c480f22 100644 --- a/crates/print/src/tests/windows_native.rs +++ b/crates/print/src/tests/windows_native.rs @@ -555,3 +555,42 @@ fn windows_jobs_reach_the_spooler() { let missing = spool::submit(&mixed, &Job { printer: Some(name.into()), ..Job::default() }); assert!(matches!(missing, Err(Error::Spool(ref m)) if m.contains(name)), "{missing:?}"); } + +#[test] +fn shrink_to_fit_refuses_a_sheet_the_output_cannot_represent() { + use crate::sheets::{Fit, plan, read}; + // A runaway-zoom export: 16 000 000 pt wide. Written with four decimals the scale would be + // "0" -- a singular matrix, a blank page, reported back as a spooled job. + let huge = TestPage { media: Some([0.0, 0.0, 16_000_000.0, 792.0]), ..LETTER }; + let pdf = test_pdf(&[huge], false); + let sheets = read(&pdf).unwrap(); + let refused = plan(&sheets.sheets, (612.0, 792.0), Fit::Shrink { margin: 18.0 }); + assert!(matches!(refused, Err(Error::Spool(ref m)) if m.contains("shrink")), "{refused:?}"); +} + +#[test] +fn shrink_to_fit_holds_the_one_percent_scale_boundary() { + use crate::sheets::{Fit, plan, read}; + // The matrix carries the scale through the four-decimal formatter, so the plan refuses when + // what would be written is more than 1% off the real scale. Letter with an 18 pt margin gives + // 576 pt of printable width: a 118 700 pt wide, 120 000 pt tall sheet (no quarter turn) scales + // by 576 / 118 700 = 0.00485257, written 0.0049 -- 0.98% short, inside the bound, so it + // prints. + let inside = TestPage { media: Some([0.0, 0.0, 118_700.0, 120_000.0]), ..LETTER }; + let pdf = test_pdf(&[inside], false); + let sheets = read(&pdf).unwrap(); + let edits = plan(&sheets.sheets, (612.0, 792.0), Fit::Shrink { margin: 18.0 }).unwrap(); + let updated = sheets.update(&edits).unwrap(); + let frame = frame_of(&updated, pdf.len()); + let written: f64 = frame.split_whitespace().nth(1).unwrap().parse().unwrap(); + let real = edits[0].frame.as_ref().unwrap().matrix[0]; + assert!(written != real && (written - real).abs() < real * 0.01, "just inside the 1% bound the written scale is accepted: {frame}"); + + // A 118 750 pt wide sheet: 576 / 118 750 = 0.00485053, written 0.0049, 1.02% off -- refused as + // an error before anything is sent, rather than printed a percent too small. + let outside = TestPage { media: Some([0.0, 0.0, 118_750.0, 120_000.0]), ..LETTER }; + let pdf = test_pdf(&[outside], false); + let sheets = read(&pdf).unwrap(); + let refused = plan(&sheets.sheets, (612.0, 792.0), Fit::Shrink { margin: 18.0 }); + assert!(matches!(refused, Err(Error::Spool(ref m)) if m.contains("shrink") && m.contains("nothing was sent")), "{refused:?}"); +} diff --git a/crates/print/src/ticket.rs b/crates/print/src/ticket.rs index ddf2c3955..e6224df69 100644 --- a/crates/print/src/ticket.rs +++ b/crates/print/src/ticket.rs @@ -197,11 +197,24 @@ pub(crate) fn paper_names(width: f64, height: f64) -> &'static [&'static str] { } /// Whether a driver's paper name is `name`: it starts with it, ignoring case, and the name ends -/// there ("A4 210 x 297 mm", not "A4 Extra" for "A4" or "ARCH E1" for "ARCH E"). +/// there — either nothing follows ("arch d"), or a separator and the sheet's dimensions ("A4 +/// 210 x 297 mm"). After a separator, dimensions are a digit: a word is a modifier, another +/// paper ("A4 Extra", "A4 (Extra)"), and anything joined to the name without a separator is +/// another paper too ("A4Extra", "ARCH E1" for "ARCH E"). pub(crate) fn names_paper(display: Option<&str>, name: &str) -> bool { let Some(display) = display else { return false }; - display.get(..name.len()).is_some_and(|start| start.eq_ignore_ascii_case(name)) - && display.get(name.len()..).is_some_and(|rest| rest.chars().next().is_none_or(|c| !c.is_alphanumeric())) + let Some(rest) = display.get(name.len()..) else { return false }; + if !display.get(..name.len()).is_some_and(|start| start.eq_ignore_ascii_case(name)) { + return false; + } + let after = rest.trim_start_matches(|c: char| !c.is_alphanumeric()); + match after.chars().next() { + // The name ends here, or a separator introduces the sheet's dimensions. + None => true, + Some(c) if !c.is_alphabetic() => after.len() < rest.len(), + // A word, directly after the name or after a separator, is a modifier: another paper. + Some(_) => false, + } } /// A driver paper offered for the job: its size in micrometres (as the driver lists it), its @@ -218,6 +231,17 @@ pub(crate) struct Paper<'a> { /// The driver paper for a sheet of `width` × `height` points (either way round): within a /// quarter of a millimetre, preferring the one named like the standard size, then a Print Schema /// keyword, then the closest; on a tie the driver's own order. Its index in `papers`. +/// +/// An entry the driver keeps only one side of (the other reads zero, a legacy host-based driver's +/// DEVMODE metric, storytold/craft-libs#7230) is a candidate of last resort (INV-4): only when no +/// full entry matched, only a Print Schema keyword entry, and only while its known side agrees +/// with the sheet, either way round. A single such entry is still refused when its own name is +/// known and contradicts the requested standard sheet (INV-4): a known conflicting name is +/// evidence against the choice, and failing closed is safer than a guess. Without a name there is +/// nothing contradicting, so the agreement alone is enough. Several such entries that all agree +/// (a Letter and a Legal option share a width) are told apart by name, as full candidates are: +/// the one named like the requested standard sheet wins, and when that leaves more than one, or +/// none, nothing is chosen — a guess here would lay the sheets out for the wrong paper. pub(crate) fn matching_paper(papers: &[Paper<'_>], width: f64, height: f64) -> Option { let (w, h) = (micrometres(width.min(height))?, micrometres(width.max(height))?); let names = paper_names(width, height); @@ -231,7 +255,33 @@ pub(crate) fn matching_paper(papers: &[Paper<'_>], width: f64, height: f64) -> O }) .collect(); candidates.sort_by_key(|(_, p, off)| (!names.iter().any(|n| names_paper(p.name, n)), !p.keyword, *off)); - candidates.first().map(|(i, _, _)| *i) + if let Some(index) = candidates.first().map(|(i, _, _)| *i) { + return Some(index); + } + let agrees = |side: u32| side.abs_diff(w) <= PAPER_TOLERANCE || side.abs_diff(h) <= PAPER_TOLERANCE; + let partials: Vec = papers + .iter() + .enumerate() + .filter(|(_, p)| p.keyword && (p.width == 0) != (p.height == 0) && (p.width == 0 || agrees(p.width)) && (p.height == 0 || agrees(p.height))) + .map(|(i, _)| i) + .collect(); + match partials.as_slice() { + [only] => { + // INV-4: a known name that contradicts the requested standard sheet is evidence + // against this single partial record — refuse it; without a name, the known side's + // agreement is all the evidence there is, and the record stands. + let contradicts = papers[*only].name.is_some_and(|name| !names.iter().any(|n| names_paper(Some(name), n))); + (!contradicts).then_some(*only) + } + // Ambiguous by agreement alone: a partial named like the requested standard sheet wins. + _ => { + let named: Vec = partials.iter().copied().filter(|i| names.iter().any(|n| names_paper(papers[*i].name, n))).collect(); + match named.as_slice() { + [only] => Some(*only), + _ => None, + } + } + } } fn named(node: roxmltree::Node<'_, '_>, local: &str) -> bool { @@ -274,12 +324,32 @@ pub(crate) fn two_sided(xml: &str) -> Option { ) } -/// The integer value of the Print Schema element under `node` with this keyword name. -fn integer(node: roxmltree::Node<'_, '_>, tag: &str, name: &str) -> Option { +/// The `Value` of the Print Schema element `element`: its own, or one a `ParameterRef` names +/// through the `ParameterInit` under the document's root that sets it (a driver may keep a paper +/// side or a resolution this way). `None` when the element has neither, or names a parameter the +/// ticket does not set. +fn resolved_value<'a>(document: &'a roxmltree::Document<'a>, element: roxmltree::Node<'a, 'a>) -> Option> { + let value = element.children().find(|v| v.has_tag_name((NS_PSF, "Value")) || v.has_tag_name((NS_PSF, "ParameterRef")))?; + if value.has_tag_name((NS_PSF, "Value")) { + return Some(value); + } + let parameter = expanded(value, value.attribute("name")?); + document + .root_element() + .children() + .filter(|p| p.has_tag_name((NS_PSF, "ParameterInit"))) + .find(|p| p.attribute("name").is_some_and(|name| expanded(*p, name) == parameter))? + .children() + .find(|v| v.has_tag_name((NS_PSF, "Value"))) +} + +/// The integer value of the Print Schema element under `node` with this keyword name, direct or +/// through a `ParameterRef` the ticket resolves ([`resolved_value`]). +fn integer(document: &roxmltree::Document<'_>, node: roxmltree::Node<'_, '_>, tag: &str, name: &str) -> Option { node.descendants() .find(|n| n.has_tag_name((NS_PSF, tag)) && named(*n, name)) - .and_then(|n| n.children().find(|v| v.has_tag_name((NS_PSF, "Value")))) - .and_then(|n| n.text()) + .and_then(|element| resolved_value(document, element)) + .and_then(|value| value.text()) .and_then(|v| v.trim().parse::().ok()) } @@ -287,14 +357,35 @@ fn integer(node: roxmltree::Node<'_, '_>, tag: &str, name: &str) -> Option /// paper (when `paper` is given, that one, to a quarter of a millimetre). Returns the paper's /// width and height in micrometres as the driver lists it. Only those schema fields are read: /// some drivers return empty vendor parameters that a stricter parser would refuse. +/// +/// A zero or unreadable side (`MediaSizeWidth` gone) is roll-like only while the request itself +/// names no sheet (INV-2): a legacy host-based driver keeps the paper metric in its DEVMODE +/// extension and reads back a zero side for a real sheet (HP LaserJet P1102, storytold/craft-libs#7230), so +/// with a sheet named the job falls back to the size the request asked for (INV-1, +/// fail-closed: every refusal here is before anything reaches the spooler). A side the driver +/// did report must agree with the sheet asked for, either way round (INV-3); both sides gone is +/// refused whatever the request names (INV-5). The fallback is logged (INV-7). pub(crate) fn verify_ticket(xml: &str, paper: Option<(u32, u32)>, landscape: bool) -> Result<(u32, u32), Error> { let document = crate::xml::parse(xml).map_err(spool_error)?; let feature = |name: &str| document.root_element().children().find(|n| n.has_tag_name((NS_PSF, "Feature")) && named(*n, name)); let media = feature("PageMediaSize").ok_or_else(|| spool_error("the printer's driver did not confirm the paper size; nothing was sent"))?; - let size = integer(media, "ScoredProperty", "MediaSizeWidth") - .zip(integer(media, "ScoredProperty", "MediaSizeHeight")) - .filter(|(w, h)| *w > 0 && *h > 0) - .ok_or_else(|| spool_error("the printer's driver gave a paper without a size (a roll?); nothing was sent"))?; + let width = integer(&document, media, "ScoredProperty", "MediaSizeWidth").filter(|w| *w > 0); + let height = integer(&document, media, "ScoredProperty", "MediaSizeHeight").filter(|h| *h > 0); + let without_a_size = spool_error("the printer's driver gave a paper without a size (a roll?); nothing was sent"); + let size = match (width, height, paper) { + (Some(w), Some(h), _) => (w, h), + (None, None, _) | (_, _, None) => return Err(without_a_size), + (Some(reported), None, Some(wanted)) | (None, Some(reported), Some(wanted)) => { + let (w, h) = wanted; + // Either way round: the one side the driver did report against the sheet's width or height. + let agrees = |other: u32| reported.abs_diff(other) <= PAPER_TOLERANCE; + if !agrees(w) && !agrees(h) { + return Err(spool_error("the printer's driver changed the paper size; nothing was sent (choose a paper size it has)")); + } + log::warn!("the printer's driver reported only {reported} µm of the paper; going on with the requested {w} × {h} µm"); + (w, h) + } + }; if let Some((w, h)) = paper { let same = |a: (u32, u32), b: (u32, u32)| a.0.abs_diff(b.0) <= PAPER_TOLERANCE && a.1.abs_diff(b.1) <= PAPER_TOLERANCE; if !same(size, (w, h)) && !same(size, (h, w)) { @@ -384,7 +475,9 @@ pub(crate) fn one_job_sides(xml: &str) -> Option { _ => return None, } } - if features(&["JobNUpAllDocumentsContiguously", "DocumentNUp"]).any(|f| integer(f, "ScoredProperty", "PagesPerSheet").is_some_and(|n| n > 1)) { + if features(&["JobNUpAllDocumentsContiguously", "DocumentNUp"]) + .any(|f| integer(&document, f, "ScoredProperty", "PagesPerSheet").is_some_and(|n| n > 1)) + { return None; } if features(&["JobBindAllDocuments", "DocumentBinding", "JobStapleAllDocuments", "DocumentStaple"]) @@ -421,7 +514,7 @@ pub(crate) fn verify_job_options(xml: &str, job: &Job, pages: usize, offered: Of }; let refused = |field: &str| spool_error(format!("the printer's driver did not retain the requested {field}; nothing was sent")); let copies = job.copies.clamp(1, 999); - if integer(root, "ParameterInit", "JobCopiesAllDocuments") != Some(copies) { + if integer(&document, root, "ParameterInit", "JobCopiesAllDocuments") != Some(copies) { return Err(refused("copy count")); } if copies > 1 && pages > 1 && !option("DocumentCollate").is_some_and(|n| named(n, if job.collate { "Collated" } else { "Uncollated" })) { @@ -497,19 +590,7 @@ fn feature_option(document: &roxmltree::Document<'_>, feature: &str) -> Option value, - reference => { - let parameter = expanded(reference, reference.attribute("name")?); - document - .root_element() - .children() - .filter(|p| p.has_tag_name((NS_PSF, "ParameterInit"))) - .find(|p| p.attribute("name").is_some_and(|name| expanded(*p, name) == parameter))? - .children() - .find(|v| v.has_tag_name((NS_PSF, "Value")))? - } - }; + let value = resolved_value(document, n)?; let text = value.text()?.trim(); let value = match text.parse::() { _ if is_qname_value(value) => expanded(value, text), @@ -766,11 +847,11 @@ pub(crate) fn prefixes_unlike_the_drivers(ticket: &str, driver: &str) -> Option< pub(crate) fn imageable_margin(capabilities_xml: &str) -> Option { let document = crate::xml::parse(capabilities_xml).ok()?; let imageable = document.root_element().children().find(|n| n.has_tag_name((NS_PSF, "Property")) && named(*n, "PageImageableSize"))?; - let width = integer(imageable, "Property", "ImageableSizeWidth")?; - let height = integer(imageable, "Property", "ImageableSizeHeight")?; + let width = integer(&document, imageable, "Property", "ImageableSizeWidth")?; + let height = integer(&document, imageable, "Property", "ImageableSizeHeight")?; let area = imageable.children().find(|n| n.has_tag_name((NS_PSF, "Property")) && named(*n, "ImageableArea"))?; - let (x, y) = (integer(area, "Property", "OriginWidth")?, integer(area, "Property", "OriginHeight")?); - let (w, h) = (integer(area, "Property", "ExtentWidth")?, integer(area, "Property", "ExtentHeight")?); + let (x, y) = (integer(&document, area, "Property", "OriginWidth")?, integer(&document, area, "Property", "OriginHeight")?); + let (w, h) = (integer(&document, area, "Property", "ExtentWidth")?, integer(&document, area, "Property", "ExtentHeight")?); let right = width.checked_sub(x)?.checked_sub(w)?; let bottom = height.checked_sub(y)?.checked_sub(h)?; Some(x.max(y).max(right).max(bottom)) @@ -1058,6 +1139,89 @@ mod tests { assert!(verify_ticket(", height: Option) -> String { + let side = |name: &str, value: Option| { + value.map_or(String::new(), |v| { + format!(r#"{v}"#) + }) + }; + format!( + r#" +{}{} + + +"#, + side("MediaSizeWidth", width), + side("MediaSizeHeight", height) + ) + } + + /// storytold/craft-libs#7230: the driver reports one side of the paper as zero for a real + /// sheet. With the sheet the request names, the job goes on with the size it asked for; a + /// side the driver did report must still agree with it, and a request that names no sheet is + /// refused as before. + #[test] + fn a_legacy_v3_drivers_zero_side_falls_back_to_the_requested_paper() { + let a4 = (210_000, 297_000); + assert_eq!(verify_ticket(&validated_partial(None, Some(297_000)), Some(a4), false), Ok(a4)); + assert_eq!(verify_ticket(&validated_partial(Some(0), Some(297_000)), Some(a4), false), Ok(a4), "a zero side is a missing one"); + assert_eq!(verify_ticket(&validated_partial(Some(297_000), None), Some(a4), false), Ok(a4), "either way round"); + // 0.5 pt (176 µm) inside the tolerance still agrees. + assert_eq!(verify_ticket(&validated_partial(Some(0), Some(297_176)), Some(a4), false), Ok(a4)); + assert_eq!(verify_ticket(&validated_partial(Some(0), Some(297_250)), Some(a4), false), Ok(a4), "exactly the tolerance"); + // A side that disagrees means the driver changed the paper, not that it has a roll. + let changed = verify_ticket(&validated_partial(Some(0), Some(279_400)), Some(a4), false).unwrap_err(); + assert!(changed.to_string().contains("changed the paper size"), "{changed}"); + assert!(!changed.to_string().contains("roll"), "{changed}"); + assert!(verify_ticket(&validated_partial(Some(0), Some(297_251)), Some(a4), false).is_err(), "one µm past the tolerance"); + // Without a sheet named the zero side stays roll-like. + assert!(verify_ticket(&validated_partial(Some(0), Some(297_000)), None, false).is_err()); + assert!(verify_ticket(&validated_partial(None, Some(297_000)), None, false).is_err()); + // Both sides gone is refused whatever the request names. + for wanted in [Some(a4), None] { + for (width, height) in [(None, None), (Some(0), Some(0))] { + let refused = verify_ticket(&validated_partial(width, height), wanted, false).unwrap_err(); + assert!(refused.to_string().contains("roll"), "{refused}"); + } + } + } + + /// A validated ticket whose width the option refers to a parameter the ticket sets + /// (`MediaSizeWidth` a `ParameterRef`, storytold/craft-libs#1225), as a driver writes one. + fn validated_referenced(width: u32, height: u32) -> String { + format!( + r#" +{width} + + +{height} + + + +"# + ) + } + + /// A paper side a driver keeps in a `ParameterRef` is resolved through the `ParameterInit` + /// the ticket sets, and checked like a direct one: a referenced width that disagrees with the + /// sheet the request names is refused, one that agrees is taken. + #[test] + fn a_referenced_paper_side_is_resolved_through_the_parameter_it_names() { + let a4 = (210_000, 297_000); + assert_eq!(verify_ticket(&validated_referenced(210_000, 297_000), Some(a4), false), Ok(a4)); + assert_eq!(verify_ticket(&validated_referenced(209_750, 297_000), Some(a4), false), Ok((209_750, 297_000)), "exactly the tolerance"); + // A referenced size that disagrees is refused, not rewritten from the request. + let changed = verify_ticket(&validated_referenced(400_000, 297_000), Some(a4), false).unwrap_err(); + assert!(changed.to_string().contains("changed the paper size"), "{changed}"); + // A reference to a parameter the ticket does not set is a missing side, which falls back + // to the requested paper like any unreadable one. + let unset = + validated_referenced(400_000, 297_000).replace(r#"name="psk:PageMediaSizeWidth"> Some((m.size().width_in_micron(), m.size().height_in_micron())), + (Some(m), _) => { + let size = (m.size().width_in_micron(), m.size().height_in_micron()); + // INV-6: a driver that keeps one side of the paper metric in its DEVMODE extension + // (storytold/craft-libs#7230) reports a zero side; the wanted size then is the sheet + // the job asked for, not the zero record, which would poison the cross-check and hand + // `sheets::plan` a width of zero. + if size.0 == 0 || size.1 == 0 { + match (ticket::micrometres(sheet.0), ticket::micrometres(sheet.1)) { + (Some(w), Some(h)) => Some((w, h)), + _ => { + return Err(spool_error(format!( + "{} has no {:.2} × {:.2} inch paper; choose a paper size it has, or add this one in its driver", + device.name(), + sheet.0 / 72.0, + sheet.1 / 72.0 + ))); + } + } + } else { + Some(size) + } + } (None, Scaling::ShrinkToFit) => None, (None, Scaling::Exact) => { return Err(spool_error(format!( diff --git a/crates/psd/src/io.rs b/crates/psd/src/io.rs index d8aa4b51d..8ca8353b9 100644 --- a/crates/psd/src/io.rs +++ b/crates/psd/src/io.rs @@ -440,6 +440,18 @@ pub(crate) fn write_unicode_units(out: &mut Vec, units: &[u16]) { #[cfg(test)] mod tests { + /// A section length that does not fit the file's 32-bit field is refused, not truncated: + /// this is what keeps every `as u32` on a length inside a serialized file from ever wrapping, + /// and it is why an over-large PSD fails the write instead of producing a short length. + #[test] + fn a_length_that_does_not_fit_32_bits_is_refused() { + let mut out = Vec::new(); + assert!(out.put_len(u64::from(u32::MAX), false).is_ok(), "the largest 32-bit length fits"); + assert!(matches!(out.put_len(u64::from(u32::MAX) + 1, false), Err(PsdError::LimitExceeded(_)))); + // A PSB length field is 64 bits, so the same value is written there. + assert!(out.put_len(u64::from(u32::MAX) + 1, true).is_ok(), "PSB has room for it"); + } + use super::*; #[test] diff --git a/crates/stream-codecs/src/ccitt.rs b/crates/stream-codecs/src/ccitt.rs index 2733cf98b..c5a213cea 100644 --- a/crates/stream-codecs/src/ccitt.rs +++ b/crates/stream-codecs/src/ccitt.rs @@ -152,6 +152,76 @@ fn starts_with_end_of_line(data: &[u8]) -> bool { /// Rows of 1-bit samples filled from a decoder's pixels: `columns` a row, each row on a new byte, at /// most `rows` rows. Pixels past a row's end and rows past the last are dropped. +/// Reason a raw CCITT fax decode stopped or failed. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum CcittErrorKind { + UnexpectedEof, + Overflow, + InvalidData, +} + +impl std::fmt::Display for CcittErrorKind { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::UnexpectedEof => f.write_str("unexpected end of CCITT stream"), + Self::Overflow => f.write_str("CCITT stream overflow"), + Self::InvalidData => f.write_str("invalid CCITT data"), + } + } +} + +/// Result of [`decode_ccitt_raw`], including any partial rows decoded before an error. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct CcittRawOutput { + pub data: Vec, + pub rows_decoded: u32, + pub bytes_consumed: usize, + pub error: Option, +} + +/// Decodes CCITT Group 3 or Group 4 fax `data` with caller-specified `params` and `max_rows`, +/// returning packed MSB-first rows, rows decoded, input bytes consumed, and any decode error. +pub fn decode_ccitt_raw(data: &[u8], p: &CcittParams, max_rows: u32) -> Result { + if p.columns <= 0 || p.columns > i64::from(MAX_CCITT_COLUMNS) { + return Err(CodecError::Corrupt { filter: "CCITTFaxDecode", detail: format!("CCITT columns {} outside 1..={MAX_CCITT_COLUMNS}", p.columns) }); + } + let columns = u32::try_from(p.columns).map_err(|_| CodecError::Corrupt { filter: "CCITTFaxDecode", detail: "invalid CCITT columns".into() })?; + let rows = if p.rows > 0 { u32::try_from(p.rows).unwrap_or(max_rows).min(max_rows) } else { max_rows }; + let settings = hayro_ccitt::DecodeSettings { + columns, + rows, + end_of_block: p.end_of_block, + end_of_line: p.end_of_line, + rows_are_byte_aligned: p.encoded_byte_align && (p.k < 0 || !starts_with_end_of_line(data)), + encoding: match p.k { + ..0 => hayro_ccitt::EncodingMode::Group4, + 0 => hayro_ccitt::EncodingMode::Group3_1D, + k => hayro_ccitt::EncodingMode::Group3_2D { k: u32::try_from(k).unwrap_or(u32::MAX) }, + }, + invert_black: false, + }; + let mut out = Rows::new(columns as usize, rows as usize, p.black_is_1); + let (bytes_consumed, error) = match hayro_ccitt::decode(data, &mut out, &mut hayro_ccitt::DecoderContext::new(settings)) { + Ok(used) => (used, None), + Err(e) => { + let kind = match e { + hayro_ccitt::DecodeError::UnexpectedEof => CcittErrorKind::UnexpectedEof, + hayro_ccitt::DecodeError::Overflow => CcittErrorKind::Overflow, + _ => CcittErrorKind::InvalidData, + }; + let used = if out.done > 0 { + let mut drop_rows = Rows::new(columns as usize, out.done, p.black_is_1); + let retry = hayro_ccitt::DecodeSettings { rows: u32::try_from(out.done).unwrap_or(u32::MAX), ..settings }; + hayro_ccitt::decode(data, &mut drop_rows, &mut hayro_ccitt::DecoderContext::new(retry)).unwrap_or(data.len()) + } else { + 0 + }; + (used, Some(kind)) + } + }; + Ok(CcittRawOutput { data: out.out, rows_decoded: u32::try_from(out.done).unwrap_or(u32::MAX), bytes_consumed, error }) +} + pub(crate) struct Rows { pub(crate) out: Vec, row: Vec, diff --git a/crates/stream-codecs/src/lib.rs b/crates/stream-codecs/src/lib.rs index d129cacaa..ee275b2ce 100644 --- a/crates/stream-codecs/src/lib.rs +++ b/crates/stream-codecs/src/lib.rs @@ -42,7 +42,7 @@ mod mq; pub mod predictor; pub mod runlength; -pub use ccitt::{CcittParams, MAX_CCITT_COLUMNS, MAX_G4_COLUMNS, decode_ccitt_within, encode_g4}; +pub use ccitt::{CcittErrorKind, CcittParams, CcittRawOutput, MAX_CCITT_COLUMNS, MAX_G4_COLUMNS, decode_ccitt_raw, decode_ccitt_within, encode_g4}; pub use jbig2::{MAX_JBIG2_COLUMNS, MAX_JBIG2_PIXELS, SharedJbig2, decode_jbig2_within, encode_jbig2, encode_jbig2_generic, encode_jbig2_shared}; /// Parameters of the predictors and of LZW, as a PDF `/DecodeParms` dictionary gives them diff --git a/crates/svg/src/tree/draw.rs b/crates/svg/src/tree/draw.rs index a7bdfd80f..f83653284 100644 --- a/crates/svg/src/tree/draw.rs +++ b/crates/svg/src/tree/draw.rs @@ -24,6 +24,8 @@ use std::collections::HashMap; use usvg::Transform; +#[cfg(feature = "tree-render")] +pub use resvg; #[cfg(feature = "tree-render")] pub use resvg::tiny_skia; diff --git a/crates/texture/Cargo.toml b/crates/texture/Cargo.toml new file mode 100644 index 000000000..78868dd87 --- /dev/null +++ b/crates/texture/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "craft-texture" +description = "Bounded linear float image planes and texture sampling" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[features] +default = [] +parallel = ["dep:rayon"] + +[lints] +workspace = true + +[dependencies] +rayon = { workspace = true, optional = true } +thiserror = { workspace = true } diff --git a/crates/texture/README.md b/crates/texture/README.md new file mode 100644 index 000000000..a41b42666 --- /dev/null +++ b/crates/texture/README.md @@ -0,0 +1,77 @@ +# craft-texture + +Linear float image planes and texture sampling. An `Image` is a gray (1 channel) or RGBA plane of +`f32`, row-major with the top row first, shared copy-on-write through an `Arc`. Values are stored +as given, so HDR values above 1 and negative values are kept. The crate has no UI, does no file or +network I/O and has no `unsafe` code. + +- **Construction.** `try_filled`, `try_generate`, `try_generate_uv` and `from_data` check the size + against `Limits` (longest side, pixel count, bytes) before allocating and return an + `ImageError`. `from_data` also checks that the buffer holds width × height × channels floats. + `filled`, `generate`, `generate_uv` and `gray` clamp each side to `1..=MAX_SIDE` (8192). + A plane is never larger than `isize::MAX` bytes (2 GiB minus one byte on `wasm32`), even with + raised limits: the `try_` forms return `ImageError::TooLarge` for a larger one, and `map`, + `to_color` and `to_kind` clamp each side to `1..=MAX_SIDE` and map the top-left part instead. + `try_map`, `try_to_kind`, `try_resize` and `try_to_rgba8_display` reserve their pixel buffer + first and return `ImageError::OutOfMemory` when that reservation fails. They still make small + allocations that are not fallible: the `Arc` around a new image's buffer, and the boxed + resampler of `try_resize`. The forms without `try_` allocate like `Vec`. A failed infallible + allocation ends the process. +- **Sampling.** `sample` (bilinear) and `sample_nearest` take normalized `(u, v)` with `(0, 0)` at + the upper-left corner and `v` increasing downward, as glTF 2.0 section 3.8.3 defines them + (). `tile` picks repeat (glTF + `REPEAT`) or clamp to edge (`CLAMP_TO_EDGE`) for both axes. The texel coordinate, the wrapping + and the bilinear weights follow the Vulkan specification, sections 20.4.1, 20.6.1 and 20.6.3 + (). + Repeat keeps its period at any finite coordinate. A NaN or infinite coordinate gives + `[0, 0, 0, 1]`. Bilinear sums are taken in `f64` and clamped to the `f32` range, so finite + texels give a finite sample. +- **Other operations.** `get` reads one texel, `map`, `to_gray`, `to_color` and `to_kind` convert, + `resize` box-averages whole-factor shrinks and samples bilinearly otherwise, and + `to_rgba8_display` encodes 8-bit sRGB RGBA. `linear_to_srgb`, `srgb_to_linear` and `luminance` + (Rec. 709) are scalar helpers. `resource_key` and `split_resource_key` tag a bitmap name with + its color space. + +Mirrored repeat, mip chains, height to normal, channel packing, decoding and texture-set +recognition are not in the crate yet. + +## Cargo features + +No default features. + +| Feature | Provides | Pulls in | +|---|---|---| +| `parallel` | generated images and display conversion fill rows on several threads; the results are the same | `rayon` | + +Without `parallel` the crate builds for `wasm32-unknown-unknown`. + +## Limits + +| Field | Default | Checked by | +|---|---|---| +| `max_side` | 8192 | every `try_` constructor and `from_data` | +| `max_pixels` | 2^26 (8192 × 8192) | the same | +| `max_bytes` | 1 GiB (an 8192 × 8192 RGBA plane) | the same | + +## Origin + +| Part | Source | +|---|---| +| `Image`, `Kind`, sampling, resizing, conversions, sRGB helpers, resource keys and the six tests `sample_survives_huge_coordinates`, `resource_keys_round_trip`, `sample_wraps_and_clamps`, `resize_box_and_bilinear`, `hostile_sizes_are_clamped`, `conversions` | MaterialCraft `crates/image/src/lib.rs` at [3133062](https://github.com/storytold/materialcraft/commit/3133062968b8a1566eda12a357cb06062db16687) | +| `Limits`, the `try_` constructors, `from_data`, private fields with getters, the `parallel` feature | written here | + +## Migration + +| App | Status | +|---|---| +| MaterialCraft | `materialcraft-image` is the source. The app switches to this crate in its own pull request. | +| EffectCraft | Its texture sampling moves into this crate later, with mirrored repeat. | + +For MaterialCraft: + +| `materialcraft-image` | craft-texture | +| --- | --- | +| `image.w`, `image.h`, `image.kind`, `image.data` | `width()`, `height()`, `kind()`, `data()` and `shared_data()` | +| `Image { w, h, kind, data }` built by hand | `Image::from_data(w, h, kind, data, &Limits::default())` | +| `rayon` always on | the `parallel` feature | +| `linear_to_srgb(NaN)` returned −0.055 | it returns NaN | diff --git a/crates/texture/src/lib.rs b/crates/texture/src/lib.rs new file mode 100644 index 000000000..2aaef3e8b --- /dev/null +++ b/crates/texture/src/lib.rs @@ -0,0 +1,895 @@ +//! # craft-texture +//! +//! Linear float image planes and texture sampling. An [`Image`] is a grayscale (1 channel) or +//! color (RGBA) plane of `f32`, row-major with the top row first, shared copy-on-write through an +//! `Arc` so a copy costs a reference count. Values are stored as given: HDR values above 1, +//! negative values and non-finite values are kept. +//! +//! Sampling takes normalized `(u, v)` with `(0, 0)` at the upper-left corner and `v` increasing +//! downward, as glTF 2.0 section 3.8.3 defines texture coordinates +//! (). The `tile` argument picks the +//! wrap mode of both axes: repeat (glTF `REPEAT`, 10497) or clamp to edge (`CLAMP_TO_EDGE`, 33071). +//! [`Image::sample_nearest`] and [`Image::sample`] (bilinear) use the texel coordinates, wrapping +//! and weights of the Vulkan specification, sections 20.4.1, 20.6.1 and 20.6.3 +//! (). +//! +//! Sizes are bounded. [`Limits`] caps the side, the pixel count and the bytes, and the `try_` +//! constructors return an [`ImageError`] before allocating anything over them. A plane is also +//! never larger than `isize::MAX` bytes, the most a `Vec` can hold on the target: `isize::MAX` is +//! 2 GiB minus one byte on 32-bit targets such as `wasm32`. The `try_` forms return +//! [`ImageError::TooLarge`] for a plane over that bound, also when the caller raised the limits +//! past it. The constructors without `try_` clamp each side to `1..=MAX_SIDE`, and [`Image::map`] +//! clamps the same way when its result would be over the bound. Every operation that allocates a +//! pixel buffer has a `try_` form that reserves that buffer first and returns +//! [`ImageError::OutOfMemory`] when the reservation fails. The `try_` forms still make small +//! allocations that are not fallible: the `Arc` that holds a new image's buffer, and the boxed +//! resampler of [`Image::try_resize`]. The forms without `try_` allocate like `Vec`. A failed +//! infallible allocation ends the process. Rows are generated on several threads only with the +//! `parallel` feature. +#![forbid(unsafe_code)] +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +use std::sync::Arc; + +#[cfg(feature = "parallel")] +use rayon::prelude::*; + +/// Largest side the clamping constructors make, and the default [`Limits::max_side`]. +pub const MAX_SIDE: u32 = 8192; + +/// Whether an image holds one channel or four. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum Kind { + Gray, + Color, +} + +impl Kind { + pub fn channels(self) -> usize { + match self { + Kind::Gray => 1, + Kind::Color => 4, + } + } +} + +/// Why an image could not be made. +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +#[non_exhaustive] +pub enum ImageError { + /// A side is zero or longer than [`Limits::max_side`]. + #[error("image size {0}x{1} has a zero side or a side over the limit")] + BadSize(u32, u32), + /// The pixel count or the byte count is over [`Limits`], or the byte count is over + /// `isize::MAX`, the largest allocation the target allows. + #[error("image size {0}x{1} needs more pixels or bytes than the limits or the target allow")] + TooLarge(u32, u32), + /// The float buffer does not hold width × height × channels values. + #[error("image data holds {found} floats where {expected} are needed")] + BadDataLength { expected: usize, found: usize }, + /// Memory for the pixels could not be reserved. + #[error("not enough memory for a {0}x{1} image")] + OutOfMemory(u32, u32), +} + +/// Caps on the size of an image, checked before anything is allocated. +/// `#[non_exhaustive]`: start from [`Limits::default`] and set the fields you change. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[non_exhaustive] +pub struct Limits { + /// Longest side in pixels. Default [`MAX_SIDE`]. + pub max_side: u32, + /// Most pixels (width × height). Default 2^26, which is 8192 × 8192. + pub max_pixels: u64, + /// Most bytes of float data (pixels × channels × 4). Default 1 GiB, an 8192 × 8192 color plane. + pub max_bytes: u64, +} + +impl Default for Limits { + fn default() -> Self { + Self { max_side: MAX_SIDE, max_pixels: 1 << 26, max_bytes: 1 << 30 } + } +} + +/// The largest allocation in bytes that a `Vec` can make on the target. +const MAX_ALLOC_BYTES: u64 = isize::MAX as u64; + +/// Bytes of float data in a `w × h` image of `kind`, or `None` when the count overflows `u64`. +fn checked_byte_len(w: u32, h: u32, kind: Kind) -> Option { + u64::from(w).checked_mul(u64::from(h))?.checked_mul(kind.channels() as u64)?.checked_mul(size_of::() as u64) +} + +impl Limits { + /// The number of floats a `w × h` image of `kind` holds, or the error the limits give for that + /// size. A size whose bytes are over `isize::MAX` is [`ImageError::TooLarge`] whatever the + /// limits are. Nothing is allocated. + pub fn check(&self, w: u32, h: u32, kind: Kind) -> Result { + self.check_within(w, h, kind, MAX_ALLOC_BYTES) + } + + /// [`Limits::check`] with `max_alloc_bytes` as the largest allocation the target allows. + fn check_within(&self, w: u32, h: u32, kind: Kind, max_alloc_bytes: u64) -> Result { + if w == 0 || h == 0 || w > self.max_side || h > self.max_side { + return Err(ImageError::BadSize(w, h)); + } + let too_large = ImageError::TooLarge(w, h); + let pixels = u64::from(w).checked_mul(u64::from(h)).ok_or_else(|| too_large.clone())?; + let floats = pixels.checked_mul(kind.channels() as u64).ok_or_else(|| too_large.clone())?; + let bytes = floats.checked_mul(size_of::() as u64).ok_or_else(|| too_large.clone())?; + if pixels > self.max_pixels || bytes > self.max_bytes || bytes > max_alloc_bytes { + return Err(too_large); + } + usize::try_from(floats).map_err(|_| too_large) + } +} + +/// A gray or RGBA plane of linear `f32`. Width and height are at least 1, and the data holds +/// exactly width × height × channels floats. +#[derive(Clone, Debug)] +pub struct Image { + w: u32, + h: u32, + kind: Kind, + /// Row-major, `kind.channels()` floats per pixel. + data: Arc>, +} + +/// Images are equal when their size, kind and floats are equal. Floats compare as `f32` does, so +/// an image holding NaN is unequal to every image, its clones included. +impl PartialEq for Image { + fn eq(&self, o: &Image) -> bool { + self.w == o.w && self.h == o.h && self.kind == o.kind && self.data == o.data + } +} + +/// Clamp a requested size into `1..=MAX_SIDE` on each side. +pub fn clamp_size(w: u32, h: u32) -> (u32, u32) { + (w.clamp(1, MAX_SIDE), h.clamp(1, MAX_SIDE)) +} + +/// The size and float count of an image of `kind` requested at `w × h` without a limit check: +/// `w × h` when its bytes fit in `max_alloc_bytes`, otherwise each side clamped to +/// `1..=MAX_SIDE`, and 1 × 1 when even the clamped size does not fit. Nothing is allocated. +fn bounded_size(w: u32, h: u32, kind: Kind, max_alloc_bytes: u64) -> (u32, u32, usize) { + let floats = |w: u32, h: u32| { + let bytes = checked_byte_len(w, h, kind).filter(|&bytes| bytes <= max_alloc_bytes)?; + usize::try_from(bytes / size_of::() as u64).ok() + }; + if let Some(len) = floats(w, h) { + return (w, h, len); + } + let (w, h) = clamp_size(w, h); + match floats(w, h) { + Some(len) => (w, h, len), + None => (1, 1, kind.channels()), + } +} + +/// Integer texel index `i` on an axis of `n >= 1` texels: `i mod n` when repeating, otherwise +/// clamped to `0..=n - 1`. +#[inline] +fn wrap(i: i64, n: u32, tile: bool) -> usize { + let n = i64::from(n.max(1)); + if tile { i.rem_euclid(n) as usize } else { i.clamp(0, n - 1) as usize } +} + +/// The largest `f32` below 1. +const BELOW_ONE: f32 = 1.0 - f32::EPSILON / 2.0; + +/// A normalized coordinate brought into a range whose texel indices fit in `i64`: its fractional +/// part, below 1, when repeating, since the repeat period is 1, and `-1..=2` when clamping, where +/// every value outside `0..=1` already selects an edge texel. `None` for NaN and infinity. +#[inline] +fn reduce(t: f32, tile: bool) -> Option { + if !t.is_finite() { + return None; + } + Some(if tile { t.rem_euclid(1.0).min(BELOW_ONE) } else { t.clamp(-1.0, 2.0) }) +} + +/// Fill `data` row by row with `f(x, y)`, keeping the first `c` channels of each result. +fn fill [f32; 4] + Sync>(data: &mut [f32], w: u32, c: usize, f: &F) { + let row_len = (w as usize).max(1) * c; + let fill_row = |(y, row): (usize, &mut [f32])| { + for (x, px) in row.chunks_exact_mut(c).enumerate() { + let p = f(x as u32, y as u32); + px.copy_from_slice(&p[..c]); + } + }; + #[cfg(feature = "parallel")] + data.par_chunks_mut(row_len).enumerate().for_each(fill_row); + #[cfg(not(feature = "parallel"))] + data.chunks_mut(row_len).enumerate().for_each(fill_row); +} + +impl Image { + /// An image of `w × h`, or of the clamped size [`bounded_size`] gives when its bytes would be + /// over `isize::MAX`. `f` is called only for pixels inside the size that was made. + fn build [f32; 4] + Sync>(w: u32, h: u32, kind: Kind, f: F) -> Image { + let (w, h, len) = bounded_size(w, h, kind, MAX_ALLOC_BYTES); + let mut data = vec![0.0f32; len]; + fill(&mut data, w, kind.channels(), &f); + Image { w, h, kind, data: Arc::new(data) } + } + + /// An image checked against `limits`, with its memory reserved before any pixel is generated. + fn try_build [f32; 4] + Sync>(w: u32, h: u32, kind: Kind, limits: &Limits, f: F) -> Result { + let len = limits.check(w, h, kind)?; + let mut data = Vec::new(); + data.try_reserve_exact(len).map_err(|_| ImageError::OutOfMemory(w, h))?; + data.resize(len, 0.0); + fill(&mut data, w, kind.channels(), &f); + Ok(Image { w, h, kind, data: Arc::new(data) }) + } + + /// A constant image, each side clamped to `1..=MAX_SIDE`. + pub fn filled(w: u32, h: u32, kind: Kind, v: [f32; 4]) -> Image { + let (w, h) = clamp_size(w, h); + Image::build(w, h, kind, |_, _| v) + } + + /// A constant image, or an error when the size is outside `limits` or memory runs out. + pub fn try_filled(w: u32, h: u32, kind: Kind, v: [f32; 4], limits: &Limits) -> Result { + Image::try_build(w, h, kind, limits, |_, _| v) + } + + /// Generate every pixel with `f(x, y)` (pixel indices), each side clamped to `1..=MAX_SIDE`. + pub fn generate(w: u32, h: u32, kind: Kind, f: impl Fn(u32, u32) -> [f32; 4] + Sync) -> Image { + let (w, h) = clamp_size(w, h); + Image::build(w, h, kind, f) + } + + /// Generate every pixel with `f(x, y)`, or return an error when the size is outside `limits` + /// or memory runs out. + pub fn try_generate(w: u32, h: u32, kind: Kind, limits: &Limits, f: impl Fn(u32, u32) -> [f32; 4] + Sync) -> Result { + Image::try_build(w, h, kind, limits, f) + } + + /// Generate from normalized coordinates at the pixel centers, `(x + 0.5) / w` and + /// `(y + 0.5) / h`, each side clamped to `1..=MAX_SIDE` first. + pub fn generate_uv(w: u32, h: u32, kind: Kind, f: impl Fn(f32, f32) -> [f32; 4] + Sync) -> Image { + let (w, h) = clamp_size(w, h); + let (iw, ih) = (1.0 / w as f32, 1.0 / h as f32); + Image::build(w, h, kind, |x, y| f((x as f32 + 0.5) * iw, (y as f32 + 0.5) * ih)) + } + + /// [`Image::generate_uv`] for a size checked against `limits`. + pub fn try_generate_uv(w: u32, h: u32, kind: Kind, limits: &Limits, f: impl Fn(f32, f32) -> [f32; 4] + Sync) -> Result { + let (iw, ih) = (1.0 / w.max(1) as f32, 1.0 / h.max(1) as f32); + Image::try_build(w, h, kind, limits, |x, y| f((x as f32 + 0.5) * iw, (y as f32 + 0.5) * ih)) + } + + /// A gray image generated with `f(x, y)`, each side clamped to `1..=MAX_SIDE`. + pub fn gray(w: u32, h: u32, f: impl Fn(u32, u32) -> f32 + Sync) -> Image { + Image::generate(w, h, Kind::Gray, |x, y| [f(x, y), 0.0, 0.0, 0.0]) + } + + /// An image over existing row-major data, checked against `limits` and against the length + /// width × height × channels. The data is not copied. + pub fn from_data(w: u32, h: u32, kind: Kind, data: impl Into>>, limits: &Limits) -> Result { + let expected = limits.check(w, h, kind)?; + let data = data.into(); + if data.len() != expected { + return Err(ImageError::BadDataLength { expected, found: data.len() }); + } + Ok(Image { w, h, kind, data }) + } + + pub fn width(&self) -> u32 { + self.w + } + + pub fn height(&self) -> u32 { + self.h + } + + pub fn kind(&self) -> Kind { + self.kind + } + + /// The row-major floats, `channels()` per pixel. + pub fn data(&self) -> &[f32] { + self.data.as_slice() + } + + /// The shared buffer behind [`Image::data`]. + pub fn shared_data(&self) -> &Arc> { + &self.data + } + + pub fn channels(&self) -> usize { + self.kind.channels() + } + + pub fn is_gray(&self) -> bool { + self.kind == Kind::Gray + } + + /// The pixel at integer coordinates (wrapped or clamped), as RGBA (gray → `[v, v, v, 1]`). + #[inline] + pub fn get(&self, x: i64, y: i64, tile: bool) -> [f32; 4] { + let (x, y) = (wrap(x, self.w, tile), wrap(y, self.h, tile)); + let i = y * self.w as usize + x; + match self.kind { + Kind::Gray => { + let v = self.data.get(i).copied().unwrap_or(0.0); + [v, v, v, 1.0] + } + Kind::Color => { + let s = self.data.get(i * 4..i * 4 + 4).unwrap_or(&[0.0, 0.0, 0.0, 1.0]); + [s[0], s[1], s[2], s[3]] + } + } + } + + /// The gray value, or the Rec. 709 luminance of a color image. + #[inline] + pub fn get_gray(&self, x: i64, y: i64, tile: bool) -> f32 { + let p = self.get(x, y, tile); + match self.kind { + Kind::Gray => p[0], + Kind::Color => luminance(p), + } + } + + /// Bilinear sample at normalized `(u, v)`; pixel centers sit at `(i + 0.5) / w`. The texel + /// coordinate is `u × w − 0.5`; its floor and the next index are wrapped or clamped, and the + /// four texels are weighted `(1 − α)(1 − β)`, `α(1 − β)`, `(1 − α)β` and `αβ` by the fractional + /// parts `α` and `β`. A texel with weight 0 does not enter the sum, so an infinite neighbor + /// leaves a sample at a texel center unchanged. NaN or infinite coordinates give `[0, 0, 0, 1]`. + #[inline] + pub fn sample(&self, u: f32, v: f32, tile: bool) -> [f32; 4] { + let (Some(u), Some(v)) = (reduce(u, tile), reduce(v, tile)) else { + return [0.0, 0.0, 0.0, 1.0]; + }; + let fx = u * self.w as f32 - 0.5; + let fy = v * self.h as f32 - 0.5; + let (x0, y0) = (fx.floor(), fy.floor()); + let (tx, ty) = (fx - x0, fy - y0); + let (x0, y0) = (x0 as i64, y0 as i64); + let taps = [((1.0 - tx) * (1.0 - ty), x0, y0), (tx * (1.0 - ty), x0 + 1, y0), ((1.0 - tx) * ty, x0, y0 + 1), (tx * ty, x0 + 1, y0 + 1)]; + // Summed in f64: the rounded weights can add up to slightly more than 1, which would + // overflow a finite HDR value near `f32::MAX` in f32. A finite sum is clamped to the f32 + // range; an infinite texel still gives an infinite or NaN sample. + let mut o = [0.0f64; 4]; + for (weight, x, y) in taps { + if weight == 0.0 { + continue; + } + let p = self.get(x, y, tile); + for (o, p) in o.iter_mut().zip(p) { + *o += f64::from(weight) * f64::from(p); + } + } + o.map(|v| if v.is_finite() { v.clamp(f64::from(f32::MIN), f64::from(f32::MAX)) as f32 } else { v as f32 }) + } + + #[inline] + pub fn sample_gray(&self, u: f32, v: f32, tile: bool) -> f32 { + let p = self.sample(u, v, tile); + match self.kind { + Kind::Gray => p[0], + Kind::Color => luminance(p), + } + } + + /// Nearest sample at normalized `(u, v)`: the texel `floor(u × w)`, wrapped or clamped. NaN or + /// infinite coordinates give `[0, 0, 0, 1]`. + #[inline] + pub fn sample_nearest(&self, u: f32, v: f32, tile: bool) -> [f32; 4] { + let (Some(u), Some(v)) = (reduce(u, tile), reduce(v, tile)) else { + return [0.0, 0.0, 0.0, 1.0]; + }; + let (x, y) = ((u * self.w as f32).floor() as i64, (v * self.h as f32).floor() as i64); + // A repeated coordinate is below 1, so its texel is below the size even where `u × w` + // rounds up to it. + let (x, y) = if tile { (x.min(i64::from(self.w) - 1), y.min(i64::from(self.h) - 1)) } else { (x, y) }; + self.get(x, y, tile) + } + + /// Map every pixel (as RGBA) to a new image of `kind`, same size. When a result of that size + /// would be over `isize::MAX` bytes, which only an image allowed by raised [`Limits`] can + /// reach, each side is clamped to `1..=MAX_SIDE` and the result maps the top-left part of this + /// image. [`Image::try_map`] returns [`ImageError::TooLarge`] instead. + pub fn map(&self, kind: Kind, f: impl Fn([f32; 4]) -> [f32; 4] + Sync) -> Image { + Image::build(self.w, self.h, kind, |x, y| f(self.get(i64::from(x), i64::from(y), false))) + } + + /// [`Image::map`] with the new image checked against `limits` and its memory reserved first. + pub fn try_map(&self, kind: Kind, limits: &Limits, f: impl Fn([f32; 4]) -> [f32; 4] + Sync) -> Result { + Image::try_build(self.w, self.h, kind, limits, |x, y| f(self.get(i64::from(x), i64::from(y), false))) + } + + /// [`Image::to_kind`] with the result checked against `limits`, also when the kind is + /// unchanged, and the memory of a new image reserved first. + pub fn try_to_kind(&self, kind: Kind, limits: &Limits) -> Result { + limits.check(self.w, self.h, kind)?; + match (self.kind, kind) { + (Kind::Gray, Kind::Gray) | (Kind::Color, Kind::Color) => Ok(self.clone()), + (Kind::Color, Kind::Gray) => self.try_map(Kind::Gray, limits, |p| [luminance(p), 0.0, 0.0, 0.0]), + (Kind::Gray, Kind::Color) => self.try_map(Kind::Color, limits, |p| p), + } + } + + /// This image as grayscale (luminance of a color image), same size. + pub fn to_gray(&self) -> Image { + match self.kind { + Kind::Gray => self.clone(), + Kind::Color => self.map(Kind::Gray, |p| [luminance(p), 0.0, 0.0, 0.0]), + } + } + + /// This image as RGBA (gray copied to RGB, alpha 1). A gray image goes through + /// [`Image::map`], so a result over `isize::MAX` bytes is clamped as `map` describes. + pub fn to_color(&self) -> Image { + match self.kind { + Kind::Color => self.clone(), + Kind::Gray => self.map(Kind::Color, |p| p), + } + } + + /// [`Image::to_gray`] or [`Image::to_color`]. + pub fn to_kind(&self, kind: Kind) -> Image { + match kind { + Kind::Gray => self.to_gray(), + Kind::Color => self.to_color(), + } + } + + /// Resample to `w × h`, each side clamped to `1..=MAX_SIDE`: a box average when both sides + /// shrink by whole factors, otherwise bilinear samples at the new pixel centers. + pub fn resize(&self, w: u32, h: u32, tile: bool) -> Image { + let (w, h) = clamp_size(w, h); + if w == self.w && h == self.h { + return self.clone(); + } + Image::build(w, h, self.kind, self.resampler(w, h, tile)) + } + + /// [`Image::resize`] without the clamp: the new size is checked against `limits`, also when it + /// equals the current size, and its memory reserved first. + pub fn try_resize(&self, w: u32, h: u32, tile: bool, limits: &Limits) -> Result { + limits.check(w, h, self.kind)?; + if w == self.w && h == self.h { + return Ok(self.clone()); + } + Image::try_build(w, h, self.kind, limits, self.resampler(w, h, tile)) + } + + /// The pixel function of a resample to `w × h`, both at least 1. + fn resampler(&self, w: u32, h: u32, tile: bool) -> Box [f32; 4] + Sync + '_> { + let (w, h) = (w.max(1), h.max(1)); + if w <= self.w && h <= self.h && self.w.is_multiple_of(w) && self.h.is_multiple_of(h) { + let (fx, fy) = (i64::from(self.w / w), i64::from(self.h / h)); + let inv = 1.0 / (fx * fy) as f64; + return Box::new(move |x, y| { + // Summed in f64, so finite HDR values near `f32::MAX` average without overflow. + let mut s = [0.0f64; 4]; + for j in 0..fy { + for i in 0..fx { + let p = self.get(i64::from(x) * fx + i, i64::from(y) * fy + j, tile); + for (s, p) in s.iter_mut().zip(p) { + *s += f64::from(p); + } + } + } + s.map(|v| (v * inv) as f32) + }); + } + let (iw, ih) = (1.0 / w as f32, 1.0 / h as f32); + Box::new(move |x, y| self.sample((x as f32 + 0.5) * iw, (y as f32 + 0.5) * ih, tile)) + } + + /// Raw float access for gray images (`None` for color). + pub fn gray_data(&self) -> Option<&[f32]> { + (self.kind == Kind::Gray).then_some(self.data.as_slice()) + } + + /// 8-bit sRGB-encoded RGBA for display (gray shown as is; color through the sRGB curve). + /// Values are clamped to `0..=1` first; NaN becomes 0. The result has 4 bytes per pixel, never + /// more than the float data's own bytes. + pub fn to_rgba8_display(&self) -> Vec { + let mut out = vec![0u8; self.display_len()]; + self.fill_display(&mut out); + out + } + + /// [`Image::to_rgba8_display`] with its memory reserved first. + pub fn try_to_rgba8_display(&self) -> Result, ImageError> { + let len = self.display_len(); + let mut out = Vec::new(); + out.try_reserve_exact(len).map_err(|_| ImageError::OutOfMemory(self.w, self.h))?; + out.resize(len, 0); + self.fill_display(&mut out); + Ok(out) + } + + /// Bytes of the display copy: 4 per pixel, never more than the float data's own bytes. + fn display_len(&self) -> usize { + self.data.len() / self.channels() * 4 + } + + fn fill_display(&self, out: &mut [u8]) { + let q = |v: f32| (v.clamp(0.0, 1.0) * 255.0 + 0.5) as u8; + let pixel = |(i, px): (usize, &mut [u8])| match self.kind { + Kind::Gray => { + let v = q(self.data.get(i).copied().unwrap_or(0.0)); + px.copy_from_slice(&[v, v, v, 255]); + } + Kind::Color => { + let s = self.data.get(i * 4..i * 4 + 4).unwrap_or(&[0.0, 0.0, 0.0, 1.0]); + px.copy_from_slice(&[q(linear_to_srgb(s[0])), q(linear_to_srgb(s[1])), q(linear_to_srgb(s[2])), q(s[3])]); + } + }; + #[cfg(feature = "parallel")] + out.par_chunks_mut(4).enumerate().for_each(pixel); + #[cfg(not(feature = "parallel"))] + out.chunks_mut(4).enumerate().for_each(pixel); + } + + /// Mean of each channel (as RGBA), e.g. for tests and auto levels. A NaN or infinite pixel + /// makes its channel's mean non-finite. + pub fn mean(&self) -> [f32; 4] { + let c = self.channels(); + let n = f64::from(self.w) * f64::from(self.h); + let mut s = [0f64; 4]; + for px in self.data.chunks(c) { + for (k, v) in px.iter().enumerate() { + s[k] += f64::from(*v); + } + } + if c == 1 { + let m = (s[0] / n) as f32; + return [m, m, m, 1.0]; + } + s.map(|v| (v / n) as f32) + } + + /// Minimum and maximum over all channels (alpha excluded). NaN values are skipped; `(0, 0)` + /// when every value is NaN. + pub fn range(&self) -> (f32, f32) { + let c = self.channels(); + let mut lo = f32::INFINITY; + let mut hi = f32::NEG_INFINITY; + for px in self.data.chunks(c) { + for v in px.iter().take(c.min(3)) { + lo = lo.min(*v); + hi = hi.max(*v); + } + } + if lo > hi { (0.0, 0.0) } else { (lo, hi) } + } +} + +/// Rec. 709 luminance of a linear RGB(A) pixel. +#[inline] +pub fn luminance(p: [f32; 4]) -> f32 { + 0.2126 * p[0] + 0.7152 * p[1] + 0.0722 * p[2] +} + +/// The sRGB encoding of a linear value. NaN stays NaN. +#[inline] +pub fn linear_to_srgb(v: f32) -> f32 { + if v.is_nan() || v <= 0.003_130_8 { v * 12.92 } else { 1.055 * v.powf(1.0 / 2.4) - 0.055 } +} + +/// The linear value of an sRGB-encoded value. NaN stays NaN. +#[inline] +pub fn srgb_to_linear(v: f32) -> f32 { + if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) } +} + +/// Encode the bitmap name and its requested color space without ambiguity. +/// Plain sRGB names remain readable by simple resource callbacks. +pub fn resource_key(name: &str, linear: bool) -> String { + if linear { + format!("\u{1}L{name}") + } else if name.starts_with('\u{1}') { + format!("\u{1}S{name}") + } else { + name.to_string() + } +} + +/// Split a key made by [`resource_key`] into its name and data-map flag. +pub fn split_resource_key(key: &str) -> (&str, bool) { + if let Some(name) = key.strip_prefix("\u{1}L") { + (name, true) + } else if let Some(name) = key.strip_prefix("\u{1}S") { + (name, false) + } else { + (key, false) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn sample_survives_huge_coordinates() { + let img = Image::gray(8, 8, |x, _| x as f32); + for u in [1.0e30_f32, -1.0e30, f32::MAX, f32::MIN] { + for tile in [true, false] { + let p = img.sample(u, u, tile); + assert!(p.iter().all(|c| c.is_finite())); + } + } + } + + #[test] + fn resource_keys_round_trip() { + for name in ["n", "", "\u{1}Lnormal", "\u{1}Sn", "n\u{1}linear"] { + for linear in [false, true] { + assert_eq!(split_resource_key(&resource_key(name, linear)), (name, linear)); + } + } + } + + #[test] + fn sample_wraps_and_clamps() { + let img = Image::gray(4, 1, |x, _| x as f32); + assert_eq!(img.get(-1, 0, true)[0], 3.0); + assert_eq!(img.get(-1, 0, false)[0], 0.0); + // Halfway between the last and the (wrapped) first pixel. + assert!((img.sample(1.0, 0.5, true)[0] - 1.5).abs() < 1e-5); + } + + #[test] + fn resize_box_and_bilinear() { + let img = Image::gray(4, 4, |x, _| if x < 2 { 0.0 } else { 1.0 }); + let small = img.resize(2, 2, true); + assert_eq!(small.data.as_slice(), &[0.0, 1.0, 0.0, 1.0]); + let big = small.resize(8, 8, false); + assert_eq!(big.w, 8); + assert!(big.data[0] < big.data[7]); + } + + #[test] + fn hostile_sizes_are_clamped() { + let img = Image::filled(0, u32::MAX, Kind::Gray, [0.5; 4]); + assert_eq!((img.w, img.h), (1, MAX_SIDE)); + assert_eq!(img.sample(f32::NAN, 0.0, true), [0.0, 0.0, 0.0, 1.0]); + } + + #[test] + fn conversions() { + let c = Image::filled(2, 2, Kind::Color, [1.0, 1.0, 1.0, 1.0]); + assert!((c.to_gray().data[0] - 1.0).abs() < 1e-5); + assert_eq!(c.to_rgba8_display()[0], 255); + } + + /// Texel values 0, 5 and 1: the blend of the last and first texels (0.5) differs from the + /// middle texel (5), so a wrong repeat period shows. + fn stripe() -> Image { + Image::gray(3, 1, |x, _| [0.0, 5.0, 1.0][x as usize]) + } + + #[test] + fn repeat_keeps_its_period_at_large_coordinates() { + let img = stripe(); + // Every whole number repeats u = 0, which falls halfway between the last and first texels. + let at_zero = img.sample(0.0, 0.5, true)[0]; + assert!((at_zero - 0.5).abs() < 1e-6); + for u in [1.0e9_f32, -1.0e9, 4.0e9, 3.0e12] { + assert_eq!(img.sample(u, 0.5, true)[0], at_zero, "bilinear u = {u}"); + assert_eq!(img.sample_nearest(u, 0.5, true)[0], 0.0, "nearest u = {u}"); + } + assert_eq!(img.sample_nearest(1.0e19, 0.5, true)[0], 0.0); + assert_eq!(img.sample_nearest(-1.0e19, 0.5, true)[0], 0.0); + } + + #[test] + fn clamp_selects_the_edges_at_large_coordinates() { + let img = stripe(); + for u in [2.0_f32, 1.0e9, 1.0e19, f32::MAX] { + assert_eq!(img.sample(u, 0.5, false)[0], 1.0); + assert_eq!(img.sample_nearest(u, 0.5, false)[0], 1.0); + } + for u in [-1.0_f32, -1.0e9, -1.0e19, f32::MIN] { + assert_eq!(img.sample(u, 0.5, false)[0], 0.0); + assert_eq!(img.sample_nearest(u, 0.5, false)[0], 0.0); + } + } + + #[test] + fn nearest_follows_the_texel_floor() { + let img = Image::gray(4, 1, |x, _| x as f32); + // floor(u × 4): u = 1.0 repeats to texel 0 and clamps to texel 3. + assert_eq!(img.sample_nearest(0.24, 0.5, true)[0], 0.0); + assert_eq!(img.sample_nearest(0.25, 0.5, true)[0], 1.0); + assert_eq!(img.sample_nearest(1.0, 0.5, true)[0], 0.0); + assert_eq!(img.sample_nearest(1.0, 0.5, false)[0], 3.0); + assert_eq!(img.sample_nearest(-0.01, 0.5, true)[0], 3.0); + } + + #[test] + fn bilinear_weights_follow_the_fractional_texel_coordinate() { + // u × 4 − 0.5 = 1.25: texel 1 weighted 0.75 and texel 2 weighted 0.25. + let img = Image::gray(4, 1, |x, _| [0.0, 2.0, 6.0, 0.0][x as usize]); + assert!((img.sample(1.75 / 4.0, 0.5, false)[0] - 3.0).abs() < 1e-6); + // Two axes: the four texels of a 2 × 2 image at the image center weigh a quarter each. + let sq = Image::gray(2, 2, |x, y| [1.0, 3.0, 5.0, 11.0][(y * 2 + x) as usize]); + assert!((sq.sample(0.5, 0.5, false)[0] - 5.0).abs() < 1e-6); + } + + #[test] + fn small_negative_coordinates_repeat_to_the_last_texel() { + let img = Image::gray(4, 1, |x, _| x as f32); + for u in [-1.0e-8_f32, -1.0e-30, -f32::MIN_POSITIVE] { + assert_eq!(img.sample_nearest(u, 0.5, true)[0], 3.0, "u = {u}"); + assert!((img.sample(u, 0.5, true)[0] - 1.5).abs() < 1e-5, "u = {u}"); + } + let wide = Image::gray(MAX_SIDE, 1, |x, _| x as f32); + assert_eq!(wide.sample_nearest(-1.0e-8, 0.5, true)[0], (MAX_SIDE - 1) as f32); + } + + #[test] + fn bilinear_ignores_zero_weight_texels_and_does_not_overflow() { + let l = Limits::default(); + let Ok(img) = Image::from_data(2, 1, Kind::Gray, vec![1.0, f32::INFINITY], &l) else { panic!("valid 2 x 1 image") }; + // The center of texel 0 gives texel 0, though its neighbor is infinite. + assert_eq!(img.sample(0.25, 0.5, false)[0], 1.0); + let Ok(hdr) = Image::from_data(2, 1, Kind::Gray, vec![f32::MAX, -f32::MAX], &l) else { panic!("valid 2 x 1 image") }; + assert_eq!(hdr.sample(0.5, 0.5, false)[0], 0.0); + // Rounded weights can sum past 1; a constant image of the largest float stays finite. + let max = Image::filled(2, 2, Kind::Gray, [f32::MAX; 4]); + let t = 0.5 + 2.0_f32.powi(-24); + for tile in [false, true] { + assert!(max.sample(0.250_012_5, t, tile)[0].is_finite(), "tile = {tile}"); + assert_eq!(max.sample(t, t, tile)[0], f32::MAX, "tile = {tile}"); + } + } + + #[test] + fn equality_does_not_depend_on_buffer_sharing() { + let img = Image::filled(2, 1, Kind::Gray, [f32::NAN; 4]); + let rebuilt = Image::filled(2, 1, Kind::Gray, [f32::NAN; 4]); + // NaN pixels compare unequal as floats do, whether or not the buffer is shared. + assert_ne!(img, rebuilt); + assert_ne!(img, img.clone()); + let finite = Image::filled(2, 1, Kind::Gray, [0.5; 4]); + assert_eq!(finite, finite.clone()); + assert_eq!(finite, Image::filled(2, 1, Kind::Gray, [0.5; 4])); + } + + #[test] + fn box_shrink_averages_large_hdr_values() { + let img = Image::filled(2, 2, Kind::Color, [f32::MAX, -f32::MAX, 1.0, 1.0]); + assert_eq!(img.resize(1, 1, false).data(), &[f32::MAX, -f32::MAX, 1.0, 1.0]); + } + + #[test] + fn fallible_operations_check_the_limits() { + let img = Image::gray(4, 4, |x, y| (x + 4 * y) as f32); + let l = Limits::default(); + assert_eq!(img.try_resize(2, 2, true, &l), Ok(img.resize(2, 2, true))); + assert_eq!(img.try_resize(7, 3, false, &l), Ok(img.resize(7, 3, false))); + assert_eq!(img.try_resize(0, 2, false, &l), Err(ImageError::BadSize(0, 2))); + let small = Limits { max_bytes: 100, ..Limits::default() }; + assert_eq!(img.try_resize(8, 8, false, &small), Err(ImageError::TooLarge(8, 8))); + assert_eq!(img.try_to_kind(Kind::Color, &small), Err(ImageError::TooLarge(4, 4))); + assert_eq!(img.try_to_kind(Kind::Color, &l), Ok(img.to_color())); + assert_eq!(img.try_to_kind(Kind::Gray, &small), Ok(img.clone())); + assert_eq!(img.try_map(Kind::Gray, &l, |p| [p[0] * 2.0; 4]), Ok(img.map(Kind::Gray, |p| [p[0] * 2.0; 4]))); + assert_eq!(img.try_to_rgba8_display(), Ok(img.to_rgba8_display())); + } + + #[test] + fn unchanged_results_are_checked_against_the_limits() { + let img = Image::gray(4, 4, |x, y| (x + 4 * y) as f32); + let tiny = Limits { max_pixels: 8, ..Limits::default() }; + assert_eq!(img.try_resize(4, 4, false, &tiny), Err(ImageError::TooLarge(4, 4))); + assert_eq!(img.try_to_kind(Kind::Gray, &tiny), Err(ImageError::TooLarge(4, 4))); + let narrow = Limits { max_side: 3, ..Limits::default() }; + assert_eq!(img.try_resize(4, 4, false, &narrow), Err(ImageError::BadSize(4, 4))); + } + + #[test] + fn generate_uv_spans_the_clamped_size() { + let img = Image::generate_uv(MAX_SIDE + 100, 1, Kind::Gray, |u, _| [u, 0.0, 0.0, 0.0]); + assert_eq!(img.w, MAX_SIDE); + let last = img.data[MAX_SIDE as usize - 1]; + assert!((last - (MAX_SIDE as f32 - 0.5) / MAX_SIDE as f32).abs() < 1e-6, "{last}"); + } + + #[test] + fn checked_sizes_reject_zero_and_long_sides() { + let l = Limits::default(); + assert_eq!(Image::try_filled(0, 4, Kind::Gray, [0.0; 4], &l), Err(ImageError::BadSize(0, 4))); + assert_eq!(Image::try_filled(4, 0, Kind::Color, [0.0; 4], &l), Err(ImageError::BadSize(4, 0))); + assert_eq!(Image::from_data(0, 1, Kind::Gray, Vec::new(), &l), Err(ImageError::BadSize(0, 1))); + assert_eq!(Image::try_generate(MAX_SIDE + 1, 1, Kind::Gray, &l, |_, _| [0.0; 4]), Err(ImageError::BadSize(MAX_SIDE + 1, 1))); + assert!(Image::try_filled(3, 2, Kind::Color, [0.25; 4], &l).is_ok_and(|i| i.data().len() == 24 && i.width() == 3)); + } + + #[test] + fn checked_sizes_respect_the_pixel_and_byte_limits() { + let l = Limits { max_bytes: 1000, ..Limits::default() }; + assert_eq!(l.check(15, 16, Kind::Gray), Ok(240)); + assert_eq!(l.check(16, 16, Kind::Gray), Err(ImageError::TooLarge(16, 16))); + assert_eq!(Image::try_filled(8, 8, Kind::Color, [0.0; 4], &l), Err(ImageError::TooLarge(8, 8))); + let l = Limits { max_pixels: 100, ..Limits::default() }; + assert_eq!(l.check(10, 10, Kind::Color), Ok(400)); + assert_eq!(l.check(10, 11, Kind::Gray), Err(ImageError::TooLarge(10, 11))); + // The default limits accept a full 8192 × 8192 color plane and nothing longer. + assert_eq!(Limits::default().check(MAX_SIDE, MAX_SIDE, Kind::Color), Ok(1 << 28)); + } + + #[test] + fn checked_sizes_do_not_overflow() { + let open = Limits { max_side: u32::MAX, max_pixels: u64::MAX, max_bytes: u64::MAX }; + assert_eq!(open.check(u32::MAX, u32::MAX, Kind::Color), Err(ImageError::TooLarge(u32::MAX, u32::MAX))); + assert_eq!(open.check(u32::MAX, u32::MAX, Kind::Gray), Err(ImageError::TooLarge(u32::MAX, u32::MAX))); + } + + /// `isize::MAX` on a 32-bit target such as `wasm32`. + const BOUND_32: u64 = i32::MAX as u64; + + #[test] + fn checked_sizes_respect_target_isize_bound() { + let open = Limits { max_side: u32::MAX, max_pixels: u64::MAX, max_bytes: u64::MAX }; + // 16,385 × 8,192 gray is 512 MiB and 16 KiB; its color form is over 2 GiB. + assert_eq!(open.check_within(16_385, 8_192, Kind::Gray, BOUND_32), Ok(16_385 * 8_192)); + assert_eq!(open.check_within(16_385, 8_192, Kind::Color, BOUND_32), Err(ImageError::TooLarge(16_385, 8_192))); + assert_eq!(open.check_within(MAX_SIDE, MAX_SIDE, Kind::Color, BOUND_32), Ok(1 << 28)); + } + + #[test] + fn infallible_expansion_clamps_at_target_bound() { + assert_eq!(bounded_size(16_385, 8_192, Kind::Gray, BOUND_32), (16_385, 8_192, 16_385 * 8_192)); + assert_eq!(bounded_size(16_385, 8_192, Kind::Color, BOUND_32), (MAX_SIDE, MAX_SIDE, 1 << 28)); + assert_eq!(bounded_size(MAX_SIDE, MAX_SIDE, Kind::Color, BOUND_32), (MAX_SIDE, MAX_SIDE, 1 << 28)); + assert_eq!(bounded_size(3, 2, Kind::Color, BOUND_32), (3, 2, 24)); + // A bound below even the clamped size gives one pixel. + assert_eq!(bounded_size(16_385, 8_192, Kind::Color, 64), (1, 1, 4)); + assert_eq!(bounded_size(u32::MAX, u32::MAX, Kind::Gray, MAX_ALLOC_BYTES), (MAX_SIDE, MAX_SIDE, 1 << 26)); + } + + #[test] + fn checked_byte_len_rejects_arithmetic_overflow() { + assert_eq!(checked_byte_len(u32::MAX, u32::MAX, Kind::Color), None); + assert_eq!(checked_byte_len(u32::MAX, u32::MAX, Kind::Gray), None); + assert_eq!(checked_byte_len(MAX_SIDE, MAX_SIDE, Kind::Color), Some(1 << 30)); + } + + #[test] + fn from_data_checks_the_length() { + let l = Limits::default(); + assert_eq!(Image::from_data(2, 2, Kind::Color, vec![0.0], &l), Err(ImageError::BadDataLength { expected: 16, found: 1 })); + assert_eq!(Image::from_data(2, 2, Kind::Color, vec![0.0; 17], &l), Err(ImageError::BadDataLength { expected: 16, found: 17 })); + let img = Image::from_data(2, 1, Kind::Gray, vec![-2.0, 3.5], &l); + assert!(img.is_ok_and(|i| i.get(0, 0, false)[0] == -2.0 && i.get(1, 0, false)[0] == 3.5)); + } + + #[test] + fn non_finite_pixels_have_defined_results() { + let l = Limits::default(); + let img = Image::from_data(1, 1, Kind::Color, vec![f32::NAN, f32::INFINITY, -1.0, 2.0], &l); + let Ok(img) = img else { panic!("valid 1 x 1 image") }; + assert_eq!(img.to_rgba8_display(), vec![0, 255, 0, 255]); + let g = Image::from_data(3, 1, Kind::Gray, vec![f32::NAN, -4.0, 8.0], &l); + let Ok(g) = g else { panic!("valid 3 x 1 image") }; + assert_eq!(g.range(), (-4.0, 8.0)); + assert!(g.mean()[0].is_nan()); + } + + #[test] + fn srgb_encoding_keeps_nan() { + assert!(linear_to_srgb(f32::NAN).is_nan()); + assert!(srgb_to_linear(f32::NAN).is_nan()); + assert_eq!(linear_to_srgb(0.0), 0.0); + assert!((linear_to_srgb(1.0) - 1.0).abs() < 1e-6); + assert!((srgb_to_linear(linear_to_srgb(0.5)) - 0.5).abs() < 1e-6); + } + + #[test] + fn whole_factor_shrink_stays_exact_with_custom_limits() { + let l = Limits { max_side: 16, ..Limits::default() }; + let img = Image::try_generate(4, 2, Kind::Color, &l, |x, y| [x as f32, y as f32, 0.0, 1.0]); + let Ok(img) = img else { panic!("valid 4 x 2 image") }; + let half = img.resize(2, 1, false); + assert_eq!(half.data(), &[0.5, 0.5, 0.0, 1.0, 2.5, 0.5, 0.0, 1.0]); + assert_eq!(img.to_gray().width(), 4); + } +} diff --git a/crates/video/src/av1/encode/mod.rs b/crates/video/src/av1/encode/mod.rs index 0d8e2b04f..10e88d0ad 100644 --- a/crates/video/src/av1/encode/mod.rs +++ b/crates/video/src/av1/encode/mod.rs @@ -600,8 +600,13 @@ impl Encoder { /// Encodes one frame. pub fn encode(&mut self, frame: &Frame) -> Result { + self.encode_with_key(frame, false) + } + + /// Encodes one frame, optionally forcing a keyframe regardless of `keyint`. + pub fn encode_with_key(&mut self, frame: &Frame, force_key: bool) -> Result { self.check_frame(frame)?; - let key = self.frame_num.is_multiple_of(self.cfg.keyint.max(1) as u64); + let key = force_key || self.frame_num.is_multiple_of(self.cfg.keyint.max(1) as u64); let src = self.load_source(frame); let (frame_obu, rf) = match self.cfg.rate { RateControl::ConstantQ(q) => self.encode_frame(&src, key, q.max(1)), diff --git a/crates/wordmodel/src/numbering.rs b/crates/wordmodel/src/numbering.rs index 5ab94ea87..91eb221b8 100644 --- a/crates/wordmodel/src/numbering.rs +++ b/crates/wordmodel/src/numbering.rs @@ -70,12 +70,18 @@ impl Default for Level { } } -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Default)] +#[derive(Clone, Debug, Serialize, Deserialize, Default)] #[serde(default)] pub struct AbstractNum { pub id: u32, pub name: Option, pub levels: Vec, + /// Which levels were defined in the source file. `None` is used by model-created definitions, + /// where the populated `levels` vector is authoritative. DOCX readers set this when they fill + /// unspecified Word levels with their effective defaults. This reader-only provenance is not + /// serialized as part of the document model. + #[serde(skip)] + pub explicit_levels: Option<[bool; 9]>, /// Word's list identity (`w:nsid`, 8 hex digits), which it uses to match lists across /// documents (paste, compare); kept from the file. #[serde(skip_serializing_if = "Option::is_none")] @@ -89,6 +95,20 @@ pub struct AbstractNum { pub multi_level_type: Option, } +// Explicit source-level presence is reader provenance used to distinguish an omitted Word level +// from an effective default. It is intentionally not part of the document model's structural +// equality, just as it is not serialized. +impl PartialEq for AbstractNum { + fn eq(&self, other: &Self) -> bool { + self.id == other.id + && self.name == other.name + && self.levels == other.levels + && self.nsid == other.nsid + && self.tmpl == other.tmpl + && self.multi_level_type == other.multi_level_type + } +} + #[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Default)] #[serde(default, rename_all = "camelCase")] pub struct Num { @@ -236,6 +256,20 @@ impl Numbering { } self.abstract_of(num)?.levels.get(level as usize) } + /// Whether the source definition supplied this level, or a per-numbering override defines it. + /// + /// [`Self::level`] also exposes Word's effective default for omitted levels. This method lets + /// importers preserve formats whose historical behavior distinguishes an explicit level from + /// that effective fallback. Older serialized and model-created definitions have no source + /// presence metadata, so their populated levels count as defined. + pub fn level_is_defined(&self, num: u32, level: u8) -> bool { + let Some(n) = self.num(num) else { return false }; + if n.level_overrides.iter().any(|(i, _)| *i == level) { + return true; + } + let Some(a) = self.abstract_of(num) else { return false }; + a.explicit_levels.map_or_else(|| a.levels.get(level as usize).is_some(), |levels| levels.get(level as usize).copied().unwrap_or(false)) + } /// Create a new list of `kind`; returns the `num` id. List ids run out only past `u32::MAX` /// lists, more than memory holds; then nothing is added and the id is 0, "no list". pub fn add_list(&mut self, kind: ListKind) -> u32 { @@ -485,6 +519,42 @@ const MAX_LABEL_BYTES: usize = 256; mod tests { use super::*; + #[test] + fn explicit_level_metadata_preserves_effective_defaults_and_old_models() { + let mut numbering = Numbering::default(); + let id = numbering.add_list(ListKind::Numbered); + assert!(numbering.level_is_defined(id, 0)); + assert!(numbering.level_is_defined(id, 8)); + + let abstract_id = numbering.num(id).unwrap().abstract_id; + let mut explicit = [false; 9]; + explicit[0] = true; + numbering.abstracts.iter_mut().find(|a| a.id == abstract_id).unwrap().explicit_levels = Some(explicit); + + // Effective Word defaults remain available to existing model callers, while importers can + // tell that this source file supplied only level zero. + assert!(numbering.level(id, 1).is_some()); + assert!(!numbering.level_is_defined(id, 1)); + assert!(numbering.level_is_defined(id, 0)); + + // Old serialized definitions have no presence marker; populated levels remain valid. + numbering.abstracts.iter_mut().find(|a| a.id == abstract_id).unwrap().explicit_levels = None; + assert!(numbering.level_is_defined(id, 8)); + + // A per-numbering level definition takes precedence over the abstract's missing level. + numbering.abstracts.iter_mut().find(|a| a.id == abstract_id).unwrap().explicit_levels = Some(explicit); + numbering.nums.iter_mut().find(|n| n.id == id).unwrap().level_overrides.push((1, Level::default())); + assert!(numbering.level_is_defined(id, 1)); + } + + #[test] + fn explicit_level_metadata_does_not_change_model_equality() { + let mut with_metadata = AbstractNum::default(); + let without_metadata = with_metadata.clone(); + with_metadata.explicit_levels = Some([true; 9]); + assert_eq!(with_metadata, without_metadata); + } + /// Read-only counter APIs retain the renderer's bounds and never advance a list. #[test] fn reference_labels_are_bounded_and_do_not_advance_counters() { diff --git a/crates/wordmodel/src/resolve.rs b/crates/wordmodel/src/resolve.rs index 40449e82c..7ef12ccdc 100644 --- a/crates/wordmodel/src/resolve.rs +++ b/crates/wordmodel/src/resolve.rs @@ -66,6 +66,11 @@ pub struct ResolvedChar { pub rtl: bool, /// The whole run uses the complex-script properties (`w:cs`). pub cs: bool, + /// The run says it is not complex script (`w:cs w:val="0"`): its Thai, Indic, Arabic or + /// Hebrew characters take its other font, size, bold and italic, as Word draws them in a run + /// that isn't complex script or right to left. + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + pub cs_off: bool, /// Complex-script font, size, bold and italic (Arabic, Persian, Hebrew… text). pub font_cs: String, /// East Asian font (Chinese, Japanese and Korean text), when one is set. @@ -115,6 +120,7 @@ impl ResolvedChar { lang: c.lang.clone(), rtl: c.rtl.unwrap_or(false), cs: c.cs.unwrap_or(false), + cs_off: c.cs == Some(false), font_cs: c.font_cs.clone().or_else(|| c.font.clone()).unwrap_or_else(|| crate::styles::BODY_FONT.to_string()), font_ea: c.font_ea.clone(), size_cs: c.size_cs.or(c.size).unwrap_or(crate::props::DEFAULT_FONT_SIZE).clamp(1.0, 1638.0), @@ -133,9 +139,11 @@ impl ResolvedChar { pub fn kerned(&self) -> bool { self.kern > 0.0 && self.size >= self.kern } - /// Does a character of this run use the complex-script properties? + /// Does a character of this run use the complex-script properties? Every character of a + /// right-to-left or complex-script run does; otherwise a complex-script character does, + /// unless the run says it is not complex script. pub fn uses_complex(&self, c: char) -> bool { - self.rtl || self.cs || crate::bidi::is_complex_script(c) + self.rtl || self.cs || (!self.cs_off && crate::bidi::is_complex_script(c)) } /// The size glyphs are drawn and measured at. Word sets superscript and subscript text at /// 0.65 of the run's size rounded to the half point, a font size the format can store diff --git a/crates/wordmodel/src/styles.rs b/crates/wordmodel/src/styles.rs index 5383c1510..1d725efa2 100644 --- a/crates/wordmodel/src/styles.rs +++ b/crates/wordmodel/src/styles.rs @@ -828,6 +828,94 @@ impl StyleSheet { out.chr.style = None; Some(out) } + /// Word draws a run's text with the complex-script font, size, bold and italic only when the run + /// is complex script or right to left (`w:cs`, `w:rtl`) by its own properties, its paragraph + /// style, its table style or the run defaults; a character style saying so, or saying not, + /// doesn't count (nor does its `w:rtl` change the order of the text). Anywhere else the text takes the run's other font and `w:sz`, Thai included, + /// whatever its language or `w:hint` (measured in Word for Windows: Thai text at `w:sz` 22 pt, + /// `w:szCs` 11 pt is drawn at 22 pt), and the same in a `.doc`. WordCraft draws Thai, Lao and Indic characters with the + /// complex-script formatting unless the run says it is not complex script, and counts a character + /// style's `w:cs`; so a run it would draw otherwise says on itself whether it is complex script + /// (`w:cs`, which Word reads the same way). A reader calls this on each paragraph it has read. `table` is the run formatting the table style gives + /// the paragraph's cell. Runs with right-to-left text are left as they are: they are written + /// right to left. + pub fn mark_runs_as_word_draws_them(&self, para: &mut crate::para::Paragraph, table: Option<&CharProps>) { + use crate::bidi::{is_complex_script, is_rtl_script}; + use crate::para::{InlineObject, OBJ}; + // What each run draws: its text, a field kept as one object standing for its result + // (drawn in the run's formatting, and measured in Word to follow the same rule). + let mut drawn = Vec::with_capacity(para.runs.len()); + let (mut at, mut objects) = (0, para.objects.iter()); + for run in ¶.runs { + let mut text = String::new(); + for c in para.text.get(at..at + run.len).unwrap_or("").chars() { + if c != OBJ { + text.push(c); + } else if let Some(InlineObject::Field { result, code: false, .. }) = objects.next() { + text.push_str(result); + } + } + at += run.len; + drawn.push(text); + } + let style = para.props.style.as_deref(); + for (run, text) in para.runs.iter_mut().zip(&drawn) { + if text.is_empty() || text.chars().any(is_rtl_script) { + continue; + } + let word = self.resolve_char_in(style, table, &CharProps { style: None, ..run.props.clone() }); + let mut ours = self.resolve_char_in(style, table, &run.props); + // Right to left only by its character style: Word draws and orders the run as it does + // one that says nothing (measured, Latin and Thai text). + if ours.rtl && !word.rtl { + run.props.rtl = Some(false); + ours = self.resolve_char_in(style, table, &run.props); + } + if word.cs || word.rtl { + if !ours.cs && !ours.rtl { + run.props.cs = Some(true); + } + } else if !ours.rtl && (ours.cs || (!ours.cs_off && text.chars().any(is_complex_script))) { + run.props.cs = Some(false); + } + } + } + /// The run formatting `t`'s table style gives the text of each of its cells, by row and then + /// cell: each of a cell's parts' over the last, lowest precedence first (ECMA-376 §17.7.6), as + /// layout applies it between document defaults and the paragraph's style. `None` when the + /// table has no style. The style and the table's size are worked out once for the whole table, + /// and each set of parts is put together once. + pub fn table_cells_chr(&self, t: &crate::table::Table) -> Option>> { + let st = self.table_style(t.props.style.as_deref()?)?; + let sizes = (st.parts.band_size.unwrap_or(0), st.parts.col_band_size.unwrap_or(0)); + let ncols = t.cols().clamp(1, crate::table::MAX_COLS); + let nrows = t.rows.len(); + let mut made: Vec<(PartSet, CharProps)> = Vec::new(); + let mut out = Vec::with_capacity(nrows); + for (r, row) in t.rows.iter().enumerate() { + let mut g = row.first_grid(); + let mut cells = Vec::with_capacity(row.cells.len()); + for cell in &row.cells { + let span = cell.span(); + let parts = PartSet::of_cell(&t.props.look, sizes, (r, nrows), (g, span, ncols)); + g = g.saturating_add(span); + let chr = match made.iter().find(|(p, _)| *p == parts) { + Some((_, chr)) => chr.clone(), + None => { + let mut chr = CharProps::default(); + for part in parts.iter() { + chr.overlay(&st.part(part).chr); + } + made.push((parts, chr.clone())); + chr + } + }; + cells.push(chr); + } + out.push(cells); + } + Some(out) + } /// Add or replace a style. pub fn upsert(&mut self, st: Style) { match self.styles.iter_mut().find(|s| s.id == st.id) { diff --git a/crates/xml/Cargo.toml b/crates/xml/Cargo.toml index f957fa625..02d68985b 100644 --- a/crates/xml/Cargo.toml +++ b/crates/xml/Cargo.toml @@ -13,3 +13,4 @@ workspace = true [dependencies] quick-xml = { version = "0.41", default-features = false } thiserror = { workspace = true } +serde = { workspace = true, features = ["derive"] } diff --git a/crates/xml/src/dtd.rs b/crates/xml/src/dtd.rs new file mode 100644 index 000000000..9fa1dd497 --- /dev/null +++ b/crates/xml/src/dtd.rs @@ -0,0 +1,1063 @@ +//! DTD content-model parsing and XML validation. + +//! Document Type Definitions: element declarations (content models) and required attributes, and +//! validation of an XML document against them (Structure ▸ Validate). + +use std::collections::{HashMap, HashSet}; + +/// How much text parameter entities may add during expansion (a pass count is not an output +/// bound: each pass replaces whole strings, so references can inflate multiplicatively). +const MAX_EXPANDED_BYTES: usize = 1024 * 1024; +const MAX_DTD_BYTES: usize = 1024 * 1024; +const MAX_VALIDATION_XML_BYTES: usize = 16 * 1024 * 1024; +const MAX_VALIDATION_NODES: usize = 100_000; +const MAX_VALIDATION_DEPTH: usize = 256; +const MAX_VALIDATION_ATTRIBUTES: usize = 1_000_000; +const MAX_MATCH_WORK: usize = 20_000_000; +const MAX_MATCH_POSITIONS: usize = 5_000_000; +const MAX_MATCH_STATES: usize = 100_000; + +/// A content particle: a name, a sequence, a choice, or one of those repeated (`?`, `*`, `+`). +#[derive(Clone, Debug, PartialEq)] +enum Cp { + Name(String), + Seq(Vec), + Choice(Vec), + Rep(Box, char), +} + +#[derive(Clone, Debug, PartialEq)] +enum Model { + Empty, + Any, + /// `(#PCDATA | a | b)*`: text and these elements in any order. + Mixed(Vec), + Children(Cp), +} + +/// A parsed DTD. +#[derive(Clone, Debug, Default)] +pub struct Dtd { + elements: Vec<(String, Model)>, + /// Element → its `#REQUIRED` attributes. + required: HashMap>, +} + +/// A problem found by validation: where (an element path such as `Root/story[2]`) and what. +#[derive(Clone, Debug, PartialEq, serde::Serialize)] +pub struct Problem { + pub path: String, + pub message: String, +} + +impl Dtd { + /// Declared element names, in order. + pub fn element_names(&self) -> impl Iterator { + self.elements.iter().map(|(n, _)| n.as_str()) + } + + fn model(&self, name: &str) -> Option<&Model> { + self.elements.iter().find(|(n, _)| n == name).map(|(_, m)| m) + } +} + +/// Markup declarations (``) with comments removed and quoted strings kept whole. +fn declarations(src: &str) -> Result, String> { + let mut out = Vec::new(); + let mut rest = src; + while let Some(i) = rest.find("") else { + return Err("unterminated DTD comment".into()); + }; + rest = &rest[j + 3..]; + continue; + } + let mut quote = None; + let mut end = None; + for (k, c) in rest.char_indices().skip(2) { + match (quote, c) { + (Some(q), c) if c == q => quote = None, + (None, '"' | '\'') => quote = Some(c), + (None, '>') => { + end = Some(k); + break; + } + _ => {} + } + } + let Some(end) = end else { + return Err("unterminated DTD declaration".into()); + }; + out.push(rest[2..end].to_string()); + rest = &rest[end + 1..]; + } + Ok(out) +} + +fn tokens(s: &str) -> Vec { + let mut out = Vec::new(); + let mut cur = String::new(); + for c in s.chars() { + if c.is_whitespace() || "(),|?*+".contains(c) { + if !cur.is_empty() { + out.push(std::mem::take(&mut cur)); + } + if !c.is_whitespace() { + out.push(c.to_string()); + } + } else { + cur.push(c); + } + } + if !cur.is_empty() { + out.push(cur); + } + out +} + +/// How deeply groups may nest in a content model; parsing, matching (`ends`) and printing (`show`) +/// recurse once per level. +const MAX_CONTENT_MODEL_DEPTH: usize = 64; + +/// Parse a content particle from `t[*i..]`, inside `depth` enclosing groups. +fn particle(t: &[String], i: &mut usize, depth: usize) -> Result { + let base = match t.get(*i).map(String::as_str) { + Some("(") => { + if depth >= MAX_CONTENT_MODEL_DEPTH { + return Err(format!("content model nested more than {MAX_CONTENT_MODEL_DEPTH} levels")); + } + *i += 1; + let mut items = vec![particle(t, i, depth + 1)?]; + let mut sep = None; + loop { + match t.get(*i).map(String::as_str) { + Some(")") => { + *i += 1; + break; + } + Some(s @ ("," | "|")) => { + if sep.is_some_and(|p| p != s) { + return Err("mixed `,` and `|` in one group".into()); + } + sep = Some(if s == "," { "," } else { "|" }); + *i += 1; + items.push(particle(t, i, depth + 1)?); + } + other => return Err(format!("unexpected `{}` in a content model", other.unwrap_or("end"))), + } + } + if sep == Some("|") { Cp::Choice(items) } else { Cp::Seq(items) } + } + Some(n) if !"),|?*+".contains(n) => { + *i += 1; + Cp::Name(n.to_string()) + } + other => return Err(format!("unexpected `{}` in a content model", other.unwrap_or("end"))), + }; + Ok(match t.get(*i).map(String::as_str) { + Some(r @ ("?" | "*" | "+")) => { + *i += 1; + Cp::Rep(Box::new(base), r.chars().next().unwrap_or('*')) + } + _ => base, + }) +} + +fn parse_model(s: &str) -> Result { + let s = s.trim(); + match s { + "EMPTY" => return Ok(Model::Empty), + "ANY" => return Ok(Model::Any), + _ => {} + } + if s.contains("#PCDATA") { + let t = tokens(s); + if t.first().map(String::as_str) != Some("(") || t.get(1).map(String::as_str) != Some("#PCDATA") { + return Err("malformed mixed content model".into()); + } + let mut i = 2; + let mut allowed = Vec::new(); + if t.get(i).map(String::as_str) == Some(")") { + i += 1; + if t.get(i).map(String::as_str) == Some("*") { + i += 1; + } + if i != t.len() { + return Err("unexpected token after mixed content model".into()); + } + return Ok(Model::Mixed(allowed)); + } + loop { + if t.get(i).map(String::as_str) != Some("|") { + return Err("expected `|` in mixed content model".into()); + } + i += 1; + let Some(name) = t.get(i).filter(|name| !"(),|?*+".contains(name.as_str())) else { + return Err("expected element name in mixed content model".into()); + }; + if allowed.contains(name) { + return Err(format!("duplicate `{name}` in mixed content model")); + } + allowed.push(name.clone()); + i += 1; + match t.get(i).map(String::as_str) { + Some("|") => continue, + Some(")") => { + i += 1; + break; + } + _ => return Err("expected `|` or `)` in mixed content model".into()), + } + } + if t.get(i).map(String::as_str) != Some("*") || i + 1 != t.len() { + return Err("mixed content with element names must end in `*`".into()); + } + return Ok(Model::Mixed(allowed)); + } + let t = tokens(s); + let mut i = 0; + let cp = particle(&t, &mut i, 0)?; + if i != t.len() { + return Err(format!("unexpected `{}` after the content model", t[i])); + } + Ok(Model::Children(cp)) +} + +/// How deeply parameter entities may nest: a reference in the DTD is level 1, a reference in its +/// value level 2, and so on. +const MAX_PARAMETER_ENTITY_DEPTH: usize = 64; + +/// Expand `%name;` references, one level per round, until a round changes nothing. +fn expand_parameter_entities(src: &str, ents: &[(String, String)]) -> Result { + let mut entities = HashMap::new(); + for (name, value) in ents { + entities.entry(name.as_str()).or_insert(value.as_str()); + } + let limit = src.len().saturating_add(MAX_EXPANDED_BYTES); + let append = |out: &mut String, value: &str| -> Result<(), String> { + if value.len() > limit.saturating_sub(out.len()) { + return Err(format!("parameter entities expand the DTD beyond {limit} bytes")); + } + out.push_str(value); + Ok(()) + }; + let too_deep = || format!("parameter entities refer to themselves or nest deeper than {MAX_PARAMETER_ENTITY_DEPTH} levels"); + let mut text = src.to_string(); + // The round after the deepest level changes nothing, hence one more round than levels. + for _ in 0..=MAX_PARAMETER_ENTITY_DEPTH { + let mut out = String::with_capacity(text.len()); + let mut rest = text.as_str(); + let mut changed = false; + loop { + let mut special = None; + for (i, c) in rest.char_indices() { + if c == '%' { + special = Some((i, false)); + break; + } + if c == '<' && rest[i..].starts_with("") else { + return Err("unterminated DTD comment".into()); + }; + append(&mut out, &comment_tail[..end + 3])?; + rest = &comment_tail[end + 3..]; + continue; + } + let (prefix, reference) = rest.split_at(i); + append(&mut out, prefix)?; + // `find` points to an ASCII `%`, so both split positions are UTF-8 boundaries. + rest = reference.split_at(1).1; + // A name has no whitespace and no `%` (`parse` ends a name at whitespace), whatever its + // length: the search for the closing `;` stops at the first of those, so the searches of + // a round never overlap and a round stays linear in the text. + let end = + rest.bytes().position(|b| matches!(b, b';' | b'%') || b.is_ascii_whitespace()).filter(|&k| rest.as_bytes().get(k) == Some(&b';')); + if let Some(end) = end + && let Some(value) = rest.get(..end).and_then(|name| entities.get(name)) + { + // A value that is its own reference (``) never gets anywhere. + if reference.get(..end + 2) == Some(*value) { + return Err(too_deep()); + } + append(&mut out, value)?; + changed = true; + rest = rest.split_at(end + 1).1; + continue; + } + append(&mut out, "%")?; + } + append(&mut out, rest)?; + text = out; + if !changed { + return Ok(text); + } + } + Err(too_deep()) +} + +/// Parse a DTD (its element and attribute-list declarations; parameter entities are expanded). +pub fn parse(src: &str) -> Result { + if src.len() > MAX_DTD_BYTES { + return Err(format!("DTD exceeds the {MAX_DTD_BYTES} byte parsing limit")); + } + // Parameter entities first, then expand references and read the declarations again. + let mut ents: Vec<(String, String)> = Vec::new(); + for d in declarations(src)? { + if let Some(rest) = d.strip_prefix("ENTITY") { + let rest = rest.trim_start(); + if let Some(rest) = rest.strip_prefix('%') { + let rest = rest.trim_start(); + let name: String = rest.chars().take_while(|c| !c.is_whitespace()).collect(); + let val = rest[name.len()..].trim(); + let q = val.chars().next().unwrap_or('"'); + if let Some(v) = val.strip_prefix(q).and_then(|v| v.find(q).map(|e| &v[..e])) { + ents.push((name, v.to_string())); + } + } + } + } + let text = expand_parameter_entities(src, &ents)?; + let mut dtd = Dtd::default(); + for d in declarations(&text)? { + if let Some(rest) = d.strip_prefix("ELEMENT") { + let rest = rest.trim_start(); + let name: String = rest.chars().take_while(|c| !c.is_whitespace()).collect(); + let model = parse_model(&rest[name.len()..]).map_err(|e| format!(": {e}"))?; + dtd.elements.push((name, model)); + } else if let Some(rest) = d.strip_prefix("ATTLIST") { + // name (attr type default)*; only `#REQUIRED` matters here. + let mut words = Vec::new(); + let mut cur = String::new(); + let mut quote = None; + let mut group = 0; + for c in rest.chars() { + match (quote, c) { + (Some(q), c) if c == q => { + quote = None; + cur.push(c); + } + (Some(_), c) => cur.push(c), + (None, '"' | '\'') => { + quote = Some(c); + cur.push(c); + } + (None, '(') => { + group += 1; + cur.push(c); + } + (None, ')') => { + group -= 1; + cur.push(c); + } + (None, c) if c.is_whitespace() && group == 0 => { + if !cur.is_empty() { + words.push(std::mem::take(&mut cur)); + } + } + (None, c) => cur.push(c), + } + } + if !cur.is_empty() { + words.push(cur); + } + let Some((el, attrs)) = words.split_first() else { continue }; + let mut k = 0; + while k < attrs.len() { + let name = &attrs[k]; + let Some(ty) = attrs.get(k + 1) else { + return Err(format!("incomplete ATTLIST for `{el}`")); + }; + k += 2; + // NOTATION is a two-token type: the keyword followed by its enumeration. + if ty == "NOTATION" { + if attrs.get(k).is_none_or(|group| !group.starts_with('(') || !group.ends_with(')')) { + return Err(format!("malformed NOTATION type for `{name}`")); + } + k += 1; + } + let Some(default) = attrs.get(k).map(String::as_str) else { + return Err(format!("missing default declaration for `{name}`")); + }; + k += 1; + if default == "#REQUIRED" { + dtd.required.entry(el.clone()).or_default().push(name.clone()); + } else if default == "#FIXED" { + if k >= attrs.len() { + return Err(format!("missing #FIXED value for `{name}`")); + } + k += 1; + } + } + } + } + if dtd.elements.is_empty() { + return Err("no element declarations".into()); + } + Ok(dtd) +} + +/// What `ends` has worked out for one sequence of children: the end positions of a group (by the +/// address of its node, which lives as long as the model) from a position. +type EndsMemo = HashMap<(usize, usize), Vec>; + +#[derive(Default)] +struct MatchBudget { + work: usize, + positions: usize, + states: usize, + exhausted: bool, + reported: bool, +} + +impl MatchBudget { + fn spend(&mut self, work: usize, positions: usize) -> Result<(), &'static str> { + if self.exhausted { + return Err("content model matching exceeded its resource budget"); + } + self.work = self.work.saturating_add(work); + self.positions = self.positions.saturating_add(positions); + if self.work > MAX_MATCH_WORK { + self.exhausted = true; + return Err("content model matching exceeded its work limit"); + } + if self.positions > MAX_MATCH_POSITIONS { + self.exhausted = true; + return Err("content model matching exceeded its memory limit"); + } + Ok(()) + } + + fn retain_state(&mut self) -> Result<(), &'static str> { + self.states = self.states.saturating_add(1); + if self.states > MAX_MATCH_STATES { + self.exhausted = true; + return Err("content model matching exceeded its state limit"); + } + Ok(()) + } +} + +/// End positions after `cp` matches a prefix of `seq[i..]`. +/// +/// The positions of each group are worked out once per start position (`memo`): nested repetitions +/// such as `((a*)*)*` ask the inner group for the same positions again and again, which is +/// exponential in their depth without it. +fn ends(cp: &Cp, seq: &[&str], i: usize, memo: &mut EndsMemo, budget: &mut MatchBudget) -> Result, &'static str> { + budget.spend(1, 0)?; + if let Cp::Name(n) = cp { + if seq.get(i) == Some(&n.as_str()) { + budget.spend(0, 1)?; + return Ok(vec![i + 1]); + } + return Ok(vec![]); + } + let key = (std::ptr::from_ref(cp) as usize, i); + if let Some(done) = memo.get(&key) { + budget.spend(0, done.len())?; + return Ok(done.clone()); + } + let mut out: Vec = match cp { + Cp::Name(_) => vec![], + // Keep the positions after each item a set: as a list of every path, `(a?, a?, …)` doubles per item. + Cp::Seq(items) => { + let mut at = vec![i]; + for c in items { + let mut next = Vec::new(); + for p in at { + let found = ends(c, seq, p, memo, budget)?; + budget.spend(found.len(), found.len())?; + next.extend(found); + } + next.sort_unstable(); + next.dedup(); + at = next; + } + at + } + Cp::Choice(items) => { + let mut all = Vec::new(); + for c in items { + let found = ends(c, seq, i, memo, budget)?; + budget.spend(found.len(), found.len())?; + all.extend(found); + } + all + } + Cp::Rep(c, r) => { + let mut all = if *r == '+' { vec![] } else { vec![i] }; + let mut seen: HashSet = all.iter().copied().collect(); + let mut frontier = ends(c, seq, i, memo, budget)?; + budget.spend(frontier.len(), frontier.len())?; + if *r == '?' { + all.extend(frontier); + } else { + while !frontier.is_empty() { + let mut new = Vec::new(); + for p in frontier.iter().copied() { + budget.spend(1, 0)?; + if seen.insert(p) { + budget.spend(0, 1)?; + new.push(p); + } + } + budget.spend(new.len(), new.len())?; + all.extend(&new); + let mut next = Vec::new(); + let mut next_seen = HashSet::new(); + for p in new.into_iter().filter(|&p| p > i) { + for found in ends(c, seq, p, memo, budget)? { + budget.spend(1, 0)?; + if !seen.contains(&found) && next_seen.insert(found) { + budget.spend(0, 1)?; + next.push(found); + } + } + } + // Two paths to one position are one position: kept apart they multiply per round. + next.sort_unstable(); + next.dedup(); + frontier = next; + } + } + all + } + }; + out.sort_unstable(); + out.dedup(); + budget.spend(out.len(), out.len())?; + budget.retain_state()?; + memo.insert(key, out.clone()); + Ok(out) +} + +fn show(cp: &Cp) -> String { + match cp { + Cp::Name(n) => n.clone(), + Cp::Seq(v) => format!("({})", v.iter().map(show).collect::>().join(", ")), + Cp::Choice(v) => format!("({})", v.iter().map(show).collect::>().join(" | ")), + Cp::Rep(c, r) => format!("{}{r}", show(c)), + } +} + +/// An element of the document being validated. +struct Node { + name: String, + attrs: Vec, + children: Vec, + text: bool, + content: bool, +} + +fn bounded_node(e: &quick_xml::events::BytesStart<'_>, depth: usize, nodes: &mut usize, attributes: &mut usize) -> Result { + if depth >= MAX_VALIDATION_DEPTH { + return Err(format!("XML nesting exceeds the {MAX_VALIDATION_DEPTH} level validation limit")); + } + *nodes = nodes.saturating_add(1); + if *nodes > MAX_VALIDATION_NODES { + return Err(format!("XML exceeds the {MAX_VALIDATION_NODES} node validation limit")); + } + let mut attrs = Vec::new(); + for attr in e.attributes().with_checks(true) { + let attr = attr.map_err(|err| format!("invalid attribute: {err}"))?; + attrs.push(String::from_utf8_lossy(attr.key.as_ref()).to_string()); + } + *attributes = attributes.saturating_add(attrs.len()); + if *attributes > MAX_VALIDATION_ATTRIBUTES { + return Err(format!("XML exceeds the {MAX_VALIDATION_ATTRIBUTES} attribute validation limit")); + } + Ok(Node { name: String::from_utf8_lossy(e.name().as_ref()).to_string(), attrs, children: vec![], text: false, content: false }) +} + +fn read_tree(xml: &str) -> Result { + use quick_xml::events::Event; + if xml.len() > MAX_VALIDATION_XML_BYTES { + return Err(format!("XML exceeds the {} byte validation limit", MAX_VALIDATION_XML_BYTES)); + } + let mut r = quick_xml::Reader::from_str(xml); + let mut stack: Vec = Vec::new(); + let mut root = None; + let mut nodes = 0usize; + let mut attributes = 0usize; + loop { + match r.read_event().map_err(|e| e.to_string())? { + Event::Start(e) => { + if stack.is_empty() && root.is_some() { + return Err("multiple root elements".into()); + } + let n = bounded_node(&e, stack.len(), &mut nodes, &mut attributes)?; + if let Some(parent) = stack.last_mut() { + parent.content = true; + } + stack.push(n); + } + Event::Empty(e) => { + if stack.is_empty() && root.is_some() { + return Err("multiple root elements".into()); + } + let n = bounded_node(&e, stack.len(), &mut nodes, &mut attributes)?; + match stack.last_mut() { + Some(p) => { + p.content = true; + p.children.push(n); + } + None => root = Some(n), + } + } + Event::End(e) => { + let n = stack.pop().ok_or("unbalanced end tag")?; + if n.name.as_bytes() != e.name().as_ref() { + return Err("mismatched end tag".into()); + } + match stack.last_mut() { + Some(p) => p.children.push(n), + None => root = Some(n), + } + } + Event::Text(t) => { + if let Some(p) = stack.last_mut() { + p.content = true; + if !t.iter().all(u8::is_ascii_whitespace) { + p.text = true; + } + } else if !t.iter().all(u8::is_ascii_whitespace) { + return Err("text outside the root element".into()); + } + } + Event::CData(_) => { + if let Some(p) = stack.last_mut() { + p.text = true; + p.content = true; + } + } + Event::GeneralRef(reference) => { + let name = reference.decode().map_err(|e| e.to_string())?; + let accepted = if let Some(character) = reference.resolve_char_ref().map_err(|e| e.to_string())? { + crate::is_xml_char(character) + } else { + matches!(name.as_ref(), "amp" | "lt" | "gt" | "apos" | "quot") + }; + if !accepted { + return Err(format!("unsupported general entity reference `&{name};`")); + } + if let Some(p) = stack.last_mut() { + p.text = true; + p.content = true; + } else { + return Err("reference outside the root element".into()); + } + } + Event::Comment(_) | Event::PI(_) => { + if let Some(parent) = stack.last_mut() { + parent.content = true; + } + } + Event::Eof => { + if !stack.is_empty() { + return Err("unclosed element".into()); + } + return root.ok_or_else(|| "no root element".into()); + } + _ => {} + } + } +} + +fn check(dtd: &Dtd, n: &Node, path: &str, out: &mut Vec, budget: &mut MatchBudget) { + let mut problem = |m: String| out.push(Problem { path: path.to_string(), message: m }); + match dtd.model(&n.name) { + None => problem(format!("`{}` isn't declared in the DTD", n.name)), + Some(Model::Any) => {} + Some(Model::Empty) => { + if n.content { + problem(format!("`{}` should be empty", n.name)); + } + } + Some(Model::Mixed(allowed)) => { + for c in &n.children { + if !allowed.contains(&c.name) { + problem(format!("`{}` isn't allowed in `{}`", c.name, n.name)); + } + } + } + Some(Model::Children(cp)) => { + if n.text { + problem(format!("`{}` can't contain text", n.name)); + } + let seq: Vec<&str> = n.children.iter().map(|c| c.name.as_str()).collect(); + let matched = ends(cp, &seq, 0, &mut EndsMemo::new(), budget); + match matched { + Ok(positions) if positions.contains(&seq.len()) => {} + Ok(_) => { + let got = if seq.is_empty() { "nothing".to_string() } else { seq.join(", ") }; + problem(format!("`{}` contains {got}; the DTD asks for {}", n.name, show(cp))); + } + Err(error) => { + if !budget.reported { + problem(format!("`{}`: {error}", n.name)); + budget.reported = true; + } + } + } + } + } + for a in dtd.required.get(&n.name).into_iter().flatten() { + if !n.attrs.contains(a) { + problem(format!("`{}` is missing the required attribute `{a}`", n.name)); + } + } + let mut seen: HashMap<&str, usize> = HashMap::new(); + for c in &n.children { + let k = seen.entry(c.name.as_str()).or_default(); + *k += 1; + check(dtd, c, &format!("{path}/{}[{k}]", c.name), out, budget); + } +} + +/// Validate `xml` against `dtd`; an empty list when it's valid. +/// +/// Validation rejects input above the internal byte, depth, node, attribute and content-model +/// matching budgets. Only predefined entities and numeric character references are resolved; a +/// general entity reference is reported as unsupported rather than expanded or fetched. +pub fn validate(dtd: &Dtd, xml: &str) -> Vec { + match read_tree(xml) { + Ok(root) => { + let mut out = Vec::new(); + let path = root.name.clone(); + check(dtd, &root, &path, &mut out, &mut MatchBudget::default()); + out + } + Err(e) => vec![Problem { path: String::new(), message: format!("not well-formed XML: {e}") }], + } +} + +#[cfg(test)] +mod tests { + use super::*; + + const DTD: &str = r##" + + + + + + + + + + b" kind (photo|chart) "photo">"##; + + #[test] + fn content_models_and_required_attributes() { + let dtd = parse(DTD).unwrap(); + assert_eq!(dtd.element_names().count(), 8); + let ok = r#"T <em>x</em>ab
"#; + assert_eq!(validate(&dtd, ok), vec![]); + let bad = r#"no title
x