diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..03b0e93 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,13 @@ +# See: https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file#about-the-dependabotyml-file +version: 2 + +updates: + # Configure check for outdated GitHub Actions actions in workflows. + # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/dependabot/README.md + # See: https://docs.github.com/code-security/dependabot/working-with-dependabot/keeping-your-actions-up-to-date-with-dependabot + - package-ecosystem: github-actions + directory: /.github/workflows/ + schedule: + interval: daily + labels: + - "topic: infrastructure" diff --git a/.github/workflows/check-arduino.yml b/.github/workflows/check-arduino.yml new file mode 100644 index 0000000..19bd32d --- /dev/null +++ b/.github/workflows/check-arduino.yml @@ -0,0 +1,27 @@ +name: Check Arduino + +# See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows +on: + push: + pull_request: + schedule: + # Run every Tuesday at 8 AM UTC to catch breakage caused by new rules added to Arduino Lint. + - cron: "0 8 * * TUE" + workflow_dispatch: + repository_dispatch: + +jobs: + lint: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Arduino Lint + uses: arduino/arduino-lint-action@v1 + with: + compliance: specification + library-manager: update + # Always use this setting for official repositories. Remove for 3rd party projects. + official: true diff --git a/.github/workflows/compile-examples.yml b/.github/workflows/compile-examples.yml new file mode 100644 index 0000000..5de7a2c --- /dev/null +++ b/.github/workflows/compile-examples.yml @@ -0,0 +1,112 @@ +name: Compile Examples + +on: + pull_request: + paths: + - ".github/workflows/compile-examples.yml" + - "examples/**" + - "src/**" + push: + paths: + - ".github/workflows/compile-examples.yml" + - "examples/**" + - "src/**" + +jobs: + build: + runs-on: ubuntu-latest + + env: + # libraries to install for all boards + UNIVERSAL_LIBRARIES: | + - source-path: ./ + - name: Arduino_DebugUtils + # sketch paths to compile (recursive) for all boards + UNIVERSAL_SKETCH_PATHS: | + - examples/lzssDecoder + - examples/crc32 + SKETCHES_REPORTS_PATH: sketches-reports + + strategy: + fail-fast: false + + matrix: + board: + - fqbn: arduino:samd:mkr1000 + type: mkr1000 + artifact-name-suffix: arduino-samd-mkr1000 + - fqbn: arduino:samd:mkrwifi1010 + type: nina + artifact-name-suffix: arduino-samd-mkrwifi1010 + - fqbn: arduino:samd:nano_33_iot + type: nina + artifact-name-suffix: arduino-samd-nano_33_iot + - fqbn: arduino:samd:mkrwan1300 + type: wan + artifact-name-suffix: arduino-samd-mkrwan1300 + - fqbn: arduino:samd:mkrgsm1400 + type: gsm + artifact-name-suffix: arduino-samd-mkrgsm1400 + - fqbn: arduino:samd:mkrnb1500 + type: nb + artifact-name-suffix: arduino-samd-mkrnb1500 + - fqbn: arduino:mbed_portenta:envie_m7 + type: mbed_portenta + artifact-name-suffix: arduino-mbed_portenta-envie_m7 + - fqbn: esp32:esp32:esp32 + type: esp32 + artifact-name-suffix: esp32-esp32-esp32 + - fqbn: arduino:mbed_nano:nanorp2040connect + type: nina + artifact-name-suffix: arduino-mbed_nano-nanorp2040connect + - fqbn: arduino:mbed_nicla:nicla_vision + type: mbed_nicla + artifact-name-suffix: arduino-mbed_nicla-nicla_vision + - fqbn: arduino:mbed_opta:opta + type: mbed_opta + artifact-name-suffix: arduino-mbed_opta-opta + - fqbn: arduino:mbed_giga:giga + type: mbed_giga + artifact-name-suffix: arduino-mbed_giga-giga + - fqbn: arduino:renesas_portenta:portenta_c33 + type: renesas_portenta + artifact-name-suffix: arduino-renesas_portenta-portenta_c33 + - fqbn: arduino:renesas_uno:unor4wifi + type: renesas_uno + artifact-name-suffix: arduino-renesas_uno-unor4wifi + - fqbn: arduino:esp32:nano_nora + type: arduino_esp32 + artifact-name-suffix: arduino-esp32-nano_nora + - fqbn: arduino:mbed_edge:edge_control + type: mbed_edge + artifact-name-suffix: arduino-mbed_edge-edge_control + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install ESP32 platform dependencies + if: matrix.board.type == 'esp32' + run: pip3 install pyserial + + - name: Compile examples + uses: arduino/compile-sketches@v1 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + platforms: ${{ matrix.platforms }} + fqbn: ${{ matrix.board.fqbn }} + libraries: | + ${{ env.UNIVERSAL_LIBRARIES }} + ${{ matrix.libraries }} + sketch-paths: | + ${{ env.UNIVERSAL_SKETCH_PATHS }} + ${{ matrix.sketch-paths }} + enable-deltas-report: "true" + sketches-report-path: ${{ env.SKETCHES_REPORTS_PATH }} + + - name: Save memory usage change report as artifact + if: github.event_name == 'pull_request' + uses: actions/upload-artifact@v4 + with: + name: sketches-report-${{ matrix.board.artifact-name-suffix }} + path: ${{ env.SKETCHES_REPORTS_PATH }} diff --git a/.github/workflows/report-size-deltas.yml b/.github/workflows/report-size-deltas.yml new file mode 100644 index 0000000..39e2a0a --- /dev/null +++ b/.github/workflows/report-size-deltas.yml @@ -0,0 +1,24 @@ +name: Report Size Deltas + +# See: https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows +on: + push: + paths: + - ".github/workflows/report-size-deltas.yml" + schedule: + # Run at the minimum interval allowed by GitHub Actions. + # Note: GitHub Actions periodically has outages which result in workflow failures. + # In this event, the workflows will start passing again once the service recovers. + - cron: "*/5 * * * *" + workflow_dispatch: + repository_dispatch: + +jobs: + report: + runs-on: ubuntu-latest + steps: + - name: Comment size deltas reports to PRs + uses: arduino/report-size-deltas@v1 + with: + # Regex matching the names of the workflow artifacts created by the "Compile Examples" workflow + sketches-reports-source: ^sketches-report-.+ diff --git a/.github/workflows/spell-check.yml b/.github/workflows/spell-check.yml new file mode 100644 index 0000000..4253ed8 --- /dev/null +++ b/.github/workflows/spell-check.yml @@ -0,0 +1,16 @@ +name: Spell Check + +on: + - push + - pull_request + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Spell check + uses: codespell-project/actions-codespell@master diff --git a/.github/workflows/sync-labels.yml b/.github/workflows/sync-labels.yml new file mode 100644 index 0000000..8c422de --- /dev/null +++ b/.github/workflows/sync-labels.yml @@ -0,0 +1,138 @@ +# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/sync-labels.md +name: Sync Labels + +# See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows +on: + push: + paths: + - ".github/workflows/sync-labels.ya?ml" + - ".github/label-configuration-files/*.ya?ml" + pull_request: + paths: + - ".github/workflows/sync-labels.ya?ml" + - ".github/label-configuration-files/*.ya?ml" + schedule: + # Run daily at 8 AM UTC to sync with changes to shared label configurations. + - cron: "0 8 * * *" + workflow_dispatch: + repository_dispatch: + +env: + CONFIGURATIONS_FOLDER: .github/label-configuration-files + CONFIGURATIONS_ARTIFACT: label-configuration-files + +jobs: + check: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Download JSON schema for labels configuration file + id: download-schema + uses: carlosperate/download-file-action@v1 + with: + file-url: https://raw.githubusercontent.com/arduino/tooling-project-assets/main/workflow-templates/assets/sync-labels/arduino-tooling-gh-label-configuration-schema.json + location: ${{ runner.temp }}/label-configuration-schema + + - name: Install JSON schema validator + run: | + sudo npm install \ + --global \ + ajv-cli \ + ajv-formats + + - name: Validate local labels configuration + run: | + # See: https://github.com/ajv-validator/ajv-cli#readme + ajv validate \ + --all-errors \ + -c ajv-formats \ + -s "${{ steps.download-schema.outputs.file-path }}" \ + -d "${{ env.CONFIGURATIONS_FOLDER }}/*.{yml,yaml}" + + download: + needs: check + runs-on: ubuntu-latest + + strategy: + matrix: + filename: + # Filenames of the shared configurations to apply to the repository in addition to the local configuration. + # https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/sync-labels + - universal.yml + + steps: + - name: Download + uses: carlosperate/download-file-action@v1 + with: + file-url: https://raw.githubusercontent.com/arduino/tooling-project-assets/main/workflow-templates/assets/sync-labels/${{ matrix.filename }} + + - name: Pass configuration files to next job via workflow artifact + uses: actions/upload-artifact@v4 + with: + path: | + *.yaml + *.yml + if-no-files-found: error + name: ${{ env.CONFIGURATIONS_ARTIFACT }} + + sync: + needs: download + runs-on: ubuntu-latest + + steps: + - name: Set environment variables + run: | + # See: https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable + echo "MERGED_CONFIGURATION_PATH=${{ runner.temp }}/labels.yml" >> "$GITHUB_ENV" + + - name: Determine whether to dry run + id: dry-run + if: > + github.event_name == 'pull_request' || + ( + ( + github.event_name == 'push' || + github.event_name == 'workflow_dispatch' + ) && + github.ref != format('refs/heads/{0}', github.event.repository.default_branch) + ) + run: | + # Use of this flag in the github-label-sync command will cause it to only check the validity of the + # configuration. + echo "::set-output name=flag::--dry-run" + + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Download configuration files artifact + uses: actions/download-artifact@v4 + with: + name: ${{ env.CONFIGURATIONS_ARTIFACT }} + path: ${{ env.CONFIGURATIONS_FOLDER }} + + - name: Remove unneeded artifact + uses: geekyeggo/delete-artifact@v5 + with: + name: ${{ env.CONFIGURATIONS_ARTIFACT }} + + - name: Merge label configuration files + run: | + # Merge all configuration files + shopt -s extglob + cat "${{ env.CONFIGURATIONS_FOLDER }}"/*.@(yml|yaml) > "${{ env.MERGED_CONFIGURATION_PATH }}" + + - name: Install github-label-sync + run: sudo npm install --global github-label-sync + + - name: Sync labels + env: + GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + # See: https://github.com/Financial-Times/github-label-sync + github-label-sync \ + --labels "${{ env.MERGED_CONFIGURATION_PATH }}" \ + ${{ steps.dry-run.outputs.flag }} \ + ${{ github.repository }} diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml new file mode 100644 index 0000000..5605d15 --- /dev/null +++ b/.github/workflows/unit-tests.yml @@ -0,0 +1,56 @@ +name: Unit Tests + +on: + pull_request: + paths: + - ".github/workflows/unit-tests.yml" + - 'extras/test/**' + - 'src/**' + + push: + paths: + - ".github/workflows/unit-tests.yml" + - 'extras/test/**' + - 'src/**' + +jobs: + test: + name: Run unit tests + runs-on: ubuntu-latest + + env: + COVERAGE_DATA_PATH: extras/coverage-data/coverage.info + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - uses: arduino/cpp-test-action@main + with: + runtime-paths: | + - extras/test/build/bin/testArduinoCloudUtils + coverage-exclude-paths: | + - '*/extras/test/*' + - '/usr/*' + coverage-data-path: ${{ env.COVERAGE_DATA_PATH }} + + # A token is used to avoid intermittent spurious job failures caused by rate limiting. + - name: Set up Codecov upload token + run: | + if [[ "${{ github.repository }}" == "arduino-libraries/ArduinoIoTCloud" ]]; then + # In order to avoid uploads of data from forks, only use the token for runs in the parent repo. + # Token is intentionally exposed. + # See: https://community.codecov.com/t/upload-issues-unable-to-locate-build-via-github-actions-api/3954 + CODECOV_TOKEN="47827969-3fda-4ba1-9506-e8d0834ed88c" + else + # codecov/codecov-action does unauthenticated upload if empty string is passed via the `token` input. + CODECOV_TOKEN="" + fi + echo "CODECOV_TOKEN=$CODECOV_TOKEN" >> "$GITHUB_ENV" + + - name: Upload coverage report to Codecov + uses: codecov/codecov-action@v4 + with: + file: "${{ env.COVERAGE_DATA_PATH }}" + fail_ci_if_error: true + token: ${{ env.CODECOV_TOKEN }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96633a9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,44 @@ +# Created by https://www.toptal.com/developers/gitignore/api/cmake,visualstudiocode +# Edit at https://www.toptal.com/developers/gitignore?templates=cmake,visualstudiocode + +### CMake ### +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +_deps + +### CMake Patch ### +# External projects +*-prefix/ + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets + +# Local History for Visual Studio Code +.history/ + +# Built Visual Studio Code Extensions +*.vsix + +### VisualStudioCode Patch ### +# Ignore all local history of files +.history +.ionide + +# End of https://www.toptal.com/developers/gitignore/api/cmake,visualstudiocode + +extras/test/bin/ +extras/test/DartConfiguration.tcl +extras/test/Makefile diff --git a/examples/crc32/buffer.h b/examples/crc32/buffer.h new file mode 100644 index 0000000..86dcaf9 --- /dev/null +++ b/examples/crc32/buffer.h @@ -0,0 +1,41 @@ +#pragma once + +/* + * The following buffer is an hex dump of extras/test/test_files/test-1k + * with the following command `hexdump extras/test/test_files/test-1k -e '32/1 "0x%02X, ""\n"'` + */ + +uint8_t buffer[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x07, + 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x0D, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x00, 0x0F, + 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x17, + 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x19, 0x00, 0x00, 0x00, 0x1A, 0x00, 0x00, 0x00, 0x1B, 0x00, 0x00, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x1D, 0x00, 0x00, 0x00, 0x1E, 0x00, 0x00, 0x00, 0x1F, + 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x25, 0x00, 0x00, 0x00, 0x26, 0x00, 0x00, 0x00, 0x27, + 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00, 0x2A, 0x00, 0x00, 0x00, 0x2B, 0x00, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x2D, 0x00, 0x00, 0x00, 0x2E, 0x00, 0x00, 0x00, 0x2F, + 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x31, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x35, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x37, + 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x39, 0x00, 0x00, 0x00, 0x3A, 0x00, 0x00, 0x00, 0x3B, 0x00, 0x00, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x3D, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x3F, + 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x00, 0x42, 0x00, 0x00, 0x00, 0x43, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x00, 0x45, 0x00, 0x00, 0x00, 0x46, 0x00, 0x00, 0x00, 0x47, + 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x49, 0x00, 0x00, 0x00, 0x4A, 0x00, 0x00, 0x00, 0x4B, 0x00, 0x00, 0x00, 0x4C, 0x00, 0x00, 0x00, 0x4D, 0x00, 0x00, 0x00, 0x4E, 0x00, 0x00, 0x00, 0x4F, + 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x51, 0x00, 0x00, 0x00, 0x52, 0x00, 0x00, 0x00, 0x53, 0x00, 0x00, 0x00, 0x54, 0x00, 0x00, 0x00, 0x55, 0x00, 0x00, 0x00, 0x56, 0x00, 0x00, 0x00, 0x57, + 0x00, 0x00, 0x00, 0x58, 0x00, 0x00, 0x00, 0x59, 0x00, 0x00, 0x00, 0x5A, 0x00, 0x00, 0x00, 0x5B, 0x00, 0x00, 0x00, 0x5C, 0x00, 0x00, 0x00, 0x5D, 0x00, 0x00, 0x00, 0x5E, 0x00, 0x00, 0x00, 0x5F, + 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x63, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x67, + 0x00, 0x00, 0x00, 0x68, 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x00, 0x6A, 0x00, 0x00, 0x00, 0x6B, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x6D, 0x00, 0x00, 0x00, 0x6E, 0x00, 0x00, 0x00, 0x6F, + 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x71, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00, 0x73, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x75, 0x00, 0x00, 0x00, 0x76, 0x00, 0x00, 0x00, 0x77, + 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x79, 0x00, 0x00, 0x00, 0x7A, 0x00, 0x00, 0x00, 0x7B, 0x00, 0x00, 0x00, 0x7C, 0x00, 0x00, 0x00, 0x7D, 0x00, 0x00, 0x00, 0x7E, 0x00, 0x00, 0x00, 0x7F, + 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x81, 0x00, 0x00, 0x00, 0x82, 0x00, 0x00, 0x00, 0x83, 0x00, 0x00, 0x00, 0x84, 0x00, 0x00, 0x00, 0x85, 0x00, 0x00, 0x00, 0x86, 0x00, 0x00, 0x00, 0x87, + 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00, 0x89, 0x00, 0x00, 0x00, 0x8A, 0x00, 0x00, 0x00, 0x8B, 0x00, 0x00, 0x00, 0x8C, 0x00, 0x00, 0x00, 0x8D, 0x00, 0x00, 0x00, 0x8E, 0x00, 0x00, 0x00, 0x8F, + 0x00, 0x00, 0x00, 0x90, 0x00, 0x00, 0x00, 0x91, 0x00, 0x00, 0x00, 0x92, 0x00, 0x00, 0x00, 0x93, 0x00, 0x00, 0x00, 0x94, 0x00, 0x00, 0x00, 0x95, 0x00, 0x00, 0x00, 0x96, 0x00, 0x00, 0x00, 0x97, + 0x00, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00, 0x99, 0x00, 0x00, 0x00, 0x9A, 0x00, 0x00, 0x00, 0x9B, 0x00, 0x00, 0x00, 0x9C, 0x00, 0x00, 0x00, 0x9D, 0x00, 0x00, 0x00, 0x9E, 0x00, 0x00, 0x00, 0x9F, + 0x00, 0x00, 0x00, 0xA0, 0x00, 0x00, 0x00, 0xA1, 0x00, 0x00, 0x00, 0xA2, 0x00, 0x00, 0x00, 0xA3, 0x00, 0x00, 0x00, 0xA4, 0x00, 0x00, 0x00, 0xA5, 0x00, 0x00, 0x00, 0xA6, 0x00, 0x00, 0x00, 0xA7, + 0x00, 0x00, 0x00, 0xA8, 0x00, 0x00, 0x00, 0xA9, 0x00, 0x00, 0x00, 0xAA, 0x00, 0x00, 0x00, 0xAB, 0x00, 0x00, 0x00, 0xAC, 0x00, 0x00, 0x00, 0xAD, 0x00, 0x00, 0x00, 0xAE, 0x00, 0x00, 0x00, 0xAF, + 0x00, 0x00, 0x00, 0xB0, 0x00, 0x00, 0x00, 0xB1, 0x00, 0x00, 0x00, 0xB2, 0x00, 0x00, 0x00, 0xB3, 0x00, 0x00, 0x00, 0xB4, 0x00, 0x00, 0x00, 0xB5, 0x00, 0x00, 0x00, 0xB6, 0x00, 0x00, 0x00, 0xB7, + 0x00, 0x00, 0x00, 0xB8, 0x00, 0x00, 0x00, 0xB9, 0x00, 0x00, 0x00, 0xBA, 0x00, 0x00, 0x00, 0xBB, 0x00, 0x00, 0x00, 0xBC, 0x00, 0x00, 0x00, 0xBD, 0x00, 0x00, 0x00, 0xBE, 0x00, 0x00, 0x00, 0xBF, + 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0xC1, 0x00, 0x00, 0x00, 0xC2, 0x00, 0x00, 0x00, 0xC3, 0x00, 0x00, 0x00, 0xC4, 0x00, 0x00, 0x00, 0xC5, 0x00, 0x00, 0x00, 0xC6, 0x00, 0x00, 0x00, 0xC7, + 0x00, 0x00, 0x00, 0xC8, 0x00, 0x00, 0x00, 0xC9, 0x00, 0x00, 0x00, 0xCA, 0x00, 0x00, 0x00, 0xCB, 0x00, 0x00, 0x00, 0xCC, 0x00, 0x00, 0x00, 0xCD, 0x00, 0x00, 0x00, 0xCE, 0x00, 0x00, 0x00, 0xCF, + 0x00, 0x00, 0x00, 0xD0, 0x00, 0x00, 0x00, 0xD1, 0x00, 0x00, 0x00, 0xD2, 0x00, 0x00, 0x00, 0xD3, 0x00, 0x00, 0x00, 0xD4, 0x00, 0x00, 0x00, 0xD5, 0x00, 0x00, 0x00, 0xD6, 0x00, 0x00, 0x00, 0xD7, + 0x00, 0x00, 0x00, 0xD8, 0x00, 0x00, 0x00, 0xD9, 0x00, 0x00, 0x00, 0xDA, 0x00, 0x00, 0x00, 0xDB, 0x00, 0x00, 0x00, 0xDC, 0x00, 0x00, 0x00, 0xDD, 0x00, 0x00, 0x00, 0xDE, 0x00, 0x00, 0x00, 0xDF, + 0x00, 0x00, 0x00, 0xE0, 0x00, 0x00, 0x00, 0xE1, 0x00, 0x00, 0x00, 0xE2, 0x00, 0x00, 0x00, 0xE3, 0x00, 0x00, 0x00, 0xE4, 0x00, 0x00, 0x00, 0xE5, 0x00, 0x00, 0x00, 0xE6, 0x00, 0x00, 0x00, 0xE7, + 0x00, 0x00, 0x00, 0xE8, 0x00, 0x00, 0x00, 0xE9, 0x00, 0x00, 0x00, 0xEA, 0x00, 0x00, 0x00, 0xEB, 0x00, 0x00, 0x00, 0xEC, 0x00, 0x00, 0x00, 0xED, 0x00, 0x00, 0x00, 0xEE, 0x00, 0x00, 0x00, 0xEF, + 0x00, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x00, 0xF1, 0x00, 0x00, 0x00, 0xF2, 0x00, 0x00, 0x00, 0xF3, 0x00, 0x00, 0x00, 0xF4, 0x00, 0x00, 0x00, 0xF5, 0x00, 0x00, 0x00, 0xF6, 0x00, 0x00, 0x00, 0xF7, + 0x00, 0x00, 0x00, 0xF8, 0x00, 0x00, 0x00, 0xF9, 0x00, 0x00, 0x00, 0xFA, 0x00, 0x00, 0x00, 0xFB, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0xFD, 0x00, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00, 0xFF, +}; \ No newline at end of file diff --git a/examples/crc32/crc32.ino b/examples/crc32/crc32.ino new file mode 100644 index 0000000..675c23e --- /dev/null +++ b/examples/crc32/crc32.ino @@ -0,0 +1,37 @@ +/* + This file is part of the Arduino_CloudUtils library. + + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + + +#include +#include "buffer.h" + +void setup() { + Serial.begin(9600); + while(!Serial); + + uint32_t value = 0; + uint64_t position = 0; + bool error = false; + + uint32_t crc32 = arduino::crc32::begin(); + + crc32 = arduino::crc32::update(crc32, buffer, sizeof(buffer)); + + crc32 = arduino::crc32::finalize(crc32); + + if(crc32 == 0xD4C2968B) { + Serial.println("CRC is valid"); + } else { + Serial.println("CRC is invalid"); + } + +} + +void loop() { } diff --git a/examples/lzssDecoder/lzssBuffer.h b/examples/lzssDecoder/lzssBuffer.h new file mode 100644 index 0000000..ad08418 --- /dev/null +++ b/examples/lzssDecoder/lzssBuffer.h @@ -0,0 +1,34 @@ +#pragma once + +/* + * The following buffer is an hex dump of extras/test/test_files/test-1k.lzss + * with the following command `hexdump extras/test/test_files/test-1k.lzss -e '32/1 "0x%02X, ""\n"'` + */ + +uint8_t lzssBuffer[] = { +0x80, 0x3F, 0x7A, 0x40, 0x5F, 0xCC, 0x60, 0x4F, 0xEE, 0x30, 0x37, 0xFB, 0x18, 0x23, 0xFF, 0x8C, 0x14, 0x00, 0xC6, 0x0C, 0x00, 0xE3, 0x07, 0x00, 0xB1, 0x84, 0x00, 0x78, 0xC2, 0x40, 0x4C, 0x61, +0x40, 0x2E, 0x30, 0xB0, 0x1B, 0x18, 0x60, 0x0F, 0x8C, 0x34, 0x08, 0xC6, 0x1C, 0x04, 0xE3, 0x0F, 0x02, 0xB1, 0x88, 0x01, 0x78, 0xC4, 0x40, 0xCC, 0x62, 0x40, 0x6E, 0x31, 0x30, 0x3B, 0x18, 0xA0, +0x1F, 0x8C, 0x54, 0x10, 0xC6, 0x2C, 0x08, 0xE3, 0x17, 0x04, 0xB1, 0x8C, 0x02, 0x78, 0xC6, 0x41, 0x4C, 0x63, 0x40, 0xAE, 0x31, 0xB0, 0x5B, 0x18, 0xE0, 0x2F, 0x8C, 0x74, 0x18, 0xC6, 0x3C, 0x0C, +0xE3, 0x1F, 0x06, 0xB1, 0x7E, 0xE2, 0x90, 0x83, 0x98, 0xC8, 0x81, 0xDC, 0x64, 0x60, 0xF6, 0x32, 0x40, 0x7F, 0x19, 0x28, 0x41, 0x8C, 0x98, 0x21, 0xC6, 0x4E, 0x11, 0x63, 0x28, 0x08, 0xF1, 0x94, +0x84, 0x98, 0xCA, 0x82, 0x5C, 0x65, 0x61, 0x36, 0x32, 0xC0, 0x9F, 0x19, 0x68, 0x51, 0x8C, 0xB8, 0x29, 0xC6, 0x5E, 0x15, 0x63, 0x30, 0x0A, 0xF1, 0x98, 0x85, 0x98, 0xCC, 0x82, 0xDC, 0x66, 0x61, +0x76, 0x33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F, 0x1F, 0x29, 0xC1, 0x8F, 0x98, 0xE1, 0xC7, 0xCE, 0x71, 0x63, 0xE8, 0x38, 0xF1, 0xF4, 0x9C, 0x98, 0xFA, 0x8E, 0x5C, 0x7D, 0x67, 0x36, +0x3E, 0xC3, 0x9F, 0x1F, 0x69, 0xD1, 0x8F, 0xB8, 0xE9, 0xC7, 0xDE, 0x75, 0x63, 0xF0, 0x3A, 0xF1, 0xF8, 0x9D, 0x98, 0xFC, 0x8E, 0xDC, 0x7E, 0x67, 0x76, 0x3F, 0x43, 0xBF, 0x1F, 0xA9, 0xE1, 0x8F, +0xD8, 0xF1, 0xC7, 0xEE, 0x79, 0x63, 0xF8, 0x3C, 0xF1, 0xFC, 0x9E, 0x98, 0xFE, 0x8F, 0x5C, 0x7F, 0x67, 0xB6, 0x3F, 0xC3, 0xDF, 0x1F, 0xE9, 0xF1, 0x8F, 0xF8, 0xF9, 0xC7, 0xFE, +}; \ No newline at end of file diff --git a/examples/lzssDecoder/lzssDecoder.ino b/examples/lzssDecoder/lzssDecoder.ino new file mode 100644 index 0000000..5f9b5c0 --- /dev/null +++ b/examples/lzssDecoder/lzssDecoder.ino @@ -0,0 +1,47 @@ +/* + This file is part of the Arduino_CloudUtils library. + + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + + +#include +#include "lzssBuffer.h" + +void setup() { + Serial.begin(9600); + while(!Serial); + + uint32_t value = 0; + uint64_t position = 0; + bool error = false; + + // the lzss buffer provided as test, when decompressed, are made by incrementing a 4 byte integer + // starting from 0, up to (file size-1)/4, thus it is easy to validate the correctness of the result + auto decoder = arduino::lzss::Decoder([&value, &position, &error](const uint8_t b) { + error = ((value >> ((3-(position%4))<<3)) & 0xFF) != b; + + if(position % 4 == 0 && position != 0) { + value++; + } + position++; + }); + + for(size_t i = 0; i < sizeof(lzssBuffer); i++) { + decoder.decompress(lzssBuffer + i, 1); + + if(error) { + Serial.println("Error during decompression of buffer"); + + return; + } + } + + Serial.println("Decompression completed with success"); +} + +void loop() { } diff --git a/extras/test/CMakeLists.txt b/extras/test/CMakeLists.txt new file mode 100644 index 0000000..02ab2ae --- /dev/null +++ b/extras/test/CMakeLists.txt @@ -0,0 +1,45 @@ +cmake_minimum_required(VERSION 3.5) +project(testArduinoCloudUtils) + +Include(FetchContent) + +FetchContent_Declare( + Catch2 + GIT_REPOSITORY https://github.com/catchorg/Catch2.git + GIT_TAG v3.4.0 +) + +FetchContent_MakeAvailable(Catch2) + +set(TEST_TARGET ${CMAKE_PROJECT_NAME}) + +########################################################################## + +set(CMAKE_CXX_STANDARD 11) + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + +include_directories(../../src) + +set(TEST_SRCS + src/lzss/test_decoder.cpp + src/crc32/test_crc32.cpp +) + +set(TEST_DUT_SRCS + ../../src/crc/crc32.cpp +) + +########################################################################## + +add_compile_definitions(HOST) +add_compile_options(-Wall -Wextra -Wpedantic -Werror) +add_compile_options(-Wno-cast-function-type) + +set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "--coverage") +set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "--coverage -Wno-deprecated-copy") + +add_executable( ${TEST_TARGET} ${TEST_SRCS} ${TEST_DUT_SRCS} ) +target_compile_definitions( ${TEST_TARGET} PUBLIC SOURCE_DIR="${CMAKE_SOURCE_DIR}" ) + +target_link_libraries( ${TEST_TARGET} Catch2WithMain ) diff --git a/extras/test/README.md b/extras/test/README.md new file mode 100644 index 0000000..9686642 --- /dev/null +++ b/extras/test/README.md @@ -0,0 +1,5 @@ +# adding tests + +follow guide in https://github.com/catchorg/Catch2/tree/devel/docs in order to add more tests + +Add the source file for the test in `extras/test/CMakeLists.txt` inside of `${TEST_SRCS}` variable and eventually the source file you want to test in `${TEST_DUT_SRCS}` \ No newline at end of file diff --git a/extras/test/src/crc32/test_crc32.cpp b/extras/test/src/crc32/test_crc32.cpp new file mode 100644 index 0000000..6459803 --- /dev/null +++ b/extras/test/src/crc32/test_crc32.cpp @@ -0,0 +1,39 @@ +/* + This file is part of the Arduino_CloudUtils library. + + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + +#include + +#include +#include + +SCENARIO( "Calculating a CRC32 checksum on a binary stream" ) { + GIVEN( "a binary stream" ) { + FILE *f = fopen(SOURCE_DIR "/test_files/test-64k", "r"); + REQUIRE( f != nullptr ); + + uint32_t crc32 = arduino::crc32::begin(); + + THEN( "CRC32 is calculated on the binary stream" ) { + uint8_t buf[64]; + size_t read_bytes; + do { + read_bytes = fread(buf, 1, sizeof(buf), f); + + if(read_bytes > 0) { + crc32 = arduino::crc32::update(crc32, buf, sizeof(buf)); + } + } while(read_bytes > 0); + + crc32 = arduino::crc32::finalize(crc32); + + REQUIRE( crc32 == 0x9512B7A7 ); + } + } +} diff --git a/extras/test/src/lzss/test_decoder.cpp b/extras/test/src/lzss/test_decoder.cpp new file mode 100644 index 0000000..6a75c04 --- /dev/null +++ b/extras/test/src/lzss/test_decoder.cpp @@ -0,0 +1,58 @@ +/* + This file is part of the Arduino_CloudUtils library. + + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + +#include + +#include +#include + +using namespace arduino::lzss; + +SCENARIO( "Decoding an LZSS stream of data", "[lzss::Decoder]" ) { + GIVEN( "A LZSS compressed file and an LZSS Decoder" ) { + // the lzss file provided as test when decompressed are made by incrementing a 4 byte integer + // starting from 0, up to (file size-1)/4, thus it is easy to validate the correctness of the result + + FILE *f = fopen(SOURCE_DIR "/test_files/test-64k.lzss", "r"); + + REQUIRE( f != nullptr ); + + uint32_t value = 0; + uint64_t position = 0; + + auto decoder = arduino::lzss::Decoder([&value, &position](const uint8_t b) { + REQUIRE(((value >> ((3-(position%4))<<3)) & 0xFF) == b); // make this an assertion, not a require + + if(position % 4 == 0 && position != 0) { + value++; + } + position++; + }); + + WHEN( "Decompress is called on the decoder" ) { + uint8_t buf[64]; + size_t read_bytes; + do { + read_bytes = fread(buf, 1, sizeof(buf), f); + + if(read_bytes > 0) { + decoder.decompress(buf, read_bytes); + } + } while(read_bytes > 0); + + REQUIRE(value == (1<<14) -1); + } + THEN( "There is no error on the file" ) { + REQUIRE(feof(f) == 0); + REQUIRE(ferror(f) == 0); + REQUIRE(fclose(f) == 0); + } + } +} diff --git a/extras/test/test_files/test-1k b/extras/test/test_files/test-1k new file mode 100644 index 0000000..d779470 Binary files /dev/null and b/extras/test/test_files/test-1k differ diff --git a/extras/test/test_files/test-1k.lzss b/extras/test/test_files/test-1k.lzss new file mode 100644 index 0000000..0a27d47 Binary files /dev/null and b/extras/test/test_files/test-1k.lzss differ diff --git a/extras/test/test_files/test-64k b/extras/test/test_files/test-64k new file mode 100644 index 0000000..139a1d3 Binary files /dev/null and b/extras/test/test_files/test-64k differ diff --git a/extras/test/test_files/test-64k.lzss b/extras/test/test_files/test-64k.lzss new file mode 100644 index 0000000..6468b0e Binary files /dev/null and b/extras/test/test_files/test-64k.lzss differ diff --git a/src/Arduino_CRC32.h b/src/Arduino_CRC32.h new file mode 100644 index 0000000..0067596 --- /dev/null +++ b/src/Arduino_CRC32.h @@ -0,0 +1,12 @@ +/* + This file is part of the Arduino_CloudUtils library. + + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ +#pragma once + +#include "./crc/crc32.h" \ No newline at end of file diff --git a/src/Arduino_Lzss.h b/src/Arduino_Lzss.h new file mode 100644 index 0000000..dabf065 --- /dev/null +++ b/src/Arduino_Lzss.h @@ -0,0 +1,12 @@ +/* + This file is part of the Arduino_CloudUtils library. + + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ +#pragma once + +#include "./lzss/decoder.h" \ No newline at end of file diff --git a/src/crc/crc32.cpp b/src/crc/crc32.cpp new file mode 100644 index 0000000..f0c7be2 --- /dev/null +++ b/src/crc/crc32.cpp @@ -0,0 +1,63 @@ +/* + This file is part of the Arduino_CloudUtils library. + + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + +#include "crc32.h" + +namespace arduino { namespace crc32 { + + static const uint32_t crc_table[256] = { + 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, + 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, + 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, + 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5, + 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, + 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, + 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f, + 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, + 0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, + 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01, + 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, + 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, + 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, + 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, + 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, + 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, + 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, + 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7, + 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, + 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, + 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79, + 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, + 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, + 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713, + 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, + 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, + 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, + 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, + 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, + 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, + 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d + }; + + uint32_t update(uint32_t crc, const void * data, size_t data_len) { + const unsigned char *d = (const unsigned char *)data; + unsigned int tbl_idx; + + while (data_len--) { + tbl_idx = (crc ^ *d) & 0xff; + crc = (crc_table[tbl_idx] ^ (crc >> 8)) & 0xffffffff; + d++; + } + + return crc & 0xffffffff; + } + +}} // arduino::crc32 diff --git a/src/crc/crc32.h b/src/crc/crc32.h new file mode 100644 index 0000000..ddf8240 --- /dev/null +++ b/src/crc/crc32.h @@ -0,0 +1,32 @@ +/* + This file is part of the Arduino_CloudUtils library. + + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + +#pragma once + +#include +#include + +namespace arduino { namespace crc32 { + /* + * This library contains the methods to calculate CRC32 checksums + * in order to calculate crc of a binary blob you need to: + * 1- call begin() and get an initialized crc32 value + * 2- call update() on the bynary chunks + * 3- finalize the value with finalize() + * 4- store the value or use it for comparison + */ + + typedef uint32_t crc32_t; + + inline crc32_t begin() { return 0xFFFFFFFF; } + crc32_t update(crc32_t crc, const void * data, size_t data_len); + inline crc32_t finalize(crc32_t crc) { return crc ^ 0xFFFFFFFF;} + +}} // arduino::crc32 diff --git a/src/lzss/decoder.h b/src/lzss/decoder.h new file mode 100644 index 0000000..b602b2c --- /dev/null +++ b/src/lzss/decoder.h @@ -0,0 +1,110 @@ +/* + This file is part of the Arduino_CloudUtils library. + + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + +#pragma once + +#include +#include + +namespace arduino { namespace lzss { + + enum status: uint8_t { + DONE, + IN_PROGRESS, + NOT_COMPLETED + }; + + constexpr int DefaultEI = 11; /* typically 10..13 */ + constexpr int DefaultEJ = 4; /* typically 4..5 */ + constexpr int DefaultN = (1 << DefaultEI); /* buffer size */ + constexpr int DefaultF = ((1 << DefaultEJ) + 1); /* lookahead buffer size */ + + template + class GenericDecoder { + public: + + /** + * Build an LZSS decoder by providing a callback for storing the decoded bytes + * @param putc_cbk: a callback that takes a char and stores it e.g. a callback to fwrite + */ + GenericDecoder(std::function putc_cbk); + + /** + * Build an LZSS decoder providing a callback for getting a char and putting a char + * in this way you need to call decompress with no parameters + * @param putc_cbk: a callback that takes a char and stores it e.g. a callback to fwrite + * @param getc_cbk: a callback that returns the next char to consume + * -1 means EOF, -2 means buffer is temporairly finished + */ + GenericDecoder(std::function getc_cbk, std::function putc_cbk); + + /** + * this enum describes the result of the computation of a single FSM computation + * DONE: the decompression is completed + * IN_PROGRESS: the decompression cycle completed successfully, ready to compute next + * NOT_COMPLETED: the current cycle didn't complete because the available data is not enough + */ + // enum status: uint8_t { + // DONE, + // IN_PROGRESS, + // NOT_COMPLETED + // }; + + /** + * decode the provided buffer until buffer ends, then pause the process + * @return DONE if the decompression is completed, NOT_COMPLETED if not + */ + status decompress(uint8_t* const buffer=nullptr, uint32_t size=0); + + static const int LZSS_EOF = -1; + static const int LZSS_BUFFER_EMPTY = -2; + private: + + + // algorithm specific buffer used to store text that could be later referenced and copied + uint8_t buffer[N * 2]; + + // this function gets 1 single char from the input buffer + int getc(); + uint8_t* in_buffer = nullptr; + uint32_t available = 0; + + status handle_state(); + + // get 1 bit from the available input buffer + int getbit(uint8_t n); + // the following 2 are variables used by getbits + uint32_t buf, buf_size=0; + + enum FSM_STATES: uint8_t { + FSM_0 = 0, + FSM_1 = 1, + FSM_2 = 2, + FSM_3 = 3, + FSM_EOF + } state; + + // these variable are used in a decode session and specific to the old C implementation + // there is no documentation about their meaning + int i, r; + + std::function put_char_cbk; + std::function get_char_cbk; + + inline void putc(const uint8_t c) { if(put_char_cbk) { put_char_cbk(c); } } + + // get the number of bits the FSM will require given its state + uint8_t bits_required(FSM_STATES s); + }; + + using Decoder = GenericDecoder; +}} + +#include "./decoder.ipp" diff --git a/src/lzss/decoder.ipp b/src/lzss/decoder.ipp new file mode 100644 index 0000000..817d5df --- /dev/null +++ b/src/lzss/decoder.ipp @@ -0,0 +1,164 @@ +/* + This file is part of the Arduino_CloudUtils library. + + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. + + This implementation took inspiration from https://okumuralab.org/~okumura/compression/lzss.c source code +*/ +#include + +namespace arduino { namespace lzss { + +// get the number of bits the algorithm will try to get given the state +template +uint8_t GenericDecoder::bits_required(GenericDecoder::FSM_STATES s) { + switch(s) { + case FSM_0: + return 1; + case FSM_1: + return 8; + case FSM_2: + return EI; + case FSM_3: + return EJ; + default: + return 0; + } +} + +template +GenericDecoder::GenericDecoder(std::function getc_cbk, std::function putc_cbk) +: available(0), state(FSM_0), put_char_cbk(putc_cbk), get_char_cbk(getc_cbk) { + for (int i = 0; i < N - F; i++) buffer[i] = ' '; + r = N - F; +} + + +template +GenericDecoder::GenericDecoder(std::function putc_cbk) +: available(0), state(FSM_0), put_char_cbk(putc_cbk), get_char_cbk(nullptr) { + for (int i = 0; i < N - F; i++) buffer[i] = ' '; + r = N - F; +} + +template +status GenericDecoder::handle_state() { + status res = IN_PROGRESS; + + int c = getbit(bits_required(this->state)); + + if(c == LZSS_BUFFER_EMPTY) { + res = NOT_COMPLETED; + } else if (c == LZSS_EOF) { + res = DONE; + this->state = FSM_EOF; + } else { + switch(this->state) { + case FSM_0: + if(c) { + this->state = FSM_1; + } else { + this->state = FSM_2; + } + break; + case FSM_1: + putc(c); + buffer[r++] = c; + r &= (N - 1); // equivalent to r = r % N when N is a power of 2 + + this->state = FSM_0; + break; + case FSM_2: + this->i = c; + this->state = FSM_3; + break; + case FSM_3: { + int j = c; + + // This is where the actual decompression takes place: we look into the local buffer for reuse + // of byte chunks. This can be improved by means of memcpy and by changing the putc function + // into a put_buf function in order to avoid buffering on the other end. + // TODO improve this section of code + for (int k = 0; k <= j + 1; k++) { + c = buffer[(this->i + k) & (N - 1)]; // equivalent to buffer[(i+k) % N] when N is a power of 2 + putc(c); + buffer[r++] = c; + r &= (N - 1); // equivalent to r = r % N + } + this->state = FSM_0; + + break; + } + case FSM_EOF: + break; + } + } + + return res; +} + +template +status GenericDecoder::decompress(uint8_t* const buffer, uint32_t size) { + if(!get_char_cbk) { + this->in_buffer = buffer; + this->available += size; + } + + status res = IN_PROGRESS; + + while((res = handle_state()) == IN_PROGRESS); + + this->in_buffer = nullptr; + + return res; +} + +template +int GenericDecoder::getbit(uint8_t n) { // get n bits from buffer + int x=0, c; + + // if the local bit buffer doesn't have enough bit get them + while(buf_size < n) { + switch(c=getc()) { + case LZSS_EOF: + case LZSS_BUFFER_EMPTY: + return c; + } + buf <<= 8; + + buf |= (uint8_t)c; + buf_size += sizeof(uint8_t)*8; + } + + // the result is the content of the buffer starting from msb to n successive bits + x = buf >> (buf_size-n); + + // remove from the buffer the read bits with a mask + buf &= (1<<(buf_size-n))-1; + + buf_size-=n; + + return x; +} + +template +int GenericDecoder::getc() { + int c; + + if(get_char_cbk) { + c = get_char_cbk(); + } else if(in_buffer == nullptr || available == 0) { + c = LZSS_BUFFER_EMPTY; + } else { + c = *in_buffer; + in_buffer++; + available--; + } + return c; +} + +}} // arduino::lzss