From 737773eb763046ad98d203f6578884ab44e609e0 Mon Sep 17 00:00:00 2001 From: mac Date: Wed, 11 Nov 2026 22:07:35 +0800 Subject: [PATCH] Add pinned APFS FUSE source and guarded read-only NAS probe --- HANDOFF.md | 9 +- README.md | 10 + UPSTREAM.json | 12 + packaging/Dockerfile | 11 + scripts/build-driver.sh | 13 + scripts/validate-card.py | 150 ++ vendor/apfs-fuse/.gitattributes | 63 + vendor/apfs-fuse/.gitignore | 275 ++ vendor/apfs-fuse/.gitmodules | 3 + vendor/apfs-fuse/3rdparty/lzfse/.gitignore | 6 + vendor/apfs-fuse/3rdparty/lzfse/.travis.yml | 158 ++ .../apfs-fuse/3rdparty/lzfse/CMakeLists.txt | 137 + vendor/apfs-fuse/3rdparty/lzfse/LICENSE | 27 + vendor/apfs-fuse/3rdparty/lzfse/Makefile | 64 + vendor/apfs-fuse/3rdparty/lzfse/README.md | 73 + vendor/apfs-fuse/3rdparty/lzfse/appveyor.yml | 20 + .../lzfse/lzfse.xcodeproj/project.pbxproj | 517 ++++ vendor/apfs-fuse/3rdparty/lzfse/src/lzfse.h | 136 + .../3rdparty/lzfse/src/lzfse_decode.c | 72 + .../3rdparty/lzfse/src/lzfse_decode_base.c | 630 +++++ .../3rdparty/lzfse/src/lzfse_encode.c | 163 ++ .../3rdparty/lzfse/src/lzfse_encode_base.c | 830 +++++++ .../3rdparty/lzfse/src/lzfse_encode_tables.h | 218 ++ .../apfs-fuse/3rdparty/lzfse/src/lzfse_fse.c | 216 ++ .../apfs-fuse/3rdparty/lzfse/src/lzfse_fse.h | 631 +++++ .../3rdparty/lzfse/src/lzfse_internal.h | 616 +++++ .../apfs-fuse/3rdparty/lzfse/src/lzfse_main.c | 336 +++ .../3rdparty/lzfse/src/lzfse_tunables.h | 60 + .../3rdparty/lzfse/src/lzvn_decode_base.c | 711 ++++++ .../3rdparty/lzfse/src/lzvn_decode_base.h | 68 + .../3rdparty/lzfse/src/lzvn_encode_base.c | 593 +++++ .../3rdparty/lzfse/src/lzvn_encode_base.h | 116 + .../3rdparty/lzfse/tests/round-trip.cmake | 53 + vendor/apfs-fuse/ApfsDump/Apfs.cpp | 483 ++++ vendor/apfs-fuse/ApfsDump/Dumper.cpp | 476 ++++ vendor/apfs-fuse/ApfsDump/Dumper.h | 40 + .../apfs-fuse/ApfsDumpQuick/ApfsDumpQuick.cpp | 202 ++ vendor/apfs-fuse/ApfsLib/ApfsContainer.cpp | 482 ++++ vendor/apfs-fuse/ApfsLib/ApfsContainer.h | 85 + vendor/apfs-fuse/ApfsLib/ApfsDir.cpp | 847 +++++++ vendor/apfs-fuse/ApfsLib/ApfsDir.h | 147 ++ vendor/apfs-fuse/ApfsLib/ApfsNodeMapper.cpp | 29 + vendor/apfs-fuse/ApfsLib/ApfsNodeMapper.h | 40 + .../apfs-fuse/ApfsLib/ApfsNodeMapperBTree.cpp | 137 + .../apfs-fuse/ApfsLib/ApfsNodeMapperBTree.h | 45 + vendor/apfs-fuse/ApfsLib/ApfsTypes.h | 29 + vendor/apfs-fuse/ApfsLib/ApfsVolume.cpp | 394 +++ vendor/apfs-fuse/ApfsLib/ApfsVolume.h | 71 + vendor/apfs-fuse/ApfsLib/BTree.cpp | 759 ++++++ vendor/apfs-fuse/ApfsLib/BTree.h | 209 ++ vendor/apfs-fuse/ApfsLib/BlockDumper.cpp | 2203 +++++++++++++++++ vendor/apfs-fuse/ApfsLib/BlockDumper.h | 117 + vendor/apfs-fuse/ApfsLib/CheckPointMap.cpp | 86 + vendor/apfs-fuse/ApfsLib/CheckPointMap.h | 27 + vendor/apfs-fuse/ApfsLib/Crc32.cpp | 97 + vendor/apfs-fuse/ApfsLib/Crc32.h | 47 + vendor/apfs-fuse/ApfsLib/Decmpfs.cpp | 341 +++ vendor/apfs-fuse/ApfsLib/Decmpfs.h | 36 + vendor/apfs-fuse/ApfsLib/Device.cpp | 111 + vendor/apfs-fuse/ApfsLib/Device.h | 45 + vendor/apfs-fuse/ApfsLib/DeviceDMG.cpp | 527 ++++ vendor/apfs-fuse/ApfsLib/DeviceDMG.h | 85 + vendor/apfs-fuse/ApfsLib/DeviceLinux.cpp | 94 + vendor/apfs-fuse/ApfsLib/DeviceLinux.h | 45 + vendor/apfs-fuse/ApfsLib/DeviceMac.cpp | 109 + vendor/apfs-fuse/ApfsLib/DeviceMac.h | 44 + .../apfs-fuse/ApfsLib/DeviceSparseImage.cpp | 188 ++ vendor/apfs-fuse/ApfsLib/DeviceSparseImage.h | 26 + vendor/apfs-fuse/ApfsLib/DeviceVDI.cpp | 154 ++ vendor/apfs-fuse/ApfsLib/DeviceVDI.h | 30 + vendor/apfs-fuse/ApfsLib/DeviceWinFile.cpp | 62 + vendor/apfs-fuse/ApfsLib/DeviceWinFile.h | 46 + vendor/apfs-fuse/ApfsLib/DeviceWinPhys.cpp | 91 + vendor/apfs-fuse/ApfsLib/DeviceWinPhys.h | 47 + vendor/apfs-fuse/ApfsLib/DiskImageFile.cpp | 518 ++++ vendor/apfs-fuse/ApfsLib/DiskImageFile.h | 39 + vendor/apfs-fuse/ApfsLib/DiskStruct.h | 1515 ++++++++++++ vendor/apfs-fuse/ApfsLib/Endian.h | 217 ++ vendor/apfs-fuse/ApfsLib/Global.h | 36 + vendor/apfs-fuse/ApfsLib/GptPartitionMap.cpp | 200 ++ vendor/apfs-fuse/ApfsLib/GptPartitionMap.h | 35 + vendor/apfs-fuse/ApfsLib/KeyMgmt.cpp | 741 ++++++ vendor/apfs-fuse/ApfsLib/KeyMgmt.h | 68 + vendor/apfs-fuse/ApfsLib/PList.cpp | 476 ++++ vendor/apfs-fuse/ApfsLib/PList.h | 182 ++ vendor/apfs-fuse/ApfsLib/Unicode.cpp | 328 +++ vendor/apfs-fuse/ApfsLib/Unicode.h | 10 + vendor/apfs-fuse/ApfsLib/UnicodeTables_v10.h | 1027 ++++++++ vendor/apfs-fuse/ApfsLib/Util.cpp | 792 ++++++ vendor/apfs-fuse/ApfsLib/Util.h | 62 + vendor/apfs-fuse/ApfsUtil/ApfsUtil.cpp | 156 ++ vendor/apfs-fuse/CMakeLists.txt | 142 ++ vendor/apfs-fuse/Crypto/Aes.cpp | 584 +++++ vendor/apfs-fuse/Crypto/Aes.h | 172 ++ vendor/apfs-fuse/Crypto/AesXts.cpp | 102 + vendor/apfs-fuse/Crypto/AesXts.h | 26 + vendor/apfs-fuse/Crypto/Asn1Der.cpp | 184 ++ vendor/apfs-fuse/Crypto/Asn1Der.h | 53 + vendor/apfs-fuse/Crypto/Crypto.cpp | 287 +++ vendor/apfs-fuse/Crypto/Crypto.h | 12 + vendor/apfs-fuse/Crypto/Des.cpp | 357 +++ vendor/apfs-fuse/Crypto/Des.h | 53 + vendor/apfs-fuse/Crypto/Sha1.cpp | 174 ++ vendor/apfs-fuse/Crypto/Sha1.h | 26 + vendor/apfs-fuse/Crypto/Sha256.cpp | 179 ++ vendor/apfs-fuse/Crypto/Sha256.h | 25 + vendor/apfs-fuse/Crypto/TripleDes.cpp | 111 + vendor/apfs-fuse/Crypto/TripleDes.h | 23 + vendor/apfs-fuse/LICENSE | 339 +++ vendor/apfs-fuse/README.md | 214 ++ vendor/apfs-fuse/apfsfuse/ApfsFuse.cpp | 963 +++++++ 111 files changed, 26555 insertions(+), 3 deletions(-) create mode 100644 UPSTREAM.json create mode 100644 packaging/Dockerfile create mode 100644 scripts/build-driver.sh create mode 100644 scripts/validate-card.py create mode 100644 vendor/apfs-fuse/.gitattributes create mode 100644 vendor/apfs-fuse/.gitignore create mode 100644 vendor/apfs-fuse/.gitmodules create mode 100644 vendor/apfs-fuse/3rdparty/lzfse/.gitignore create mode 100644 vendor/apfs-fuse/3rdparty/lzfse/.travis.yml create mode 100644 vendor/apfs-fuse/3rdparty/lzfse/CMakeLists.txt create mode 100644 vendor/apfs-fuse/3rdparty/lzfse/LICENSE create mode 100644 vendor/apfs-fuse/3rdparty/lzfse/Makefile create mode 100644 vendor/apfs-fuse/3rdparty/lzfse/README.md create mode 100644 vendor/apfs-fuse/3rdparty/lzfse/appveyor.yml create mode 100644 vendor/apfs-fuse/3rdparty/lzfse/lzfse.xcodeproj/project.pbxproj create mode 100644 vendor/apfs-fuse/3rdparty/lzfse/src/lzfse.h create mode 100644 vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_decode.c create mode 100644 vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_decode_base.c create mode 100644 vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_encode.c create mode 100644 vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_encode_base.c create mode 100644 vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_encode_tables.h create mode 100644 vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_fse.c create mode 100644 vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_fse.h create mode 100644 vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_internal.h create mode 100644 vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_main.c create mode 100644 vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_tunables.h create mode 100644 vendor/apfs-fuse/3rdparty/lzfse/src/lzvn_decode_base.c create mode 100644 vendor/apfs-fuse/3rdparty/lzfse/src/lzvn_decode_base.h create mode 100644 vendor/apfs-fuse/3rdparty/lzfse/src/lzvn_encode_base.c create mode 100644 vendor/apfs-fuse/3rdparty/lzfse/src/lzvn_encode_base.h create mode 100644 vendor/apfs-fuse/3rdparty/lzfse/tests/round-trip.cmake create mode 100644 vendor/apfs-fuse/ApfsDump/Apfs.cpp create mode 100644 vendor/apfs-fuse/ApfsDump/Dumper.cpp create mode 100644 vendor/apfs-fuse/ApfsDump/Dumper.h create mode 100644 vendor/apfs-fuse/ApfsDumpQuick/ApfsDumpQuick.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/ApfsContainer.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/ApfsContainer.h create mode 100644 vendor/apfs-fuse/ApfsLib/ApfsDir.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/ApfsDir.h create mode 100644 vendor/apfs-fuse/ApfsLib/ApfsNodeMapper.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/ApfsNodeMapper.h create mode 100644 vendor/apfs-fuse/ApfsLib/ApfsNodeMapperBTree.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/ApfsNodeMapperBTree.h create mode 100644 vendor/apfs-fuse/ApfsLib/ApfsTypes.h create mode 100644 vendor/apfs-fuse/ApfsLib/ApfsVolume.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/ApfsVolume.h create mode 100644 vendor/apfs-fuse/ApfsLib/BTree.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/BTree.h create mode 100644 vendor/apfs-fuse/ApfsLib/BlockDumper.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/BlockDumper.h create mode 100644 vendor/apfs-fuse/ApfsLib/CheckPointMap.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/CheckPointMap.h create mode 100644 vendor/apfs-fuse/ApfsLib/Crc32.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/Crc32.h create mode 100644 vendor/apfs-fuse/ApfsLib/Decmpfs.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/Decmpfs.h create mode 100644 vendor/apfs-fuse/ApfsLib/Device.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/Device.h create mode 100644 vendor/apfs-fuse/ApfsLib/DeviceDMG.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/DeviceDMG.h create mode 100644 vendor/apfs-fuse/ApfsLib/DeviceLinux.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/DeviceLinux.h create mode 100644 vendor/apfs-fuse/ApfsLib/DeviceMac.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/DeviceMac.h create mode 100644 vendor/apfs-fuse/ApfsLib/DeviceSparseImage.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/DeviceSparseImage.h create mode 100644 vendor/apfs-fuse/ApfsLib/DeviceVDI.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/DeviceVDI.h create mode 100644 vendor/apfs-fuse/ApfsLib/DeviceWinFile.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/DeviceWinFile.h create mode 100644 vendor/apfs-fuse/ApfsLib/DeviceWinPhys.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/DeviceWinPhys.h create mode 100644 vendor/apfs-fuse/ApfsLib/DiskImageFile.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/DiskImageFile.h create mode 100644 vendor/apfs-fuse/ApfsLib/DiskStruct.h create mode 100644 vendor/apfs-fuse/ApfsLib/Endian.h create mode 100644 vendor/apfs-fuse/ApfsLib/Global.h create mode 100644 vendor/apfs-fuse/ApfsLib/GptPartitionMap.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/GptPartitionMap.h create mode 100644 vendor/apfs-fuse/ApfsLib/KeyMgmt.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/KeyMgmt.h create mode 100644 vendor/apfs-fuse/ApfsLib/PList.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/PList.h create mode 100644 vendor/apfs-fuse/ApfsLib/Unicode.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/Unicode.h create mode 100644 vendor/apfs-fuse/ApfsLib/UnicodeTables_v10.h create mode 100644 vendor/apfs-fuse/ApfsLib/Util.cpp create mode 100644 vendor/apfs-fuse/ApfsLib/Util.h create mode 100644 vendor/apfs-fuse/ApfsUtil/ApfsUtil.cpp create mode 100644 vendor/apfs-fuse/CMakeLists.txt create mode 100644 vendor/apfs-fuse/Crypto/Aes.cpp create mode 100644 vendor/apfs-fuse/Crypto/Aes.h create mode 100644 vendor/apfs-fuse/Crypto/AesXts.cpp create mode 100644 vendor/apfs-fuse/Crypto/AesXts.h create mode 100644 vendor/apfs-fuse/Crypto/Asn1Der.cpp create mode 100644 vendor/apfs-fuse/Crypto/Asn1Der.h create mode 100644 vendor/apfs-fuse/Crypto/Crypto.cpp create mode 100644 vendor/apfs-fuse/Crypto/Crypto.h create mode 100644 vendor/apfs-fuse/Crypto/Des.cpp create mode 100644 vendor/apfs-fuse/Crypto/Des.h create mode 100644 vendor/apfs-fuse/Crypto/Sha1.cpp create mode 100644 vendor/apfs-fuse/Crypto/Sha1.h create mode 100644 vendor/apfs-fuse/Crypto/Sha256.cpp create mode 100644 vendor/apfs-fuse/Crypto/Sha256.h create mode 100644 vendor/apfs-fuse/Crypto/TripleDes.cpp create mode 100644 vendor/apfs-fuse/Crypto/TripleDes.h create mode 100644 vendor/apfs-fuse/LICENSE create mode 100644 vendor/apfs-fuse/README.md create mode 100644 vendor/apfs-fuse/apfsfuse/ApfsFuse.cpp diff --git a/HANDOFF.md b/HANDOFF.md index 88ff043..959472f 100644 --- a/HANDOFF.md +++ b/HANDOFF.md @@ -19,14 +19,17 @@ ## 工程与恢复 - 本机:`/Users/mac/Documents/dayu-git/repositories/fnos-apfs`。 -- 目标远程:`https://dayu.media/git/dayu/fnos-apfs.git`,分支 main;待创建并核验首个基线。 +- 私有远程:`https://dayu.media/git/dayu/fnos-apfs.git`,分支 main;首个基线 `08e7da7f237c1a21aedad17b7a259726b8182060` 已推送并回读核验。 +- `vendor/apfs-fuse` 保存完整源码快照,含 LZFSE 子模块内容;固定版本见 `UPSTREAM.json`。apfs-fuse `66b86bd525e8cb90f9012543be89b1f092b75cf3`,LZFSE `e634ca58b4821d9f3d560cdc6df5dec02ffc93fd`。 +- `packaging/Dockerfile` 在固定 Debian 12 基础镜像中编译 Release;`scripts/build-driver.sh` 导出驱动;`scripts/validate-card.py` 按 PARTUUID + USB 序列号核对设备,临时设置内核只读,测试后卸载并恢复原标志。 +- 验证程序检查挂载只读、样本复制 SHA-256、APFS 分区写计数和首 MiB 内容;这不是对整盘所有文件或全部 APFS 特性的兼容性保证。 - 不使用当前聊天原工作目录“大鱼调色”,不修改 NAS 网站生产源。 - 凭据在用户指定的本机安全位置,不进入工程。 ## 待完成 -1. 保存并核验远程工程基线。 -2. 固定上游源码,在隔离的 Debian 12 容器内构建 Release 只读驱动。 +1. 首次恢复克隆核验。 +2. 在隔离的 Debian 12 容器内构建 Release 只读驱动。 3. 核验设备身份及驱动只读实现,测试列目录、读取与复制内容校验;不写源卡。 4. 清理临时挂载与容器,更新证据和远程检查点。 5. 应用界面、FPK、自动识别、系统文件管理集成及真实 SSD 性能尚未实现。 diff --git a/README.md b/README.md index 32c1542..2eaa644 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,16 @@ - [HANDOFF.md](HANDOFF.md):当前状态与下一步。 - 驱动来源:[sgan81/apfs-fuse](https://github.com/sgan81/apfs-fuse),只读 FUSE 实现;后续记录固定源码版本及许可证。 +## 构建与验证 + +完整驱动快照及 LZFSE 源码位于 `vendor/`,版本见 `UPSTREAM.json`,许可证保留在各自目录内;不需要从上游重新下载源码。 + +在 x86_64 Linux + Docker 中,于工程根执行 `bash scripts/build-driver.sh`。仅容器内安装编译依赖;输出 `build/bin/apfs-fuse`、`apfsutil` 及依赖版本清单。NAS 本机需有 libfuse3、libstdc++、zlib、libbz2 的运行库。 + +验证入口为 `scripts/validate-card.py --partuuid --serial --workdir <工程根目录>`,需管理员运行。它会暂时挂载、读取最多三个 512 MiB 以内样本、将副本写入工程测试目录并比对 SHA-256,然后删除副本及卸载。不会尝试写 APFS 源盘。结果保存在工作目录 `result.json`。 + +SD 卡只适合验证基本功能,不能代表外置 SSD 的吞吐;隐藏索引文件的读取也不能当作素材导入测试。 + ## 恢复 目标远程:`https://dayu.media/git/dayu/fnos-apfs.git`,分支 `main`。 diff --git a/UPSTREAM.json b/UPSTREAM.json new file mode 100644 index 0000000..aac033e --- /dev/null +++ b/UPSTREAM.json @@ -0,0 +1,12 @@ +{ + "apfs_fuse": { + "url": "https://github.com/sgan81/apfs-fuse.git", + "commit": "66b86bd525e8cb90f9012543be89b1f092b75cf3", + "license": "GPL-2.0-or-later" + }, + "lzfse": { + "url": "https://github.com/lzfse/lzfse.git", + "commit": "e634ca58b4821d9f3d560cdc6df5dec02ffc93fd", + "license": "BSD-3-Clause" + } +} diff --git a/packaging/Dockerfile b/packaging/Dockerfile new file mode 100644 index 0000000..421b9e7 --- /dev/null +++ b/packaging/Dockerfile @@ -0,0 +1,11 @@ +FROM debian:12-slim@sha256:734a72b3d6142a71993b4bc580ca01d8ca687f77a2c422d96a961d3376b179bc +RUN apt-get update && apt-get install -y --no-install-recommends \ + cmake g++ make libfuse3-dev zlib1g-dev libbz2-dev libattr1-dev \ + && rm -rf /var/lib/apt/lists/* +COPY vendor/apfs-fuse /src/apfs-fuse +RUN cmake -S /src/apfs-fuse -B /build -DUSE_FUSE3=ON \ + && cmake --build /build --target apfs-fuse apfsutil --parallel 2 \ + && mkdir /out \ + && cp /build/apfs-fuse /build/apfsutil /out/ \ + && strip /out/apfs-fuse /out/apfsutil \ + && dpkg-query -W > /out/build-packages.txt diff --git a/scripts/build-driver.sh b/scripts/build-driver.sh new file mode 100644 index 0000000..3f89b38 --- /dev/null +++ b/scripts/build-driver.sh @@ -0,0 +1,13 @@ +#!/bin/bash +set -euo pipefail +cd "$(dirname "$0")/.." +image=dayu-fnos-apfs-validation:20261111 +container=dayu-apfs-validation-export +docker build --progress=plain --tag "$image" --file packaging/Dockerfile . +docker create --name "$container" "$image" >/dev/null +trap 'docker rm "$container" >/dev/null' EXIT +mkdir -p build/bin +docker cp "$container:/out/." build/bin/ +sha256sum build/bin/apfs-fuse build/bin/apfsutil +ldd build/bin/apfs-fuse +docker image inspect "$image" --format '{{.Id}}' diff --git a/scripts/validate-card.py b/scripts/validate-card.py new file mode 100644 index 0000000..9e73e51 --- /dev/null +++ b/scripts/validate-card.py @@ -0,0 +1,150 @@ +#!/usr/bin/env python3 +"""Read-only compatibility probe. Does not write or repair the source device.""" +import argparse +import hashlib +import json +import os +from pathlib import Path +import shutil +import stat +import subprocess +import time + + +def run(*args, timeout=30): + return subprocess.check_output(args, text=True, timeout=timeout).strip() + + +def partition_writes(device): + fields = (Path('/sys/class/block') / device.name / 'stat').read_text().split() + return {'writes_completed': int(fields[4]), 'sectors_written': int(fields[6])} + + +def sample_hash(device): + with device.open('rb', buffering=0) as stream: + return hashlib.sha256(stream.read(1024 * 1024)).hexdigest() + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--partuuid', required=True) + parser.add_argument('--serial', required=True) + parser.add_argument('--workdir', required=True, type=Path) + args = parser.parse_args() + if os.geteuid() != 0: + raise SystemExit('Run this hardware probe as root.') + work = args.workdir.resolve(strict=True) + device = (Path('/dev/disk/by-partuuid') / args.partuuid).resolve(strict=True) + if not stat.S_ISBLK(device.stat().st_mode): + raise SystemExit('Expected a block device.') + props = dict(line.split('=', 1) for line in run( + 'udevadm', 'info', '--query=property', '--name=' + str(device) + ).splitlines() if '=' in line) + if props.get('ID_BUS') != 'usb' or props.get('ID_SERIAL_SHORT') != args.serial: + raise SystemExit('USB identity mismatch; refusing to continue.') + if props.get('ID_PART_ENTRY_TYPE', '').lower() != '7c3457ef-0000-11aa-aa11-00306543ecac': + raise SystemExit('Partition is not declared APFS.') + with device.open('rb', buffering=0) as stream: + stream.seek(32) + if stream.read(4) != b'NXSB': + raise SystemExit('Missing APFS container signature.') + if subprocess.run(['findmnt', '-rn', '-S', str(device)], stdout=subprocess.DEVNULL).returncode == 0: + raise SystemExit('Source partition is already mounted.') + mount = work / 'mnt' + copies = work / 'read-check' + mount.mkdir(exist_ok=True) + if os.path.ismount(mount) or any(mount.iterdir()) or copies.exists(): + raise SystemExit('Probe paths are occupied; refusing to reuse them.') + copies.mkdir() + report = { + 'device': str(device), 'partuuid': args.partuuid, + 'source_write_stats_before': partition_writes(device), + 'source_header_sha256_before': sample_hash(device), + 'files': [], + } + original_ro = run('blockdev', '--getro', str(device)) + mounted = False + try: + subprocess.run(['blockdev', '--setro', str(device)], check=True) + report['block_read_only_during_test'] = run('blockdev', '--getro', str(device)) + # The upstream implementation hard-codes read-only device opens and mounts. + subprocess.run([str(work / 'build/bin/apfs-fuse'), '-o', 'ro,nodev,nosuid,noexec', + str(device), str(mount)], check=True, timeout=30) + mounted = os.path.ismount(mount) + if not mounted: + raise RuntimeError('Driver exited without mounting.') + report['mount'] = json.loads(run('findmnt', '-J', '-M', str(mount), + '-o', 'SOURCE,FSTYPE,OPTIONS'))['filesystems'][0] + if 'ro' not in report['mount']['options'].split(','): + raise RuntimeError('Mount is not read-only.') + user_files = [] + hidden_files = [] + scanned = 0 + for base, dirs, files in os.walk(mount, followlinks=False): + dirs.sort() + for name in sorted(files): + path = Path(base) / name + info = path.lstat() + scanned += 1 + if stat.S_ISREG(info.st_mode) and 0 < info.st_size <= 512 * 1024**2: + relative = path.relative_to(mount) + hidden = any(part.startswith('.') for part in relative.parts) + (hidden_files if hidden else user_files).append((path, info.st_size)) + if scanned >= 10000: + break + if scanned >= 10000: + break + report['scanned_files'] = scanned + report['eligible_user_files'] = len(user_files) + report['eligible_hidden_files'] = len(hidden_files) + selected = sorted(user_files or hidden_files, key=lambda item: item[1], reverse=True)[:3] + report['test_file_kind'] = 'user_files' if user_files else 'hidden_metadata_only' + for index, (source, expected_size) in enumerate(selected, 1): + target = copies / ('sample-' + str(index)) + source_hash = hashlib.sha256() + started = time.monotonic() + read_bytes = 0 + with source.open('rb') as inp, target.open('xb') as out: + while chunk := inp.read(1024 * 1024): + out.write(chunk) + source_hash.update(chunk) + read_bytes += len(chunk) + out.flush() + os.fsync(out.fileno()) + elapsed = time.monotonic() - started + destination_hash = hashlib.sha256() + with target.open('rb') as stream: + while chunk := stream.read(1024 * 1024): + destination_hash.update(chunk) + if read_bytes != expected_size or source_hash.digest() != destination_hash.digest(): + raise RuntimeError('Copied content did not match.') + report['files'].append({'index': index, 'bytes': read_bytes, + 'sha256': source_hash.hexdigest(), 'copy_matches': True, + 'elapsed_seconds': round(elapsed, 6), + 'copy_MB_per_second': round(read_bytes / elapsed / 1e6, 3)}) + report['status'] = 'passed' if selected else 'mounted_but_no_sample_files' + except Exception as error: + report['status'] = 'failed' + report['error'] = str(error) + raise + finally: + if mounted or os.path.ismount(mount): + subprocess.run(['umount', str(mount)], check=True, timeout=30) + if original_ro == '0': + subprocess.run(['blockdev', '--setrw', str(device)], check=True) + report['source_write_stats_after'] = partition_writes(device) + report['source_header_sha256_after'] = sample_hash(device) + report['source_writes_unchanged'] = report['source_write_stats_before'] == report['source_write_stats_after'] + report['source_header_unchanged'] = report['source_header_sha256_before'] == report['source_header_sha256_after'] + report['unmounted_after_test'] = not os.path.ismount(mount) + report['block_read_only_restored'] = run('blockdev', '--getro', str(device)) == original_ro + (work / 'result.json').write_text(json.dumps(report, indent=2) + '\n') + # Only this probe's destination copies are removed; source files remain untouched. + shutil.rmtree(copies) + print(json.dumps(report, indent=2)) + if not report['source_writes_unchanged'] or not report['source_header_unchanged']: + raise SystemExit('Unexpected source changes detected.') + + +if __name__ == '__main__': + main() diff --git a/vendor/apfs-fuse/.gitattributes b/vendor/apfs-fuse/.gitattributes new file mode 100644 index 0000000..1ff0c42 --- /dev/null +++ b/vendor/apfs-fuse/.gitattributes @@ -0,0 +1,63 @@ +############################################################################### +# Set default behavior to automatically normalize line endings. +############################################################################### +* text=auto + +############################################################################### +# Set default behavior for command prompt diff. +# +# This is need for earlier builds of msysgit that does not have it on by +# default for csharp files. +# Note: This is only used by command line +############################################################################### +#*.cs diff=csharp + +############################################################################### +# Set the merge driver for project and solution files +# +# Merging from the command prompt will add diff markers to the files if there +# are conflicts (Merging from VS is not affected by the settings below, in VS +# the diff markers are never inserted). Diff markers may cause the following +# file extensions to fail to load in VS. An alternative would be to treat +# these files as binary and thus will always conflict and require user +# intervention with every merge. To do so, just uncomment the entries below +############################################################################### +#*.sln merge=binary +#*.csproj merge=binary +#*.vbproj merge=binary +#*.vcxproj merge=binary +#*.vcproj merge=binary +#*.dbproj merge=binary +#*.fsproj merge=binary +#*.lsproj merge=binary +#*.wixproj merge=binary +#*.modelproj merge=binary +#*.sqlproj merge=binary +#*.wwaproj merge=binary + +############################################################################### +# behavior for image files +# +# image files are treated as binary by default. +############################################################################### +#*.jpg binary +#*.png binary +#*.gif binary + +############################################################################### +# diff behavior for common document formats +# +# Convert binary document formats to text before diffing them. This feature +# is only available from the command line. Turn it on by uncommenting the +# entries below. +############################################################################### +#*.doc diff=astextplain +#*.DOC diff=astextplain +#*.docx diff=astextplain +#*.DOCX diff=astextplain +#*.dot diff=astextplain +#*.DOT diff=astextplain +#*.pdf diff=astextplain +#*.PDF diff=astextplain +#*.rtf diff=astextplain +#*.RTF diff=astextplain diff --git a/vendor/apfs-fuse/.gitignore b/vendor/apfs-fuse/.gitignore new file mode 100644 index 0000000..a77eb87 --- /dev/null +++ b/vendor/apfs-fuse/.gitignore @@ -0,0 +1,275 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +project.fragment.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +#*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Own Files +Data/ + +# Linux +build/ +.kdev4/ +data/ +doc/ + +# Mac +.DS_Store +xcode/ + diff --git a/vendor/apfs-fuse/.gitmodules b/vendor/apfs-fuse/.gitmodules new file mode 100644 index 0000000..f926f12 --- /dev/null +++ b/vendor/apfs-fuse/.gitmodules @@ -0,0 +1,3 @@ +[submodule "3rdparty/lzfse"] + path = 3rdparty/lzfse + url = https://github.com/lzfse/lzfse.git diff --git a/vendor/apfs-fuse/3rdparty/lzfse/.gitignore b/vendor/apfs-fuse/3rdparty/lzfse/.gitignore new file mode 100644 index 0000000..d281a46 --- /dev/null +++ b/vendor/apfs-fuse/3rdparty/lzfse/.gitignore @@ -0,0 +1,6 @@ +*~ +.DS_Store +**/project.xcworkspace/ +**/xcuserdata/ +build/ + diff --git a/vendor/apfs-fuse/3rdparty/lzfse/.travis.yml b/vendor/apfs-fuse/3rdparty/lzfse/.travis.yml new file mode 100644 index 0000000..2994d80 --- /dev/null +++ b/vendor/apfs-fuse/3rdparty/lzfse/.travis.yml @@ -0,0 +1,158 @@ +language: c +sudo: false +matrix: + include: + ### + ## Linux builds using various versions of GCC. + ### + - os: linux + env: C_COMPILER=gcc-6 + addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - gcc-6 + - os: linux + env: C_COMPILER=gcc-5 + addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - gcc-5 + - os: linux + env: C_COMPILER=gcc-4.9 + addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - gcc-4.9 + - os: linux + env: C_COMPILER=gcc-4.8 + addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - gcc-4.8 + - os: linux + env: C_COMPILER=gcc-4.7 + addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - gcc-4.7 + - os: linux + env: C_COMPILER=gcc-4.6 + addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - gcc-4.6 + - os: linux + env: C_COMPILER=gcc-4.5 + addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - gcc-4.5 + - os: linux + env: C_COMPILER=gcc-4.4 + addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - gcc-4.4 + + ### + ## clang on Linux + ### + - os: linux + env: C_COMPILER=clang-3.8 + addons: + apt: + sources: + - llvm-toolchain-precise-3.8 + - ubuntu-toolchain-r-test + packages: + - clang-3.8 + - os: linux + env: C_COMPILER=clang-3.7 + addons: + apt: + sources: + - llvm-toolchain-precise-3.7 + - ubuntu-toolchain-r-test + packages: + - clang-3.7 + - os: linux + env: C_COMPILER=clang-3.6 + addons: + apt: + sources: + - llvm-toolchain-precise-3.6 + - ubuntu-toolchain-r-test + packages: + - clang-3.6 + - os: linux + env: C_COMPILER=clang-3.5 + addons: + apt: + sources: + - llvm-toolchain-precise-3.5 + - ubuntu-toolchain-r-test + packages: + - clang-3.5 + + ### + ## Sanitizers + ### + - os: linux + env: C_COMPILER=clang-3.8 SANITIZER=address + addons: + apt: + sources: + - ubuntu-toolchain-r-test + - llvm-toolchain-precise-3.8 + packages: + - clang-3.8 + - os: linux + env: C_COMPILER=clang-3.8 SANITIZER=thread + addons: + apt: + sources: + - ubuntu-toolchain-r-test + - llvm-toolchain-precise-3.8 + packages: + - clang-3.8 + - os: linux + env: C_COMPILER=clang-3.8 SANITIZER=undefined CFLAGS="-fno-sanitize-recover=undefined,integer" + addons: + apt: + sources: + - ubuntu-toolchain-r-test + - llvm-toolchain-precise-3.8 + packages: + - clang-3.8 + +before_install: +### +## If we use the matrix to set CC/CXX Travis, overwrites the values, +## so instead we use C/CXX_COMPILER, then copy the values to CC/CXX +## here (after Travis has set CC/CXX). +### +- if [ -n "${C_COMPILER}" ]; then export CC="${C_COMPILER}"; fi +- mkdir build +- cd build + +script: +- cmake --version +- cmake .. -DCMAKE_C_FLAGS="${CFLAGS}" -DENABLE_SANITIZER="${SANITIZER}" +- make VERBOSE=1 +- ctest -V diff --git a/vendor/apfs-fuse/3rdparty/lzfse/CMakeLists.txt b/vendor/apfs-fuse/3rdparty/lzfse/CMakeLists.txt new file mode 100644 index 0000000..336a68d --- /dev/null +++ b/vendor/apfs-fuse/3rdparty/lzfse/CMakeLists.txt @@ -0,0 +1,137 @@ +project(lzfse C) +cmake_minimum_required(VERSION 2.8.6) + +include(CheckCCompilerFlag) + +# If LZFSE is being bundled in another project, we don't want to +# install anything. However, we want to let people override this, so +# we'll use the LZFSE_BUNDLE_MODE variable to let them do that; just +# set it to OFF in your project before you add_subdirectory(lzfse). +get_directory_property(LZFSE_PARENT_DIRECTORY PARENT_DIRECTORY) +if("${LZFSE_BUNDLE_MODE}" STREQUAL "") + # Bundled mode hasn't been set one way or the other, set the default + # depending on whether or not we are the top-level project. + if(LZFSE_PARENT_DIRECTORY) + set(LZFSE_BUNDLE_MODE ON) + else() + set(LZFSE_BUNDLE_MODE OFF) + endif(LZFSE_PARENT_DIRECTORY) +endif() +mark_as_advanced(LZFSE_BUNDLE_MODE) + +if(NOT LZFSE_BUNDLE_MODE) + option(BUILD_SHARED_LIBS "Build shared libraries" ON) +else() + set(BUILD_SHARED_LIBS OFF) +endif() + +if (CMAKE_VERSION VERSION_GREATER 3.2) + cmake_policy (SET CMP0063 NEW) +endif () + +# Compiler flags +function(lzfse_add_compiler_flags target) + set (flags ${ARGV}) + list (REMOVE_AT flags 0) + + foreach (FLAG ${flags}) + if(CMAKE_C_COMPILER_ID STREQUAL GNU) + # Because https://gcc.gnu.org/wiki/FAQ#wnowarning + string(REGEX REPLACE "\\-Wno\\-(.+)" "-W\\1" flag_to_test "${FLAG}") + else() + set (flag_to_test ${FLAG}) + endif() + + string(REGEX REPLACE "[^a-zA-Z0-9]+" "_" test_name "CFLAG_${flag_to_test}") + + check_c_compiler_flag("${flag_to_test}" "${test_name}") + if(${${test_name}}) + set_property(TARGET "${target}" APPEND_STRING PROPERTY COMPILE_FLAGS " ${FLAG}") + endif() + endforeach() +endfunction() + +if (ENABLE_SANITIZER) + set(CMAKE_C_FLAGS " ${CMAKE_C_FLAGS} -fsanitize=${ENABLE_SANITIZER}") + set(CMAKE_CXX_FLAGS " ${CMAKE_CXX_FLAGS} -fsanitize=${ENABLE_SANITIZER}") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=${ENABLE_SANITIZER}") +endif () + +add_library(lzfse + src/lzfse_decode.c + src/lzfse_decode_base.c + src/lzfse_encode.c + src/lzfse_encode_base.c + src/lzfse_fse.c + src/lzvn_decode_base.c + src/lzvn_encode_base.c) +lzfse_add_compiler_flags(lzfse -Wall -Wno-unknown-pragmas -Wno-unused-variable) + +add_executable(lzfse_cli + src/lzfse_main.c) +target_link_libraries(lzfse_cli lzfse) +set_target_properties(lzfse_cli PROPERTIES OUTPUT_NAME lzfse) +lzfse_add_compiler_flags(lzfse_cli -Wall -Wno-unknown-pragmas -Wno-unused-variable) + +if(CMAKE_VERSION VERSION_LESS 3.1 OR CMAKE_C_COMPLIER_ID STREQUAL "Intel") + lzfse_add_compiler_flags(lzfse -std=c99) + lzfse_add_compiler_flags(lzfse_cli -std=c99) +else() + set_property(TARGET lzfse PROPERTY C_STANDARD 99) + set_property(TARGET lzfse_cli PROPERTY C_STANDARD 99) +endif() + +set_target_properties(lzfse PROPERTIES + POSITION_INDEPENDENT_CODE TRUE + C_VISIBILITY_PRESET hidden + INTERPROCEDURAL_OPTIMIZATION TRUE) + +if(BUILD_SHARED_LIBS) + set_property(TARGET lzfse APPEND PROPERTY COMPILE_DEFINITIONS LZFSE_DLL LZFSE_DLL_EXPORTS) + set_property(TARGET lzfse APPEND PROPERTY INTERFACE_COMPILE_DEFINITIONS LZFSE_DLL) +endif() + +# Installation +if(NOT LZFSE_BUNDLE_MODE) + include(GNUInstallDirs) + + install(TARGETS lzfse lzfse_cli + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}") + install(FILES src/lzfse.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") +endif() + +# Tests + +# If we're targeting Windows but not running on Windows, we need Wine +# to run the tests... +if(NOT LZFSE_DISABLE_TESTS) + if(WIN32 AND NOT CMAKE_HOST_WIN32) + find_program(LZFSE_WINE NAMES wine) + + if(NOT LZFSE_WINE) + message(STATUS "wine not found, disabling tests") + set(LZFSE_DISABLE_TESTS TRUE) + endif() + endif() +endif() + +if(NOT LZFSE_DISABLE_TESTS) + include(CTest) + enable_testing() + + file(GLOB_RECURSE + ROUNDTRIP_INPUTS + RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} + src/*) + + foreach(INPUT ${ROUNDTRIP_INPUTS}) + add_test(NAME "${LZFSE_TEST_PREFIX}roundtrip/${INPUT}" + COMMAND "${CMAKE_COMMAND}" + -DLZFSE_WRAPPER=${LZFSE_WINE} + -DLZFSE_CLI=$ + -DINPUT=${CMAKE_CURRENT_SOURCE_DIR}/${INPUT} + -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/round-trip.cmake) + endforeach() +endif() diff --git a/vendor/apfs-fuse/3rdparty/lzfse/LICENSE b/vendor/apfs-fuse/3rdparty/lzfse/LICENSE new file mode 100644 index 0000000..c81aa17 --- /dev/null +++ b/vendor/apfs-fuse/3rdparty/lzfse/LICENSE @@ -0,0 +1,27 @@ +Copyright (c) 2015-2016, Apple Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder(s) nor the names of any contributors + may be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT +SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING +IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY +OF SUCH DAMAGE. + diff --git a/vendor/apfs-fuse/3rdparty/lzfse/Makefile b/vendor/apfs-fuse/3rdparty/lzfse/Makefile new file mode 100644 index 0000000..f364a1c --- /dev/null +++ b/vendor/apfs-fuse/3rdparty/lzfse/Makefile @@ -0,0 +1,64 @@ +# Copyright (c) 2015-2016, Apple Inc. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the +# following disclaimer in the documentation and/or other materials provided with the distribution. +# +# 3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or +# promote products derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, +# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE +# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +INSTALL_PREFIX ?= /usr/local + +BUILD_DIR := ./build +BIN_DIR := $(BUILD_DIR)/bin +OBJ_DIR := $(BUILD_DIR)/obj + +LZFSE_LIB := $(BIN_DIR)/liblzfse.a +LZFSE_CMD := $(BIN_DIR)/lzfse +LIB_OBJS := $(OBJ_DIR)/lzfse_encode.o $(OBJ_DIR)/lzfse_decode.o \ + $(OBJ_DIR)/lzfse_encode_base.o $(OBJ_DIR)/lzfse_decode_base.o \ + $(OBJ_DIR)/lzvn_encode_base.o $(OBJ_DIR)/lzvn_decode_base.o \ + $(OBJ_DIR)/lzfse_fse.o +CMD_OBJS := $(OBJ_DIR)/lzfse_main.o +OBJS := $(LIB_OBJS) $(CMD_OBJS) + +CFLAGS := -Os -Wall -Wno-unknown-pragmas -Wno-unused-variable -DNDEBUG -D_POSIX_C_SOURCE -std=c99 -fvisibility=hidden + +all: $(LZFSE_LIB) $(LZFSE_CMD) $(OBJS) + +install: $(LZFSE_LIB) $(LZFSE_CMD) + @[ -d $(INSTALL_PREFIX)/include ] || mkdir -p $(INSTALL_PREFIX)/include + @[ -d $(INSTALL_PREFIX)/lib ] || mkdir -p $(INSTALL_PREFIX)/lib + @[ -d $(INSTALL_PREFIX)/bin ] || mkdir -p $(INSTALL_PREFIX)/bin + install ./src/lzfse.h $(INSTALL_PREFIX)/include/lzfse.h + install $(LZFSE_LIB) $(INSTALL_PREFIX)/lib/liblzfse.a + install $(LZFSE_CMD) $(INSTALL_PREFIX)/bin/lzfse + +$(LZFSE_LIB): $(LIB_OBJS) + @[ -d $(BIN_DIR) ] || mkdir -p $(BIN_DIR) + $(LD) -r -o $(OBJ_DIR)/liblzfse_master.o $(LIB_OBJS) + ar rvs $@ $(OBJ_DIR)/liblzfse_master.o + +$(LZFSE_CMD): $(CMD_OBJS) $(LZFSE_LIB) + @[ -d $(BIN_DIR) ] || mkdir -p $(BIN_DIR) + $(CC) $(CFLAGS) -o $@ $(CMD_OBJS) $(LZFSE_LIB) + +clean: + /bin/rm -rf $(BUILD_DIR) + +$(OBJ_DIR)/%.o: src/%.c + @[ -d $(OBJ_DIR) ] || mkdir -p $(OBJ_DIR) + $(CC) $(CFLAGS) -c $< -o $@ diff --git a/vendor/apfs-fuse/3rdparty/lzfse/README.md b/vendor/apfs-fuse/3rdparty/lzfse/README.md new file mode 100644 index 0000000..88f3f9d --- /dev/null +++ b/vendor/apfs-fuse/3rdparty/lzfse/README.md @@ -0,0 +1,73 @@ +LZFSE +===== + +This is a reference C implementation of the LZFSE compressor introduced in the +[Compression library](https://developer.apple.com/library/mac/documentation/Performance/Reference/Compression/index.html) with OS X 10.11 and iOS 9. + +LZFSE is a Lempel-Ziv style data compression algorithm using Finite State Entropy coding. +It targets similar compression rates at higher compression and decompression speed compared to deflate using zlib. + +Files +----- + README.md This file ;-) + Makefile Linux / macOS Makefile + lzfse.xcodeproj Xcode project + + src/lzfse.h Main LZFSE header + src/lzfse_tunables.h LZFSE encoder configuration + src/lzfse_internal.h LZFSE internal header + src/lzfse_decode.c LZFSE decoder API entry point + src/lzfse_encode.c LZFSE encoder API entry point + src/lzfse_decode_base.c LZFSE decoder internal functions + src/lzfse_encode_base.c LZFSE encoder internal functions + src/lzfse_encode_tables.h LZFSE encoder tables + + src/lzfse_fse.h FSE entropy encoder/decoder header + src/lzfse_fse.c FSE entropy encoder/decoder functions + + src/lzvn_decode_base.h LZVN decoder + src/lzvn_decode_base.c + src/lzvn_encode_base.h LZVN encoder + src/lzvn_encode_base.c + + src/lzfse_main.c Command line tool + +Building on OS X +---------------- + + $ xcodebuild install DSTROOT=/tmp/lzfse.dst + +Produces the following files in `/tmp/lzfse.dst`: + + usr/local/bin/lzfse command line tool + usr/local/include/lzfse.h LZFSE library header + usr/local/lib/liblzfse.a LZFSE library + +Building on Linux +----------------- + +Tested on Ubuntu 15.10 with gcc 5.2.1 and clang 3.6.2. Should work on any recent distribution. + + $ make install INSTALL_PREFIX=/tmp/lzfse.dst/usr/local + +Produces the following files in `/tmp/lzfse.dst`: + + usr/local/bin/lzfse command line tool + usr/local/include/lzfse.h LZFSE library header + usr/local/lib/liblzfse.a LZFSE library + +Building with cmake +------------------- + + $ mkdir build + $ cd build + $ cmake .. + $ make install + +Installs the header, library, and command line tool in `/usr/local`. + +Bindings +-------- + +Python: [dimkr/pylzfse](https://github.com/dimkr/pylzfse) + diff --git a/vendor/apfs-fuse/3rdparty/lzfse/appveyor.yml b/vendor/apfs-fuse/3rdparty/lzfse/appveyor.yml new file mode 100644 index 0000000..250aaac --- /dev/null +++ b/vendor/apfs-fuse/3rdparty/lzfse/appveyor.yml @@ -0,0 +1,20 @@ +environment: + matrix: + - GENERATOR: Visual Studio 14 2015 Win64 + - GENERATOR: Visual Studio 14 2015 + - GENERATOR: Visual Studio 12 2013 Win64 + - GENERATOR: Visual Studio 12 2013 + +before_build: + - ps: | + mkdir build + cd build + cmake -G "$env:GENERATOR" .. + +build_script: + - ps: | + cmake --build . --config Debug + +test_script: + - ps: | + ctest --output-on-failure --interactive-debug-mode 0 -C Debug diff --git a/vendor/apfs-fuse/3rdparty/lzfse/lzfse.xcodeproj/project.pbxproj b/vendor/apfs-fuse/3rdparty/lzfse/lzfse.xcodeproj/project.pbxproj new file mode 100644 index 0000000..6b4847e --- /dev/null +++ b/vendor/apfs-fuse/3rdparty/lzfse/lzfse.xcodeproj/project.pbxproj @@ -0,0 +1,517 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXAggregateTarget section */ + 0B1AF7D91B8D0F0B005BF60D /* all */ = { + isa = PBXAggregateTarget; + buildConfigurationList = 0B1AF7DA1B8D0F0B005BF60D /* Build configuration list for PBXAggregateTarget "all" */; + buildPhases = ( + ); + dependencies = ( + 0B1AF7DE1B8D0F16005BF60D /* PBXTargetDependency */, + 0B1AF7E01B8D0F16005BF60D /* PBXTargetDependency */, + ); + name = all; + productName = all; + }; +/* End PBXAggregateTarget section */ + +/* Begin PBXBuildFile section */ + 0B1AF7C01B8CF8A2005BF60D /* lzvn_decode_base.c in Sources */ = {isa = PBXBuildFile; fileRef = 0B1AF7B71B8CF8A2005BF60D /* lzvn_decode_base.c */; }; + 0B1AF7C41B8CF8A2005BF60D /* lzvn_encode_base.c in Sources */ = {isa = PBXBuildFile; fileRef = 0B1AF7BB1B8CF8A2005BF60D /* lzvn_encode_base.c */; }; + 0B1AF7E31B8D0F65005BF60D /* lzfse_main.c in Sources */ = {isa = PBXBuildFile; fileRef = 0B1AF7E11B8D0F59005BF60D /* lzfse_main.c */; }; + 0B1AF7E41B8D1EFD005BF60D /* liblzfse.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 0B57822C1B8BC12F002F950F /* liblzfse.a */; }; + 0B57823B1B8BC151002F950F /* lzfse_decode_base.c in Sources */ = {isa = PBXBuildFile; fileRef = 0B5782321B8BC151002F950F /* lzfse_decode_base.c */; }; + 0B57823D1B8BC151002F950F /* lzfse_decode.c in Sources */ = {isa = PBXBuildFile; fileRef = 0B5782341B8BC151002F950F /* lzfse_decode.c */; }; + 0B57823E1B8BC151002F950F /* lzfse_encode_base.c in Sources */ = {isa = PBXBuildFile; fileRef = 0B5782351B8BC151002F950F /* lzfse_encode_base.c */; }; + 0B5782401B8BC151002F950F /* lzfse_encode.c in Sources */ = {isa = PBXBuildFile; fileRef = 0B5782371B8BC151002F950F /* lzfse_encode.c */; }; + 0B5782431B8BC151002F950F /* lzfse.h in Headers */ = {isa = PBXBuildFile; fileRef = 0B57823A1B8BC151002F950F /* lzfse.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 0BB39B9F1BE140C3004F4733 /* lzfse_fse.c in Sources */ = {isa = PBXBuildFile; fileRef = 0BB39B9E1BE13F12004F4733 /* lzfse_fse.c */; }; +/* End PBXBuildFile section */ + +/* Begin PBXContainerItemProxy section */ + 0B1AF7DD1B8D0F16005BF60D /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = 0B7CB3821B8BBEA500730478 /* Project object */; + proxyType = 1; + remoteGlobalIDString = 0B57822B1B8BC12F002F950F; + remoteInfo = "lzfse lib"; + }; + 0B1AF7DF1B8D0F16005BF60D /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = 0B7CB3821B8BBEA500730478 /* Project object */; + proxyType = 1; + remoteGlobalIDString = 0B1AF7D11B8D0EBF005BF60D; + remoteInfo = "lzfse command"; + }; + 0BC009291C481C600068DBA1 /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = 0B7CB3821B8BBEA500730478 /* Project object */; + proxyType = 1; + remoteGlobalIDString = 0B57822B1B8BC12F002F950F; + remoteInfo = "lzfse lib"; + }; +/* End PBXContainerItemProxy section */ + +/* Begin PBXFileReference section */ + 0B1AF7B71B8CF8A2005BF60D /* lzvn_decode_base.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = lzvn_decode_base.c; path = src/lzvn_decode_base.c; sourceTree = ""; }; + 0B1AF7B81B8CF8A2005BF60D /* lzvn_decode_base.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = lzvn_decode_base.h; path = src/lzvn_decode_base.h; sourceTree = ""; }; + 0B1AF7BB1B8CF8A2005BF60D /* lzvn_encode_base.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = lzvn_encode_base.c; path = src/lzvn_encode_base.c; sourceTree = ""; }; + 0B1AF7BC1B8CF8A2005BF60D /* lzvn_encode_base.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = lzvn_encode_base.h; path = src/lzvn_encode_base.h; sourceTree = ""; }; + 0B1AF7CB1B8CFF58005BF60D /* Makefile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.make; path = Makefile; sourceTree = ""; }; + 0B1AF7D21B8D0EBF005BF60D /* lzfse */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = lzfse; sourceTree = BUILT_PRODUCTS_DIR; }; + 0B1AF7E11B8D0F59005BF60D /* lzfse_main.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = lzfse_main.c; path = src/lzfse_main.c; sourceTree = ""; }; + 0B494D921D18A060002BE277 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; + 0B57822C1B8BC12F002F950F /* liblzfse.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = liblzfse.a; sourceTree = BUILT_PRODUCTS_DIR; }; + 0B5782321B8BC151002F950F /* lzfse_decode_base.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = lzfse_decode_base.c; path = src/lzfse_decode_base.c; sourceTree = ""; }; + 0B5782341B8BC151002F950F /* lzfse_decode.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = lzfse_decode.c; path = src/lzfse_decode.c; sourceTree = ""; }; + 0B5782351B8BC151002F950F /* lzfse_encode_base.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = lzfse_encode_base.c; path = src/lzfse_encode_base.c; sourceTree = ""; }; + 0B5782361B8BC151002F950F /* lzfse_encode_tables.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = lzfse_encode_tables.h; path = src/lzfse_encode_tables.h; sourceTree = ""; }; + 0B5782371B8BC151002F950F /* lzfse_encode.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = lzfse_encode.c; path = src/lzfse_encode.c; sourceTree = ""; }; + 0B5782381B8BC151002F950F /* lzfse_fse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = lzfse_fse.h; path = src/lzfse_fse.h; sourceTree = ""; }; + 0B5782391B8BC151002F950F /* lzfse_internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = lzfse_internal.h; path = src/lzfse_internal.h; sourceTree = ""; }; + 0B57823A1B8BC151002F950F /* lzfse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = lzfse.h; path = src/lzfse.h; sourceTree = ""; }; + 0BA652BD1D0733B20019BFE3 /* LICENSE */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = LICENSE; sourceTree = ""; }; + 0BB39B9E1BE13F12004F4733 /* lzfse_fse.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = lzfse_fse.c; path = src/lzfse_fse.c; sourceTree = ""; }; + 6301162F1BFE759100D6CAA3 /* lzfse_tunables.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = lzfse_tunables.h; path = src/lzfse_tunables.h; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 0B1AF7CF1B8D0EBF005BF60D /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + 0B1AF7E41B8D1EFD005BF60D /* liblzfse.a in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 0B5782291B8BC12F002F950F /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 0B1AF7CD1B8D0E88005BF60D /* LZFSE command */ = { + isa = PBXGroup; + children = ( + 0B1AF7E11B8D0F59005BF60D /* lzfse_main.c */, + ); + name = "LZFSE command"; + sourceTree = ""; + }; + 0B57822D1B8BC12F002F950F /* Products */ = { + isa = PBXGroup; + children = ( + 0B57822C1B8BC12F002F950F /* liblzfse.a */, + 0B1AF7D21B8D0EBF005BF60D /* lzfse */, + ); + name = Products; + sourceTree = ""; + }; + 0B5782311B8BC13D002F950F /* LZFSE lib */ = { + isa = PBXGroup; + children = ( + 0B57823A1B8BC151002F950F /* lzfse.h */, + 6301162F1BFE759100D6CAA3 /* lzfse_tunables.h */, + 0B5782391B8BC151002F950F /* lzfse_internal.h */, + 0B5782381B8BC151002F950F /* lzfse_fse.h */, + 0BB39B9E1BE13F12004F4733 /* lzfse_fse.c */, + 0B5782361B8BC151002F950F /* lzfse_encode_tables.h */, + 0B5782351B8BC151002F950F /* lzfse_encode_base.c */, + 0B5782371B8BC151002F950F /* lzfse_encode.c */, + 0B5782321B8BC151002F950F /* lzfse_decode_base.c */, + 0B5782341B8BC151002F950F /* lzfse_decode.c */, + 0B1AF7BC1B8CF8A2005BF60D /* lzvn_encode_base.h */, + 0B1AF7BB1B8CF8A2005BF60D /* lzvn_encode_base.c */, + 0B1AF7B81B8CF8A2005BF60D /* lzvn_decode_base.h */, + 0B1AF7B71B8CF8A2005BF60D /* lzvn_decode_base.c */, + ); + name = "LZFSE lib"; + sourceTree = ""; + }; + 0B7CB3811B8BBEA500730478 = { + isa = PBXGroup; + children = ( + 0B494D921D18A060002BE277 /* README.md */, + 0BA652BD1D0733B20019BFE3 /* LICENSE */, + 0B1AF7CB1B8CFF58005BF60D /* Makefile */, + 0B1AF7CD1B8D0E88005BF60D /* LZFSE command */, + 0B5782311B8BC13D002F950F /* LZFSE lib */, + 0B57822D1B8BC12F002F950F /* Products */, + ); + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXHeadersBuildPhase section */ + 0B57822A1B8BC12F002F950F /* Headers */ = { + isa = PBXHeadersBuildPhase; + buildActionMask = 2147483647; + files = ( + 0B5782431B8BC151002F950F /* lzfse.h in Headers */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXHeadersBuildPhase section */ + +/* Begin PBXNativeTarget section */ + 0B1AF7D11B8D0EBF005BF60D /* lzfse command */ = { + isa = PBXNativeTarget; + buildConfigurationList = 0B1AF7D61B8D0EBF005BF60D /* Build configuration list for PBXNativeTarget "lzfse command" */; + buildPhases = ( + 0B1AF7CE1B8D0EBF005BF60D /* Sources */, + 0B1AF7CF1B8D0EBF005BF60D /* Frameworks */, + ); + buildRules = ( + ); + dependencies = ( + 0BC0092A1C481C600068DBA1 /* PBXTargetDependency */, + ); + name = "lzfse command"; + productName = "lzfse command"; + productReference = 0B1AF7D21B8D0EBF005BF60D /* lzfse */; + productType = "com.apple.product-type.tool"; + }; + 0B57822B1B8BC12F002F950F /* lzfse lib */ = { + isa = PBXNativeTarget; + buildConfigurationList = 0B5782301B8BC12F002F950F /* Build configuration list for PBXNativeTarget "lzfse lib" */; + buildPhases = ( + 0B5782281B8BC12F002F950F /* Sources */, + 0B5782291B8BC12F002F950F /* Frameworks */, + 0B57822A1B8BC12F002F950F /* Headers */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = "lzfse lib"; + productName = lzfse; + productReference = 0B57822C1B8BC12F002F950F /* liblzfse.a */; + productType = "com.apple.product-type.library.static"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 0B7CB3821B8BBEA500730478 /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 0700; + TargetAttributes = { + 0B1AF7D11B8D0EBF005BF60D = { + CreatedOnToolsVersion = 7.0; + }; + 0B1AF7D91B8D0F0B005BF60D = { + CreatedOnToolsVersion = 7.0; + }; + 0B57822B1B8BC12F002F950F = { + CreatedOnToolsVersion = 7.0; + }; + }; + }; + buildConfigurationList = 0B7CB3851B8BBEA500730478 /* Build configuration list for PBXProject "lzfse" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = 0B7CB3811B8BBEA500730478; + productRefGroup = 0B57822D1B8BC12F002F950F /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 0B1AF7D91B8D0F0B005BF60D /* all */, + 0B57822B1B8BC12F002F950F /* lzfse lib */, + 0B1AF7D11B8D0EBF005BF60D /* lzfse command */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + 0B1AF7CE1B8D0EBF005BF60D /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 0B1AF7E31B8D0F65005BF60D /* lzfse_main.c in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 0B5782281B8BC12F002F950F /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 0B5782401B8BC151002F950F /* lzfse_encode.c in Sources */, + 0B1AF7C01B8CF8A2005BF60D /* lzvn_decode_base.c in Sources */, + 0B57823D1B8BC151002F950F /* lzfse_decode.c in Sources */, + 0B57823E1B8BC151002F950F /* lzfse_encode_base.c in Sources */, + 0B1AF7C41B8CF8A2005BF60D /* lzvn_encode_base.c in Sources */, + 0B57823B1B8BC151002F950F /* lzfse_decode_base.c in Sources */, + 0BB39B9F1BE140C3004F4733 /* lzfse_fse.c in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXTargetDependency section */ + 0B1AF7DE1B8D0F16005BF60D /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = 0B57822B1B8BC12F002F950F /* lzfse lib */; + targetProxy = 0B1AF7DD1B8D0F16005BF60D /* PBXContainerItemProxy */; + }; + 0B1AF7E01B8D0F16005BF60D /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = 0B1AF7D11B8D0EBF005BF60D /* lzfse command */; + targetProxy = 0B1AF7DF1B8D0F16005BF60D /* PBXContainerItemProxy */; + }; + 0BC0092A1C481C600068DBA1 /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = 0B57822B1B8BC12F002F950F /* lzfse lib */; + targetProxy = 0BC009291C481C600068DBA1 /* PBXContainerItemProxy */; + }; +/* End PBXTargetDependency section */ + +/* Begin XCBuildConfiguration section */ + 0B1AF7D71B8D0EBF005BF60D /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.11; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + PRODUCT_NAME = lzfse; + SDKROOT = macosx; + }; + name = Debug; + }; + 0B1AF7D81B8D0EBF005BF60D /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.11; + MTL_ENABLE_DEBUG_INFO = NO; + PRODUCT_NAME = lzfse; + SDKROOT = macosx; + }; + name = Release; + }; + 0B1AF7DB1B8D0F0B005BF60D /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + 0B1AF7DC1B8D0F0B005BF60D /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; + 0B57822E1B8BC12F002F950F /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + EXECUTABLE_PREFIX = lib; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_SYMBOLS_PRIVATE_EXTERN = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + GENERATE_MASTER_OBJECT_FILE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.11; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + PRODUCT_NAME = lzfse; + SDKROOT = macosx; + }; + name = Debug; + }; + 0B57822F1B8BC12F002F950F /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + EXECUTABLE_PREFIX = lib; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_SYMBOLS_PRIVATE_EXTERN = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + GENERATE_MASTER_OBJECT_FILE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.11; + MTL_ENABLE_DEBUG_INFO = NO; + PRODUCT_NAME = lzfse; + SDKROOT = macosx; + }; + name = Release; + }; + 0B7CB3861B8BBEA500730478 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + GCC_C_LANGUAGE_STANDARD = c99; + }; + name = Debug; + }; + 0B7CB3871B8BBEA500730478 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + GCC_C_LANGUAGE_STANDARD = c99; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 0B1AF7D61B8D0EBF005BF60D /* Build configuration list for PBXNativeTarget "lzfse command" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 0B1AF7D71B8D0EBF005BF60D /* Debug */, + 0B1AF7D81B8D0EBF005BF60D /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 0B1AF7DA1B8D0F0B005BF60D /* Build configuration list for PBXAggregateTarget "all" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 0B1AF7DB1B8D0F0B005BF60D /* Debug */, + 0B1AF7DC1B8D0F0B005BF60D /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 0B5782301B8BC12F002F950F /* Build configuration list for PBXNativeTarget "lzfse lib" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 0B57822E1B8BC12F002F950F /* Debug */, + 0B57822F1B8BC12F002F950F /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 0B7CB3851B8BBEA500730478 /* Build configuration list for PBXProject "lzfse" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 0B7CB3861B8BBEA500730478 /* Debug */, + 0B7CB3871B8BBEA500730478 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 0B7CB3821B8BBEA500730478 /* Project object */; +} diff --git a/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse.h b/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse.h new file mode 100644 index 0000000..e522cbe --- /dev/null +++ b/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse.h @@ -0,0 +1,136 @@ +/* +Copyright (c) 2015-2016, Apple Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef LZFSE_H +#define LZFSE_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#if defined(_MSC_VER) && !defined(__clang__) +# define __attribute__(X) +# pragma warning(disable : 4068) +#endif + +#if defined(LZFSE_DLL) +# if defined(_WIN32) || defined(__CYGWIN__) +# if defined(LZFSE_DLL_EXPORTS) +# define LZFSE_API __declspec(dllexport) +# else +# define LZFSE_API __declspec(dllimport) +# endif +# endif +#endif + +#if !defined(LZFSE_API) +# if __GNUC__ >= 4 +# define LZFSE_API __attribute__((visibility("default"))) +# else +# define LZFSE_API +# endif +#endif + +/*! @abstract Get the required scratch buffer size to compress using LZFSE. */ +LZFSE_API size_t lzfse_encode_scratch_size(); + +/*! @abstract Compress a buffer using LZFSE. + * + * @param dst_buffer + * Pointer to the first byte of the destination buffer. + * + * @param dst_size + * Size of the destination buffer in bytes. + * + * @param src_buffer + * Pointer to the first byte of the source buffer. + * + * @param src_size + * Size of the source buffer in bytes. + * + * @param scratch_buffer + * If non-NULL, a pointer to scratch space for the routine to use as workspace; + * the routine may use up to lzfse_encode_scratch_size( ) bytes of workspace + * during its operation, and will not perform any internal allocations. If + * NULL, the routine may allocate its own memory to use during operation via + * a single call to malloc( ), and will release it by calling free( ) prior + * to returning. For most use, passing NULL is perfectly satisfactory, but if + * you require strict control over allocation, you will want to pass an + * explicit scratch buffer. + * + * @return + * The number of bytes written to the destination buffer if the input is + * successfully compressed. If the input cannot be compressed to fit into + * the provided buffer, or an error occurs, zero is returned, and the + * contents of dst_buffer are unspecified. */ +LZFSE_API size_t lzfse_encode_buffer(uint8_t *__restrict dst_buffer, + size_t dst_size, + const uint8_t *__restrict src_buffer, + size_t src_size, + void *__restrict scratch_buffer); + +/*! @abstract Get the required scratch buffer size to decompress using LZFSE. */ +LZFSE_API size_t lzfse_decode_scratch_size(); + +/*! @abstract Decompress a buffer using LZFSE. + * + * @param dst_buffer + * Pointer to the first byte of the destination buffer. + * + * @param dst_size + * Size of the destination buffer in bytes. + * + * @param src_buffer + * Pointer to the first byte of the source buffer. + * + * @param src_size + * Size of the source buffer in bytes. + * + * @param scratch_buffer + * If non-NULL, a pointer to scratch space for the routine to use as workspace; + * the routine may use up to lzfse_decode_scratch_size( ) bytes of workspace + * during its operation, and will not perform any internal allocations. If + * NULL, the routine may allocate its own memory to use during operation via + * a single call to malloc( ), and will release it by calling free( ) prior + * to returning. For most use, passing NULL is perfectly satisfactory, but if + * you require strict control over allocation, you will want to pass an + * explicit scratch buffer. + * + * @return + * The number of bytes written to the destination buffer if the input is + * successfully decompressed. If there is not enough space in the destination + * buffer to hold the entire expanded output, only the first dst_size bytes + * will be written to the buffer and dst_size is returned. Note that this + * behavior differs from that of lzfse_encode_buffer. */ +LZFSE_API size_t lzfse_decode_buffer(uint8_t *__restrict dst_buffer, + size_t dst_size, + const uint8_t *__restrict src_buffer, + size_t src_size, + void *__restrict scratch_buffer); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LZFSE_H */ diff --git a/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_decode.c b/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_decode.c new file mode 100644 index 0000000..8fb850d --- /dev/null +++ b/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_decode.c @@ -0,0 +1,72 @@ +/* +Copyright (c) 2015-2016, Apple Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +// LZFSE decode API + +#include "lzfse.h" +#include "lzfse_internal.h" + +size_t lzfse_decode_scratch_size() { return sizeof(lzfse_decoder_state); } + +size_t lzfse_decode_buffer_with_scratch(uint8_t *__restrict dst_buffer, + size_t dst_size, const uint8_t *__restrict src_buffer, + size_t src_size, void *__restrict scratch_buffer) { + lzfse_decoder_state *s = (lzfse_decoder_state *)scratch_buffer; + memset(s, 0x00, sizeof(*s)); + + // Initialize state + s->src = src_buffer; + s->src_begin = src_buffer; + s->src_end = s->src + src_size; + s->dst = dst_buffer; + s->dst_begin = dst_buffer; + s->dst_end = dst_buffer + dst_size; + + // Decode + int status = lzfse_decode(s); + if (status == LZFSE_STATUS_DST_FULL) + return dst_size; + if (status != LZFSE_STATUS_OK) + return 0; // failed + return (size_t)(s->dst - dst_buffer); // bytes written +} + +size_t lzfse_decode_buffer(uint8_t *__restrict dst_buffer, size_t dst_size, + const uint8_t *__restrict src_buffer, + size_t src_size, void *__restrict scratch_buffer) { + int has_malloc = 0; + size_t ret = 0; + + // Deal with the possible NULL pointer + if (scratch_buffer == NULL) { + // +1 in case scratch size could be zero + scratch_buffer = malloc(lzfse_decode_scratch_size() + 1); + has_malloc = 1; + } + if (scratch_buffer == NULL) + return 0; + ret = lzfse_decode_buffer_with_scratch(dst_buffer, + dst_size, src_buffer, + src_size, scratch_buffer); + if (has_malloc) + free(scratch_buffer); + return ret; +} diff --git a/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_decode_base.c b/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_decode_base.c new file mode 100644 index 0000000..31fe859 --- /dev/null +++ b/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_decode_base.c @@ -0,0 +1,630 @@ +/* +Copyright (c) 2015-2016, Apple Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "lzfse_internal.h" +#include "lzvn_decode_base.h" + +/*! @abstract Decode an entry value from next bits of stream. + * Return \p value, and set \p *nbits to the number of bits to consume + * (starting with LSB). */ +static inline int lzfse_decode_v1_freq_value(uint32_t bits, int *nbits) { + static const int8_t lzfse_freq_nbits_table[32] = { + 2, 3, 2, 5, 2, 3, 2, 8, 2, 3, 2, 5, 2, 3, 2, 14, + 2, 3, 2, 5, 2, 3, 2, 8, 2, 3, 2, 5, 2, 3, 2, 14}; + static const int8_t lzfse_freq_value_table[32] = { + 0, 2, 1, 4, 0, 3, 1, -1, 0, 2, 1, 5, 0, 3, 1, -1, + 0, 2, 1, 6, 0, 3, 1, -1, 0, 2, 1, 7, 0, 3, 1, -1}; + + uint32_t b = bits & 31; // lower 5 bits + int n = lzfse_freq_nbits_table[b]; + *nbits = n; + + // Special cases for > 5 bits encoding + if (n == 8) + return 8 + ((bits >> 4) & 0xf); + if (n == 14) + return 24 + ((bits >> 4) & 0x3ff); + + // <= 5 bits encoding from table + return lzfse_freq_value_table[b]; +} + +/*! @abstract Extracts up to 32 bits from a 64-bit field beginning at + * \p offset, and zero-extends them to a \p uint32_t. + * + * If we number the bits of \p v from 0 (least significant) to 63 (most + * significant), the result is bits \p offset to \p offset+nbits-1. */ +static inline uint32_t get_field(uint64_t v, int offset, int nbits) { + assert(offset + nbits < 64 && offset >= 0 && nbits <= 32); + if (nbits == 32) + return (uint32_t)(v >> offset); + return (uint32_t)((v >> offset) & ((1 << nbits) - 1)); +} + +/*! @abstract Return \c header_size field from a \c lzfse_compressed_block_header_v2. */ +static inline uint32_t +lzfse_decode_v2_header_size(const lzfse_compressed_block_header_v2 *in) { + return get_field(in->packed_fields[2], 0, 32); +} + +/*! @abstract Decode all fields from a \c lzfse_compressed_block_header_v2 to a + * \c lzfse_compressed_block_header_v1. + * @return 0 on success. + * @return -1 on failure. */ +static inline int lzfse_decode_v1(lzfse_compressed_block_header_v1 *out, + const lzfse_compressed_block_header_v2 *in) { + // Clear all fields + memset(out, 0x00, sizeof(lzfse_compressed_block_header_v1)); + + uint64_t v0 = in->packed_fields[0]; + uint64_t v1 = in->packed_fields[1]; + uint64_t v2 = in->packed_fields[2]; + + out->magic = LZFSE_COMPRESSEDV1_BLOCK_MAGIC; + out->n_raw_bytes = in->n_raw_bytes; + + // Literal state + out->n_literals = get_field(v0, 0, 20); + out->n_literal_payload_bytes = get_field(v0, 20, 20); + out->literal_bits = (int)get_field(v0, 60, 3) - 7; + out->literal_state[0] = get_field(v1, 0, 10); + out->literal_state[1] = get_field(v1, 10, 10); + out->literal_state[2] = get_field(v1, 20, 10); + out->literal_state[3] = get_field(v1, 30, 10); + + // L,M,D state + out->n_matches = get_field(v0, 40, 20); + out->n_lmd_payload_bytes = get_field(v1, 40, 20); + out->lmd_bits = (int)get_field(v1, 60, 3) - 7; + out->l_state = get_field(v2, 32, 10); + out->m_state = get_field(v2, 42, 10); + out->d_state = get_field(v2, 52, 10); + + // Total payload size + out->n_payload_bytes = + out->n_literal_payload_bytes + out->n_lmd_payload_bytes; + + // Freq tables + uint16_t *dst = &(out->l_freq[0]); + const uint8_t *src = &(in->freq[0]); + const uint8_t *src_end = + (const uint8_t *)in + get_field(v2, 0, 32); // first byte after header + uint32_t accum = 0; + int accum_nbits = 0; + + // No freq tables? + if (src_end == src) + return 0; // OK, freq tables were omitted + + for (int i = 0; i < LZFSE_ENCODE_L_SYMBOLS + LZFSE_ENCODE_M_SYMBOLS + + LZFSE_ENCODE_D_SYMBOLS + LZFSE_ENCODE_LITERAL_SYMBOLS; + i++) { + // Refill accum, one byte at a time, until we reach end of header, or accum + // is full + while (src < src_end && accum_nbits + 8 <= 32) { + accum |= (uint32_t)(*src) << accum_nbits; + accum_nbits += 8; + src++; + } + + // Decode and store value + int nbits = 0; + dst[i] = lzfse_decode_v1_freq_value(accum, &nbits); + + if (nbits > accum_nbits) + return -1; // failed + + // Consume nbits bits + accum >>= nbits; + accum_nbits -= nbits; + } + + if (accum_nbits >= 8 || src != src_end) + return -1; // we need to end up exactly at the end of header, with less than + // 8 bits in accumulator + + return 0; +} + +static inline void copy(uint8_t *dst, const uint8_t *src, size_t length) { + const uint8_t *dst_end = dst + length; + do { + copy8(dst, src); + dst += 8; + src += 8; + } while (dst < dst_end); +} + +static int lzfse_decode_lmd(lzfse_decoder_state *s) { + lzfse_compressed_block_decoder_state *bs = &(s->compressed_lzfse_block_state); + fse_state l_state = bs->l_state; + fse_state m_state = bs->m_state; + fse_state d_state = bs->d_state; + fse_in_stream in = bs->lmd_in_stream; + const uint8_t *src_start = s->src_begin; + const uint8_t *src = s->src + bs->lmd_in_buf; + const uint8_t *lit = bs->current_literal; + uint8_t *dst = s->dst; + uint32_t symbols = bs->n_matches; + int32_t L = bs->l_value; + int32_t M = bs->m_value; + int32_t D = bs->d_value; + + assert(l_state < LZFSE_ENCODE_L_STATES); + assert(m_state < LZFSE_ENCODE_M_STATES); + assert(d_state < LZFSE_ENCODE_D_STATES); + + // Number of bytes remaining in the destination buffer, minus 32 to + // provide a margin of safety for using overlarge copies on the fast path. + // This is a signed quantity, and may go negative when we are close to the + // end of the buffer. That's OK; we're careful about how we handle it + // in the slow-and-careful match execution path. + ptrdiff_t remaining_bytes = s->dst_end - dst - 32; + + // If L or M is non-zero, that means that we have already started decoding + // this block, and that we needed to interrupt decoding to get more space + // from the caller. There's a pending L, M, D triplet that we weren't + // able to completely process. Jump ahead to finish executing that symbol + // before decoding new values. + if (L || M) + goto ExecuteMatch; + + while (symbols > 0) { + int res; + // Decode the next L, M, D symbol from the input stream. + res = fse_in_flush(&in, &src, src_start); + if (res) { + return LZFSE_STATUS_ERROR; + } + L = fse_value_decode(&l_state, bs->l_decoder, &in); + assert(l_state < LZFSE_ENCODE_L_STATES); + if ((lit + L) >= (bs->literals + LZFSE_LITERALS_PER_BLOCK + 64)) { + return LZFSE_STATUS_ERROR; + } + res = fse_in_flush2(&in, &src, src_start); + if (res) { + return LZFSE_STATUS_ERROR; + } + M = fse_value_decode(&m_state, bs->m_decoder, &in); + assert(m_state < LZFSE_ENCODE_M_STATES); + res = fse_in_flush2(&in, &src, src_start); + if (res) { + return LZFSE_STATUS_ERROR; + } + int32_t new_d = fse_value_decode(&d_state, bs->d_decoder, &in); + assert(d_state < LZFSE_ENCODE_D_STATES); + D = new_d ? new_d : D; + symbols--; + + ExecuteMatch: + // Error if D is out of range, so that we avoid passing through + // uninitialized data or accesssing memory out of the destination + // buffer. + if ((uint32_t)D > dst + L - s->dst_begin) + return LZFSE_STATUS_ERROR; + + if (L + M <= remaining_bytes) { + // If we have plenty of space remaining, we can copy the literal + // and match with 16- and 32-byte operations, without worrying + // about writing off the end of the buffer. + remaining_bytes -= L + M; + copy(dst, lit, L); + dst += L; + lit += L; + // For the match, we have two paths; a fast copy by 16-bytes if + // the match distance is large enough to allow it, and a more + // careful path that applies a permutation to account for the + // possible overlap between source and destination if the distance + // is small. + if (D >= 8 || D >= M) + copy(dst, dst - D, M); + else + for (size_t i = 0; i < M; i++) + dst[i] = dst[i - D]; + dst += M; + } + + else { + // Otherwise, we are very close to the end of the destination + // buffer, so we cannot use wide copies that slop off the end + // of the region that we are copying to. First, we restore + // the true length remaining, rather than the sham value we've + // been using so far. + remaining_bytes += 32; + // Now, we process the literal. Either there's space for it + // or there isn't; if there is, we copy the whole thing and + // update all the pointers and lengths to reflect the copy. + if (L <= remaining_bytes) { + for (size_t i = 0; i < L; i++) + dst[i] = lit[i]; + dst += L; + lit += L; + remaining_bytes -= L; + L = 0; + } + // There isn't enough space to fit the whole literal. Copy as + // much of it as we can, update the pointers and the value of + // L, and report that the destination buffer is full. Note that + // we always write right up to the end of the destination buffer. + else { + for (size_t i = 0; i < remaining_bytes; i++) + dst[i] = lit[i]; + dst += remaining_bytes; + lit += remaining_bytes; + L -= remaining_bytes; + goto DestinationBufferIsFull; + } + // The match goes just like the literal does. We copy as much as + // we can byte-by-byte, and if we reach the end of the buffer + // before finishing, we return to the caller indicating that + // the buffer is full. + if (M <= remaining_bytes) { + for (size_t i = 0; i < M; i++) + dst[i] = dst[i - D]; + dst += M; + remaining_bytes -= M; + M = 0; + (void)M; // no dead store warning + // We don't need to update M = 0, because there's no partial + // symbol to continue executing. Either we're at the end of + // the block, in which case we will never need to resume with + // this state, or we're going to decode another L, M, D set, + // which will overwrite M anyway. + // + // But we still set M = 0, to maintain the post-condition. + } else { + for (size_t i = 0; i < remaining_bytes; i++) + dst[i] = dst[i - D]; + dst += remaining_bytes; + M -= remaining_bytes; + DestinationBufferIsFull: + // Because we want to be able to resume decoding where we've left + // off (even in the middle of a literal or match), we need to + // update all of the block state fields with the current values + // so that we can resume execution from this point once the + // caller has given us more space to write into. + bs->l_value = L; + bs->m_value = M; + bs->d_value = D; + bs->l_state = l_state; + bs->m_state = m_state; + bs->d_state = d_state; + bs->lmd_in_stream = in; + bs->n_matches = symbols; + bs->lmd_in_buf = (uint32_t)(src - s->src); + bs->current_literal = lit; + s->dst = dst; + return LZFSE_STATUS_DST_FULL; + } + // Restore the "sham" decremented value of remaining_bytes and + // continue to the next L, M, D triple. We'll just be back in + // the careful path again, but this only happens at the very end + // of the buffer, so a little minor inefficiency here is a good + // tradeoff for simpler code. + remaining_bytes -= 32; + } + } + // Because we've finished with the whole block, we don't need to update + // any of the blockstate fields; they will not be used again. We just + // update the destination pointer in the state object and return. + s->dst = dst; + return LZFSE_STATUS_OK; +} + +int lzfse_decode(lzfse_decoder_state *s) { + while (1) { + // Are we inside a block? + switch (s->block_magic) { + case LZFSE_NO_BLOCK_MAGIC: { + // We need at least 4 bytes of magic number to identify next block + if (s->src + 4 > s->src_end) + return LZFSE_STATUS_SRC_EMPTY; // SRC truncated + uint32_t magic = load4(s->src); + + if (magic == LZFSE_ENDOFSTREAM_BLOCK_MAGIC) { + s->src += 4; + s->end_of_stream = 1; + return LZFSE_STATUS_OK; // done + } + + if (magic == LZFSE_UNCOMPRESSED_BLOCK_MAGIC) { + if (s->src + sizeof(uncompressed_block_header) > s->src_end) + return LZFSE_STATUS_SRC_EMPTY; // SRC truncated + // Setup state for uncompressed block + uncompressed_block_decoder_state *bs = &(s->uncompressed_block_state); + bs->n_raw_bytes = + load4(s->src + offsetof(uncompressed_block_header, n_raw_bytes)); + s->src += sizeof(uncompressed_block_header); + s->block_magic = magic; + break; + } + + if (magic == LZFSE_COMPRESSEDLZVN_BLOCK_MAGIC) { + if (s->src + sizeof(lzvn_compressed_block_header) > s->src_end) + return LZFSE_STATUS_SRC_EMPTY; // SRC truncated + // Setup state for compressed LZVN block + lzvn_compressed_block_decoder_state *bs = + &(s->compressed_lzvn_block_state); + bs->n_raw_bytes = + load4(s->src + offsetof(lzvn_compressed_block_header, n_raw_bytes)); + bs->n_payload_bytes = load4( + s->src + offsetof(lzvn_compressed_block_header, n_payload_bytes)); + bs->d_prev = 0; + s->src += sizeof(lzvn_compressed_block_header); + s->block_magic = magic; + break; + } + + if (magic == LZFSE_COMPRESSEDV1_BLOCK_MAGIC || + magic == LZFSE_COMPRESSEDV2_BLOCK_MAGIC) { + lzfse_compressed_block_header_v1 header1; + size_t header_size = 0; + + // Decode compressed headers + if (magic == LZFSE_COMPRESSEDV2_BLOCK_MAGIC) { + // Check we have the fixed part of the structure + if (s->src + offsetof(lzfse_compressed_block_header_v2, freq) > s->src_end) + return LZFSE_STATUS_SRC_EMPTY; // SRC truncated + + // Get size, and check we have the entire structure + const lzfse_compressed_block_header_v2 *header2 = + (const lzfse_compressed_block_header_v2 *)s->src; // not aligned, OK + header_size = lzfse_decode_v2_header_size(header2); + if (s->src + header_size > s->src_end) + return LZFSE_STATUS_SRC_EMPTY; // SRC truncated + int decodeStatus = lzfse_decode_v1(&header1, header2); + if (decodeStatus != 0) + return LZFSE_STATUS_ERROR; // failed + } else { + if (s->src + sizeof(lzfse_compressed_block_header_v1) > s->src_end) + return LZFSE_STATUS_SRC_EMPTY; // SRC truncated + memcpy(&header1, s->src, sizeof(lzfse_compressed_block_header_v1)); + header_size = sizeof(lzfse_compressed_block_header_v1); + } + + // We require the header + entire encoded block to be present in SRC + // during the entire block decoding. + // This can be relaxed somehow, if it becomes a limiting factor, at the + // price of a more complex state maintenance. + // For DST, we can't easily require space for the entire decoded block, + // because it may expand to something very very large. + if (s->src + header_size + header1.n_literal_payload_bytes + + header1.n_lmd_payload_bytes > + s->src_end) + return LZFSE_STATUS_SRC_EMPTY; // need all encoded block + + // Sanity checks + if (lzfse_check_block_header_v1(&header1) != 0) { + return LZFSE_STATUS_ERROR; + } + + // Skip header + s->src += header_size; + + // Setup state for compressed V1 block from header + lzfse_compressed_block_decoder_state *bs = + &(s->compressed_lzfse_block_state); + bs->n_lmd_payload_bytes = header1.n_lmd_payload_bytes; + bs->n_matches = header1.n_matches; + fse_init_decoder_table(LZFSE_ENCODE_LITERAL_STATES, + LZFSE_ENCODE_LITERAL_SYMBOLS, + header1.literal_freq, bs->literal_decoder); + fse_init_value_decoder_table( + LZFSE_ENCODE_L_STATES, LZFSE_ENCODE_L_SYMBOLS, header1.l_freq, + l_extra_bits, l_base_value, bs->l_decoder); + fse_init_value_decoder_table( + LZFSE_ENCODE_M_STATES, LZFSE_ENCODE_M_SYMBOLS, header1.m_freq, + m_extra_bits, m_base_value, bs->m_decoder); + fse_init_value_decoder_table( + LZFSE_ENCODE_D_STATES, LZFSE_ENCODE_D_SYMBOLS, header1.d_freq, + d_extra_bits, d_base_value, bs->d_decoder); + + // Decode literals + { + fse_in_stream in; + const uint8_t *buf_start = s->src_begin; + s->src += header1.n_literal_payload_bytes; // skip literal payload + const uint8_t *buf = s->src; // read bits backwards from the end + if (fse_in_init(&in, header1.literal_bits, &buf, buf_start) != 0) + return LZFSE_STATUS_ERROR; + + fse_state state0 = header1.literal_state[0]; + fse_state state1 = header1.literal_state[1]; + fse_state state2 = header1.literal_state[2]; + fse_state state3 = header1.literal_state[3]; + + for (uint32_t i = 0; i < header1.n_literals; i += 4) // n_literals is multiple of 4 + { +#if FSE_IOSTREAM_64 + if (fse_in_flush(&in, &buf, buf_start) != 0) + return LZFSE_STATUS_ERROR; // [57, 64] bits + bs->literals[i + 0] = + fse_decode(&state0, bs->literal_decoder, &in); // 10b max + bs->literals[i + 1] = + fse_decode(&state1, bs->literal_decoder, &in); // 10b max + bs->literals[i + 2] = + fse_decode(&state2, bs->literal_decoder, &in); // 10b max + bs->literals[i + 3] = + fse_decode(&state3, bs->literal_decoder, &in); // 10b max +#else + if (fse_in_flush(&in, &buf, buf_start) != 0) + return LZFSE_STATUS_ERROR; // [25, 23] bits + bs->literals[i + 0] = + fse_decode(&state0, bs->literal_decoder, &in); // 10b max + bs->literals[i + 1] = + fse_decode(&state1, bs->literal_decoder, &in); // 10b max + if (fse_in_flush(&in, &buf, buf_start) != 0) + return LZFSE_STATUS_ERROR; // [25, 23] bits + bs->literals[i + 2] = + fse_decode(&state2, bs->literal_decoder, &in); // 10b max + bs->literals[i + 3] = + fse_decode(&state3, bs->literal_decoder, &in); // 10b max +#endif + } + + bs->current_literal = bs->literals; + } // literals + + // SRC is not incremented to skip the LMD payload, since we need it + // during block decode. + // We will increment SRC at the end of the block only after this point. + + // Initialize the L,M,D decode stream, do not start decoding matches + // yet, and store decoder state + { + fse_in_stream in; + // read bits backwards from the end + const uint8_t *buf = s->src + header1.n_lmd_payload_bytes; + if (fse_in_init(&in, header1.lmd_bits, &buf, s->src) != 0) + return LZFSE_STATUS_ERROR; + + bs->l_state = header1.l_state; + bs->m_state = header1.m_state; + bs->d_state = header1.d_state; + bs->lmd_in_buf = (uint32_t)(buf - s->src); + bs->l_value = bs->m_value = 0; + // Initialize D to an illegal value so we can't erroneously use + // an uninitialized "previous" value. + bs->d_value = -1; + bs->lmd_in_stream = in; + } + + s->block_magic = magic; + break; + } + + // Here we have an invalid magic number + return LZFSE_STATUS_ERROR; + } // LZFSE_NO_BLOCK_MAGIC + + case LZFSE_UNCOMPRESSED_BLOCK_MAGIC: { + uncompressed_block_decoder_state *bs = &(s->uncompressed_block_state); + + // Compute the size (in bytes) of the data that we will actually copy. + // This size is minimum(bs->n_raw_bytes, space in src, space in dst). + + uint32_t copy_size = bs->n_raw_bytes; // bytes left to copy + if (copy_size == 0) { + s->block_magic = 0; + break; + } // end of block + + if (s->src_end <= s->src) + return LZFSE_STATUS_SRC_EMPTY; // need more SRC data + const size_t src_space = s->src_end - s->src; + if (copy_size > src_space) + copy_size = (uint32_t)src_space; // limit to SRC data (> 0) + + if (s->dst_end <= s->dst) + return LZFSE_STATUS_DST_FULL; // need more DST capacity + const size_t dst_space = s->dst_end - s->dst; + if (copy_size > dst_space) + copy_size = (uint32_t)dst_space; // limit to DST capacity (> 0) + + // Now that we know that the copy size is bounded to the source and + // dest buffers, go ahead and copy the data. + // We always have copy_size > 0 here + memcpy(s->dst, s->src, copy_size); + s->src += copy_size; + s->dst += copy_size; + bs->n_raw_bytes -= copy_size; + + break; + } // LZFSE_UNCOMPRESSED_BLOCK_MAGIC + + case LZFSE_COMPRESSEDV1_BLOCK_MAGIC: + case LZFSE_COMPRESSEDV2_BLOCK_MAGIC: { + lzfse_compressed_block_decoder_state *bs = + &(s->compressed_lzfse_block_state); + // Require the entire LMD payload to be in SRC + if (s->src_end <= s->src || + bs->n_lmd_payload_bytes > (size_t)(s->src_end - s->src)) + return LZFSE_STATUS_SRC_EMPTY; + + int status = lzfse_decode_lmd(s); + if (status != LZFSE_STATUS_OK) + return status; + + s->block_magic = LZFSE_NO_BLOCK_MAGIC; + s->src += bs->n_lmd_payload_bytes; // to next block + break; + } // LZFSE_COMPRESSEDV1_BLOCK_MAGIC || LZFSE_COMPRESSEDV2_BLOCK_MAGIC + + case LZFSE_COMPRESSEDLZVN_BLOCK_MAGIC: { + lzvn_compressed_block_decoder_state *bs = + &(s->compressed_lzvn_block_state); + if (bs->n_payload_bytes > 0 && s->src_end <= s->src) + return LZFSE_STATUS_SRC_EMPTY; // need more SRC data + + // Init LZVN decoder state + lzvn_decoder_state dstate; + memset(&dstate, 0x00, sizeof(dstate)); + dstate.src = s->src; + dstate.src_end = s->src_end; + if (dstate.src_end - s->src > bs->n_payload_bytes) + dstate.src_end = s->src + bs->n_payload_bytes; // limit to payload bytes + dstate.dst_begin = s->dst_begin; + dstate.dst = s->dst; + dstate.dst_end = s->dst_end; + if (dstate.dst_end - s->dst > bs->n_raw_bytes) + dstate.dst_end = s->dst + bs->n_raw_bytes; // limit to raw bytes + dstate.d_prev = bs->d_prev; + dstate.end_of_stream = 0; + + // Run LZVN decoder + lzvn_decode(&dstate); + + // Update our state + size_t src_used = dstate.src - s->src; + size_t dst_used = dstate.dst - s->dst; + if (src_used > bs->n_payload_bytes || dst_used > bs->n_raw_bytes) + return LZFSE_STATUS_ERROR; // sanity check + s->src = dstate.src; + s->dst = dstate.dst; + bs->n_payload_bytes -= (uint32_t)src_used; + bs->n_raw_bytes -= (uint32_t)dst_used; + bs->d_prev = (uint32_t)dstate.d_prev; + + // Test end of block + if (bs->n_payload_bytes == 0 && bs->n_raw_bytes == 0 && + dstate.end_of_stream) { + s->block_magic = 0; + break; + } // block done + + // Check for invalid state + if (bs->n_payload_bytes == 0 || bs->n_raw_bytes == 0 || + dstate.end_of_stream) + return LZFSE_STATUS_ERROR; + + // Here, block is not done and state is valid, so we need more space in dst. + return LZFSE_STATUS_DST_FULL; + } + + default: + return LZFSE_STATUS_ERROR; // invalid magic + + } // switch magic + + } // block loop + + return LZFSE_STATUS_OK; +} diff --git a/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_encode.c b/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_encode.c new file mode 100644 index 0000000..4819a0f --- /dev/null +++ b/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_encode.c @@ -0,0 +1,163 @@ +/* +Copyright (c) 2015-2016, Apple Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +// LZFSE encode API + +#include "lzfse.h" +#include "lzfse_internal.h" + +size_t lzfse_encode_scratch_size() { + size_t s1 = sizeof(lzfse_encoder_state); + size_t s2 = lzvn_encode_scratch_size(); + return (s1 > s2) ? s1 : s2; // max(lzfse,lzvn) +} + +size_t lzfse_encode_buffer_with_scratch(uint8_t *__restrict dst_buffer, + size_t dst_size, const uint8_t *__restrict src_buffer, + size_t src_size, void *__restrict scratch_buffer) { + const size_t original_size = src_size; + + // If input is really really small, go directly to uncompressed buffer + // (because LZVN will refuse to encode it, and we will report a failure) + if (src_size < LZVN_ENCODE_MIN_SRC_SIZE) + goto try_uncompressed; + + // If input is too small, try encoding with LZVN + if (src_size < LZFSE_ENCODE_LZVN_THRESHOLD) { + // need header + end-of-stream marker + size_t extra_size = 4 + sizeof(lzvn_compressed_block_header); + if (dst_size <= extra_size) + goto try_uncompressed; // DST is really too small, give up + + size_t sz = lzvn_encode_buffer( + dst_buffer + sizeof(lzvn_compressed_block_header), + dst_size - extra_size, src_buffer, src_size, scratch_buffer); + if (sz == 0 || sz >= src_size) + goto try_uncompressed; // failed, or no compression, fall back to + // uncompressed block + + // If we could encode, setup header and end-of-stream marker (we left room + // for them, no need to test) + lzvn_compressed_block_header header; + header.magic = LZFSE_COMPRESSEDLZVN_BLOCK_MAGIC; + header.n_raw_bytes = (uint32_t)src_size; + header.n_payload_bytes = (uint32_t)sz; + memcpy(dst_buffer, &header, sizeof(header)); + store4(dst_buffer + sizeof(lzvn_compressed_block_header) + sz, + LZFSE_ENDOFSTREAM_BLOCK_MAGIC); + + return sz + extra_size; + } + + // Try encoding with LZFSE + { + lzfse_encoder_state *state = scratch_buffer; + memset(state, 0x00, sizeof *state); + if (lzfse_encode_init(state) != LZFSE_STATUS_OK) + goto try_uncompressed; + state->dst = dst_buffer; + state->dst_begin = dst_buffer; + state->dst_end = &dst_buffer[dst_size]; + state->src = src_buffer; + state->src_encode_i = 0; + + if (src_size >= 0xffffffffU) { + // lzfse only uses 32 bits for offsets internally, so if the input + // buffer is really huge, we need to process it in smaller chunks. + // Note that we switch over to this path for sizes much smaller + // 2GB because it's actually faster to change algorithms well before + // it's necessary for correctness. + // The first chunk, we just process normally. + const lzfse_offset encoder_block_size = 262144; + state->src_end = encoder_block_size; + if (lzfse_encode_base(state) != LZFSE_STATUS_OK) + goto try_uncompressed; + src_size -= encoder_block_size; + while (src_size >= encoder_block_size) { + // All subsequent chunks require a translation to keep the offsets + // from getting too big. Note that we are always going from + // encoder_block_size up to 2*encoder_block_size so that the + // offsets remain positive (as opposed to resetting to zero and + // having negative offsets). + state->src_end = 2 * encoder_block_size; + if (lzfse_encode_base(state) != LZFSE_STATUS_OK) + goto try_uncompressed; + lzfse_encode_translate(state, encoder_block_size); + src_size -= encoder_block_size; + } + // Set the end for the final chunk. + state->src_end = encoder_block_size + (lzfse_offset)src_size; + } + // If the source buffer is small enough to use 32-bit offsets, we simply + // encode the whole thing in a single chunk. + else + state->src_end = (lzfse_offset)src_size; + // This is either the trailing chunk (if the source file is huge), or + // the whole source file. + if (lzfse_encode_base(state) != LZFSE_STATUS_OK) + goto try_uncompressed; + if (lzfse_encode_finish(state) != LZFSE_STATUS_OK) + goto try_uncompressed; + // No error occured, return compressed size. + return state->dst - dst_buffer; + } + +try_uncompressed: + // Compression failed for some reason. If we can fit the data into the + // output buffer uncompressed, go ahead and do that instead. + if (original_size + 12 <= dst_size && original_size < INT32_MAX) { + uncompressed_block_header header = {.magic = LZFSE_UNCOMPRESSED_BLOCK_MAGIC, + .n_raw_bytes = (uint32_t)src_size}; + uint8_t *dst_end = dst_buffer; + memcpy(dst_end, &header, sizeof header); + dst_end += sizeof header; + memcpy(dst_end, src_buffer, original_size); + dst_end += original_size; + store4(dst_end, LZFSE_ENDOFSTREAM_BLOCK_MAGIC); + dst_end += 4; + return dst_end - dst_buffer; + } + + // Otherwise, there's nothing we can do, so return zero. + return 0; +} + +size_t lzfse_encode_buffer(uint8_t *__restrict dst_buffer, size_t dst_size, + const uint8_t *__restrict src_buffer, + size_t src_size, void *__restrict scratch_buffer) { + int has_malloc = 0; + size_t ret = 0; + + // Deal with the possible NULL pointer + if (scratch_buffer == NULL) { + // +1 in case scratch size could be zero + scratch_buffer = malloc(lzfse_encode_scratch_size() + 1); + has_malloc = 1; + } + if (scratch_buffer == NULL) + return 0; + ret = lzfse_encode_buffer_with_scratch(dst_buffer, + dst_size, src_buffer, + src_size, scratch_buffer); + if (has_malloc) + free(scratch_buffer); + return ret; +} diff --git a/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_encode_base.c b/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_encode_base.c new file mode 100644 index 0000000..367d7a2 --- /dev/null +++ b/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_encode_base.c @@ -0,0 +1,830 @@ +/* +Copyright (c) 2015-2016, Apple Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +// LZFSE encoder + +#include "lzfse_internal.h" +#include "lzfse_encode_tables.h" + +/*! @abstract Get hash in range [0, LZFSE_ENCODE_HASH_VALUES-1] from 4 bytes in X. */ +static inline uint32_t hashX(uint32_t x) { + return (x * 2654435761U) >> + (32 - LZFSE_ENCODE_HASH_BITS); // Knuth multiplicative hash +} + +/*! @abstract Return value with all 0 except nbits<=32 unsigned bits from V + * at bit offset OFFSET. + * V is assumed to fit on nbits bits. */ +static inline uint64_t setField(uint32_t v, int offset, int nbits) { + assert(offset + nbits < 64 && offset >= 0 && nbits <= 32); + assert(nbits == 32 || (v < (1U << nbits))); + return ((uint64_t)v << (uint64_t)offset); +} + +/*! @abstract Encode all fields, except freq, from a + * lzfse_compressed_block_header_v1 to a lzfse_compressed_block_header_v2. + * All but the header_size and freq fields of the output are modified. */ +static inline void +lzfse_encode_v1_state(lzfse_compressed_block_header_v2 *out, + const lzfse_compressed_block_header_v1 *in) { + out->magic = LZFSE_COMPRESSEDV2_BLOCK_MAGIC; + out->n_raw_bytes = in->n_raw_bytes; + + // Literal state + out->packed_fields[0] = setField(in->n_literals, 0, 20) | + setField(in->n_literal_payload_bytes, 20, 20) | + setField(in->n_matches, 40, 20) | + setField(7 + in->literal_bits, 60, 3); + out->packed_fields[1] = setField(in->literal_state[0], 0, 10) | + setField(in->literal_state[1], 10, 10) | + setField(in->literal_state[2], 20, 10) | + setField(in->literal_state[3], 30, 10) | + setField(in->n_lmd_payload_bytes, 40, 20) | + setField(7 + in->lmd_bits, 60, 3); + out->packed_fields[2] = out->packed_fields[2] // header_size already stored in v[2] + | setField(in->l_state, 32, 10) | setField(in->m_state, 42, 10) | + setField(in->d_state, 52, 10); +} + +/*! @abstract Encode an entry value in a freq table. Return bits, and sets + * *nbits to the number of bits to serialize. */ +static inline uint32_t lzfse_encode_v1_freq_value(int value, int *nbits) { + // Fixed Huffman code, bits are read from LSB. + // Note that we rely on the position of the first '0' bit providing the number + // of bits. + switch (value) { + case 0: + *nbits = 2; + return 0; // 0.0 + case 1: + *nbits = 2; + return 2; // 1.0 + case 2: + *nbits = 3; + return 1; // 0.01 + case 3: + *nbits = 3; + return 5; // 1.01 + case 4: + *nbits = 5; + return 3; // 00.011 + case 5: + *nbits = 5; + return 11; // 01.011 + case 6: + *nbits = 5; + return 19; // 10.011 + case 7: + *nbits = 5; + return 27; // 11.011 + default: + break; + } + if (value < 24) { + *nbits = 8; // 4+4 + return 7 + ((value - 8) << 4); // xxxx.0111 + } + // 24..1047 + *nbits = 14; // 4+10 + return ((value - 24) << 4) + 15; // xxxxxxxxxx.1111 +} + +/*! @abstract Encode all tables from a lzfse_compressed_block_header_v1 + * to a lzfse_compressed_block_header_v2. + * Only the header_size and freq fields of the output are modified. + * @return Size of the lzfse_compressed_block_header_v2 */ +static inline size_t +lzfse_encode_v1_freq_table(lzfse_compressed_block_header_v2 *out, + const lzfse_compressed_block_header_v1 *in) { + uint32_t accum = 0; + int accum_nbits = 0; + const uint16_t *src = &(in->l_freq[0]); // first value of first table (struct + // will not be modified, so this code + // will remain valid) + uint8_t *dst = &(out->freq[0]); + for (int i = 0; i < LZFSE_ENCODE_L_SYMBOLS + LZFSE_ENCODE_M_SYMBOLS + + LZFSE_ENCODE_D_SYMBOLS + LZFSE_ENCODE_LITERAL_SYMBOLS; + i++) { + // Encode one value to accum + int nbits = 0; + uint32_t bits = lzfse_encode_v1_freq_value(src[i], &nbits); + assert(bits < (1 << nbits)); + accum |= bits << accum_nbits; + accum_nbits += nbits; + + // Store bytes from accum to output buffer + while (accum_nbits >= 8) { + *dst = (uint8_t)(accum & 0xff); + accum >>= 8; + accum_nbits -= 8; + dst++; + } + } + // Store final byte if needed + if (accum_nbits > 0) { + *dst = (uint8_t)(accum & 0xff); + dst++; + } + + // Return final size of out + uint32_t header_size = (uint32_t)(dst - (uint8_t *)out); + out->packed_fields[0] = 0; + out->packed_fields[1] = 0; + out->packed_fields[2] = setField(header_size, 0, 32); + + return header_size; +} + +// We need to limit forward match length to make sure it won't split into a too +// large number of LMD. +// The limit itself is quite large, so it doesn't really impact compression +// ratio. +// The matches may still be expanded backwards by a few bytes, so the final +// length may be greater than this limit, which is OK. +#define LZFSE_ENCODE_MAX_MATCH_LENGTH (100 * LZFSE_ENCODE_MAX_M_VALUE) + +// =============================================================== +// Encoder back end + +/*! @abstract Encode matches stored in STATE into a compressed/uncompressed block. + * @return LZFSE_STATUS_OK on success. + * @return LZFSE_STATUS_DST_FULL and restore initial state if output buffer is + * full. */ +static int lzfse_encode_matches(lzfse_encoder_state *s) { + if (s->n_literals == 0 && s->n_matches == 0) + return LZFSE_STATUS_OK; // nothing to store, OK + + uint32_t l_occ[LZFSE_ENCODE_L_SYMBOLS]; + uint32_t m_occ[LZFSE_ENCODE_M_SYMBOLS]; + uint32_t d_occ[LZFSE_ENCODE_D_SYMBOLS]; + uint32_t literal_occ[LZFSE_ENCODE_LITERAL_SYMBOLS]; + fse_encoder_entry l_encoder[LZFSE_ENCODE_L_SYMBOLS]; + fse_encoder_entry m_encoder[LZFSE_ENCODE_M_SYMBOLS]; + fse_encoder_entry d_encoder[LZFSE_ENCODE_D_SYMBOLS]; + fse_encoder_entry literal_encoder[LZFSE_ENCODE_LITERAL_SYMBOLS]; + int ok = 1; + lzfse_compressed_block_header_v1 header1 = {0}; + lzfse_compressed_block_header_v2 *header2 = 0; + + // Keep initial state to be able to restore it if DST full + uint8_t *dst0 = s->dst; + uint32_t n_literals0 = s->n_literals; + + // Add 0x00 literals until n_literals multiple of 4, since we encode 4 + // interleaved literal streams. + while (s->n_literals & 3) { + uint32_t n = s->n_literals++; + s->literals[n] = 0; + } + + // Encode previous distance + uint32_t d_prev = 0; + for (uint32_t i = 0; i < s->n_matches; i++) { + uint32_t d = s->d_values[i]; + if (d == d_prev) + s->d_values[i] = 0; + else + d_prev = d; + } + + // Clear occurrence tables + memset(l_occ, 0, sizeof(l_occ)); + memset(m_occ, 0, sizeof(m_occ)); + memset(d_occ, 0, sizeof(d_occ)); + memset(literal_occ, 0, sizeof(literal_occ)); + + // Update occurrence tables in all 4 streams (L,M,D,literals) + uint32_t l_sum = 0; + uint32_t m_sum = 0; + for (uint32_t i = 0; i < s->n_matches; i++) { + uint32_t l = s->l_values[i]; + l_sum += l; + l_occ[l_base_from_value(l)]++; + } + for (uint32_t i = 0; i < s->n_matches; i++) { + uint32_t m = s->m_values[i]; + m_sum += m; + m_occ[m_base_from_value(m)]++; + } + for (uint32_t i = 0; i < s->n_matches; i++) + d_occ[d_base_from_value(s->d_values[i])]++; + for (uint32_t i = 0; i < s->n_literals; i++) + literal_occ[s->literals[i]]++; + + // Make sure we have enough room for a _full_ V2 header + if (s->dst + sizeof(lzfse_compressed_block_header_v2) > s->dst_end) { + ok = 0; + goto END; + } + header2 = (lzfse_compressed_block_header_v2 *)(s->dst); + + // Setup header V1 + header1.magic = LZFSE_COMPRESSEDV1_BLOCK_MAGIC; + header1.n_raw_bytes = m_sum + l_sum; + header1.n_matches = s->n_matches; + header1.n_literals = s->n_literals; + + // Normalize occurrence tables to freq tables + fse_normalize_freq(LZFSE_ENCODE_L_STATES, LZFSE_ENCODE_L_SYMBOLS, l_occ, + header1.l_freq); + fse_normalize_freq(LZFSE_ENCODE_M_STATES, LZFSE_ENCODE_M_SYMBOLS, m_occ, + header1.m_freq); + fse_normalize_freq(LZFSE_ENCODE_D_STATES, LZFSE_ENCODE_D_SYMBOLS, d_occ, + header1.d_freq); + fse_normalize_freq(LZFSE_ENCODE_LITERAL_STATES, LZFSE_ENCODE_LITERAL_SYMBOLS, + literal_occ, header1.literal_freq); + + // Compress freq tables to V2 header, and get actual size of V2 header + s->dst += lzfse_encode_v1_freq_table(header2, &header1); + + // Initialize encoder tables from freq tables + fse_init_encoder_table(LZFSE_ENCODE_L_STATES, LZFSE_ENCODE_L_SYMBOLS, + header1.l_freq, l_encoder); + fse_init_encoder_table(LZFSE_ENCODE_M_STATES, LZFSE_ENCODE_M_SYMBOLS, + header1.m_freq, m_encoder); + fse_init_encoder_table(LZFSE_ENCODE_D_STATES, LZFSE_ENCODE_D_SYMBOLS, + header1.d_freq, d_encoder); + fse_init_encoder_table(LZFSE_ENCODE_LITERAL_STATES, + LZFSE_ENCODE_LITERAL_SYMBOLS, header1.literal_freq, + literal_encoder); + + // Encode literals + { + fse_out_stream out; + fse_out_init(&out); + fse_state state0, state1, state2, state3; + state0 = state1 = state2 = state3 = 0; + + uint8_t *buf = s->dst; + uint32_t i = s->n_literals; // I multiple of 4 + // We encode starting from the last literal so we can decode starting from + // the first + while (i > 0) { + if (buf + 16 > s->dst_end) { + ok = 0; + goto END; + } // out full + i -= 4; + fse_encode(&state3, literal_encoder, &out, s->literals[i + 3]); // 10b + fse_encode(&state2, literal_encoder, &out, s->literals[i + 2]); // 10b +#if !FSE_IOSTREAM_64 + fse_out_flush(&out, &buf); +#endif + fse_encode(&state1, literal_encoder, &out, s->literals[i + 1]); // 10b + fse_encode(&state0, literal_encoder, &out, s->literals[i + 0]); // 10b + fse_out_flush(&out, &buf); + } + fse_out_finish(&out, &buf); + + // Update header with final encoder state + header1.literal_bits = out.accum_nbits; // [-7, 0] + header1.n_literal_payload_bytes = (uint32_t)(buf - s->dst); + header1.literal_state[0] = state0; + header1.literal_state[1] = state1; + header1.literal_state[2] = state2; + header1.literal_state[3] = state3; + + // Update state + s->dst = buf; + + } // literals + + // Encode L,M,D + { + fse_out_stream out; + fse_out_init(&out); + fse_state l_state, m_state, d_state; + l_state = m_state = d_state = 0; + + uint8_t *buf = s->dst; + uint32_t i = s->n_matches; + + // Add 8 padding bytes to the L,M,D payload + if (buf + 8 > s->dst_end) { + ok = 0; + goto END; + } // out full + store8(buf, 0); + buf += 8; + + // We encode starting from the last match so we can decode starting from the + // first + while (i > 0) { + if (buf + 16 > s->dst_end) { + ok = 0; + goto END; + } // out full + i -= 1; + + // D requires 23b max + int32_t d_value = s->d_values[i]; + uint8_t d_symbol = d_base_from_value(d_value); + int32_t d_nbits = d_extra_bits[d_symbol]; + int32_t d_bits = d_value - d_base_value[d_symbol]; + fse_out_push(&out, d_nbits, d_bits); + fse_encode(&d_state, d_encoder, &out, d_symbol); +#if !FSE_IOSTREAM_64 + fse_out_flush(&out, &buf); +#endif + + // M requires 17b max + int32_t m_value = s->m_values[i]; + uint8_t m_symbol = m_base_from_value(m_value); + int32_t m_nbits = m_extra_bits[m_symbol]; + int32_t m_bits = m_value - m_base_value[m_symbol]; + fse_out_push(&out, m_nbits, m_bits); + fse_encode(&m_state, m_encoder, &out, m_symbol); +#if !FSE_IOSTREAM_64 + fse_out_flush(&out, &buf); +#endif + + // L requires 14b max + int32_t l_value = s->l_values[i]; + uint8_t l_symbol = l_base_from_value(l_value); + int32_t l_nbits = l_extra_bits[l_symbol]; + int32_t l_bits = l_value - l_base_value[l_symbol]; + fse_out_push(&out, l_nbits, l_bits); + fse_encode(&l_state, l_encoder, &out, l_symbol); + fse_out_flush(&out, &buf); + } + fse_out_finish(&out, &buf); + + // Update header with final encoder state + header1.n_lmd_payload_bytes = (uint32_t)(buf - s->dst); + header1.lmd_bits = out.accum_nbits; // [-7, 0] + header1.l_state = l_state; + header1.m_state = m_state; + header1.d_state = d_state; + + // Update state + s->dst = buf; + + } // L,M,D + + // Final state update, here we had enough space in DST, and are not going to + // revert state + s->n_literals = 0; + s->n_matches = 0; + + // Final payload size + header1.n_payload_bytes = + header1.n_literal_payload_bytes + header1.n_lmd_payload_bytes; + + // Encode state info in V2 header (we previously encoded the tables, now we + // set the other fields) + lzfse_encode_v1_state(header2, &header1); + +END: + if (!ok) { + // Revert state, DST was full + + // Revert the d_prev encoding + uint32_t d_prev = 0; + for (uint32_t i = 0; i < s->n_matches; i++) { + uint32_t d = s->d_values[i]; + if (d == 0) + s->d_values[i] = d_prev; + else + d_prev = d; + } + + // Revert literal count + s->n_literals = n_literals0; + + // Revert DST + s->dst = dst0; + + return LZFSE_STATUS_DST_FULL; // DST full + } + + return LZFSE_STATUS_OK; +} + +/*! @abstract Push a L,M,D match into the STATE. + * @return LZFSE_STATUS_OK if OK. + * @return LZFSE_STATUS_DST_FULL if the match can't be pushed, meaning one of + * the buffers is full. In that case the state is not modified. */ +static inline int lzfse_push_lmd(lzfse_encoder_state *s, uint32_t L, + uint32_t M, uint32_t D) { + // Check if we have enough space to push the match (we add some margin to copy + // literals faster here, and round final count later) + if (s->n_matches + 1 + 8 > LZFSE_MATCHES_PER_BLOCK) + return LZFSE_STATUS_DST_FULL; // state full + if (s->n_literals + L + 16 > LZFSE_LITERALS_PER_BLOCK) + return LZFSE_STATUS_DST_FULL; // state full + + // Store match + uint32_t n = s->n_matches++; + s->l_values[n] = L; + s->m_values[n] = M; + s->d_values[n] = D; + + // Store literals + uint8_t *dst = s->literals + s->n_literals; + const uint8_t *src = s->src + s->src_literal; + uint8_t *dst_end = dst + L; + if (s->src_literal + L + 16 > s->src_end) { + // Careful at the end of SRC, we can't read 16 bytes + if (L > 0) + memcpy(dst, src, L); + } else { + copy16(dst, src); + dst += 16; + src += 16; + while (dst < dst_end) { + copy16(dst, src); + dst += 16; + src += 16; + } + } + s->n_literals += L; + + // Update state + s->src_literal += L + M; + + return LZFSE_STATUS_OK; +} + +/*! @abstract Split MATCH into one or more L,M,D parts, and push to STATE. + * @return LZFSE_STATUS_OK if OK. + * @return LZFSE_STATUS_DST_FULL if the match can't be pushed, meaning one of the + * buffers is full. In that case the state is not modified. */ +static int lzfse_push_match(lzfse_encoder_state *s, const lzfse_match *match) { + // Save the initial n_matches, n_literals, src_literal + uint32_t n_matches0 = s->n_matches; + uint32_t n_literals0 = s->n_literals; + lzfse_offset src_literals0 = s->src_literal; + + // L,M,D + uint32_t L = (uint32_t)(match->pos - s->src_literal); // literal count + uint32_t M = match->length; // match length + uint32_t D = (uint32_t)(match->pos - match->ref); // match distance + int ok = 1; + + // Split L if too large + while (L > LZFSE_ENCODE_MAX_L_VALUE) { + if (lzfse_push_lmd(s, LZFSE_ENCODE_MAX_L_VALUE, 0, 1) != 0) { + ok = 0; + goto END; + } // take D=1 because most frequent, but not actually used + L -= LZFSE_ENCODE_MAX_L_VALUE; + } + + // Split if M too large + while (M > LZFSE_ENCODE_MAX_M_VALUE) { + if (lzfse_push_lmd(s, L, LZFSE_ENCODE_MAX_M_VALUE, D) != 0) { + ok = 0; + goto END; + } + L = 0; + M -= LZFSE_ENCODE_MAX_M_VALUE; + } + + // L,M in range + if (L > 0 || M > 0) { + if (lzfse_push_lmd(s, L, M, D) != 0) { + ok = 0; + goto END; + } + L = M = 0; + (void)L; + (void)M; // dead stores + } + +END: + if (!ok) { + // Revert state + s->n_matches = n_matches0; + s->n_literals = n_literals0; + s->src_literal = src_literals0; + + return LZFSE_STATUS_DST_FULL; // state tables full + } + + return LZFSE_STATUS_OK; // OK +} + +/*! @abstract Backend: add MATCH to state S. Encode block if necessary, when + * state is full. + * @return LZFSE_STATUS_OK if OK. + * @return LZFSE_STATUS_DST_FULL if the match can't be added, meaning one of the + * buffers is full. In that case the state is not modified. */ +static int lzfse_backend_match(lzfse_encoder_state *s, + const lzfse_match *match) { + // Try to push the match in state + if (lzfse_push_match(s, match) == LZFSE_STATUS_OK) + return LZFSE_STATUS_OK; // OK, match added to state + + // Here state tables are full, try to emit block + if (lzfse_encode_matches(s) != LZFSE_STATUS_OK) + return LZFSE_STATUS_DST_FULL; // DST full, match not added + + // Here block has been emitted, re-try to push the match in state + return lzfse_push_match(s, match); +} + +/*! @abstract Backend: add L literals to state S. Encode block if necessary, + * when state is full. + * @return LZFSE_STATUS_OK if OK. + * @return LZFSE_STATUS_DST_FULL if the literals can't be added, meaning one of + * the buffers is full. In that case the state is not modified. */ +static int lzfse_backend_literals(lzfse_encoder_state *s, lzfse_offset L) { + // Create a fake match with M=0, D=1 + lzfse_match match; + lzfse_offset pos = s->src_literal + L; + match.pos = pos; + match.ref = match.pos - 1; + match.length = 0; + return lzfse_backend_match(s, &match); +} + +/*! @abstract Backend: flush final block, and emit end of stream + * @return LZFSE_STATUS_OK if OK. + * @return LZFSE_STATUS_DST_FULL if either the final block, or the end-of-stream + * can't be added, meaning one of the buffers is full. If the block was emitted, + * the state is updated to reflect this. Otherwise, it is left unchanged. */ +static int lzfse_backend_end_of_stream(lzfse_encoder_state *s) { + // Final match triggers write, otherwise emit blocks when we have enough + // matches stored + if (lzfse_encode_matches(s) != LZFSE_STATUS_OK) + return LZFSE_STATUS_DST_FULL; // DST full + + // Emit end-of-stream block + if (s->dst + 4 > s->dst_end) + return LZFSE_STATUS_DST_FULL; // DST full + store4(s->dst, LZFSE_ENDOFSTREAM_BLOCK_MAGIC); + s->dst += 4; + + return LZFSE_STATUS_OK; // OK +} + +// =============================================================== +// Encoder state management + +/*! @abstract Initialize state: + * @code + * - hash table with all invalid pos, and value 0. + * - pending match to NO_MATCH. + * - src_literal to 0. + * - d_prev to 0. + @endcode + * @return LZFSE_STATUS_OK */ +int lzfse_encode_init(lzfse_encoder_state *s) { + const lzfse_match NO_MATCH = {0}; + lzfse_history_set line; + for (int i = 0; i < LZFSE_ENCODE_HASH_WIDTH; i++) { + line.pos[i] = -4 * LZFSE_ENCODE_MAX_D_VALUE; // invalid pos + line.value[i] = 0; + } + // Fill table + for (int i = 0; i < LZFSE_ENCODE_HASH_VALUES; i++) + s->history_table[i] = line; + s->pending = NO_MATCH; + s->src_literal = 0; + + return LZFSE_STATUS_OK; // OK +} + +/*! @abstract Translate state \p src forward by \p delta > 0. + * Offsets in \p src are updated backwards to point to the same positions. + * @return LZFSE_STATUS_OK */ +int lzfse_encode_translate(lzfse_encoder_state *s, lzfse_offset delta) { + assert(delta >= 0); + if (delta == 0) + return LZFSE_STATUS_OK; // OK + + // SRC + s->src += delta; + + // Offsets in SRC + s->src_end -= delta; + s->src_encode_i -= delta; + s->src_encode_end -= delta; + s->src_literal -= delta; + + // Pending match + s->pending.pos -= delta; + s->pending.ref -= delta; + + // history_table positions, translated, and clamped to invalid pos + int32_t invalidPos = -4 * LZFSE_ENCODE_MAX_D_VALUE; + for (int i = 0; i < LZFSE_ENCODE_HASH_VALUES; i++) { + int32_t *p = &(s->history_table[i].pos[0]); + for (int j = 0; j < LZFSE_ENCODE_HASH_WIDTH; j++) { + lzfse_offset newPos = p[j] - delta; // translate + p[j] = (int32_t)((newPos < invalidPos) ? invalidPos : newPos); // clamp + } + } + + return LZFSE_STATUS_OK; // OK +} + +// =============================================================== +// Encoder front end + +int lzfse_encode_base(lzfse_encoder_state *s) { + lzfse_history_set *history_table = s->history_table; + lzfse_history_set *hashLine = 0; + lzfse_history_set newH; + const lzfse_match NO_MATCH = {0}; + int ok = 1; + + memset(&newH, 0x00, sizeof(newH)); + + // 8 byte padding at end of buffer + s->src_encode_end = s->src_end - 8; + for (; s->src_encode_i < s->src_encode_end; s->src_encode_i++) { + lzfse_offset pos = s->src_encode_i; // pos >= 0 + + // Load 4 byte value and get hash line + uint32_t x = load4(s->src + pos); + hashLine = history_table + hashX(x); + lzfse_history_set h = *hashLine; + + // Prepare next hash line (component 0 is the most recent) to prepare new + // entries (stored later) + { + newH.pos[0] = (int32_t)pos; + for (int k = 0; k < LZFSE_ENCODE_HASH_WIDTH - 1; k++) + newH.pos[k + 1] = h.pos[k]; + newH.value[0] = x; + for (int k = 0; k < LZFSE_ENCODE_HASH_WIDTH - 1; k++) + newH.value[k + 1] = h.value[k]; + } + + // Do not look for a match if we are still covered by a previous match + if (pos < s->src_literal) + goto END_POS; + + // Search best incoming match + lzfse_match incoming = {.pos = pos, .ref = 0, .length = 0}; + + // Check for matches. We consider matches of length >= 4 only. + for (int k = 0; k < LZFSE_ENCODE_HASH_WIDTH; k++) { + uint32_t d = h.value[k] ^ x; + if (d) + continue; // no 4 byte match + int32_t ref = h.pos[k]; + if (ref + LZFSE_ENCODE_MAX_D_VALUE < pos) + continue; // too far + + const uint8_t *src_ref = s->src + ref; + const uint8_t *src_pos = s->src + pos; + uint32_t length = 4; + uint32_t maxLength = + (uint32_t)(s->src_end - pos - 8); // ensure we don't hit the end of SRC + while (length < maxLength) { + uint64_t d = load8(src_ref + length) ^ load8(src_pos + length); + if (d == 0) { + length += 8; + continue; + } + + length += + (__builtin_ctzll(d) >> 3); // ctzll must be called only with D != 0 + break; + } + if (length > incoming.length) { + incoming.length = length; + incoming.ref = ref; + } // keep if longer + } + + // No incoming match? + if (incoming.length == 0) { + // We may still want to emit some literals here, to not lag too far behind + // the current search point, and avoid + // ending up with a literal block not fitting in the state. + lzfse_offset n_literals = pos - s->src_literal; + // The threshold here should be larger than a couple of MAX_L_VALUE, and + // much smaller than LITERALS_PER_BLOCK + if (n_literals > 8 * LZFSE_ENCODE_MAX_L_VALUE) { + // Here, we need to consume some literals. Emit pending match if there + // is one + if (s->pending.length > 0) { + if (lzfse_backend_match(s, &s->pending) != LZFSE_STATUS_OK) { + ok = 0; + goto END; + } + s->pending = NO_MATCH; + } else { + // No pending match, emit a full LZFSE_ENCODE_MAX_L_VALUE block of + // literals + if (lzfse_backend_literals(s, LZFSE_ENCODE_MAX_L_VALUE) != + LZFSE_STATUS_OK) { + ok = 0; + goto END; + } + } + } + goto END_POS; // no incoming match + } + + // Limit match length (it may still be expanded backwards, but this is + // bounded by the limit on literals we tested before) + if (incoming.length > LZFSE_ENCODE_MAX_MATCH_LENGTH) { + incoming.length = LZFSE_ENCODE_MAX_MATCH_LENGTH; + } + + // Expand backwards (since this is expensive, we do this for the best match + // only) + while (incoming.pos > s->src_literal && incoming.ref > 0 && + s->src[incoming.ref - 1] == s->src[incoming.pos - 1]) { + incoming.pos--; + incoming.ref--; + } + incoming.length += pos - incoming.pos; // update length after expansion + + // Match filtering heuristic (from LZVN). INCOMING is always defined here. + + // Incoming is 'good', emit incoming + if (incoming.length >= LZFSE_ENCODE_GOOD_MATCH) { + if (lzfse_backend_match(s, &incoming) != LZFSE_STATUS_OK) { + ok = 0; + goto END; + } + s->pending = NO_MATCH; + goto END_POS; + } + + // No pending, keep incoming + if (s->pending.length == 0) { + s->pending = incoming; + goto END_POS; + } + + // No overlap, emit pending, keep incoming + if (s->pending.pos + s->pending.length <= incoming.pos) { + if (lzfse_backend_match(s, &s->pending) != LZFSE_STATUS_OK) { + ok = 0; + goto END; + } + s->pending = incoming; + goto END_POS; + } + + // Overlap: emit longest + if (incoming.length > s->pending.length) { + if (lzfse_backend_match(s, &incoming) != LZFSE_STATUS_OK) { + ok = 0; + goto END; + } + } else { + if (lzfse_backend_match(s, &s->pending) != LZFSE_STATUS_OK) { + ok = 0; + goto END; + } + } + s->pending = NO_MATCH; + + END_POS: + // We are done with this src_encode_i. + // Update state now (s->pending has already been updated). + *hashLine = newH; + } + +END: + return ok ? LZFSE_STATUS_OK : LZFSE_STATUS_DST_FULL; +} + +int lzfse_encode_finish(lzfse_encoder_state *s) { + const lzfse_match NO_MATCH = {0}; + + // Emit pending match + if (s->pending.length > 0) { + if (lzfse_backend_match(s, &s->pending) != LZFSE_STATUS_OK) + return LZFSE_STATUS_DST_FULL; + s->pending = NO_MATCH; + } + + // Emit final literals if any + lzfse_offset L = s->src_end - s->src_literal; + if (L > 0) { + if (lzfse_backend_literals(s, L) != LZFSE_STATUS_OK) + return LZFSE_STATUS_DST_FULL; + } + + // Emit all matches, and end-of-stream block + if (lzfse_backend_end_of_stream(s) != LZFSE_STATUS_OK) + return LZFSE_STATUS_DST_FULL; + + return LZFSE_STATUS_OK; +} diff --git a/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_encode_tables.h b/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_encode_tables.h new file mode 100644 index 0000000..1b78c52 --- /dev/null +++ b/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_encode_tables.h @@ -0,0 +1,218 @@ +/* +Copyright (c) 2015-2016, Apple Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef LZFSE_ENCODE_TABLES_H +#define LZFSE_ENCODE_TABLES_H + +#if defined(_MSC_VER) && !defined(__clang__) +# define inline __inline +#endif + +static inline uint8_t l_base_from_value(int32_t value) { + static const uint8_t sym[LZFSE_ENCODE_MAX_L_VALUE + 1] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 16, + 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, + 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, + 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19}; + return sym[value]; +} +static inline uint8_t m_base_from_value(int32_t value) { + static const uint8_t sym[LZFSE_ENCODE_MAX_M_VALUE + 1] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 16, + 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, + 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, + 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, + 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, + 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, + 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, + 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, + 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, + 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, + 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, + 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, + 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, + 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, + 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, + 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, + 19, 19}; + return sym[value]; +} + +static inline uint8_t d_base_from_value(int32_t value) { + static const uint8_t sym[64 * 4] = { + 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9, + 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, + 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, + 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 17, 18, 19, 20, 20, 21, 21, + 22, 22, 23, 23, 24, 24, 24, 24, 25, 25, 25, 25, 26, 26, 26, 26, 27, 27, + 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, + 30, 30, 30, 30, 30, 30, 30, 30, 31, 31, 31, 31, 31, 31, 31, 31, 32, 32, + 32, 32, 32, 33, 34, 35, 36, 36, 37, 37, 38, 38, 39, 39, 40, 40, 40, 40, + 41, 41, 41, 41, 42, 42, 42, 42, 43, 43, 43, 43, 44, 44, 44, 44, 44, 44, + 44, 44, 45, 45, 45, 45, 45, 45, 45, 45, 46, 46, 46, 46, 46, 46, 46, 46, + 47, 47, 47, 47, 47, 47, 47, 47, 48, 48, 48, 48, 48, 49, 50, 51, 52, 52, + 53, 53, 54, 54, 55, 55, 56, 56, 56, 56, 57, 57, 57, 57, 58, 58, 58, 58, + 59, 59, 59, 59, 60, 60, 60, 60, 60, 60, 60, 60, 61, 61, 61, 61, 61, 61, + 61, 61, 62, 62, 62, 62, 62, 62, 62, 62, 63, 63, 63, 63, 63, 63, 63, 63, + 0, 0, 0, 0}; + int index = 0; + int in_range_k; + in_range_k = (value >= 0 && value < 60); + index |= (((value - 0) >> 0) + 0) & -in_range_k; + in_range_k = (value >= 60 && value < 1020); + index |= (((value - 60) >> 4) + 64) & -in_range_k; + in_range_k = (value >= 1020 && value < 16380); + index |= (((value - 1020) >> 8) + 128) & -in_range_k; + in_range_k = (value >= 16380 && value < 262140); + index |= (((value - 16380) >> 12) + 192) & -in_range_k; + return sym[index & 255]; +} + +#endif // LZFSE_ENCODE_TABLES_H diff --git a/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_fse.c b/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_fse.c new file mode 100644 index 0000000..631ada0 --- /dev/null +++ b/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_fse.c @@ -0,0 +1,216 @@ +/* +Copyright (c) 2015-2016, Apple Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "lzfse_internal.h" + +// Initialize encoder table T[NSYMBOLS]. +// NSTATES = sum FREQ[i] is the number of states (a power of 2) +// NSYMBOLS is the number of symbols. +// FREQ[NSYMBOLS] is a normalized histogram of symbol frequencies, with FREQ[i] +// >= 0. +// Some symbols may have a 0 frequency. In that case, they should not be +// present in the data. +void fse_init_encoder_table(int nstates, int nsymbols, + const uint16_t *__restrict freq, + fse_encoder_entry *__restrict t) { + int offset = 0; // current offset + int n_clz = __builtin_clz(nstates); + for (int i = 0; i < nsymbols; i++) { + int f = (int)freq[i]; + if (f == 0) + continue; // skip this symbol, no occurrences + int k = + __builtin_clz(f) - n_clz; // shift needed to ensure N <= (F<> k)); + t[i].delta1 = (int16_t)(offset - f + (nstates >> (k - 1))); + offset += f; + } +} + +// Initialize decoder table T[NSTATES]. +// NSTATES = sum FREQ[i] is the number of states (a power of 2) +// NSYMBOLS is the number of symbols. +// FREQ[NSYMBOLS] is a normalized histogram of symbol frequencies, with FREQ[i] +// >= 0. +// Some symbols may have a 0 frequency. In that case, they should not be +// present in the data. +int fse_init_decoder_table(int nstates, int nsymbols, + const uint16_t *__restrict freq, + int32_t *__restrict t) { + assert(nsymbols <= 256); + assert(fse_check_freq(freq, nsymbols, nstates) == 0); + int n_clz = __builtin_clz(nstates); + int sum_of_freq = 0; + for (int i = 0; i < nsymbols; i++) { + int f = (int)freq[i]; + if (f == 0) + continue; // skip this symbol, no occurrences + + sum_of_freq += f; + + if (sum_of_freq > nstates) { + return -1; + } + + int k = + __builtin_clz(f) - n_clz; // shift needed to ensure N <= (F<> k) - f; + + // Initialize all states S reached by this symbol: OFFSET <= S < OFFSET + F + for (int j = 0; j < f; j++) { + fse_decoder_entry e; + + e.symbol = (uint8_t)i; + if (j < j0) { + e.k = (int8_t)k; + e.delta = (int16_t)(((f + j) << k) - nstates); + } else { + e.k = (int8_t)(k - 1); + e.delta = (int16_t)((j - j0) << (k - 1)); + } + + memcpy(t, &e, sizeof(e)); + t++; + } + } + + return 0; // OK +} + +// Initialize value decoder table T[NSTATES]. +// NSTATES = sum FREQ[i] is the number of states (a power of 2) +// NSYMBOLS is the number of symbols. +// FREQ[NSYMBOLS] is a normalized histogram of symbol frequencies, with FREQ[i] +// >= 0. +// SYMBOL_VBITS[NSYMBOLS] and SYMBOLS_VBASE[NSYMBOLS] are the number of value +// bits to read and the base value for each symbol. +// Some symbols may have a 0 frequency. In that case, they should not be +// present in the data. +void fse_init_value_decoder_table(int nstates, int nsymbols, + const uint16_t *__restrict freq, + const uint8_t *__restrict symbol_vbits, + const int32_t *__restrict symbol_vbase, + fse_value_decoder_entry *__restrict t) { + assert(nsymbols <= 256); + assert(fse_check_freq(freq, nsymbols, nstates) == 0); + + int n_clz = __builtin_clz(nstates); + for (int i = 0; i < nsymbols; i++) { + int f = (int)freq[i]; + if (f == 0) + continue; // skip this symbol, no occurrences + + int k = + __builtin_clz(f) - n_clz; // shift needed to ensure N <= (F<> k) - f; + + fse_value_decoder_entry ei = {0}; + ei.value_bits = symbol_vbits[i]; + ei.vbase = symbol_vbase[i]; + + // Initialize all states S reached by this symbol: OFFSET <= S < OFFSET + F + for (int j = 0; j < f; j++) { + fse_value_decoder_entry e = ei; + + if (j < j0) { + e.total_bits = (uint8_t)k + e.value_bits; + e.delta = (int16_t)(((f + j) << k) - nstates); + } else { + e.total_bits = (uint8_t)(k - 1) + e.value_bits; + e.delta = (int16_t)((j - j0) << (k - 1)); + } + + memcpy(t, &e, 8); + t++; + } + } +} + +// Remove states from symbols until the correct number of states is used. +static void fse_adjust_freqs(uint16_t *freq, int overrun, int nsymbols) { + for (int shift = 3; overrun != 0; shift--) { + for (int sym = 0; sym < nsymbols; sym++) { + if (freq[sym] > 1) { + int n = (freq[sym] - 1) >> shift; + if (n > overrun) + n = overrun; + freq[sym] -= n; + overrun -= n; + if (overrun == 0) + break; + } + } + } +} + +// Normalize a table T[NSYMBOLS] of occurrences to FREQ[NSYMBOLS]. +void fse_normalize_freq(int nstates, int nsymbols, const uint32_t *__restrict t, + uint16_t *__restrict freq) { + uint32_t s_count = 0; + int remaining = nstates; // must be signed; this may become < 0 + int max_freq = 0; + int max_freq_sym = 0; + int shift = __builtin_clz(nstates) - 1; + uint32_t highprec_step; + + // Compute the total number of symbol occurrences + for (int i = 0; i < nsymbols; i++) + s_count += t[i]; + + if (s_count == 0) + highprec_step = 0; // no symbols used + else + highprec_step = ((uint32_t)1 << 31) / s_count; + + for (int i = 0; i < nsymbols; i++) { + + // Rescale the occurrence count to get the normalized frequency. + // Round up if the fractional part is >= 0.5; otherwise round down. + // For efficiency, we do this calculation using integer arithmetic. + int f = (((t[i] * highprec_step) >> shift) + 1) >> 1; + + // If a symbol was used, it must be given a nonzero normalized frequency. + if (f == 0 && t[i] != 0) + f = 1; + + freq[i] = f; + remaining -= f; + + // Remember the maximum frequency and which symbol had it. + if (f > max_freq) { + max_freq = f; + max_freq_sym = i; + } + } + + // If there remain states to be assigned, then just assign them to the most + // frequent symbol. Alternatively, if we assigned more states than were + // actually available, then either remove states from the most frequent symbol + // (for minor overruns) or use the slower adjustment algorithm (for major + // overruns). + if (-remaining < (max_freq >> 2)) { + freq[max_freq_sym] += remaining; + } else { + fse_adjust_freqs(freq, -remaining, nsymbols); + } +} diff --git a/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_fse.h b/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_fse.h new file mode 100644 index 0000000..8846012 --- /dev/null +++ b/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_fse.h @@ -0,0 +1,631 @@ +/* +Copyright (c) 2015-2016, Apple Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +// Finite state entropy coding (FSE) +// This is an implementation of the tANS algorithm described by Jarek Duda, +// we use the more descriptive name "Finite State Entropy". + +#pragma once + +#include +#include +#include +#include +#include + +// Select between 32/64-bit I/O streams for FSE. Note that the FSE stream +// size need not match the word size of the machine, but in practice you +// want to use 64b streams on 64b systems for better performance. +#if defined(_M_AMD64) || defined(__x86_64__) || defined(__arm64__) +#define FSE_IOSTREAM_64 1 +#else +#define FSE_IOSTREAM_64 0 +#endif + +#if defined(_MSC_VER) && !defined(__clang__) +# define FSE_INLINE __forceinline +# define inline __inline +# pragma warning(disable : 4068) // warning C4068: unknown pragma +#else +# define FSE_INLINE static inline __attribute__((__always_inline__)) +#endif + +// MARK: - Bit utils + +/*! @abstract Signed type used to represent bit count. */ +typedef int32_t fse_bit_count; + +/*! @abstract Unsigned type used to represent FSE state. */ +typedef uint16_t fse_state; + +// Mask the NBITS lsb of X. 0 <= NBITS < 64 +static inline uint64_t fse_mask_lsb64(uint64_t x, fse_bit_count nbits) { + static const uint64_t mtable[65] = { + 0x0000000000000000LLU, 0x0000000000000001LLU, 0x0000000000000003LLU, + 0x0000000000000007LLU, 0x000000000000000fLLU, 0x000000000000001fLLU, + 0x000000000000003fLLU, 0x000000000000007fLLU, 0x00000000000000ffLLU, + 0x00000000000001ffLLU, 0x00000000000003ffLLU, 0x00000000000007ffLLU, + 0x0000000000000fffLLU, 0x0000000000001fffLLU, 0x0000000000003fffLLU, + 0x0000000000007fffLLU, 0x000000000000ffffLLU, 0x000000000001ffffLLU, + 0x000000000003ffffLLU, 0x000000000007ffffLLU, 0x00000000000fffffLLU, + 0x00000000001fffffLLU, 0x00000000003fffffLLU, 0x00000000007fffffLLU, + 0x0000000000ffffffLLU, 0x0000000001ffffffLLU, 0x0000000003ffffffLLU, + 0x0000000007ffffffLLU, 0x000000000fffffffLLU, 0x000000001fffffffLLU, + 0x000000003fffffffLLU, 0x000000007fffffffLLU, 0x00000000ffffffffLLU, + 0x00000001ffffffffLLU, 0x00000003ffffffffLLU, 0x00000007ffffffffLLU, + 0x0000000fffffffffLLU, 0x0000001fffffffffLLU, 0x0000003fffffffffLLU, + 0x0000007fffffffffLLU, 0x000000ffffffffffLLU, 0x000001ffffffffffLLU, + 0x000003ffffffffffLLU, 0x000007ffffffffffLLU, 0x00000fffffffffffLLU, + 0x00001fffffffffffLLU, 0x00003fffffffffffLLU, 0x00007fffffffffffLLU, + 0x0000ffffffffffffLLU, 0x0001ffffffffffffLLU, 0x0003ffffffffffffLLU, + 0x0007ffffffffffffLLU, 0x000fffffffffffffLLU, 0x001fffffffffffffLLU, + 0x003fffffffffffffLLU, 0x007fffffffffffffLLU, 0x00ffffffffffffffLLU, + 0x01ffffffffffffffLLU, 0x03ffffffffffffffLLU, 0x07ffffffffffffffLLU, + 0x0fffffffffffffffLLU, 0x1fffffffffffffffLLU, 0x3fffffffffffffffLLU, + 0x7fffffffffffffffLLU, 0xffffffffffffffffLLU, + }; + return x & mtable[nbits]; +} + +// Mask the NBITS lsb of X. 0 <= NBITS < 32 +static inline uint32_t fse_mask_lsb32(uint32_t x, fse_bit_count nbits) { + static const uint32_t mtable[33] = { + 0x0000000000000000U, 0x0000000000000001U, 0x0000000000000003U, + 0x0000000000000007U, 0x000000000000000fU, 0x000000000000001fU, + 0x000000000000003fU, 0x000000000000007fU, 0x00000000000000ffU, + 0x00000000000001ffU, 0x00000000000003ffU, 0x00000000000007ffU, + 0x0000000000000fffU, 0x0000000000001fffU, 0x0000000000003fffU, + 0x0000000000007fffU, 0x000000000000ffffU, 0x000000000001ffffU, + 0x000000000003ffffU, 0x000000000007ffffU, 0x00000000000fffffU, + 0x00000000001fffffU, 0x00000000003fffffU, 0x00000000007fffffU, + 0x0000000000ffffffU, 0x0000000001ffffffU, 0x0000000003ffffffU, + 0x0000000007ffffffU, 0x000000000fffffffU, 0x000000001fffffffU, + 0x000000003fffffffU, 0x000000007fffffffU, 0x00000000ffffffffU, + }; + return x & mtable[nbits]; +} + +/*! @abstract Select \c nbits at index \c start from \c x. + * 0 <= start <= start+nbits <= 64 */ +FSE_INLINE uint64_t fse_extract_bits64(uint64_t x, fse_bit_count start, + fse_bit_count nbits) { +#if defined(__GNUC__) + // If START and NBITS are constants, map to bit-field extraction instructions + if (__builtin_constant_p(start) && __builtin_constant_p(nbits)) + return (x >> start) & ((1LLU << nbits) - 1LLU); +#endif + + // Otherwise, shift and mask + return fse_mask_lsb64(x >> start, nbits); +} + +/*! @abstract Select \c nbits at index \c start from \c x. + * 0 <= start <= start+nbits <= 32 */ +FSE_INLINE uint32_t fse_extract_bits32(uint32_t x, fse_bit_count start, + fse_bit_count nbits) { +#if defined(__GNUC__) + // If START and NBITS are constants, map to bit-field extraction instructions + if (__builtin_constant_p(start) && __builtin_constant_p(nbits)) + return (x >> start) & ((1U << nbits) - 1U); +#endif + + // Otherwise, shift and mask + return fse_mask_lsb32(x >> start, nbits); +} + +// MARK: - Bit stream + +// I/O streams +// The streams can be shared between several FSE encoders/decoders, which is why +// they are not in the state struct + +/*! @abstract Output stream, 64-bit accum. */ +typedef struct { + uint64_t accum; // Output bits + fse_bit_count accum_nbits; // Number of valid bits in ACCUM, other bits are 0 +} fse_out_stream64; + +/*! @abstract Output stream, 32-bit accum. */ +typedef struct { + uint32_t accum; // Output bits + fse_bit_count accum_nbits; // Number of valid bits in ACCUM, other bits are 0 +} fse_out_stream32; + +/*! @abstract Object representing an input stream. */ +typedef struct { + uint64_t accum; // Input bits + fse_bit_count accum_nbits; // Number of valid bits in ACCUM, other bits are 0 +} fse_in_stream64; + +/*! @abstract Object representing an input stream. */ +typedef struct { + uint32_t accum; // Input bits + fse_bit_count accum_nbits; // Number of valid bits in ACCUM, other bits are 0 +} fse_in_stream32; + +/*! @abstract Initialize an output stream object. */ +FSE_INLINE void fse_out_init64(fse_out_stream64 *s) { + s->accum = 0; + s->accum_nbits = 0; +} + +/*! @abstract Initialize an output stream object. */ +FSE_INLINE void fse_out_init32(fse_out_stream32 *s) { + s->accum = 0; + s->accum_nbits = 0; +} + +/*! @abstract Write full bytes from the accumulator to output buffer, ensuring + * accum_nbits is in [0, 7]. + * We assume we can write 8 bytes to the output buffer \c (*pbuf[0..7]) in all + * cases. + * @note *pbuf is incremented by the number of written bytes. */ +FSE_INLINE void fse_out_flush64(fse_out_stream64 *s, uint8_t **pbuf) { + fse_bit_count nbits = + s->accum_nbits & -8; // number of bits written, multiple of 8 + + // Write 8 bytes of current accumulator + memcpy(*pbuf, &(s->accum), 8); + *pbuf += (nbits >> 3); // bytes + + // Update state + s->accum >>= nbits; // remove nbits + s->accum_nbits -= nbits; + + assert(s->accum_nbits >= 0 && s->accum_nbits <= 7); + assert(s->accum_nbits == 64 || (s->accum >> s->accum_nbits) == 0); +} + +/*! @abstract Write full bytes from the accumulator to output buffer, ensuring + * accum_nbits is in [0, 7]. + * We assume we can write 4 bytes to the output buffer \c (*pbuf[0..3]) in all + * cases. + * @note *pbuf is incremented by the number of written bytes. */ +FSE_INLINE void fse_out_flush32(fse_out_stream32 *s, uint8_t **pbuf) { + fse_bit_count nbits = + s->accum_nbits & -8; // number of bits written, multiple of 8 + + // Write 4 bytes of current accumulator + memcpy(*pbuf, &(s->accum), 4); + *pbuf += (nbits >> 3); // bytes + + // Update state + s->accum >>= nbits; // remove nbits + s->accum_nbits -= nbits; + + assert(s->accum_nbits >= 0 && s->accum_nbits <= 7); + assert(s->accum_nbits == 32 || (s->accum >> s->accum_nbits) == 0); +} + +/*! @abstract Write the last bytes from the accumulator to output buffer, + * ensuring accum_nbits is in [-7, 0]. Bits are padded with 0 if needed. + * We assume we can write 8 bytes to the output buffer \c (*pbuf[0..7]) in all + * cases. + * @note *pbuf is incremented by the number of written bytes. */ +FSE_INLINE void fse_out_finish64(fse_out_stream64 *s, uint8_t **pbuf) { + fse_bit_count nbits = + (s->accum_nbits + 7) & -8; // number of bits written, multiple of 8 + + // Write 8 bytes of current accumulator + memcpy(*pbuf, &(s->accum), 8); + *pbuf += (nbits >> 3); // bytes + + // Update state + s->accum = 0; // remove nbits + s->accum_nbits -= nbits; + + assert(s->accum_nbits >= -7 && s->accum_nbits <= 0); +} + +/*! @abstract Write the last bytes from the accumulator to output buffer, + * ensuring accum_nbits is in [-7, 0]. Bits are padded with 0 if needed. + * We assume we can write 4 bytes to the output buffer \c (*pbuf[0..3]) in all + * cases. + * @note *pbuf is incremented by the number of written bytes. */ +FSE_INLINE void fse_out_finish32(fse_out_stream32 *s, uint8_t **pbuf) { + fse_bit_count nbits = + (s->accum_nbits + 7) & -8; // number of bits written, multiple of 8 + + // Write 8 bytes of current accumulator + memcpy(*pbuf, &(s->accum), 4); + *pbuf += (nbits >> 3); // bytes + + // Update state + s->accum = 0; // remove nbits + s->accum_nbits -= nbits; + + assert(s->accum_nbits >= -7 && s->accum_nbits <= 0); +} + +/*! @abstract Accumulate \c n bits \c b to output stream \c s. We \b must have: + * 0 <= b < 2^n, and N + s->accum_nbits <= 64. + * @note The caller must ensure out_flush is called \b before the accumulator + * overflows to more than 64 bits. */ +FSE_INLINE void fse_out_push64(fse_out_stream64 *s, fse_bit_count n, + uint64_t b) { + s->accum |= b << s->accum_nbits; + s->accum_nbits += n; + + assert(s->accum_nbits >= 0 && s->accum_nbits <= 64); + assert(s->accum_nbits == 64 || (s->accum >> s->accum_nbits) == 0); +} + +/*! @abstract Accumulate \c n bits \c b to output stream \c s. We \b must have: + * 0 <= n < 2^n, and n + s->accum_nbits <= 32. + * @note The caller must ensure out_flush is called \b before the accumulator + * overflows to more than 32 bits. */ +FSE_INLINE void fse_out_push32(fse_out_stream32 *s, fse_bit_count n, + uint32_t b) { + s->accum |= b << s->accum_nbits; + s->accum_nbits += n; + + assert(s->accum_nbits >= 0 && s->accum_nbits <= 32); + assert(s->accum_nbits == 32 || (s->accum >> s->accum_nbits) == 0); +} + +#if FSE_IOSTREAM_64 +#define DEBUG_CHECK_INPUT_STREAM_PARAMETERS \ + assert(s->accum_nbits >= 56 && s->accum_nbits < 64); \ + assert((s->accum >> s->accum_nbits) == 0); +#else +#define DEBUG_CHECK_INPUT_STREAM_PARAMETERS \ + assert(s->accum_nbits >= 24 && s->accum_nbits < 32); \ + assert((s->accum >> s->accum_nbits) == 0); +#endif + +/*! @abstract Initialize the fse input stream so that accum holds between 56 + * and 63 bits. We never want to have 64 bits in the stream, because that allows + * us to avoid a special case in the fse_in_pull function (eliminating an + * unpredictable branch), while not requiring any additional fse_flush + * operations. This is why we have the special case for n == 0 (in which case + * we want to load only 7 bytes instead of 8). */ +FSE_INLINE int fse_in_checked_init64(fse_in_stream64 *s, fse_bit_count n, + const uint8_t **pbuf, + const uint8_t *buf_start) { + if (n) { + if (*pbuf < buf_start + 8) + return -1; // out of range + *pbuf -= 8; + memcpy(&(s->accum), *pbuf, 8); + s->accum_nbits = n + 64; + } else { + if (*pbuf < buf_start + 7) + return -1; // out of range + *pbuf -= 7; + memcpy(&(s->accum), *pbuf, 7); + s->accum &= 0xffffffffffffff; + s->accum_nbits = n + 56; + } + + if ((s->accum_nbits < 56 || s->accum_nbits >= 64) || + ((s->accum >> s->accum_nbits) != 0)) { + return -1; // the incoming input is wrong (encoder should have zeroed the + // upper bits) + } + + return 0; // OK +} + +/*! @abstract Identical to previous function, but for 32-bit operation + * (resulting bit count is between 24 and 31 bits). */ +FSE_INLINE int fse_in_checked_init32(fse_in_stream32 *s, fse_bit_count n, + const uint8_t **pbuf, + const uint8_t *buf_start) { + if (n) { + if (*pbuf < buf_start + 4) + return -1; // out of range + *pbuf -= 4; + memcpy(&(s->accum), *pbuf, 4); + s->accum_nbits = n + 32; + } else { + if (*pbuf < buf_start + 3) + return -1; // out of range + *pbuf -= 3; + memcpy(&(s->accum), *pbuf, 3); + s->accum &= 0xffffff; + s->accum_nbits = n + 24; + } + + if ((s->accum_nbits < 24 || s->accum_nbits >= 32) || + ((s->accum >> s->accum_nbits) != 0)) { + return -1; // the incoming input is wrong (encoder should have zeroed the + // upper bits) + } + + return 0; // OK +} + +/*! @abstract Read in new bytes from buffer to ensure that we have a full + * complement of bits in the stream object (again, between 56 and 63 bits). + * checking the new value of \c *pbuf remains >= \c buf_start. + * @return 0 if OK. + * @return -1 on failure. */ +FSE_INLINE int fse_in_checked_flush64(fse_in_stream64 *s, const uint8_t **pbuf, + const uint8_t *buf_start) { + // Get number of bits to add to bring us into the desired range. + fse_bit_count nbits = (63 - s->accum_nbits) & -8; + // Convert bits to bytes and decrement buffer address, then load new data. + const uint8_t *buf = (*pbuf) - (nbits >> 3); + if (buf < buf_start) { + return -1; // out of range + } + *pbuf = buf; + uint64_t incoming; + memcpy(&incoming, buf, 8); + // Update the state object and verify its validity (in DEBUG). + s->accum = (s->accum << nbits) | fse_mask_lsb64(incoming, nbits); + s->accum_nbits += nbits; + DEBUG_CHECK_INPUT_STREAM_PARAMETERS + return 0; // OK +} + +/*! @abstract Identical to previous function (but again, we're only filling + * a 32-bit field with between 24 and 31 bits). */ +FSE_INLINE int fse_in_checked_flush32(fse_in_stream32 *s, const uint8_t **pbuf, + const uint8_t *buf_start) { + // Get number of bits to add to bring us into the desired range. + fse_bit_count nbits = (31 - s->accum_nbits) & -8; + + if (nbits > 0) { + // Convert bits to bytes and decrement buffer address, then load new data. + const uint8_t *buf = (*pbuf) - (nbits >> 3); + if (buf < buf_start) { + return -1; // out of range + } + + *pbuf = buf; + + uint32_t incoming = *((uint32_t *)buf); + + // Update the state object and verify its validity (in DEBUG). + s->accum = (s->accum << nbits) | fse_mask_lsb32(incoming, nbits); + s->accum_nbits += nbits; + } + DEBUG_CHECK_INPUT_STREAM_PARAMETERS + return 0; // OK +} + +/*! @abstract Pull n bits out of the fse stream object. */ +FSE_INLINE uint64_t fse_in_pull64(fse_in_stream64 *s, fse_bit_count n) { + assert(n >= 0 && n <= s->accum_nbits); + s->accum_nbits -= n; + uint64_t result = s->accum >> s->accum_nbits; + s->accum = fse_mask_lsb64(s->accum, s->accum_nbits); + return result; +} + +/*! @abstract Pull n bits out of the fse stream object. */ +FSE_INLINE uint32_t fse_in_pull32(fse_in_stream32 *s, fse_bit_count n) { + assert(n >= 0 && n <= s->accum_nbits); + s->accum_nbits -= n; + uint32_t result = s->accum >> s->accum_nbits; + s->accum = fse_mask_lsb32(s->accum, s->accum_nbits); + return result; +} + +// MARK: - Encode/Decode + +// Map to 32/64-bit implementations and types for I/O +#if FSE_IOSTREAM_64 + +typedef uint64_t fse_bits; +typedef fse_out_stream64 fse_out_stream; +typedef fse_in_stream64 fse_in_stream; +#define fse_mask_lsb fse_mask_lsb64 +#define fse_extract_bits fse_extract_bits64 +#define fse_out_init fse_out_init64 +#define fse_out_flush fse_out_flush64 +#define fse_out_finish fse_out_finish64 +#define fse_out_push fse_out_push64 +#define fse_in_init fse_in_checked_init64 +#define fse_in_checked_init fse_in_checked_init64 +#define fse_in_flush fse_in_checked_flush64 +#define fse_in_checked_flush fse_in_checked_flush64 +#define fse_in_flush2(_unused, _parameters, _unused2) 0 /* nothing */ +#define fse_in_checked_flush2(_unused, _parameters) /* nothing */ +#define fse_in_pull fse_in_pull64 + +#else + +typedef uint32_t fse_bits; +typedef fse_out_stream32 fse_out_stream; +typedef fse_in_stream32 fse_in_stream; +#define fse_mask_lsb fse_mask_lsb32 +#define fse_extract_bits fse_extract_bits32 +#define fse_out_init fse_out_init32 +#define fse_out_flush fse_out_flush32 +#define fse_out_finish fse_out_finish32 +#define fse_out_push fse_out_push32 +#define fse_in_init fse_in_checked_init32 +#define fse_in_checked_init fse_in_checked_init32 +#define fse_in_flush fse_in_checked_flush32 +#define fse_in_checked_flush fse_in_checked_flush32 +#define fse_in_flush2 fse_in_checked_flush32 +#define fse_in_checked_flush2 fse_in_checked_flush32 +#define fse_in_pull fse_in_pull32 + +#endif + +/*! @abstract Entry for one symbol in the encoder table (64b). */ +typedef struct { + int16_t s0; // First state requiring a K-bit shift + int16_t k; // States S >= S0 are shifted K bits. States S < S0 are + // shifted K-1 bits + int16_t delta0; // Relative increment used to compute next state if S >= S0 + int16_t delta1; // Relative increment used to compute next state if S < S0 +} fse_encoder_entry; + +/*! @abstract Entry for one state in the decoder table (32b). */ +typedef struct { // DO NOT REORDER THE FIELDS + int8_t k; // Number of bits to read + uint8_t symbol; // Emitted symbol + int16_t delta; // Signed increment used to compute next state (+bias) +} fse_decoder_entry; + +/*! @abstract Entry for one state in the value decoder table (64b). */ +typedef struct { // DO NOT REORDER THE FIELDS + uint8_t total_bits; // state bits + extra value bits = shift for next decode + uint8_t value_bits; // extra value bits + int16_t delta; // state base (delta) + int32_t vbase; // value base +} fse_value_decoder_entry; + +/*! @abstract Encode SYMBOL using the encoder table, and update \c *pstate, + * \c out. + * @note The caller must ensure we have enough bits available in the output + * stream accumulator. */ +FSE_INLINE void fse_encode(fse_state *__restrict pstate, + const fse_encoder_entry *__restrict encoder_table, + fse_out_stream *__restrict out, uint8_t symbol) { + int s = *pstate; + fse_encoder_entry e = encoder_table[symbol]; + int s0 = e.s0; + int k = e.k; + int delta0 = e.delta0; + int delta1 = e.delta1; + + // Number of bits to write + int hi = s >= s0; + fse_bit_count nbits = hi ? k : (k - 1); + fse_state delta = hi ? delta0 : delta1; + + // Write lower NBITS of state + fse_bits b = fse_mask_lsb(s, nbits); + fse_out_push(out, nbits, b); + + // Update state with remaining bits and delta + *pstate = delta + (s >> nbits); +} + +/*! @abstract Decode and return symbol using the decoder table, and update + * \c *pstate, \c in. + * @note The caller must ensure we have enough bits available in the input + * stream accumulator. */ +FSE_INLINE uint8_t fse_decode(fse_state *__restrict pstate, + const int32_t *__restrict decoder_table, + fse_in_stream *__restrict in) { + int32_t e = decoder_table[*pstate]; + + // Update state from K bits of input + DELTA + *pstate = (fse_state)(e >> 16) + (fse_state)fse_in_pull(in, e & 0xff); + + // Return the symbol for this state + return fse_extract_bits(e, 8, 8); // symbol +} + +/*! @abstract Decode and return value using the decoder table, and update \c + * *pstate, \c in. + * \c value_decoder_table[nstates] + * @note The caller must ensure we have enough bits available in the input + * stream accumulator. */ +FSE_INLINE int32_t +fse_value_decode(fse_state *__restrict pstate, + const fse_value_decoder_entry *value_decoder_table, + fse_in_stream *__restrict in) { + fse_value_decoder_entry entry = value_decoder_table[*pstate]; + uint32_t state_and_value_bits = (uint32_t)fse_in_pull(in, entry.total_bits); + *pstate = + (fse_state)(entry.delta + (state_and_value_bits >> entry.value_bits)); + return (int32_t)(entry.vbase + + fse_mask_lsb(state_and_value_bits, entry.value_bits)); +} + +// MARK: - Tables + +// IMPORTANT: To properly decode an FSE encoded stream, both encoder/decoder +// tables shall be initialized with the same parameters, including the +// FREQ[NSYMBOL] array. +// + +/*! @abstract Sanity check on frequency table, verify sum of \c freq + * is <= \c number_of_states. */ +FSE_INLINE int fse_check_freq(const uint16_t *freq_table, + const size_t table_size, + const size_t number_of_states) { + size_t sum_of_freq = 0; + for (int i = 0; i < table_size; i++) { + sum_of_freq += freq_table[i]; + } + return (sum_of_freq > number_of_states) ? -1 : 0; +} + +/*! @abstract Initialize encoder table \c t[nsymbols]. + * + * @param nstates + * sum \c freq[i]; the number of states (a power of 2). + * + * @param nsymbols + * the number of symbols. + * + * @param freq[nsymbols] + * is a normalized histogram of symbol frequencies, with \c freq[i] >= 0. + * Some symbols may have a 0 frequency. In that case they should not be + * present in the data. + */ +void fse_init_encoder_table(int nstates, int nsymbols, + const uint16_t *__restrict freq, + fse_encoder_entry *__restrict t); + +/*! @abstract Initialize decoder table \c t[nstates]. + * + * @param nstates + * sum \c freq[i]; the number of states (a power of 2). + * + * @param nsymbols + * the number of symbols. + * + * @param feq[nsymbols] + * a normalized histogram of symbol frequencies, with \c freq[i] >= 0. + * Some symbols may have a 0 frequency. In that case they should not be + * present in the data. + * + * @return 0 if OK. + * @return -1 on failure. + */ +int fse_init_decoder_table(int nstates, int nsymbols, + const uint16_t *__restrict freq, + int32_t *__restrict t); + +/*! @abstract Initialize value decoder table \c t[nstates]. + * + * @param nstates + * sum \cfreq[i]; the number of states (a power of 2). + * + * @param nsymbols + * the number of symbols. + * + * @param freq[nsymbols] + * a normalized histogram of symbol frequencies, with \c freq[i] >= 0. + * \c symbol_vbits[nsymbols] and \c symbol_vbase[nsymbols] are the number of + * value bits to read and the base value for each symbol. + * Some symbols may have a 0 frequency. In that case they should not be + * present in the data. + */ +void fse_init_value_decoder_table(int nstates, int nsymbols, + const uint16_t *__restrict freq, + const uint8_t *__restrict symbol_vbits, + const int32_t *__restrict symbol_vbase, + fse_value_decoder_entry *__restrict t); + +/*! @abstract Normalize a table \c t[nsymbols] of occurrences to + * \c freq[nsymbols]. */ +void fse_normalize_freq(int nstates, int nsymbols, const uint32_t *__restrict t, + uint16_t *__restrict freq); diff --git a/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_internal.h b/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_internal.h new file mode 100644 index 0000000..7ea5a30 --- /dev/null +++ b/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_internal.h @@ -0,0 +1,616 @@ +/* +Copyright (c) 2015-2016, Apple Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef LZFSE_INTERNAL_H +#define LZFSE_INTERNAL_H + +// Unlike the tunable parameters defined in lzfse_tunables.h, you probably +// should not modify the values defined in this header. Doing so will either +// break the compressor, or result in a compressed data format that is +// incompatible. + +#include "lzfse_fse.h" +#include "lzfse_tunables.h" +#include +#include +#include + +#if defined(_MSC_VER) && !defined(__clang__) +# define LZFSE_INLINE __forceinline +# define __builtin_expect(X, Y) (X) +# define __attribute__(X) +# pragma warning(disable : 4068) // warning C4068: unknown pragma +#else +# define LZFSE_INLINE static inline __attribute__((__always_inline__)) +#endif + +// Implement GCC bit scan builtins for MSVC +#if defined(_MSC_VER) && !defined(__clang__) +#include + +LZFSE_INLINE int __builtin_clz(unsigned int val) { + unsigned long r = 0; + if (_BitScanReverse(&r, val)) { + return 31 - r; + } + return 32; +} + +LZFSE_INLINE int __builtin_ctzl(unsigned long val) { + unsigned long r = 0; + if (_BitScanForward(&r, val)) { + return r; + } + return 32; +} + +LZFSE_INLINE int __builtin_ctzll(uint64_t val) { + unsigned long r = 0; +#if defined(_M_AMD64) || defined(_M_ARM) + if (_BitScanForward64(&r, val)) { + return r; + } +#else + if (_BitScanForward(&r, (uint32_t)val)) { + return r; + } + if (_BitScanForward(&r, (uint32_t)(val >> 32))) { + return 32 + r; + } +#endif + return 64; +} +#endif + +// Throughout LZFSE we refer to "L", "M" and "D"; these will always appear as +// a triplet, and represent a "usual" LZ-style literal and match pair. "L" +// is the number of literal bytes, "M" is the number of match bytes, and "D" +// is the match "distance"; the distance in bytes between the current pointer +// and the start of the match. +#define LZFSE_ENCODE_HASH_VALUES (1 << LZFSE_ENCODE_HASH_BITS) +#define LZFSE_ENCODE_L_SYMBOLS 20 +#define LZFSE_ENCODE_M_SYMBOLS 20 +#define LZFSE_ENCODE_D_SYMBOLS 64 +#define LZFSE_ENCODE_LITERAL_SYMBOLS 256 +#define LZFSE_ENCODE_L_STATES 64 +#define LZFSE_ENCODE_M_STATES 64 +#define LZFSE_ENCODE_D_STATES 256 +#define LZFSE_ENCODE_LITERAL_STATES 1024 +#define LZFSE_MATCHES_PER_BLOCK 10000 +#define LZFSE_LITERALS_PER_BLOCK (4 * LZFSE_MATCHES_PER_BLOCK) +#define LZFSE_DECODE_LITERALS_PER_BLOCK (4 * LZFSE_DECODE_MATCHES_PER_BLOCK) + +// LZFSE internal status. These values are used by internal LZFSE routines +// as return codes. There should not be any good reason to change their +// values; it is plausible that additional codes might be added in the +// future. +#define LZFSE_STATUS_OK 0 +#define LZFSE_STATUS_SRC_EMPTY -1 +#define LZFSE_STATUS_DST_FULL -2 +#define LZFSE_STATUS_ERROR -3 + +// Type representing an offset between elements in a buffer. On 64-bit +// systems, this is stored in a 64-bit container to avoid extra sign- +// extension operations in addressing arithmetic, but the value is always +// representable as a 32-bit signed value in LZFSE's usage. +#if defined(_M_AMD64) || defined(__x86_64__) || defined(__arm64__) +typedef int64_t lzfse_offset; +#else +typedef int32_t lzfse_offset; +#endif + +/*! @abstract History table set. Each line of the history table represents a set + * of candidate match locations, each of which begins with four bytes with the + * same hash. The table contains not only the positions, but also the first + * four bytes at each position. This doubles the memory footprint of the + * table, but allows us to quickly eliminate false-positive matches without + * doing any pointer chasing and without pulling in any additional cachelines. + * This provides a large performance win in practice. */ +typedef struct { + int32_t pos[LZFSE_ENCODE_HASH_WIDTH]; + uint32_t value[LZFSE_ENCODE_HASH_WIDTH]; +} lzfse_history_set; + +/*! @abstract An lzfse match is a sequence of bytes in the source buffer that + * exactly matches an earlier (but possibly overlapping) sequence of bytes in + * the same buffer. + * @code + * exeMPLARYexaMPLE + * | | | ||-|--- lzfse_match2.length=3 + * | | | ||----- lzfse_match2.pos + * | | |-|------ lzfse_match1.length=3 + * | | |-------- lzfse_match1.pos + * | |-------------- lzfse_match2.ref + * |----------------- lzfse_match1.ref + * @endcode + */ +typedef struct { + // Offset of the first byte in the match. + lzfse_offset pos; + // First byte of the source -- the earlier location in the buffer with the + // same contents. + lzfse_offset ref; + // Length of the match. + uint32_t length; +} lzfse_match; + +// MARK: - Encoder and Decoder state objects + +/*! @abstract Encoder state object. */ +typedef struct { + // Pointer to first byte of the source buffer. + const uint8_t *src; + // Length of the source buffer in bytes. Note that this is not a size_t, + // but rather lzfse_offset, which is a signed type. The largest + // representable buffer is 2GB, but arbitrarily large buffers may be + // handled by repeatedly calling the encoder function and "translating" + // the state between calls. When doing this, it is beneficial to use + // blocks smaller than 2GB in order to maintain residency in the last-level + // cache. Consult the implementation of lzfse_encode_buffer for details. + lzfse_offset src_end; + // Offset of the first byte of the next literal to encode in the source + // buffer. + lzfse_offset src_literal; + // Offset of the byte currently being checked for a match. + lzfse_offset src_encode_i; + // The last byte offset to consider for a match. In some uses it makes + // sense to use a smaller offset than src_end. + lzfse_offset src_encode_end; + // Pointer to the next byte to be written in the destination buffer. + uint8_t *dst; + // Pointer to the first byte of the destination buffer. + uint8_t *dst_begin; + // Pointer to one byte past the end of the destination buffer. + uint8_t *dst_end; + // Pending match; will be emitted unless a better match is found. + lzfse_match pending; + // The number of matches written so far. Note that there is no problem in + // using a 32-bit field for this quantity, because the state already limits + // us to at most 2GB of data; there cannot possibly be more matches than + // there are bytes in the input. + uint32_t n_matches; + // The number of literals written so far. + uint32_t n_literals; + // Lengths of found literals. + uint32_t l_values[LZFSE_MATCHES_PER_BLOCK]; + // Lengths of found matches. + uint32_t m_values[LZFSE_MATCHES_PER_BLOCK]; + // Distances of found matches. + uint32_t d_values[LZFSE_MATCHES_PER_BLOCK]; + // Concatenated literal bytes. + uint8_t literals[LZFSE_LITERALS_PER_BLOCK]; + // History table used to search for matches. Each entry of the table + // corresponds to a group of four byte sequences in the input stream + // that hash to the same value. + lzfse_history_set history_table[LZFSE_ENCODE_HASH_VALUES]; +} lzfse_encoder_state; + +/*! @abstract Decoder state object for lzfse compressed blocks. */ +typedef struct { + // Number of matches remaining in the block. + uint32_t n_matches; + // Number of bytes used to encode L, M, D triplets for the block. + uint32_t n_lmd_payload_bytes; + // Pointer to the next literal to emit. + const uint8_t *current_literal; + // L, M, D triplet for the match currently being emitted. This is used only + // if we need to restart after reaching the end of the destination buffer in + // the middle of a literal or match. + int32_t l_value, m_value, d_value; + // FSE stream object. + fse_in_stream lmd_in_stream; + // Offset of L,M,D encoding in the input buffer. Because we read through an + // FSE stream *backwards* while decoding, this is decremented as we move + // through a block. + uint32_t lmd_in_buf; + // The current state of the L, M, and D FSE decoders. + uint16_t l_state, m_state, d_state; + // Internal FSE decoder tables for the current block. These have + // alignment forced to 8 bytes to guarantee that a single state's + // entry cannot span two cachelines. + fse_value_decoder_entry l_decoder[LZFSE_ENCODE_L_STATES] __attribute__((__aligned__(8))); + fse_value_decoder_entry m_decoder[LZFSE_ENCODE_M_STATES] __attribute__((__aligned__(8))); + fse_value_decoder_entry d_decoder[LZFSE_ENCODE_D_STATES] __attribute__((__aligned__(8))); + int32_t literal_decoder[LZFSE_ENCODE_LITERAL_STATES]; + // The literal stream for the block, plus padding to allow for faster copy + // operations. + uint8_t literals[LZFSE_LITERALS_PER_BLOCK + 64]; +} lzfse_compressed_block_decoder_state; + +// Decoder state object for uncompressed blocks. +typedef struct { uint32_t n_raw_bytes; } uncompressed_block_decoder_state; + +/*! @abstract Decoder state object for lzvn-compressed blocks. */ +typedef struct { + uint32_t n_raw_bytes; + uint32_t n_payload_bytes; + uint32_t d_prev; +} lzvn_compressed_block_decoder_state; + +/*! @abstract Decoder state object. */ +typedef struct { + // Pointer to next byte to read from source buffer (this is advanced as we + // decode; src_begin describe the buffer and do not change). + const uint8_t *src; + // Pointer to first byte of source buffer. + const uint8_t *src_begin; + // Pointer to one byte past the end of the source buffer. + const uint8_t *src_end; + // Pointer to the next byte to write to destination buffer (this is advanced + // as we decode; dst_begin and dst_end describe the buffer and do not change). + uint8_t *dst; + // Pointer to first byte of destination buffer. + uint8_t *dst_begin; + // Pointer to one byte past the end of the destination buffer. + uint8_t *dst_end; + // 1 if we have reached the end of the stream, 0 otherwise. + int end_of_stream; + // magic number of the current block if we are within a block, + // LZFSE_NO_BLOCK_MAGIC otherwise. + uint32_t block_magic; + lzfse_compressed_block_decoder_state compressed_lzfse_block_state; + lzvn_compressed_block_decoder_state compressed_lzvn_block_state; + uncompressed_block_decoder_state uncompressed_block_state; +} lzfse_decoder_state; + +// MARK: - Block header objects + +#define LZFSE_NO_BLOCK_MAGIC 0x00000000 // 0 (invalid) +#define LZFSE_ENDOFSTREAM_BLOCK_MAGIC 0x24787662 // bvx$ (end of stream) +#define LZFSE_UNCOMPRESSED_BLOCK_MAGIC 0x2d787662 // bvx- (raw data) +#define LZFSE_COMPRESSEDV1_BLOCK_MAGIC 0x31787662 // bvx1 (lzfse compressed, uncompressed tables) +#define LZFSE_COMPRESSEDV2_BLOCK_MAGIC 0x32787662 // bvx2 (lzfse compressed, compressed tables) +#define LZFSE_COMPRESSEDLZVN_BLOCK_MAGIC 0x6e787662 // bvxn (lzvn compressed) + +/*! @abstract Uncompressed block header in encoder stream. */ +typedef struct { + // Magic number, always LZFSE_UNCOMPRESSED_BLOCK_MAGIC. + uint32_t magic; + // Number of raw bytes in block. + uint32_t n_raw_bytes; +} uncompressed_block_header; + +/*! @abstract Compressed block header with uncompressed tables. */ +typedef struct { + // Magic number, always LZFSE_COMPRESSEDV1_BLOCK_MAGIC. + uint32_t magic; + // Number of decoded (output) bytes in block. + uint32_t n_raw_bytes; + // Number of encoded (source) bytes in block. + uint32_t n_payload_bytes; + // Number of literal bytes output by block (*not* the number of literals). + uint32_t n_literals; + // Number of matches in block (which is also the number of literals). + uint32_t n_matches; + // Number of bytes used to encode literals. + uint32_t n_literal_payload_bytes; + // Number of bytes used to encode matches. + uint32_t n_lmd_payload_bytes; + + // Final encoder states for the block, which will be the initial states for + // the decoder: + // Final accum_nbits for literals stream. + int32_t literal_bits; + // There are four interleaved streams of literals, so there are four final + // states. + uint16_t literal_state[4]; + // accum_nbits for the l, m, d stream. + int32_t lmd_bits; + // Final L (literal length) state. + uint16_t l_state; + // Final M (match length) state. + uint16_t m_state; + // Final D (match distance) state. + uint16_t d_state; + + // Normalized frequency tables for each stream. Sum of values in each + // array is the number of states. + uint16_t l_freq[LZFSE_ENCODE_L_SYMBOLS]; + uint16_t m_freq[LZFSE_ENCODE_M_SYMBOLS]; + uint16_t d_freq[LZFSE_ENCODE_D_SYMBOLS]; + uint16_t literal_freq[LZFSE_ENCODE_LITERAL_SYMBOLS]; +} lzfse_compressed_block_header_v1; + +/*! @abstract Compressed block header with compressed tables. Note that because + * freq[] is compressed, the structure-as-stored-in-the-stream is *truncated*; + * we only store the used bytes of freq[]. This means that some extra care must + * be taken when reading one of these headers from the stream. */ +typedef struct { + // Magic number, always LZFSE_COMPRESSEDV2_BLOCK_MAGIC. + uint32_t magic; + // Number of decoded (output) bytes in block. + uint32_t n_raw_bytes; + // The fields n_payload_bytes ... d_state from the + // lzfse_compressed_block_header_v1 object are packed into three 64-bit + // fields in the compressed header, as follows: + // + // offset bits value + // 0 20 n_literals + // 20 20 n_literal_payload_bytes + // 40 20 n_matches + // 60 3 literal_bits + // 63 1 --- unused --- + // + // 0 10 literal_state[0] + // 10 10 literal_state[1] + // 20 10 literal_state[2] + // 30 10 literal_state[3] + // 40 20 n_lmd_payload_bytes + // 60 3 lmd_bits + // 63 1 --- unused --- + // + // 0 32 header_size (total header size in bytes; this does not + // correspond to a field in the uncompressed header version, + // but is required; we wouldn't know the size of the + // compresssed header otherwise. + // 32 10 l_state + // 42 10 m_state + // 52 10 d_state + // 62 2 --- unused --- + uint64_t packed_fields[3]; + // Variable size freq tables, using a Huffman-style fixed encoding. + // Size allocated here is an upper bound (all values stored on 16 bits). + uint8_t freq[2 * (LZFSE_ENCODE_L_SYMBOLS + LZFSE_ENCODE_M_SYMBOLS + + LZFSE_ENCODE_D_SYMBOLS + LZFSE_ENCODE_LITERAL_SYMBOLS)]; +} __attribute__((__packed__, __aligned__(1))) +lzfse_compressed_block_header_v2; + +/*! @abstract LZVN compressed block header. */ +typedef struct { + // Magic number, always LZFSE_COMPRESSEDLZVN_BLOCK_MAGIC. + uint32_t magic; + // Number of decoded (output) bytes. + uint32_t n_raw_bytes; + // Number of encoded (source) bytes. + uint32_t n_payload_bytes; +} lzvn_compressed_block_header; + +// MARK: - LZFSE encode/decode interfaces + +int lzfse_encode_init(lzfse_encoder_state *s); +int lzfse_encode_translate(lzfse_encoder_state *s, lzfse_offset delta); +int lzfse_encode_base(lzfse_encoder_state *s); +int lzfse_encode_finish(lzfse_encoder_state *s); +int lzfse_decode(lzfse_decoder_state *s); + +// MARK: - LZVN encode/decode interfaces + +// Minimum source buffer size for compression. Smaller buffers will not be +// compressed; the lzvn encoder will simply return. +#define LZVN_ENCODE_MIN_SRC_SIZE ((size_t)8) + +// Maximum source buffer size for compression. Larger buffers will be +// compressed partially. +#define LZVN_ENCODE_MAX_SRC_SIZE ((size_t)0xffffffffU) + +// Minimum destination buffer size for compression. No compression will take +// place if smaller. +#define LZVN_ENCODE_MIN_DST_SIZE ((size_t)8) + +size_t lzvn_decode_scratch_size(void); +size_t lzvn_encode_scratch_size(void); +size_t lzvn_encode_buffer(void *__restrict dst, size_t dst_size, + const void *__restrict src, size_t src_size, + void *__restrict work); +size_t lzvn_decode_buffer(void *__restrict dst, size_t dst_size, + const void *__restrict src, size_t src_size); + +/*! @abstract Signed offset in buffers, stored on either 32 or 64 bits. */ +#if defined(_M_AMD64) || defined(__x86_64__) || defined(__arm64__) +typedef int64_t lzvn_offset; +#else +typedef int32_t lzvn_offset; +#endif + +// MARK: - LZFSE utility functions + +/*! @abstract Load bytes from memory location SRC. */ +LZFSE_INLINE uint16_t load2(const void *ptr) { + uint16_t data; + memcpy(&data, ptr, sizeof data); + return data; +} + +LZFSE_INLINE uint32_t load4(const void *ptr) { + uint32_t data; + memcpy(&data, ptr, sizeof data); + return data; +} + +LZFSE_INLINE uint64_t load8(const void *ptr) { + uint64_t data; + memcpy(&data, ptr, sizeof data); + return data; +} + +/*! @abstract Store bytes to memory location DST. */ +LZFSE_INLINE void store2(void *ptr, uint16_t data) { + memcpy(ptr, &data, sizeof data); +} + +LZFSE_INLINE void store4(void *ptr, uint32_t data) { + memcpy(ptr, &data, sizeof data); +} + +LZFSE_INLINE void store8(void *ptr, uint64_t data) { + memcpy(ptr, &data, sizeof data); +} + +/*! @abstract Load+store bytes from locations SRC to DST. Not intended for use + * with overlapping buffers. Note that for LZ-style compression, you need + * copies to behave like naive memcpy( ) implementations do, splatting the + * leading sequence if the buffers overlap. This copy does not do that, so + * should not be used with overlapping buffers. */ +LZFSE_INLINE void copy8(void *dst, const void *src) { store8(dst, load8(src)); } +LZFSE_INLINE void copy16(void *dst, const void *src) { + uint64_t m0 = load8(src); + uint64_t m1 = load8((const unsigned char *)src + 8); + store8(dst, m0); + store8((unsigned char *)dst + 8, m1); +} + +// =============================================================== +// Bitfield Operations + +/*! @abstract Extracts \p width bits from \p container, starting with \p lsb; if + * we view \p container as a bit array, we extract \c container[lsb:lsb+width]. */ +LZFSE_INLINE uintmax_t extract(uintmax_t container, unsigned lsb, + unsigned width) { + static const size_t container_width = sizeof container * 8; + assert(lsb < container_width); + assert(width > 0 && width <= container_width); + assert(lsb + width <= container_width); + if (width == container_width) + return container; + return (container >> lsb) & (((uintmax_t)1 << width) - 1); +} + +/*! @abstract Inserts \p width bits from \p data into \p container, starting with \p lsb. + * Viewed as bit arrays, the operations is: + * @code + * container[:lsb] is unchanged + * container[lsb:lsb+width] <-- data[0:width] + * container[lsb+width:] is unchanged + * @endcode + */ +LZFSE_INLINE uintmax_t insert(uintmax_t container, uintmax_t data, unsigned lsb, + unsigned width) { + static const size_t container_width = sizeof container * 8; + assert(lsb < container_width); + assert(width > 0 && width <= container_width); + assert(lsb + width <= container_width); + if (width == container_width) + return container; + uintmax_t mask = ((uintmax_t)1 << width) - 1; + return (container & ~(mask << lsb)) | (data & mask) << lsb; +} + +/*! @abstract Perform sanity checks on the values of lzfse_compressed_block_header_v1. + * Test that the field values are in the allowed limits, verify that the + * frequency tables sum to value less than total number of states. + * @return 0 if all tests passed. + * @return negative error code with 1 bit set for each failed test. */ +LZFSE_INLINE int lzfse_check_block_header_v1( + const lzfse_compressed_block_header_v1 *header) { + int tests_results = 0; + tests_results = + tests_results | + ((header->magic == LZFSE_COMPRESSEDV1_BLOCK_MAGIC) ? 0 : (1 << 0)); + tests_results = + tests_results | + ((header->n_literals <= LZFSE_LITERALS_PER_BLOCK) ? 0 : (1 << 1)); + tests_results = + tests_results | + ((header->n_matches <= LZFSE_MATCHES_PER_BLOCK) ? 0 : (1 << 2)); + + uint16_t literal_state[4]; + memcpy(literal_state, header->literal_state, sizeof(uint16_t) * 4); + + tests_results = + tests_results | + ((literal_state[0] < LZFSE_ENCODE_LITERAL_STATES) ? 0 : (1 << 3)); + tests_results = + tests_results | + ((literal_state[1] < LZFSE_ENCODE_LITERAL_STATES) ? 0 : (1 << 4)); + tests_results = + tests_results | + ((literal_state[2] < LZFSE_ENCODE_LITERAL_STATES) ? 0 : (1 << 5)); + tests_results = + tests_results | + ((literal_state[3] < LZFSE_ENCODE_LITERAL_STATES) ? 0 : (1 << 6)); + + tests_results = tests_results | + ((header->l_state < LZFSE_ENCODE_L_STATES) ? 0 : (1 << 7)); + tests_results = tests_results | + ((header->m_state < LZFSE_ENCODE_M_STATES) ? 0 : (1 << 8)); + tests_results = tests_results | + ((header->d_state < LZFSE_ENCODE_D_STATES) ? 0 : (1 << 9)); + + int res; + res = fse_check_freq(header->l_freq, LZFSE_ENCODE_L_SYMBOLS, + LZFSE_ENCODE_L_STATES); + tests_results = tests_results | ((res == 0) ? 0 : (1 << 10)); + res = fse_check_freq(header->m_freq, LZFSE_ENCODE_M_SYMBOLS, + LZFSE_ENCODE_M_STATES); + tests_results = tests_results | ((res == 0) ? 0 : (1 << 11)); + res = fse_check_freq(header->d_freq, LZFSE_ENCODE_D_SYMBOLS, + LZFSE_ENCODE_D_STATES); + tests_results = tests_results | ((res == 0) ? 0 : (1 << 12)); + res = fse_check_freq(header->literal_freq, LZFSE_ENCODE_LITERAL_SYMBOLS, + LZFSE_ENCODE_LITERAL_STATES); + tests_results = tests_results | ((res == 0) ? 0 : (1 << 13)); + + if (tests_results) { + return tests_results | 0x80000000; // each 1 bit is a test that failed + // (except for the sign bit) + } + + return 0; // OK +} + +// MARK: - L, M, D encoding constants for LZFSE + +// Largest encodable L (literal length), M (match length) and D (match +// distance) values. +#define LZFSE_ENCODE_MAX_L_VALUE 315 +#define LZFSE_ENCODE_MAX_M_VALUE 2359 +#define LZFSE_ENCODE_MAX_D_VALUE 262139 + +/*! @abstract The L, M, D data streams are all encoded as a "base" value, which is + * FSE-encoded, and an "extra bits" value, which is the difference between + * value and base, and is simply represented as a raw bit value (because it + * is the low-order bits of a larger number, not much entropy can be + * extracted from these bits by more complex encoding schemes). The following + * tables represent the number of low-order bits to encode separately and the + * base values for each of L, M, and D. + * + * @note The inverse tables for mapping the other way are significantly larger. + * Those tables have been split out to lzfse_encode_tables.h in order to keep + * this file relatively small. */ +static const uint8_t l_extra_bits[LZFSE_ENCODE_L_SYMBOLS] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 5, 8 +}; +static const int32_t l_base_value[LZFSE_ENCODE_L_SYMBOLS] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 28, 60 +}; +static const uint8_t m_extra_bits[LZFSE_ENCODE_M_SYMBOLS] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 5, 8, 11 +}; +static const int32_t m_base_value[LZFSE_ENCODE_M_SYMBOLS] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 24, 56, 312 +}; +static const uint8_t d_extra_bits[LZFSE_ENCODE_D_SYMBOLS] = { + 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, + 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, + 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, + 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15 +}; +static const int32_t d_base_value[LZFSE_ENCODE_D_SYMBOLS] = { + 0, 1, 2, 3, 4, 6, 8, 10, 12, 16, + 20, 24, 28, 36, 44, 52, 60, 76, 92, 108, + 124, 156, 188, 220, 252, 316, 380, 444, 508, 636, + 764, 892, 1020, 1276, 1532, 1788, 2044, 2556, 3068, 3580, + 4092, 5116, 6140, 7164, 8188, 10236, 12284, 14332, 16380, 20476, + 24572, 28668, 32764, 40956, 49148, 57340, 65532, 81916, 98300, 114684, + 131068, 163836, 196604, 229372 +}; + +#endif // LZFSE_INTERNAL_H diff --git a/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_main.c b/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_main.c new file mode 100644 index 0000000..dd4df99 --- /dev/null +++ b/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_main.c @@ -0,0 +1,336 @@ +/* +Copyright (c) 2015-2016, Apple Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +// LZFSE command line tool + +#if !defined(_POSIX_C_SOURCE) || (_POSIX_C_SOURCE < 200112L) +# undef _POSIX_C_SOURCE +# define _POSIX_C_SOURCE 200112L +#endif + +#if defined(_MSC_VER) +# if !defined(_CRT_NONSTDC_NO_DEPRECATE) +# define _CRT_NONSTDC_NO_DEPRECATE +# endif +# if !defined(_CRT_SECURE_NO_WARNINGS) +# define _CRT_SECURE_NO_WARNINGS +# endif +# if !defined(__clang__) +# define inline __inline +# endif +#endif + +#include "lzfse.h" +#include +#include +#include +#include +#include +#include + +#if defined(_MSC_VER) +# include +# include +#else +# include +# include +#endif + +// Same as realloc(x,s), except x is freed when realloc fails +static inline void *lzfse_reallocf(void *x, size_t s) { + void *y = realloc(x, s); + if (y == 0) { + free(x); + return 0; + } + return y; +} + +static double get_time() { +#if defined(_MSC_VER) + LARGE_INTEGER count, freq; + if (QueryPerformanceFrequency(&freq) && QueryPerformanceCounter(&count)) { + return (double)count.QuadPart / (double)freq.QuadPart; + } + return 1.0e-3 * (double)GetTickCount(); +#else + struct timeval tv; + if (gettimeofday(&tv, 0) != 0) { + perror("gettimeofday"); + exit(1); + } + return (double)tv.tv_sec + 1.0e-6 * (double)tv.tv_usec; +#endif +} + +//-------------------- + +enum { LZFSE_ENCODE = 0, LZFSE_DECODE }; + +void usage(int argc, char **argv) { + fprintf( + stderr, + "Usage: %s -encode|-decode [-i input_file] [-o output_file] [-h] [-v]\n", + argv[0]); +} + +#define USAGE(argc, argv) \ + do { \ + usage(argc, argv); \ + exit(0); \ + } while (0) +#define USAGE_MSG(argc, argv, ...) \ + do { \ + usage(argc, argv); \ + fprintf(stderr, __VA_ARGS__); \ + exit(1); \ + } while (0) + +int main(int argc, char **argv) { + const char *in_file = 0; // stdin + const char *out_file = 0; // stdout + int op = -1; // invalid op + int verbosity = 0; // quiet + + // Parse options + for (int i = 1; i < argc;) { + // no args + const char *a = argv[i++]; + if (strcmp(a, "-h") == 0) + USAGE(argc, argv); + if (strcmp(a, "-v") == 0) { + verbosity++; + continue; + } + if (strcmp(a, "-encode") == 0) { + op = LZFSE_ENCODE; + continue; + } + if (strcmp(a, "-decode") == 0) { + op = LZFSE_DECODE; + continue; + } + + // one arg + const char **arg_var = 0; + if (strcmp(a, "-i") == 0 && in_file == 0) + arg_var = &in_file; + else if (strcmp(a, "-o") == 0 && out_file == 0) + arg_var = &out_file; + if (arg_var != 0) { + // Flag is recognized. Check if there is an argument. + if (i == argc) + USAGE_MSG(argc, argv, "Error: Missing arg after %s\n", a); + *arg_var = argv[i++]; + continue; + } + + USAGE_MSG(argc, argv, "Error: invalid flag %s\n", a); + } + if (op < 0) + USAGE_MSG(argc, argv, "Error: -encode|-decode required\n"); + + // Info + if (verbosity > 0) { + if (op == LZFSE_ENCODE) + fprintf(stderr, "LZFSE encode\n"); + if (op == LZFSE_DECODE) + fprintf(stderr, "LZFSE decode\n"); + fprintf(stderr, "Input: %s\n", in_file ? in_file : "stdin"); + fprintf(stderr, "Output: %s\n", out_file ? out_file : "stdout"); + } + + // Load input + size_t in_allocated = 0; // allocated in IN + size_t in_size = 0; // used in IN + uint8_t *in = 0; // input buffer + int in_fd = -1; // input file desc + + if (in_file != 0) { + // If we have a file name, open it, and allocate the exact input size + struct stat st; +#if defined(_WIN32) + in_fd = open(in_file, O_RDONLY | O_BINARY); +#else + in_fd = open(in_file, O_RDONLY); +#endif + if (in_fd < 0) { + perror(in_file); + exit(1); + } + if (fstat(in_fd, &st) != 0) { + perror(in_file); + exit(1); + } + if (st.st_size > SIZE_MAX) { + fprintf(stderr, "File is too large\n"); + exit(1); + } + in_allocated = (size_t)st.st_size; + } else { + // Otherwise, read from stdin, and allocate to 1 MB, grow as needed + in_allocated = 1 << 20; + in_fd = 0; +#if defined(_WIN32) + if (setmode(in_fd, O_BINARY) == -1) { + perror("setmode"); + exit(1); + } +#endif + } + in = (uint8_t *)malloc(in_allocated); + if (in == 0) { + perror("malloc"); + exit(1); + } + + while (1) { + // re-alloc if needed + if (in_size == in_allocated) { + if (in_allocated < (100 << 20)) + in_allocated <<= 1; // double it + else + in_allocated += (100 << 20); // or add 100 MB if already large + in = lzfse_reallocf(in, in_allocated); + if (in == 0) { + perror("malloc"); + exit(1); + } + } + + ptrdiff_t r = read(in_fd, in + in_size, in_allocated - in_size); + if (r < 0) { + perror("read"); + exit(1); + } + if (r == 0) + break; // end of file + in_size += (size_t)r; + } + + if (in_file != 0) { + close(in_fd); + in_fd = -1; + } + + // Size info + if (verbosity > 0) { + fprintf(stderr, "Input size: %zu B\n", in_size); + } + + // Encode/decode + // Compute size for result buffer; we assume here that encode shrinks size, + // and that decode grows by no more than 4x. These are reasonable common- + // case guidelines, but are not formally guaranteed to be satisfied. + size_t out_allocated = (op == LZFSE_ENCODE) ? in_size : (4 * in_size); + size_t out_size = 0; + size_t aux_allocated = (op == LZFSE_ENCODE) ? lzfse_encode_scratch_size() + : lzfse_decode_scratch_size(); + void *aux = aux_allocated ? malloc(aux_allocated) : 0; + if (aux_allocated != 0 && aux == 0) { + perror("malloc"); + exit(1); + } + uint8_t *out = (uint8_t *)malloc(out_allocated); + if (out == 0) { + perror("malloc"); + exit(1); + } + + double c0 = get_time(); + while (1) { + if (op == LZFSE_ENCODE) + out_size = lzfse_encode_buffer(out, out_allocated, in, in_size, aux); + else + out_size = lzfse_decode_buffer(out, out_allocated, in, in_size, aux); + + // If output buffer was too small, grow and retry. + if (out_size == 0 || (op == LZFSE_DECODE && out_size == out_allocated)) { + if (verbosity > 0) + fprintf(stderr, "Output buffer was too small, increasing size...\n"); + out_allocated <<= 1; + out = (uint8_t *)lzfse_reallocf(out, out_allocated); + if (out == 0) { + perror("malloc"); + exit(1); + } + continue; + } + + break; + } + double c1 = get_time(); + + if (verbosity > 0) { + fprintf(stderr, "Output size: %zu B\n", out_size); + size_t raw_size = (op == LZFSE_ENCODE) ? in_size : out_size; + size_t compressed_size = (op == LZFSE_ENCODE) ? out_size : in_size; + fprintf(stderr, "Compression ratio: %.3f\n", + (double)raw_size / (double)compressed_size); + double ns_per_byte = 1.0e9 * (c1 - c0) / (double)raw_size; + double mb_per_s = (double)raw_size / 1024.0 / 1024.0 / (c1 - c0); + fprintf(stderr, "Speed: %.2f ns/B, %.2f MB/s\n",ns_per_byte,mb_per_s); + } + + // Write output + int out_fd = -1; + if (out_file) { +#if defined(_WIN32) + out_fd = open(out_file, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, + S_IWRITE); +#else + out_fd = open(out_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); +#endif + if (out_fd < 0) { + perror(out_file); + exit(1); + } + } else { + out_fd = 1; // stdout +#if defined(_WIN32) + if (setmode(out_fd, O_BINARY) == -1) { + perror("setmode"); + exit(1); + } +#endif + } + for (size_t out_pos = 0; out_pos < out_size;) { + ptrdiff_t w = write(out_fd, out + out_pos, out_size - out_pos); + if (w < 0) { + perror("write"); + exit(1); + } + if (w == 0) { + fprintf(stderr, "Failed to write to output file\n"); + exit(1); + } + out_pos += (size_t)w; + } + if (out_file != 0) { + close(out_fd); + out_fd = -1; + } + + free(in); + free(out); + free(aux); + return 0; // OK +} diff --git a/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_tunables.h b/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_tunables.h new file mode 100644 index 0000000..4485a80 --- /dev/null +++ b/vendor/apfs-fuse/3rdparty/lzfse/src/lzfse_tunables.h @@ -0,0 +1,60 @@ +/* +Copyright (c) 2015-2016, Apple Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef LZFSE_TUNABLES_H +#define LZFSE_TUNABLES_H + +// Parameters controlling details of the LZ-style match search. These values +// may be modified to fine tune compression ratio vs. encoding speed, while +// keeping the compressed format compatible with LZFSE. Note that +// modifying them will also change the amount of work space required by +// the encoder. The values here are those used in the compression library +// on iOS and OS X. + +// Number of bits for hash function to produce. Should be in the range +// [10, 16]. Larger values reduce the number of false-positive found during +// the match search, and expand the history table, which may allow additional +// matches to be found, generally improving the achieved compression ratio. +// Larger values also increase the workspace size, and make it less likely +// that the history table will be present in cache, which reduces performance. +#define LZFSE_ENCODE_HASH_BITS 14 + +// Number of positions to store for each line in the history table. May +// be either 4 or 8. Using 8 doubles the size of the history table, which +// increases the chance of finding matches (thus improving compression ratio), +// but also increases the workspace size. +#define LZFSE_ENCODE_HASH_WIDTH 4 + +// Match length in bytes to cause immediate emission. Generally speaking, +// LZFSE maintains multiple candidate matches and waits to decide which match +// to emit until more information is available. When a match exceeds this +// threshold, it is emitted immediately. Thus, smaller values may give +// somewhat better performance, and larger values may give somewhat better +// compression ratios. +#define LZFSE_ENCODE_GOOD_MATCH 40 + +// When the source buffer is very small, LZFSE doesn't compress as well as +// some simpler algorithms. To maintain reasonable compression for these +// cases, we transition to use LZVN instead if the size of the source buffer +// is below this threshold. +#define LZFSE_ENCODE_LZVN_THRESHOLD 4096 + +#endif // LZFSE_TUNABLES_H diff --git a/vendor/apfs-fuse/3rdparty/lzfse/src/lzvn_decode_base.c b/vendor/apfs-fuse/3rdparty/lzfse/src/lzvn_decode_base.c new file mode 100644 index 0000000..c30180e --- /dev/null +++ b/vendor/apfs-fuse/3rdparty/lzfse/src/lzvn_decode_base.c @@ -0,0 +1,711 @@ +/* +Copyright (c) 2015-2016, Apple Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +// LZVN low-level decoder + +#include "lzvn_decode_base.h" + +#if !defined(HAVE_LABELS_AS_VALUES) +# if defined(__GNUC__) || defined(__clang__) +# define HAVE_LABELS_AS_VALUES 1 +# else +# define HAVE_LABELS_AS_VALUES 0 +# endif +#endif + +// Both the source and destination buffers are represented by a pointer and +// a length; they are *always* updated in concert using this macro; however +// many bytes the pointer is advanced, the length is decremented by the same +// amount. Thus, pointer + length always points to the byte one past the end +// of the buffer. +#define PTR_LEN_INC(_pointer, _length, _increment) \ + (_pointer += _increment, _length -= _increment) + +// Update state with current positions and distance, corresponding to the +// beginning of an instruction in both streams +#define UPDATE_GOOD \ + (state->src = src_ptr, state->dst = dst_ptr, state->d_prev = D) + +void lzvn_decode(lzvn_decoder_state *state) { +#if HAVE_LABELS_AS_VALUES + // Jump table for all instructions + static const void *opc_tbl[256] = { + &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&eos, &&lrg_d, + &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&nop, &&lrg_d, + &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&nop, &&lrg_d, + &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&udef, &&lrg_d, + &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&udef, &&lrg_d, + &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&udef, &&lrg_d, + &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&udef, &&lrg_d, + &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&udef, &&lrg_d, + &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&pre_d, &&lrg_d, + &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&pre_d, &&lrg_d, + &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&pre_d, &&lrg_d, + &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&pre_d, &&lrg_d, + &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&pre_d, &&lrg_d, + &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&pre_d, &&lrg_d, + &&udef, &&udef, &&udef, &&udef, &&udef, &&udef, &&udef, &&udef, + &&udef, &&udef, &&udef, &&udef, &&udef, &&udef, &&udef, &&udef, + &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&pre_d, &&lrg_d, + &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&pre_d, &&lrg_d, + &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&pre_d, &&lrg_d, + &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&pre_d, &&lrg_d, + &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, + &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, + &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, + &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, + &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&pre_d, &&lrg_d, + &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&pre_d, &&lrg_d, + &&udef, &&udef, &&udef, &&udef, &&udef, &&udef, &&udef, &&udef, + &&udef, &&udef, &&udef, &&udef, &&udef, &&udef, &&udef, &&udef, + &&lrg_l, &&sml_l, &&sml_l, &&sml_l, &&sml_l, &&sml_l, &&sml_l, &&sml_l, + &&sml_l, &&sml_l, &&sml_l, &&sml_l, &&sml_l, &&sml_l, &&sml_l, &&sml_l, + &&lrg_m, &&sml_m, &&sml_m, &&sml_m, &&sml_m, &&sml_m, &&sml_m, &&sml_m, + &&sml_m, &&sml_m, &&sml_m, &&sml_m, &&sml_m, &&sml_m, &&sml_m, &&sml_m}; +#endif + size_t src_len = state->src_end - state->src; + size_t dst_len = state->dst_end - state->dst; + if (src_len == 0 || dst_len == 0) + return; // empty buffer + + const unsigned char *src_ptr = state->src; + unsigned char *dst_ptr = state->dst; + size_t D = state->d_prev; + size_t M; + size_t L; + size_t opc_len; + + // Do we have a partially expanded match saved in state? + if (state->L != 0 || state->M != 0) { + L = state->L; + M = state->M; + D = state->D; + opc_len = 0; // we already skipped the op + state->L = state->M = state->D = 0; + if (M == 0) + goto copy_literal; + if (L == 0) + goto copy_match; + goto copy_literal_and_match; + } + + unsigned char opc = src_ptr[0]; + +#if HAVE_LABELS_AS_VALUES + goto *opc_tbl[opc]; +#else + for (;;) { + switch (opc) { +#endif +// =============================================================== +// These four opcodes (sml_d, med_d, lrg_d, and pre_d) encode both a +// literal and a match. The bulk of their implementations are shared; +// each label here only does the work of setting the opcode length (not +// including any literal bytes), and extracting the literal length, match +// length, and match distance (except in pre_d). They then jump into the +// shared implementation to actually output the literal and match bytes. +// +// No error checking happens in the first stage, except for ensuring that +// the source has enough length to represent the full opcode before +// reading past the first byte. +sml_d: +#if !HAVE_LABELS_AS_VALUES + case 0: + case 1: + case 2: + case 3: + case 4: + case 5: + case 8: + case 9: + case 10: + case 11: + case 12: + case 13: + case 16: + case 17: + case 18: + case 19: + case 20: + case 21: + case 24: + case 25: + case 26: + case 27: + case 28: + case 29: + case 32: + case 33: + case 34: + case 35: + case 36: + case 37: + case 40: + case 41: + case 42: + case 43: + case 44: + case 45: + case 48: + case 49: + case 50: + case 51: + case 52: + case 53: + case 56: + case 57: + case 58: + case 59: + case 60: + case 61: + case 64: + case 65: + case 66: + case 67: + case 68: + case 69: + case 72: + case 73: + case 74: + case 75: + case 76: + case 77: + case 80: + case 81: + case 82: + case 83: + case 84: + case 85: + case 88: + case 89: + case 90: + case 91: + case 92: + case 93: + case 96: + case 97: + case 98: + case 99: + case 100: + case 101: + case 104: + case 105: + case 106: + case 107: + case 108: + case 109: + case 128: + case 129: + case 130: + case 131: + case 132: + case 133: + case 136: + case 137: + case 138: + case 139: + case 140: + case 141: + case 144: + case 145: + case 146: + case 147: + case 148: + case 149: + case 152: + case 153: + case 154: + case 155: + case 156: + case 157: + case 192: + case 193: + case 194: + case 195: + case 196: + case 197: + case 200: + case 201: + case 202: + case 203: + case 204: + case 205: +#endif + UPDATE_GOOD; + // "small distance": This opcode has the structure LLMMMDDD DDDDDDDD LITERAL + // where the length of literal (0-3 bytes) is encoded by the high 2 bits of + // the first byte. We first extract the literal length so we know how long + // the opcode is, then check that the source can hold both this opcode and + // at least one byte of the next (because any valid input stream must be + // terminated with an eos token). + opc_len = 2; + L = (size_t)extract(opc, 6, 2); + M = (size_t)extract(opc, 3, 3) + 3; + // We need to ensure that the source buffer is long enough that we can + // safely read this entire opcode, the literal that follows, and the first + // byte of the next opcode. Once we satisfy this requirement, we can + // safely unpack the match distance. A check similar to this one is + // present in all the opcode implementations. + if (src_len <= opc_len + L) + return; // source truncated + D = (size_t)extract(opc, 0, 3) << 8 | src_ptr[1]; + goto copy_literal_and_match; + +med_d: +#if !HAVE_LABELS_AS_VALUES + case 160: + case 161: + case 162: + case 163: + case 164: + case 165: + case 166: + case 167: + case 168: + case 169: + case 170: + case 171: + case 172: + case 173: + case 174: + case 175: + case 176: + case 177: + case 178: + case 179: + case 180: + case 181: + case 182: + case 183: + case 184: + case 185: + case 186: + case 187: + case 188: + case 189: + case 190: + case 191: +#endif + UPDATE_GOOD; + // "medium distance": This is a minor variant of the "small distance" + // encoding, where we will now use two extra bytes instead of one to encode + // the restof the match length and distance. This allows an extra two bits + // for the match length, and an extra three bits for the match distance. The + // full structure of the opcode is 101LLMMM DDDDDDMM DDDDDDDD LITERAL. + opc_len = 3; + L = (size_t)extract(opc, 3, 2); + if (src_len <= opc_len + L) + return; // source truncated + uint16_t opc23 = load2(&src_ptr[1]); + M = (size_t)((extract(opc, 0, 3) << 2 | extract(opc23, 0, 2)) + 3); + D = (size_t)extract(opc23, 2, 14); + goto copy_literal_and_match; + +lrg_d: +#if !HAVE_LABELS_AS_VALUES + case 7: + case 15: + case 23: + case 31: + case 39: + case 47: + case 55: + case 63: + case 71: + case 79: + case 87: + case 95: + case 103: + case 111: + case 135: + case 143: + case 151: + case 159: + case 199: + case 207: +#endif + UPDATE_GOOD; + // "large distance": This is another variant of the "small distance" + // encoding, where we will now use two extra bytes to encode the match + // distance, which allows distances up to 65535 to be represented. The full + // structure of the opcode is LLMMM111 DDDDDDDD DDDDDDDD LITERAL. + opc_len = 3; + L = (size_t)extract(opc, 6, 2); + M = (size_t)extract(opc, 3, 3) + 3; + if (src_len <= opc_len + L) + return; // source truncated + D = load2(&src_ptr[1]); + goto copy_literal_and_match; + +pre_d: +#if !HAVE_LABELS_AS_VALUES + case 70: + case 78: + case 86: + case 94: + case 102: + case 110: + case 134: + case 142: + case 150: + case 158: + case 198: + case 206: +#endif + UPDATE_GOOD; + // "previous distance": This opcode has the structure LLMMM110, where the + // length of the literal (0-3 bytes) is encoded by the high 2 bits of the + // first byte. We first extract the literal length so we know how long + // the opcode is, then check that the source can hold both this opcode and + // at least one byte of the next (because any valid input stream must be + // terminated with an eos token). + opc_len = 1; + L = (size_t)extract(opc, 6, 2); + M = (size_t)extract(opc, 3, 3) + 3; + if (src_len <= opc_len + L) + return; // source truncated + goto copy_literal_and_match; + +copy_literal_and_match: + // Common implementation of writing data for opcodes that have both a + // literal and a match. We begin by advancing the source pointer past + // the opcode, so that it points at the first literal byte (if L + // is non-zero; otherwise it points at the next opcode). + PTR_LEN_INC(src_ptr, src_len, opc_len); + // Now we copy the literal from the source pointer to the destination. + if (__builtin_expect(dst_len >= 4 && src_len >= 4, 1)) { + // The literal is 0-3 bytes; if we are not near the end of the buffer, + // we can safely just do a 4 byte copy (which is guaranteed to cover + // the complete literal, and may include some other bytes as well). + store4(dst_ptr, load4(src_ptr)); + } else if (L <= dst_len) { + // We are too close to the end of either the input or output stream + // to be able to safely use a four-byte copy, but we will not exhaust + // either stream (we already know that the source will not be + // exhausted from checks in the individual opcode implementations, + // and we just tested that dst_len > L). Thus, we need to do a + // byte-by-byte copy of the literal. This is slow, but it can only ever + // happen near the very end of a buffer, so it is not an important case to + // optimize. + for (size_t i = 0; i < L; ++i) + dst_ptr[i] = src_ptr[i]; + } else { + // Destination truncated: fill DST, and store partial match + + // Copy partial literal + for (size_t i = 0; i < dst_len; ++i) + dst_ptr[i] = src_ptr[i]; + // Save state + state->src = src_ptr + dst_len; + state->dst = dst_ptr + dst_len; + state->L = L - dst_len; + state->M = M; + state->D = D; + return; // destination truncated + } + // Having completed the copy of the literal, we advance both the source + // and destination pointers by the number of literal bytes. + PTR_LEN_INC(dst_ptr, dst_len, L); + PTR_LEN_INC(src_ptr, src_len, L); + // Check if the match distance is valid; matches must not reference + // bytes that preceed the start of the output buffer, nor can the match + // distance be zero. + if (D > dst_ptr - state->dst_begin || D == 0) + goto invalid_match_distance; +copy_match: + // Now we copy the match from dst_ptr - D to dst_ptr. It is important to keep + // in mind that we may have D < M, in which case the source and destination + // windows overlap in the copy. The semantics of the match copy are *not* + // those of memmove( ); if the buffers overlap it needs to behave as though + // we were copying byte-by-byte in increasing address order. If, for example, + // D is 1, the copy operation is equivalent to: + // + // memset(dst_ptr, dst_ptr[-1], M); + // + // i.e. it splats the previous byte. This means that we need to be very + // careful about using wide loads or stores to perform the copy operation. + if (__builtin_expect(dst_len >= M + 7 && D >= 8, 1)) { + // We are not near the end of the buffer, and the match distance + // is at least eight. Thus, we can safely loop using eight byte + // copies. The last of these may slop over the intended end of + // the match, but this is OK because we know we have a safety bound + // away from the end of the destination buffer. + for (size_t i = 0; i < M; i += 8) + store8(&dst_ptr[i], load8(&dst_ptr[i - D])); + } else if (M <= dst_len) { + // Either the match distance is too small, or we are too close to + // the end of the buffer to safely use eight byte copies. Fall back + // on a simple byte-by-byte implementation. + for (size_t i = 0; i < M; ++i) + dst_ptr[i] = dst_ptr[i - D]; + } else { + // Destination truncated: fill DST, and store partial match + + // Copy partial match + for (size_t i = 0; i < dst_len; ++i) + dst_ptr[i] = dst_ptr[i - D]; + // Save state + state->src = src_ptr; + state->dst = dst_ptr + dst_len; + state->L = 0; + state->M = M - dst_len; + state->D = D; + return; // destination truncated + } + // Update the destination pointer and length to account for the bytes + // written by the match, then load the next opcode byte and branch to + // the appropriate implementation. + PTR_LEN_INC(dst_ptr, dst_len, M); + opc = src_ptr[0]; +#if HAVE_LABELS_AS_VALUES + goto *opc_tbl[opc]; +#else + break; +#endif + +// =============================================================== +// Opcodes representing only a match (no literal). +// These two opcodes (lrg_m and sml_m) encode only a match. The match +// distance is carried over from the previous opcode, so all they need +// to encode is the match length. We are able to reuse the match copy +// sequence from the literal and match opcodes to perform the actual +// copy implementation. +sml_m: +#if !HAVE_LABELS_AS_VALUES + case 241: + case 242: + case 243: + case 244: + case 245: + case 246: + case 247: + case 248: + case 249: + case 250: + case 251: + case 252: + case 253: + case 254: + case 255: +#endif + UPDATE_GOOD; + // "small match": This opcode has no literal, and uses the previous match + // distance (i.e. it encodes only the match length), in a single byte as + // 1111MMMM. + opc_len = 1; + if (src_len <= opc_len) + return; // source truncated + M = (size_t)extract(opc, 0, 4); + PTR_LEN_INC(src_ptr, src_len, opc_len); + goto copy_match; + +lrg_m: +#if !HAVE_LABELS_AS_VALUES + case 240: +#endif + UPDATE_GOOD; + // "large match": This opcode has no literal, and uses the previous match + // distance (i.e. it encodes only the match length). It is encoded in two + // bytes as 11110000 MMMMMMMM. Because matches smaller than 16 bytes can + // be represented by sml_m, there is an implicit bias of 16 on the match + // length; the representable values are [16,271]. + opc_len = 2; + if (src_len <= opc_len) + return; // source truncated + M = src_ptr[1] + 16; + PTR_LEN_INC(src_ptr, src_len, opc_len); + goto copy_match; + +// =============================================================== +// Opcodes representing only a literal (no match). +// These two opcodes (lrg_l and sml_l) encode only a literal. There is no +// match length or match distance to worry about (but we need to *not* +// touch D, as it must be preserved between opcodes). +sml_l: +#if !HAVE_LABELS_AS_VALUES + case 225: + case 226: + case 227: + case 228: + case 229: + case 230: + case 231: + case 232: + case 233: + case 234: + case 235: + case 236: + case 237: + case 238: + case 239: +#endif + UPDATE_GOOD; + // "small literal": This opcode has no match, and encodes only a literal + // of length up to 15 bytes. The format is 1110LLLL LITERAL. + opc_len = 1; + L = (size_t)extract(opc, 0, 4); + goto copy_literal; + +lrg_l: +#if !HAVE_LABELS_AS_VALUES + case 224: +#endif + UPDATE_GOOD; + // "large literal": This opcode has no match, and uses the previous match + // distance (i.e. it encodes only the match length). It is encoded in two + // bytes as 11100000 LLLLLLLL LITERAL. Because literals smaller than 16 + // bytes can be represented by sml_l, there is an implicit bias of 16 on + // the literal length; the representable values are [16,271]. + opc_len = 2; + if (src_len <= 2) + return; // source truncated + L = src_ptr[1] + 16; + goto copy_literal; + +copy_literal: + // Check that the source buffer is large enough to hold the complete + // literal and at least the first byte of the next opcode. If so, advance + // the source pointer to point to the first byte of the literal and adjust + // the source length accordingly. + if (src_len <= opc_len + L) + return; // source truncated + PTR_LEN_INC(src_ptr, src_len, opc_len); + // Now we copy the literal from the source pointer to the destination. + if (dst_len >= L + 7 && src_len >= L + 7) { + // We are not near the end of the source or destination buffers; thus + // we can safely copy the literal using wide copies, without worrying + // about reading or writing past the end of either buffer. + for (size_t i = 0; i < L; i += 8) + store8(&dst_ptr[i], load8(&src_ptr[i])); + } else if (L <= dst_len) { + // We are too close to the end of either the input or output stream + // to be able to safely use an eight-byte copy. Instead we copy the + // literal byte-by-byte. + for (size_t i = 0; i < L; ++i) + dst_ptr[i] = src_ptr[i]; + } else { + // Destination truncated: fill DST, and store partial match + + // Copy partial literal + for (size_t i = 0; i < dst_len; ++i) + dst_ptr[i] = src_ptr[i]; + // Save state + state->src = src_ptr + dst_len; + state->dst = dst_ptr + dst_len; + state->L = L - dst_len; + state->M = 0; + state->D = D; + return; // destination truncated + } + // Having completed the copy of the literal, we advance both the source + // and destination pointers by the number of literal bytes. + PTR_LEN_INC(dst_ptr, dst_len, L); + PTR_LEN_INC(src_ptr, src_len, L); + // Load the first byte of the next opcode, and jump to its implementation. + opc = src_ptr[0]; +#if HAVE_LABELS_AS_VALUES + goto *opc_tbl[opc]; +#else + break; +#endif + +// =============================================================== +// Other opcodes +nop: +#if !HAVE_LABELS_AS_VALUES + case 14: + case 22: +#endif + UPDATE_GOOD; + opc_len = 1; + if (src_len <= opc_len) + return; // source truncated + PTR_LEN_INC(src_ptr, src_len, opc_len); + opc = src_ptr[0]; +#if HAVE_LABELS_AS_VALUES + goto *opc_tbl[opc]; +#else + break; +#endif + +eos: +#if !HAVE_LABELS_AS_VALUES + case 6: +#endif + opc_len = 8; + if (src_len < opc_len) + return; // source truncated (here we don't need an extra byte for next op + // code) + PTR_LEN_INC(src_ptr, src_len, opc_len); + state->end_of_stream = 1; + UPDATE_GOOD; + return; // end-of-stream + +// =============================================================== +// Return on error +udef: +#if !HAVE_LABELS_AS_VALUES + case 30: + case 38: + case 46: + case 54: + case 62: + case 112: + case 113: + case 114: + case 115: + case 116: + case 117: + case 118: + case 119: + case 120: + case 121: + case 122: + case 123: + case 124: + case 125: + case 126: + case 127: + case 208: + case 209: + case 210: + case 211: + case 212: + case 213: + case 214: + case 215: + case 216: + case 217: + case 218: + case 219: + case 220: + case 221: + case 222: + case 223: +#endif +invalid_match_distance: + + return; // we already updated state +#if !HAVE_LABELS_AS_VALUES + } + } +#endif +} diff --git a/vendor/apfs-fuse/3rdparty/lzfse/src/lzvn_decode_base.h b/vendor/apfs-fuse/3rdparty/lzfse/src/lzvn_decode_base.h new file mode 100644 index 0000000..b4307a5 --- /dev/null +++ b/vendor/apfs-fuse/3rdparty/lzfse/src/lzvn_decode_base.h @@ -0,0 +1,68 @@ +/* +Copyright (c) 2015-2016, Apple Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +// LZVN low-level decoder (v2) +// Functions in the low-level API should switch to these at some point. +// Apr 2014 + +#ifndef LZVN_DECODE_BASE_H +#define LZVN_DECODE_BASE_H + +#include "lzfse_internal.h" + +/*! @abstract Base decoder state. */ +typedef struct { + + // Decoder I/O + + // Next byte to read in source buffer + const unsigned char *src; + // Next byte after source buffer + const unsigned char *src_end; + + // Next byte to write in destination buffer (by decoder) + unsigned char *dst; + // Valid range for destination buffer is [dst_begin, dst_end - 1] + unsigned char *dst_begin; + unsigned char *dst_end; + // Next byte to read in destination buffer (modified by caller) + unsigned char *dst_current; + + // Decoder state + + // Partially expanded match, or 0,0,0. + // In that case, src points to the next literal to copy, or the next op-code + // if L==0. + size_t L, M, D; + + // Distance for last emitted match, or 0 + lzvn_offset d_prev; + + // Did we decode end-of-stream? + int end_of_stream; + +} lzvn_decoder_state; + +/*! @abstract Decode source to destination. + * Updates \p state (src,dst,d_prev). */ +void lzvn_decode(lzvn_decoder_state *state); + +#endif // LZVN_DECODE_BASE_H diff --git a/vendor/apfs-fuse/3rdparty/lzfse/src/lzvn_encode_base.c b/vendor/apfs-fuse/3rdparty/lzfse/src/lzvn_encode_base.c new file mode 100644 index 0000000..c86b551 --- /dev/null +++ b/vendor/apfs-fuse/3rdparty/lzfse/src/lzvn_encode_base.c @@ -0,0 +1,593 @@ +/* +Copyright (c) 2015-2016, Apple Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +// LZVN low-level encoder + +#include "lzvn_encode_base.h" + +#if defined(_MSC_VER) && !defined(__clang__) +# define restrict __restrict +#endif + +// =============================================================== +// Coarse/fine copy, non overlapping buffers + +/*! @abstract Copy at least \p nbytes bytes from \p src to \p dst, by blocks + * of 8 bytes (may go beyond range). No overlap. + * @return \p dst + \p nbytes. */ +static inline unsigned char *lzvn_copy64(unsigned char *restrict dst, + const unsigned char *restrict src, + size_t nbytes) { + for (size_t i = 0; i < nbytes; i += 8) + store8(dst + i, load8(src + i)); + return dst + nbytes; +} + +/*! @abstract Copy exactly \p nbytes bytes from \p src to \p dst (respects range). + * No overlap. + * @return \p dst + \p nbytes. */ +static inline unsigned char *lzvn_copy8(unsigned char *restrict dst, + const unsigned char *restrict src, + size_t nbytes) { + for (size_t i = 0; i < nbytes; i++) + dst[i] = src[i]; + return dst + nbytes; +} + +/*! @abstract Emit (L,0,0) instructions (final literal). + * We read at most \p L bytes from \p p. + * @param p input stream + * @param q1 the first byte after the output buffer. + * @return pointer to the next output, <= \p q1. + * @return \p q1 if output is full. In that case, output will be partially invalid. + */ +static inline unsigned char *emit_literal(const unsigned char *p, + unsigned char *q, unsigned char *q1, + size_t L) { + size_t x; + while (L > 15) { + x = L < 271 ? L : 271; + if (q + x + 10 >= q1) + goto OUT_FULL; + store2(q, 0xE0 + ((x - 16) << 8)); + q += 2; + L -= x; + q = lzvn_copy8(q, p, x); + p += x; + } + if (L > 0) { + if (q + L + 10 >= q1) + goto OUT_FULL; + *q++ = 0xE0 + L; // 1110LLLL + q = lzvn_copy8(q, p, L); + } + return q; + +OUT_FULL: + return q1; +} + +/*! @abstract Emit (L,M,D) instructions. M>=3. + * @param p input stream pointing to the beginning of the literal. We read at + * most \p L+4 bytes from \p p. + * @param q1 the first byte after the output buffer. + * @return pointer to the next output, <= \p q1. + * @return \p q1 if output is full. In that case, output will be partially invalid. + */ +static inline unsigned char *emit(const unsigned char *p, unsigned char *q, + unsigned char *q1, size_t L, size_t M, + size_t D, size_t D_prev) { + size_t x; + while (L > 15) { + x = L < 271 ? L : 271; + if (q + x + 10 >= q1) + goto OUT_FULL; + store2(q, 0xE0 + ((x - 16) << 8)); + q += 2; + L -= x; + q = lzvn_copy64(q, p, x); + p += x; + } + if (L > 3) { + if (q + L + 10 >= q1) + goto OUT_FULL; + *q++ = 0xE0 + L; // 1110LLLL + q = lzvn_copy64(q, p, L); + p += L; + L = 0; + } + x = M <= 10 - 2 * L ? M : 10 - 2 * L; // x = min(10-2*L,M) + M -= x; + x -= 3; // M = (x+3) + M' max value for x is 7-2*L + + // Here L<4 literals remaining, we read them here + uint32_t literal = load4(p); + // P is not accessed after this point + + // Relaxed capacity test covering all cases + if (q + 8 >= q1) + goto OUT_FULL; + + if (D == D_prev) { + if (L == 0) { + *q++ = 0xF0 + (x + 3); // XM! + } else { + *q++ = (L << 6) + (x << 3) + 6; // LLxxx110 + } + store4(q, literal); + q += L; + } else if (D < 2048 - 2 * 256) { + // Short dist D>>8 in 0..5 + *q++ = (D >> 8) + (L << 6) + (x << 3); // LLxxxDDD + *q++ = D & 0xFF; + store4(q, literal); + q += L; + } else if (D >= (1 << 14) || M == 0 || (x + 3) + M > 34) { + // Long dist + *q++ = (L << 6) + (x << 3) + 7; + store2(q, D); + q += 2; + store4(q, literal); + q += L; + } else { + // Medium distance + x += M; + M = 0; + *q++ = 0xA0 + (x >> 2) + (L << 3); + store2(q, D << 2 | (x & 3)); + q += 2; + store4(q, literal); + q += L; + } + + // Issue remaining match + while (M > 15) { + if (q + 2 >= q1) + goto OUT_FULL; + x = M < 271 ? M : 271; + store2(q, 0xf0 + ((x - 16) << 8)); + q += 2; + M -= x; + } + if (M > 0) { + if (q + 1 >= q1) + goto OUT_FULL; + *q++ = 0xF0 + M; // M = 0..15 + } + + return q; + +OUT_FULL: + return q1; +} + +// =============================================================== +// Conversions + +/*! @abstract Return 32-bit value to store for offset x. */ +static inline int32_t offset_to_s32(lzvn_offset x) { return (int32_t)x; } + +/*! @abstract Get offset from 32-bit stored value x. */ +static inline lzvn_offset offset_from_s32(int32_t x) { return (lzvn_offset)x; } + +// =============================================================== +// Hash and Matching + +/*! @abstract Get hash in range \c [0,LZVN_ENCODE_HASH_VALUES-1] from 3 bytes in i. */ +static inline uint32_t hash3i(uint32_t i) { + i &= 0xffffff; // truncate to 24-bit input (slightly increases compression ratio) + uint32_t h = (i * (1 + (1 << 6) + (1 << 12))) >> 12; + return h & (LZVN_ENCODE_HASH_VALUES - 1); +} + +/*! @abstract Return the number [0, 4] of zero bytes in \p x, starting from the + * least significant byte. */ +static inline lzvn_offset trailing_zero_bytes(uint32_t x) { + return (x == 0) ? 4 : (__builtin_ctzl(x) >> 3); +} + +/*! @abstract Return the number [0, 4] of matching chars between values at + * \p src+i and \p src+j, starting from the least significant byte. + * Assumes we can read 4 chars from each position. */ +static inline lzvn_offset nmatch4(const unsigned char *src, lzvn_offset i, + lzvn_offset j) { + uint32_t vi = load4(src + i); + uint32_t vj = load4(src + j); + return trailing_zero_bytes(vi ^ vj); +} + +/*! @abstract Check if l_begin, m_begin, m0_begin (m0_begin < m_begin) can be + * expanded to a match of length at least 3. + * @param m_begin new string to match. + * @param m0_begin candidate old string. + * @param src source buffer, with valid indices src_begin <= i < src_end. + * (src_begin may be <0) + * @return If a match can be found, return 1 and set all \p match fields, + * otherwise return 0. + * @note \p *match should be 0 before the call. */ +static inline int lzvn_find_match(const unsigned char *src, + lzvn_offset src_begin, + lzvn_offset src_end, lzvn_offset l_begin, + lzvn_offset m0_begin, lzvn_offset m_begin, + lzvn_match_info *match) { + lzvn_offset n = nmatch4(src, m_begin, m0_begin); + if (n < 3) + return 0; // no match + + lzvn_offset D = m_begin - m0_begin; // actual distance + if (D <= 0 || D > LZVN_ENCODE_MAX_DISTANCE) + return 0; // distance out of range + + // Expand forward + lzvn_offset m_end = m_begin + n; + while (n == 4 && m_end + 4 < src_end) { + n = nmatch4(src, m_end, m_end - D); + m_end += n; + } + + // Expand backwards over literal + while (m0_begin > src_begin && m_begin > l_begin && + src[m_begin - 1] == src[m0_begin - 1]) { + m0_begin--; + m_begin--; + } + + // OK, we keep it, update MATCH + lzvn_offset M = m_end - m_begin; // match length + match->m_begin = m_begin; + match->m_end = m_end; + match->K = M - ((D < 0x600) ? 2 : 3); + match->M = M; + match->D = D; + + return 1; // OK +} + +/*! @abstract Same as lzvn_find_match, but we already know that N bytes do + * match (N<=4). */ +static inline int lzvn_find_matchN(const unsigned char *src, + lzvn_offset src_begin, + lzvn_offset src_end, lzvn_offset l_begin, + lzvn_offset m0_begin, lzvn_offset m_begin, + lzvn_offset n, lzvn_match_info *match) { + // We can skip the first comparison on 4 bytes + if (n < 3) + return 0; // no match + + lzvn_offset D = m_begin - m0_begin; // actual distance + if (D <= 0 || D > LZVN_ENCODE_MAX_DISTANCE) + return 0; // distance out of range + + // Expand forward + lzvn_offset m_end = m_begin + n; + while (n == 4 && m_end + 4 < src_end) { + n = nmatch4(src, m_end, m_end - D); + m_end += n; + } + + // Expand backwards over literal + while (m0_begin > src_begin && m_begin > l_begin && + src[m_begin - 1] == src[m0_begin - 1]) { + m0_begin--; + m_begin--; + } + + // OK, we keep it, update MATCH + lzvn_offset M = m_end - m_begin; // match length + match->m_begin = m_begin; + match->m_end = m_end; + match->K = M - ((D < 0x600) ? 2 : 3); + match->M = M; + match->D = D; + + return 1; // OK +} + +// =============================================================== +// Encoder Backend + +/*! @abstract Emit a match and update state. + * @return number of bytes written to \p dst. May be 0 if there is no more space + * in \p dst to emit the match. */ +static inline lzvn_offset lzvn_emit_match(lzvn_encoder_state *state, + lzvn_match_info match) { + size_t L = (size_t)(match.m_begin - state->src_literal); // literal count + size_t M = (size_t)match.M; // match length + size_t D = (size_t)match.D; // match distance + size_t D_prev = (size_t)state->d_prev; // previously emitted match distance + unsigned char *dst = emit(state->src + state->src_literal, state->dst, + state->dst_end, L, M, D, D_prev); + // Check if DST is full + if (dst >= state->dst_end) { + return 0; // FULL + } + + // Update state + lzvn_offset dst_used = dst - state->dst; + state->d_prev = match.D; + state->dst = dst; + state->src_literal = match.m_end; + return dst_used; +} + +/*! @abstract Emit a n-bytes literal and update state. + * @return number of bytes written to \p dst. May be 0 if there is no more space + * in \p dst to emit the literal. */ +static inline lzvn_offset lzvn_emit_literal(lzvn_encoder_state *state, + lzvn_offset n) { + size_t L = (size_t)n; + unsigned char *dst = emit_literal(state->src + state->src_literal, state->dst, + state->dst_end, L); + // Check if DST is full + if (dst >= state->dst_end) + return 0; // FULL + + // Update state + lzvn_offset dst_used = dst - state->dst; + state->dst = dst; + state->src_literal += n; + return dst_used; +} + +/*! @abstract Emit end-of-stream and update state. + * @return number of bytes written to \p dst. May be 0 if there is no more space + * in \p dst to emit the instruction. */ +static inline lzvn_offset lzvn_emit_end_of_stream(lzvn_encoder_state *state) { + // Do we have 8 byte in dst? + if (state->dst_end < state->dst + 8) + return 0; // FULL + + // Insert end marker and update state + store8(state->dst, 0x06); // end-of-stream command + state->dst += 8; + return 8; // dst_used +} + +// =============================================================== +// Encoder Functions + +/*! @abstract Initialize encoder table in \p state, uses current I/O parameters. */ +static inline void lzvn_init_table(lzvn_encoder_state *state) { + lzvn_offset index = -LZVN_ENCODE_MAX_DISTANCE; // max match distance + if (index < state->src_begin) + index = state->src_begin; + uint32_t value = load4(state->src + index); + + lzvn_encode_entry_type e; + for (int i = 0; i < 4; i++) { + e.indices[i] = offset_to_s32(index); + e.values[i] = value; + } + for (int u = 0; u < LZVN_ENCODE_HASH_VALUES; u++) + state->table[u] = e; // fill entire table +} + +void lzvn_encode(lzvn_encoder_state *state) { + const lzvn_match_info NO_MATCH = {0}; + + for (; state->src_current < state->src_current_end; state->src_current++) { + // Get 4 bytes at src_current + uint32_t vi = load4(state->src + state->src_current); + + // Compute new hash H at position I, and push value into position table + int h = hash3i(vi); // index of first entry + + // Read table entries for H + lzvn_encode_entry_type e = state->table[h]; + + // Update entry with index=current and value=vi + lzvn_encode_entry_type updated_e; // rotate values, so we will replace the oldest + updated_e.indices[0] = offset_to_s32(state->src_current); + updated_e.indices[1] = e.indices[0]; + updated_e.indices[2] = e.indices[1]; + updated_e.indices[3] = e.indices[2]; + updated_e.values[0] = vi; + updated_e.values[1] = e.values[0]; + updated_e.values[2] = e.values[1]; + updated_e.values[3] = e.values[2]; + + // Do not check matches if still in previously emitted match + if (state->src_current < state->src_literal) + goto after_emit; + +// Update best with candidate if better +#define UPDATE(best, candidate) \ + do { \ + if (candidate.K > best.K || \ + ((candidate.K == best.K) && (candidate.m_end > best.m_end + 1))) { \ + best = candidate; \ + } \ + } while (0) +// Check candidate. Keep if better. +#define CHECK_CANDIDATE(ik, nk) \ + do { \ + lzvn_match_info m1; \ + if (lzvn_find_matchN(state->src, state->src_begin, state->src_end, \ + state->src_literal, ik, state->src_current, nk, &m1)) { \ + UPDATE(incoming, m1); \ + } \ + } while (0) +// Emit match M. Return if we don't have enough space in the destination buffer +#define EMIT_MATCH(m) \ + do { \ + if (lzvn_emit_match(state, m) == 0) \ + return; \ + } while (0) +// Emit literal of length L. Return if we don't have enough space in the +// destination buffer +#define EMIT_LITERAL(l) \ + do { \ + if (lzvn_emit_literal(state, l) == 0) \ + return; \ + } while (0) + + lzvn_match_info incoming = NO_MATCH; + + // Check candidates in order (closest first) + uint32_t diffs[4]; + for (int k = 0; k < 4; k++) + diffs[k] = e.values[k] ^ vi; // XOR, 0 if equal + lzvn_offset ik; // index + lzvn_offset nk; // match byte count + + // The values stored in e.xyzw are 32-bit signed indices, extended to signed + // type lzvn_offset + ik = offset_from_s32(e.indices[0]); + nk = trailing_zero_bytes(diffs[0]); + CHECK_CANDIDATE(ik, nk); + ik = offset_from_s32(e.indices[1]); + nk = trailing_zero_bytes(diffs[1]); + CHECK_CANDIDATE(ik, nk); + ik = offset_from_s32(e.indices[2]); + nk = trailing_zero_bytes(diffs[2]); + CHECK_CANDIDATE(ik, nk); + ik = offset_from_s32(e.indices[3]); + nk = trailing_zero_bytes(diffs[3]); + CHECK_CANDIDATE(ik, nk); + + // Check candidate at previous distance + if (state->d_prev != 0) { + lzvn_match_info m1; + if (lzvn_find_match(state->src, state->src_begin, state->src_end, + state->src_literal, state->src_current - state->d_prev, + state->src_current, &m1)) { + m1.K = m1.M - 1; // fix K for D_prev + UPDATE(incoming, m1); + } + } + + // Here we have the best candidate in incoming, may be NO_MATCH + + // If no incoming match, and literal backlog becomes too high, emit pending + // match, or literals if there is no pending match + if (incoming.M == 0) { + if (state->src_current - state->src_literal >= + LZVN_ENCODE_MAX_LITERAL_BACKLOG) // at this point, we always have + // current >= literal + { + if (state->pending.M != 0) { + EMIT_MATCH(state->pending); + state->pending = NO_MATCH; + } else { + EMIT_LITERAL(271); // emit long literal (271 is the longest literal size we allow) + } + } + goto after_emit; + } + + if (state->pending.M == 0) { + // NOTE. Here, we can also emit incoming right away. It will make the + // encoder 1.5x faster, at a cost of ~10% lower compression ratio: + // EMIT_MATCH(incoming); + // state->pending = NO_MATCH; + + // No pending match, emit nothing, keep incoming + state->pending = incoming; + } else { + // Here we have both incoming and pending + if (state->pending.m_end <= incoming.m_begin) { + // No overlap: emit pending, keep incoming + EMIT_MATCH(state->pending); + state->pending = incoming; + } else { + // If pending is better, emit pending and discard incoming. + // Otherwise, emit incoming and discard pending. + if (incoming.K > state->pending.K) + state->pending = incoming; + EMIT_MATCH(state->pending); + state->pending = NO_MATCH; + } + } + + after_emit: + + // We commit state changes only after we tried to emit instructions, so we + // can restart in the same state in case dst was full and we quit the loop. + state->table[h] = updated_e; + + } // i loop + + // Do not emit pending match here. We do it only at the end of stream. +} + +// =============================================================== +// API entry points + +size_t lzvn_encode_scratch_size(void) { return LZVN_ENCODE_WORK_SIZE; } + +static size_t lzvn_encode_partial(void *__restrict dst, size_t dst_size, + const void *__restrict src, size_t src_size, + size_t *src_used, void *__restrict work) { + // Min size checks to avoid accessing memory outside buffers. + if (dst_size < LZVN_ENCODE_MIN_DST_SIZE) { + *src_used = 0; + return 0; + } + // Max input size check (limit to offsets on uint32_t). + if (src_size > LZVN_ENCODE_MAX_SRC_SIZE) { + src_size = LZVN_ENCODE_MAX_SRC_SIZE; + } + + // Setup encoder state + lzvn_encoder_state state; + memset(&state, 0, sizeof(state)); + + state.src = src; + state.src_begin = 0; + state.src_end = (lzvn_offset)src_size; + state.src_literal = 0; + state.src_current = 0; + state.dst = dst; + state.dst_begin = dst; + state.dst_end = (unsigned char *)dst + dst_size - 8; // reserve 8 bytes for end-of-stream + state.table = work; + + // Do not encode if the input buffer is too small. We'll emit a literal instead. + if (src_size >= LZVN_ENCODE_MIN_SRC_SIZE) { + + state.src_current_end = (lzvn_offset)src_size - LZVN_ENCODE_MIN_MARGIN; + lzvn_init_table(&state); + lzvn_encode(&state); + + } + + // No need to test the return value: src_literal will not be updated on failure, + // and we will fail later. + lzvn_emit_literal(&state, state.src_end - state.src_literal); + + // Restore original size, so end-of-stream always succeeds, and emit it + state.dst_end = (unsigned char *)dst + dst_size; + lzvn_emit_end_of_stream(&state); + + *src_used = state.src_literal; + return (size_t)(state.dst - state.dst_begin); +} + +size_t lzvn_encode_buffer(void *__restrict dst, size_t dst_size, + const void *__restrict src, size_t src_size, + void *__restrict work) { + size_t src_used = 0; + size_t dst_used = + lzvn_encode_partial(dst, dst_size, src, src_size, &src_used, work); + if (src_used != src_size) + return 0; // could not encode entire input stream = fail + return dst_used; // return encoded size +} diff --git a/vendor/apfs-fuse/3rdparty/lzfse/src/lzvn_encode_base.h b/vendor/apfs-fuse/3rdparty/lzfse/src/lzvn_encode_base.h new file mode 100644 index 0000000..1b93d0e --- /dev/null +++ b/vendor/apfs-fuse/3rdparty/lzfse/src/lzvn_encode_base.h @@ -0,0 +1,116 @@ +/* +Copyright (c) 2015-2016, Apple Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +// LZVN low-level encoder + +#ifndef LZVN_ENCODE_BASE_H +#define LZVN_ENCODE_BASE_H + +#include "lzfse_internal.h" + +// =============================================================== +// Types and Constants + +#define LZVN_ENCODE_HASH_BITS \ + 14 // number of bits returned by the hash function [10, 16] +#define LZVN_ENCODE_OFFSETS_PER_HASH \ + 4 // stored offsets stack for each hash value, MUST be 4 +#define LZVN_ENCODE_HASH_VALUES \ + (1 << LZVN_ENCODE_HASH_BITS) // number of entries in hash table +#define LZVN_ENCODE_MAX_DISTANCE \ + 0xffff // max match distance we can represent with LZVN encoding, MUST be + // 0xFFFF +#define LZVN_ENCODE_MIN_MARGIN \ + 8 // min number of bytes required between current and end during encoding, + // MUST be >= 8 +#define LZVN_ENCODE_MAX_LITERAL_BACKLOG \ + 400 // if the number of pending literals exceeds this size, emit a long + // literal, MUST be >= 271 + +/*! @abstract Type of table entry. */ +typedef struct { + int32_t indices[4]; // signed indices in source buffer + uint32_t values[4]; // corresponding 32-bit values +} lzvn_encode_entry_type; + +// Work size +#define LZVN_ENCODE_WORK_SIZE \ + (LZVN_ENCODE_HASH_VALUES * sizeof(lzvn_encode_entry_type)) + +/*! @abstract Match */ +typedef struct { + lzvn_offset m_begin; // beginning of match, current position + lzvn_offset m_end; // end of match, this is where the next literal would begin + // if we emit the entire match + lzvn_offset M; // match length M: m_end - m_begin + lzvn_offset D; // match distance D + lzvn_offset K; // match gain: M - distance storage (L not included) +} lzvn_match_info; + +// =============================================================== +// Internal encoder state + +/*! @abstract Base encoder state and I/O. */ +typedef struct { + + // Encoder I/O + + // Source buffer + const unsigned char *src; + // Valid range in source buffer: we can access src[i] for src_begin <= i < + // src_end. src_begin may be negative. + lzvn_offset src_begin; + lzvn_offset src_end; + // Next byte to process in source buffer + lzvn_offset src_current; + // Next byte after the last byte to process in source buffer. We MUST have: + // src_current_end + 8 <= src_end. + lzvn_offset src_current_end; + // Next byte to encode in source buffer, may be before or after src_current. + lzvn_offset src_literal; + + // Next byte to write in destination buffer + unsigned char *dst; + // Valid range in destination buffer: [dst_begin, dst_end - 1] + unsigned char *dst_begin; + unsigned char *dst_end; + + // Encoder state + + // Pending match + lzvn_match_info pending; + + // Distance for last emitted match, or 0 + lzvn_offset d_prev; + + // Hash table used to find matches. Stores LZVN_ENCODE_OFFSETS_PER_HASH 32-bit + // signed indices in the source buffer, and the corresponding 4-byte values. + // The number of entries in the table is LZVN_ENCODE_HASH_VALUES. + lzvn_encode_entry_type *table; + +} lzvn_encoder_state; + +/*! @abstract Encode source to destination. + * Update \p state. + * The call ensures \c src_literal is never left too far behind \c src_current. */ +void lzvn_encode(lzvn_encoder_state *state); + +#endif // LZVN_ENCODE_BASE_H diff --git a/vendor/apfs-fuse/3rdparty/lzfse/tests/round-trip.cmake b/vendor/apfs-fuse/3rdparty/lzfse/tests/round-trip.cmake new file mode 100644 index 0000000..c376137 --- /dev/null +++ b/vendor/apfs-fuse/3rdparty/lzfse/tests/round-trip.cmake @@ -0,0 +1,53 @@ +get_filename_component(INPUT_NAME "${INPUT}" NAME) + +set(LZFSE_TMP_DIR "${CMAKE_CURRENT_BINARY_DIR}/test-tmp") +set(COMPRESSED_FILE "${LZFSE_TMP_DIR}/${INPUT_NAME}.lzfse") +set(DECOMPRESSED_FILE "${LZFSE_TMP_DIR}/${INPUT_NAME}") + +file(MAKE_DIRECTORY "${LZFSE_TMP_DIR}") + +if(EXISTS COMPRESSED_FILE) + message(FATAL_ERROR "${COMPRESSED_FILE} exists") + file(REMOVE ${COMPRESSED_FILE}) +endif() +if(EXISTS DECOMPRESSED_FILE) + message(FATAL_ERROR "${DECOMPRESSED_FILE} exists") + file(REMOVE ${DECOMPRESSED_FILE}) +endif() + +execute_process( + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/.." + COMMAND ${LZFSE_WRAPPER} ${LZFSE_CLI} -encode -i ${INPUT} -o ${COMPRESSED_FILE} + RESULT_VARIABLE result + ERROR_VARIABLE result_stderr) +if(result) + message(FATAL_ERROR "Compression failed: ${result_stderr}") +endif() + +execute_process( + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/.." + COMMAND ${LZFSE_WRAPPER} ${LZFSE_CLI} -decode -i ${COMPRESSED_FILE} -o ${DECOMPRESSED_FILE} + RESULT_VARIABLE result) +if(result) + message(FATAL_ERROR "Decompression failed") +endif() + +function(test_file_equality f1 f2) + if(NOT CMAKE_VERSION VERSION_LESS 2.8.7) + file(SHA512 "${f1}" f1_cs) + file(SHA512 "${f2}" f2_cs) + if(NOT "${f1_cs}" STREQUAL "${f2_cs}") + message(FATAL_ERROR "Files do not match") + endif() + else() + file(READ "${f1}" f1_contents) + file(READ "${f2}" f2_contents) + if(NOT "${f1_contents}" STREQUAL "${f2_contents}") + message(FATAL_ERROR "Files do not match") + endif() + endif() +endfunction() + +test_file_equality("${INPUT}" "${DECOMPRESSED_FILE}") +file(REMOVE ${COMPRESSED_FILE}) +file(REMOVE ${DECOMPRESSED_FILE}) diff --git a/vendor/apfs-fuse/ApfsDump/Apfs.cpp b/vendor/apfs-fuse/ApfsDump/Apfs.cpp new file mode 100644 index 0000000..9ffc6ca --- /dev/null +++ b/vendor/apfs-fuse/ApfsDump/Apfs.cpp @@ -0,0 +1,483 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "Dumper.h" + +#ifdef __linux__ +#include +#endif + +#undef RAW_VERBOSE + + +constexpr size_t BLOCKSIZE = 0x1000; + +volatile bool g_abort = 0; + +void DumpBlockTrunc(std::ostream &os, const uint8_t *data) +{ + unsigned int sz = BLOCKSIZE - 1; + + while (sz > 0 && data[sz] == 0) + sz--; + + sz = (sz + 0x10) & 0xFFFFFFF0; + + DumpHex(os, data, sz); +} + +#if 0 + +void MapBlocks(std::ostream &os, Device &dev, uint64_t bid_start, uint64_t bcnt) +{ + using namespace std; + + uint64_t bid; + uint8_t block[BLOCKSIZE]; + const APFS_ObjHeader * const blk = reinterpret_cast(block); + const APFS_TableHeader * const tbl = reinterpret_cast(block + sizeof(APFS_ObjHeader)); + bool last_was_used = false; + + os << hex << uppercase << setfill('0'); + + os << "[Block] | oid | xid | type | subtype | Page | Levl | Entries | Description" << endl; + os << "---------+----------+----------+----------+----------+------+------+----------+---------------------------------" << endl; + + for (bid = 0; bid < bcnt && !g_abort; bid++) + { + if ((bid & 0xFFF) == 0) + { + std::cout << '.'; + std::cout.flush(); + } + + dev.Read(block, (bid_start + bid) * BLOCKSIZE, BLOCKSIZE); + + if (IsEmptyBlock(block, BLOCKSIZE)) + { + if (last_was_used) + os << "---------+----------+----------+----------+----------+------+------+----------+ Empty" << endl; + last_was_used = false; + continue; + } + + if (VerifyBlock(block, BLOCKSIZE)) + { + os << setw(8) << bid << " | "; + os << setw(8) << blk->oid << " | "; + os << setw(8) << blk->xid << " | "; + os << setw(8) << blk->type << " | "; + os << setw(8) << blk->subtype << " | "; + os << setw(4) << tbl->page << " | "; + os << setw(4) << tbl->level << " | "; + os << setw(8) << tbl->entries_cnt << " | "; + os << BlockDumper::GetNodeType(blk->type, blk->subtype); + if (APFS_OBJ_TYPE(blk->type) == 2) + os << " [Root]"; + os << endl; + last_was_used = true; + } + else + { + os << setw(8) << bid; + os << " | | | | | | | | Data" << endl; + last_was_used = true; + } + } + + os << endl; +} + +void ScanBlocks(std::ostream &os, Device &dev, uint64_t bid_start, uint64_t bcnt) +{ + BlockDumper bd(os, BLOCKSIZE); + uint64_t bid; + uint8_t block[BLOCKSIZE]; + + bd.SetTextFlags(0x00); + + for (bid = 0; bid < bcnt && !g_abort; bid++) + { + if ((bid & 0xFFF) == 0) + { + std::cout << '.'; + std::cout.flush(); + } + + dev.Read(block, (bid_start + bid) * BLOCKSIZE, BLOCKSIZE); + + if (IsEmptyBlock(block, BLOCKSIZE)) + continue; + + if (VerifyBlock(block, BLOCKSIZE)) + bd.DumpNode(block, bid); + else + { +#if 0 + os << std::hex << std::setw(16) << blk_nr << std::endl; + DumpBlockTrunc(os, block); + os << std::endl; + os << "===========================================================================================================================" << std::endl; + os << std::endl; +#endif + } + } +} + +void DumpSpaceman(std::ostream &os, Device &dev, uint64_t bid_start, uint64_t bcnt) +{ + uint8_t sb[BLOCKSIZE]; + uint8_t data[BLOCKSIZE]; + BlockDumper bd(os, BLOCKSIZE); + const APFS_ObjHeader *bhdr; + const APFS_Spaceman *sm; + const APFS_NX_Superblock *nxsb; + uint64_t bid; + uint64_t cnt; + uint64_t smbmp_bid = 0; + uint64_t smbmp_cnt = 0; + uint64_t volbmp_bid = 0; + uint64_t volbmp_cnt = 0; + + (void)bcnt; + + if (!dev.Read(sb, bid_start * BLOCKSIZE, BLOCKSIZE)) + return; + + if (!VerifyBlock(sb, BLOCKSIZE)) + return; + + nxsb = reinterpret_cast(sb); + if (nxsb->hdr.type != 0x80000001) + return; + + if (nxsb->nx_magic != 0x4253584E) + return; + + bd.DumpNode(sb, 0); + + os << std::endl; + os << "Dumping Superblock Area" << std::endl; + os << std::endl; + + bid = nxsb->nx_xp_desc_base; + cnt = nxsb->nx_xp_desc_blocks; + + while (cnt > 0) + { + dev.Read(data, (bid_start + bid) * BLOCKSIZE, BLOCKSIZE); + bd.DumpNode(data, bid); + cnt--; + bid++; + } + + os << std::endl; + os << "Dumping Spaceman Area" << std::endl; + os << std::endl; + + bid = nxsb->nx_xp_data_base; + cnt = nxsb->nx_xp_data_blocks; + + while (cnt > 0) + { + dev.Read(data, (bid_start + bid) * BLOCKSIZE, BLOCKSIZE); + bd.DumpNode(data, bid); + cnt--; + bid++; + + bhdr = reinterpret_cast(data); + if (bhdr->type == 0x80000005) + { + sm = reinterpret_cast(data); + smbmp_bid = sm->ip_bm_base_address; + smbmp_cnt = sm->ip_bitmap_block_count; + volbmp_bid = sm->ip_base_address; + volbmp_cnt = sm->ip_block_count; + } + } + + bid = smbmp_bid; + cnt = smbmp_cnt; + + while (cnt > 0) + { + dev.Read(data, (bid_start + bid) * BLOCKSIZE, BLOCKSIZE); + bd.DumpNode(data, bid); + cnt--; + bid++; + } + + bid = volbmp_bid; + cnt = volbmp_cnt; + + while (cnt > 0) + { + dev.Read(data, (bid_start + bid) * BLOCKSIZE, BLOCKSIZE); + bd.DumpNode(data, bid); + cnt--; + bid++; + } +} + +#endif + +static void ctrl_c_handler(int sig) +{ + (void)sig; + g_abort = true; +} + +#if 0 + +int main(int argc, const char *argv[]) +{ + if (argc < 3) + { + std::cerr << "Syntax: apfs-dump file.img output.txt [map.txt]" << std::endl; + return 1; + } + + Device *dev; + std::ofstream os; + uint64_t bid_start = 0; + uint64_t bcnt = 0; + + g_debug = 255; + +#if defined(__linux__) || defined(__APPLE__) + signal(SIGINT, ctrl_c_handler); +#endif + + dev = Device::OpenDevice(argv[1]); + + if (!dev) + { + std::cerr << "Device " << argv[1] << " not found." << std::endl; + return 2; + } + + { + GptPartitionMap pmap; + if (pmap.LoadAndVerify(*dev)) + { + int partid = pmap.FindFirstAPFSPartition(); + if (partid >= 0) + { + std::cout << "Dumping EFI partition" << std::endl; + pmap.GetPartitionOffsetAndSize(partid, bid_start, bcnt); + bid_start /= BLOCKSIZE; + bcnt /= BLOCKSIZE; + } + } + } + + if (bcnt == 0) + bcnt = dev->GetSize() / BLOCKSIZE; + + if (argc > 3) + { + os.open(argv[3]); + if (!os.is_open()) + { + std::cerr << "Could not open output file " << argv[3] << std::endl; + dev->Close(); + delete dev; + return 3; + } + + MapBlocks(os, *dev, bid_start, bcnt); + os.close(); + } + + os.open(argv[2]); + if (!os.is_open()) + { + std::cerr << "Could not open output file " << argv[2] << std::endl; + dev->Close(); + delete dev; + return 3; + } + + DumpSpaceman(os, *dev, bid_start, bcnt); + ScanBlocks(os, *dev, bid_start, bcnt); + + dev->Close(); + os.close(); + + delete dev; + + return 0; +} + +#else + +void usage() +{ + std::cerr << "Syntax:" << std::endl; + std::cerr << "apfs-dump [-map mapfile.txt] file.img output.txt" << std::endl; + std::cerr << "apfs-dump [-map mapfile.txt] -fusion main.img tier2.img output.txt" << std::endl; +} + +int main(int argc, const char *argv[]) +{ + const char *name_dev_main = 0; + const char *name_dev_tier2 = 0; + const char *name_map = 0; + const char *name_output = 0; + bool use_fusion = false; + + int n; + int idx = 0; + + for (n = 1; n < argc; n++) + { + if (!strcmp(argv[n], "-map")) + { + n++; + if (n >= argc) + { + usage(); + return 1; + } + name_map = argv[n]; + } + + else if (!strcmp(argv[n], "-fusion")) + use_fusion = true; + + else + { + switch (idx) + { + case 0: + name_dev_main = argv[n]; + printf("main: %s\n", name_dev_main); + if (use_fusion) + idx++; + else + idx += 2; + break; + case 1: + name_dev_tier2 = argv[n]; + printf("tier2: %s\n", name_dev_tier2); + idx++; + break; + case 2: + name_output = argv[n]; + printf("out: %s\n", name_output); + idx++; + break; + default: + usage(); + return 1; + break; + } + } + } + + if ((name_output == 0) || (use_fusion && name_dev_tier2 == 0)) + { + usage(); + return 1; + } + + std::unique_ptr dev_main; + std::unique_ptr dev_tier2; + std::ofstream os; + + g_debug = 255; + +#if defined(__linux__) || defined(__APPLE__) + signal(SIGINT, ctrl_c_handler); +#endif + + dev_main.reset(Device::OpenDevice(name_dev_main)); + if (use_fusion) + dev_tier2.reset(Device::OpenDevice(name_dev_tier2)); + + if (!dev_main) + { + std::cerr << "Device " << name_dev_main << " not found." << std::endl; + return 2; + } + + if (use_fusion && !dev_tier2) + { + std::cerr << "Device " << name_dev_tier2 << " not found." << std::endl; + return 2; + } + + { + Dumper dmp(dev_main.get(), dev_tier2.get()); + + if (!dmp.Initialize()) + return -1; + +#if 1 + if (name_map) + { + os.open(name_map); + if (!os.is_open()) + { + std::cerr << "Could not open map file " << name_map << std::endl; + dev_main->Close(); + if (dev_tier2) + dev_tier2->Close(); + return 3; + } + + dmp.DumpBlockList(os); + os.close(); + } +#endif + + os.open(name_output); + if (!os.is_open()) + { + std::cerr << "Could not open output file " << name_output << std::endl; + dev_main->Close(); + if (dev_tier2) + dev_tier2->Close(); + return 3; + } + + dmp.DumpContainer(os); + } + + dev_main->Close(); + if (dev_tier2) + dev_tier2->Close(); + os.close(); + + return 0; +} + +#endif diff --git a/vendor/apfs-fuse/ApfsDump/Dumper.cpp b/vendor/apfs-fuse/ApfsDump/Dumper.cpp new file mode 100644 index 0000000..e894b14 --- /dev/null +++ b/vendor/apfs-fuse/ApfsDump/Dumper.cpp @@ -0,0 +1,476 @@ +#include +#include + +#include +#include +#include +#include +#include + +#include "Dumper.h" + +Dumper::Dumper(Device *dev_main, Device *dev_tier2) +{ + m_dev_main = dev_main; + m_dev_tier2 = dev_tier2; + + m_base_main = 0; + m_size_main = 0; + + m_base_tier2 = 0; + m_size_tier2 = 0; + + m_blocksize = 0; + m_is_encrypted = false; +} + +Dumper::~Dumper() +{ +} + +bool Dumper::Initialize() +{ + m_base_main = 0; + m_size_main = 0; + m_base_tier2 = 0; + m_size_tier2 = 0; + + m_blocksize = 0; + + GptPartitionMap pmap; + + if (pmap.LoadAndVerify(*m_dev_main)) + { + int partid = pmap.FindFirstAPFSPartition(); + + if (partid >= 0) + { + std::cout << "Dumping EFI partition on main" << std::endl; + pmap.GetPartitionOffsetAndSize(partid, m_base_main, m_size_main); + } + } + + if (m_size_main == 0) + m_size_main = m_dev_main->GetSize(); + if (m_size_main == 0) + return false; + + if (m_dev_tier2) + { + if (pmap.LoadAndVerify(*m_dev_tier2)) + { + int partid = pmap.FindFirstAPFSPartition(); + + if (partid >= 0) + { + std::cout << "Dumping EFI partition on tier2" << std::endl; + pmap.GetPartitionOffsetAndSize(partid, m_base_tier2, m_size_tier2); + } + } + + if (m_size_tier2 == 0) + m_size_tier2 = m_dev_tier2->GetSize(); + if (m_size_tier2 == 0) + return false; + } + + std::vector nx_data; + const nx_superblock_t *nx; + + nx_data.resize(0x1000); + m_dev_main->Read(nx_data.data(), m_base_main, 0x1000); + + nx = reinterpret_cast(nx_data.data()); + + if (nx->nx_magic != NX_MAGIC) + { + std::cerr << "Could not load NX superblock. Invalid magic number." << std::endl; + return false; + } + + m_blocksize = nx->nx_block_size; + + if (m_blocksize != 0x1000) + { + nx_data.resize(m_blocksize); + nx = reinterpret_cast(nx_data.data()); + m_dev_main->Read(nx_data.data(), m_base_main, m_blocksize); + } + + if (!VerifyBlock(nx_data.data(), m_blocksize)) + { + std::cerr << "NX superblock checksum incorrect!" << std::endl; + return false; + } + + return true; +} + +bool Dumper::DumpContainer(std::ostream &os) +{ + BlockDumper bd(os, m_blocksize); + + std::vector nx_data; + std::vector cpm_data; + std::vector sm_data; + std::vector cxb_data; + std::vector bmp_data; + std::vector blk_data; + + const nx_superblock_t *nx = nullptr; + const checkpoint_map_phys_t *cpm = nullptr; + const spaceman_phys_t *sm = nullptr; + const chunk_info_block_t *cib = nullptr; + const cib_addr_block_t *cab = nullptr; + const checkpoint_mapping_t *cm = nullptr; + + uint64_t paddr; + + uint64_t block_count; + uint32_t block_size; + uint32_t blocks_per_chunk; + uint32_t chunks_per_cib; + uint32_t cibs_per_cab; + uint32_t cib_count; + uint32_t cab_count; + const le_uint64_t *cxb_oid; + + uint32_t cab_id; + uint32_t cib_id; + uint32_t chunk_id; + uint32_t blk_id; + size_t n; + + int devidx; + uint64_t offs = 0; + + std::vector cib_oid_list; + + // Get NX superblock + + if (!Read(nx_data, 0, 1)) + return false; + + bd.DumpNode(nx_data.data(), 0); + + if (!VerifyBlock(nx_data.data(), nx_data.size())) + { + std::cerr << "Superblock checksum error" << std::endl; + return false; + } + + nx = reinterpret_cast(nx_data.data()); + + // TODO: Scan xp_desc for most recent nxsb + + // Get Check Point Info Block + + if (!Read(cpm_data, nx->nx_xp_desc_base + nx->nx_xp_desc_index, 1)) + { + std::cerr << "Error reading the cpm block" << std::endl; + return false; + } + + bd.DumpNode(cpm_data.data(), cpm_data.size()); + + if (!VerifyBlock(cpm_data.data(), cpm_data.size())) + return false; + + cpm = reinterpret_cast(cpm_data.data()); + + cm = cpm_lookup(cpm, nx->nx_spaceman_oid); + + if (cm == 0) + return false; + + // Get Spaceman + + if (!Read(sm_data, cm->cpm_paddr, cm->cpm_size / nx->nx_block_size)) + return false; + + bd.DumpNode(sm_data.data(), cm->cpm_paddr); + + if (!VerifyBlock(sm_data.data(), sm_data.size())) + return false; + + sm = reinterpret_cast(sm_data.data()); + + os << "Now dumping blocks according to bitmap ..." << std::endl; + + block_size = sm->sm_block_size; + blocks_per_chunk = sm->sm_blocks_per_chunk; + chunks_per_cib = sm->sm_chunks_per_cib; + cibs_per_cab = sm->sm_cibs_per_cab; + + for (devidx = SD_MAIN; devidx < SD_COUNT; devidx++) + { + std::cout << "Dumping device " << devidx << std::endl; + + if (devidx == SD_TIER2 && m_dev_tier2 == 0) + { + std::cout << "Aborting" << std::endl; + break; + } + + offs = (devidx == SD_TIER2) ? (FUSION_TIER2_DEVICE_BYTE_ADDR / m_blocksize) : 0; + + block_count = sm->sm_dev[devidx].sm_block_count; + cib_count = sm->sm_dev[devidx].sm_cib_count; + cab_count = sm->sm_dev[devidx].sm_cab_count; + cxb_oid = reinterpret_cast(sm_data.data() + sm->sm_dev[devidx].sm_addr_offset); + + paddr = 0; + + std::cout << std::hex; + + #if 0 + static const uint8_t vek[0x20] = { 0 /* enter VEK here */ }; + m_is_encrypted = true; + m_aes.SetKey(vek, vek + 0x10); + #endif + + cib_oid_list.clear(); + cib_oid_list.reserve(cib_count); + + if (cab_count > 0) + { + for (cab_id = 0; cab_id < cab_count; cab_id++) + { + if (!Read(cxb_data, cxb_oid[cab_id], 1)) + return false; + if (!VerifyBlock(cxb_data.data(), cxb_data.size())) + return false; + + cab = reinterpret_cast(cxb_data.data()); + + for (n = 0; n < cab->cab_cib_count; n++) + cib_oid_list.push_back(cab->cab_cib_addr[n]); + } + } + else + { + for (n = 0; n < cib_count; n++) + cib_oid_list.push_back(cxb_oid[n]); + } + + for (cib_id = 0; cib_id < cib_count; cib_id++) + { + std::cout << "cib " << cib_id << std::endl; + + if (!Read(cxb_data, cib_oid_list[cib_id], 1)) + return false; + if (!VerifyBlock(cxb_data.data(), cxb_data.size())) + return false; + + cib = reinterpret_cast(cxb_data.data()); + + for (chunk_id = 0; chunk_id < cib->cib_chunk_info_count; chunk_id++) + { + if (g_abort) + return false; + + std::cout << " chunk " << chunk_id << " avail=" << cib->cib_chunk_info[chunk_id].ci_free_count << " paddr=" << paddr << std::endl; + + if (cib->cib_chunk_info[chunk_id].ci_bitmap_addr == 0) + { + paddr += blocks_per_chunk; + continue; + } + + if (!Read(bmp_data, cib->cib_chunk_info[chunk_id].ci_bitmap_addr, 1)) + return false; + + for (blk_id = 0; blk_id < blocks_per_chunk && paddr < block_count; blk_id++) + { + if (bmp_data[blk_id >> 3] & (1 << (blk_id & 7))) + { + Read(blk_data, paddr + offs, 1); + if (VerifyBlock(blk_data.data(), m_blocksize)) + bd.DumpNode(blk_data.data(), paddr + offs); + else if (m_is_encrypted) + { + Decrypt(blk_data.data(), blk_data.size(), paddr + offs); + if (VerifyBlock(blk_data.data(), m_blocksize)) + bd.DumpNode(blk_data.data(), paddr + offs); + } + } + + ++paddr; + } + } + } + } + + + return true; +} + +bool Dumper::DumpBlockList(std::ostream& os) +{ + using namespace std; + + constexpr size_t BLOCKSIZE = 0x1000; // Should make this dynamic ... + + uint64_t bid; + uint8_t block[BLOCKSIZE]; + const obj_phys_t * const o = reinterpret_cast(block); + const btree_node_phys_t * const bt = reinterpret_cast(block); + bool last_was_used = false; + + os << hex << uppercase << setfill('0'); + + os << "[Block] | oid | xid | type | subtype | Page | Levl | Entries | Description" << endl; + os << "---------+----------+----------+----------+----------+------+------+----------+---------------------------------" << endl; + + for (bid = 0; bid < (m_size_main / BLOCKSIZE) && !g_abort; bid++) + { + if ((bid & 0xFFF) == 0) + { + std::cout << '.'; + std::cout.flush(); + } + + Read(block, bid, 1); + + if (IsEmptyBlock(block, BLOCKSIZE)) + { + if (last_was_used) + os << "---------+----------+----------+----------+----------+------+------+----------+ Empty" << endl; + last_was_used = false; + continue; + } + + if (VerifyBlock(block, BLOCKSIZE)) + { + os << setw(8) << bid << " | "; + os << setw(8) << o->o_oid << " | "; + os << setw(8) << o->o_xid << " | "; + os << setw(8) << o->o_type << " | "; + os << setw(8) << o->o_subtype << " | "; + os << setw(4) << bt->btn_flags << " | "; + os << setw(4) << bt->btn_level << " | "; + os << setw(8) << bt->btn_nkeys << " | "; + os << BlockDumper::GetNodeType(o->o_type, o->o_subtype); + if ((o->o_type & OBJECT_TYPE_MASK) == OBJECT_TYPE_BTREE) + os << " [Root]"; + os << endl; + last_was_used = true; + } + else + { + os << setw(8) << bid; + os << " | | | | | | | | Data" << endl; + last_was_used = true; + } + } + + if (m_dev_tier2) + { + const uint64_t off = FUSION_TIER2_DEVICE_BYTE_ADDR / m_blocksize; + + for (bid = 0; bid < (m_size_tier2 / BLOCKSIZE) && !g_abort; bid++) + { + if ((bid & 0xFFF) == 0) + { + std::cout << '.'; + std::cout.flush(); + } + + Read(block + off, bid, 1); + + if (IsEmptyBlock(block, BLOCKSIZE)) + { + if (last_was_used) + os << "---------+----------+----------+----------+----------+------+------+----------+ Empty" << endl; + last_was_used = false; + continue; + } + + if (VerifyBlock(block, BLOCKSIZE)) + { + os << setw(8) << bid << " | "; + os << setw(8) << o->o_oid << " | "; + os << setw(8) << o->o_xid << " | "; + os << setw(8) << o->o_type << " | "; + os << setw(8) << o->o_subtype << " | "; + os << setw(4) << bt->btn_flags << " | "; + os << setw(4) << bt->btn_level << " | "; + os << setw(8) << bt->btn_nkeys << " | "; + os << BlockDumper::GetNodeType(o->o_type, o->o_subtype); + if ((o->o_type & OBJECT_TYPE_MASK) == OBJECT_TYPE_BTREE) + os << " [Root]"; + os << endl; + last_was_used = true; + } + else + { + os << setw(8) << bid; + os << " | | | | | | | | Data" << endl; + last_was_used = true; + } + } + } + + os << endl; + + return true; +} + +bool Dumper::Read(void* data, uint64_t paddr, uint64_t cnt) +{ + uint64_t offs; + uint64_t size; + + offs = paddr * m_blocksize; + size = cnt * m_blocksize; + + if (offs & FUSION_TIER2_DEVICE_BYTE_ADDR) + { + offs = offs - FUSION_TIER2_DEVICE_BYTE_ADDR + m_base_tier2; + + return m_dev_tier2->Read(data, offs, size); + } + else + { + offs = offs + m_base_main; + + return m_dev_main->Read(data, offs, size); + } +} + +bool Dumper::Read(std::vector& data, uint64_t paddr, uint64_t cnt) +{ + if (data.size() != cnt * m_blocksize) + data.resize(cnt * m_blocksize); + + return Read(data.data(), paddr, cnt); +} + +void Dumper::Decrypt(uint8_t* data, size_t size, uint64_t paddr) +{ + uint64_t cs_factor = m_blocksize / 0x200; + uint64_t uno = paddr * cs_factor; + size_t k; + + for (k = 0; k < size; k += 0x200) + { + m_aes.Decrypt(data + k, data + k, 0x200, uno); + uno++; + } +} + +const checkpoint_mapping_t *Dumper::cpm_lookup(const checkpoint_map_phys_t* cpm, uint64_t oid) +{ + uint32_t k; + uint32_t cnt; + + cnt = cpm->cpm_count; + + for (k = 0; k < cnt; k++) + { + if (cpm->cpm_map[k].cpm_oid == oid) + return &cpm->cpm_map[k]; + } + + return nullptr; +} diff --git a/vendor/apfs-fuse/ApfsDump/Dumper.h b/vendor/apfs-fuse/ApfsDump/Dumper.h new file mode 100644 index 0000000..fc5c903 --- /dev/null +++ b/vendor/apfs-fuse/ApfsDump/Dumper.h @@ -0,0 +1,40 @@ +#pragma once + +#include +#include + +#include + +extern volatile bool g_abort; // In Apfs.cpp + +class Device; + +class Dumper +{ +public: + Dumper(Device *dev_main, Device *dev_tier2); + ~Dumper(); + + bool Initialize(); + bool DumpContainer(std::ostream &os); + bool DumpBlockList(std::ostream &os); + +private: + bool Read(void *data, uint64_t paddr, uint64_t cnt); + bool Read(std::vector &data, uint64_t paddr, uint64_t cnt); + + void Decrypt(uint8_t *data, size_t size, uint64_t paddr); + + const checkpoint_mapping_t* cpm_lookup(const checkpoint_map_phys_t *cpm, uint64_t oid); + + Device *m_dev_main; + Device *m_dev_tier2; + uint64_t m_base_main; + uint64_t m_size_main; + uint64_t m_base_tier2; + uint64_t m_size_tier2; + uint32_t m_blocksize; + + AesXts m_aes; + bool m_is_encrypted; +}; diff --git a/vendor/apfs-fuse/ApfsDumpQuick/ApfsDumpQuick.cpp b/vendor/apfs-fuse/ApfsDumpQuick/ApfsDumpQuick.cpp new file mode 100644 index 0000000..912809e --- /dev/null +++ b/vendor/apfs-fuse/ApfsDumpQuick/ApfsDumpQuick.cpp @@ -0,0 +1,202 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + bool rc; + int volume_id; + int n; + std::unique_ptr main_disk; + std::unique_ptr tier2_disk; + uint64_t main_offset; + uint64_t tier2_offset; + uint64_t main_size; + uint64_t tier2_size; + + const char *main_name; + const char *tier2_name; + const char *output_name; + + if (argc < 3) + { + std::cerr << "Syntax: apfs-dump-quick [-f fusion-device] " << std::endl; + return -1; + } + + main_name = argv[1]; + if (!strcmp(argv[2], "-f")) + { + if (argc < 5) + std::cerr << "Syntax: apfs-dump-quick [-f fusion-secondary-device] " << std::endl; + + tier2_name = argv[3]; + output_name = argv[4]; + } + else + { + tier2_name = nullptr; + output_name = argv[2]; + } + + g_debug = 255; + + main_disk.reset(Device::OpenDevice(main_name)); + if (tier2_name) + tier2_disk.reset(Device::OpenDevice(tier2_name)); + + if (!main_disk) + { + std::cerr << "Unable to open device " << main_name << std::endl; + return -1; + } + + if (tier2_name && !tier2_disk) + { + std::cerr << "Unable to open secondary device " << tier2_name << std::endl; + return -1; + } + + std::ofstream st; + st.open(output_name); + + if (!st.is_open()) + { + std::cerr << "Unable to open output file " << output_name << std::endl; + main_disk->Close(); + return -1; + } + + main_offset = 0; + main_size = main_disk->GetSize(); + + tier2_offset = 0; + if (tier2_disk) + tier2_size = tier2_disk->GetSize(); + else + tier2_size = 0; + + GptPartitionMap gpt; + if (gpt.LoadAndVerify(*main_disk.get())) + { + std::cout << "Info: Found valid GPT partition table on main device. Dumping first APFS partition." << std::endl; + + n = gpt.FindFirstAPFSPartition(); + if (n != -1) + gpt.GetPartitionOffsetAndSize(n, main_offset, main_size); + } + + if (tier2_disk && gpt.LoadAndVerify(*tier2_disk.get())) + { + std::cout << "Info: Found valid GPT partition table on tier2 device. Dumping first APFS partition." << std::endl; + + n = gpt.FindFirstAPFSPartition(); + if (n != -1) + gpt.GetPartitionOffsetAndSize(n, tier2_offset, tier2_size); + } + + std::unique_ptr container(new ApfsContainer(main_disk.get(), main_offset, main_size, tier2_disk.get(), tier2_offset, tier2_size)); + + rc = container->Init(); + + if (!rc) + { + std::cerr << "Unable to init container." << std::endl; + container.reset(); + main_disk->Close(); + return -1; + } + +#if 1 + BlockDumper bd(st, container->GetBlocksize()); + + container->dump(bd); + +#if 1 + for (volume_id = 0; volume_id < NX_MAX_FILE_SYSTEMS; volume_id++) + { + ApfsVolume *vol; + + vol = container->GetVolume(volume_id); + + if (vol) + { + std::cout << "Volume " << volume_id << ": " << vol->name() << std::endl; + + vol->dump(bd); + + delete vol; + } + } +#endif +#endif + +#if 0 + ApfsVolume *vol; + + vol = container->GetVolume(0); + + if (vol) + { + ApfsDir dir(*vol); + ApfsDir::Inode ino; + std::vector root_list; + +#if 0 + dir.GetInode(ino, 0x15); + + std::cout << "Directory is called " << ino.name << std::endl; + + dir.ListDirectory(root_list, 0x15); + + for (auto it = root_list.cbegin(); it != root_list.cend(); ++it) + { + std::cout << it->name << " : " << it->inode_id << std::endl; + } +#endif + +#if 1 + std::vector buf; + buf.resize(0x29000); + + dir.ReadFile(buf.data(), 0x39, 0, 0x29000); +#endif + } + +#endif + + container.reset(); + + st.close(); + + main_disk->Close(); + + return 0; +} diff --git a/vendor/apfs-fuse/ApfsLib/ApfsContainer.cpp b/vendor/apfs-fuse/ApfsLib/ApfsContainer.cpp new file mode 100644 index 0000000..61103d5 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/ApfsContainer.cpp @@ -0,0 +1,482 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#include +#include +#include + +#include "ApfsContainer.h" +#include "ApfsVolume.h" +#include "Util.h" +#include "BlockDumper.h" +#include "Global.h" + +int g_debug = 0; +bool g_lax = false; + +ApfsContainer::ApfsContainer(Device *disk_main, uint64_t main_start, uint64_t main_len, Device *disk_tier2, uint64_t tier2_start, uint64_t tier2_len) : + m_main_disk(disk_main), + m_main_part_start(main_start), + m_main_part_len(main_len), + m_tier2_disk(disk_tier2), + m_tier2_part_start(tier2_start), + m_tier2_part_len(tier2_len), + m_cpm(*this), + m_omap(*this), + // m_omap_tree(*this), + m_fq_tree_mgr(*this), + m_fq_tree_vol(*this), + m_keymgr(*this) +{ + m_sm = nullptr; +} + +ApfsContainer::~ApfsContainer() +{ +} + +bool ApfsContainer::Init(xid_t req_xid) +{ + std::vector blk; + + blk.resize(0x1000); + + if (!m_main_disk->Read(blk.data(), m_main_part_start, 0x1000)) + { + std::cerr << "Reading block 0 from main device failed." << std::endl; + return false; + } + + memcpy(&m_nx, blk.data(), sizeof(nx_superblock_t)); + + if (m_nx.nx_magic != NX_MAGIC) + { + std::cerr << "This doesn't seem to be an apfs volume (invalid superblock)." << std::endl; + return false; + } + + if (m_nx.nx_block_size != 0x1000) + { + blk.resize(m_nx.nx_block_size); + m_main_disk->Read(blk.data(), m_main_part_start, blk.size()); + } + + if (!VerifyBlock(blk.data(), blk.size())) + return false; + + memcpy(&m_nx, blk.data(), sizeof(nx_superblock_t)); + + // Scan container for most recent superblock (might fix segfaults) + uint64_t max_xid = 0; + paddr_t max_paddr = 0; + paddr_t paddr; + std::vector tmp; + + tmp.resize(m_nx.nx_block_size); + + for (paddr = m_nx.nx_xp_desc_base; paddr < (m_nx.nx_xp_desc_base + m_nx.nx_xp_desc_blocks); paddr++) + { + if (!ReadBlocks(tmp.data(), paddr, 1)) + return false; + + if (!VerifyBlock(tmp.data(), tmp.size())) + continue; + + const nx_superblock_t *sb = reinterpret_cast(tmp.data()); + if ((sb->nx_o.o_type & OBJECT_TYPE_MASK) != OBJECT_TYPE_NX_SUPERBLOCK) + continue; + + if (req_xid) { + if (req_xid == sb->nx_o.o_xid) { + max_xid = req_xid; + max_paddr = paddr; + break; + } + } else { + if (sb->nx_o.o_xid > max_xid) + { + max_xid = sb->nx_o.o_xid; + max_paddr = paddr; + } + } + } + + if (max_paddr) + { + // if (g_debug & Dbg_Errors) + // std::cout << "Found more recent xid " << max_xid << " than superblock 0 contained (" << m_nx.nx_o.o_xid << ")." << std::endl; + if (g_debug & Dbg_Info) + std::cout << "Mounting xid different from NXSB at 0 (xid = " << m_nx.nx_o.o_xid << "). xid = " << max_xid << std::endl; + + ReadBlocks(tmp.data(), max_paddr, 1); + + memcpy(&m_nx, tmp.data(), sizeof(nx_superblock_t)); + } + + if (g_debug & Dbg_Info) + std::cout << "Mounting xid " << m_nx.nx_o.o_xid << std::endl; + + if ((m_nx.nx_incompatible_features & NX_INCOMPAT_FUSION) && !m_tier2_disk) + { + std::cerr << "Need to specify two devices for a fusion drive." << std::endl; + return false; + } + + if (!m_cpm.Init(m_nx.nx_xp_desc_base + m_nx.nx_xp_desc_index, m_nx.nx_xp_desc_len - 1)) + { + std::cerr << "Failed to load checkpoint map" << std::endl; + return false; + } + + if (!m_omap.Init(m_nx.nx_omap_oid, m_nx.nx_o.o_xid)) + { + std::cerr << "Failed to load nx omap" << std::endl; + return false; + } + + omap_res_t omr; + if (!m_cpm.Lookup(omr, m_nx.nx_spaceman_oid, m_nx.nx_o.o_xid)) + { + std::cerr << "Failed to map spaceman oid" << std::endl; + return false; + } + + m_sm_data.resize(omr.size); + ReadBlocks(m_sm_data.data(), omr.paddr, omr.size / GetBlocksize()); + m_sm = reinterpret_cast(m_sm_data.data()); + + if (!VerifyBlock(m_sm_data.data(), m_sm_data.size())) { + std::cerr << "Checksum error in spaceman" << std::endl; + return false; + } + + if ((m_sm->sm_o.o_type & OBJECT_TYPE_MASK) != OBJECT_TYPE_SPACEMAN) + { + std::cerr << "Spaceman has wrong type " << m_sm->sm_o.o_type << std::endl; + return false; + } + + if (m_sm->sm_fq[SFQ_IP].sfq_tree_oid != 0) + m_fq_tree_mgr.Init(m_sm->sm_fq[SFQ_IP].sfq_tree_oid, m_sm->sm_o.o_xid, &m_cpm); + + if (m_sm->sm_fq[SFQ_MAIN].sfq_tree_oid != 0) + m_fq_tree_vol.Init(m_sm->sm_fq[SFQ_MAIN].sfq_tree_oid, m_sm->sm_o.o_xid, &m_cpm); + + // m_omap_tree.Init(m_nx.nx_omap_oid, m_nx.hdr.o_xid, nullptr); + + // m_sb.nx_spaceman_oid + + if ((m_nx.nx_keylocker.pr_start_addr != 0) && (m_nx.nx_keylocker.pr_block_count != 0)) + { + if (!m_keymgr.Init(m_nx.nx_keylocker.pr_start_addr, m_nx.nx_keylocker.pr_block_count, m_nx.nx_uuid)) + { + std::cerr << "Initialization of KeyManager failed." << std::endl; + return false; + } + } + + return true; +} + +ApfsVolume *ApfsContainer::GetVolume(unsigned int fsid, const std::string &passphrase, xid_t snap_xid) +{ + ApfsVolume *vol = nullptr; + oid_t oid; + omap_res_t omr; + bool rc; + + if (fsid >= 100) + return nullptr; + + m_passphrase = passphrase; + + oid = m_nx.nx_fs_oid[fsid]; + + if (oid == 0) + return nullptr; + + if (!m_omap.Lookup(omr, oid, m_nx.nx_o.o_xid)) + return nullptr; + + // std::cout << std::hex << "Loading Volume " << index << ", nodeid = " << nodeid << ", version = " << m_sb.hdr.version << ", blkid = " << blkid << std::endl; + + if (omr.paddr == 0) + return nullptr; + + vol = new ApfsVolume(*this); + if (snap_xid != 0) + rc = vol->MountSnapshot(omr.paddr, snap_xid); + else + rc = vol->Init(omr.paddr); + + if (rc == false) + { + delete vol; + vol = nullptr; + } + + return vol; +} + +bool ApfsContainer::GetVolumeInfo(unsigned int fsid, apfs_superblock_t& apsb) +{ + oid_t oid; + omap_res_t omr; + std::vector apsb_raw; + + if (fsid >= NX_MAX_FILE_SYSTEMS) + return false; + + oid = m_nx.nx_fs_oid[fsid]; + + if (oid == 0) + return false; + + if (!m_omap.Lookup(omr, oid, m_nx.nx_o.o_xid)) + return false; + + if (omr.paddr == 0) + return false; + + apsb_raw.resize(GetBlocksize()); + + if (!ReadAndVerifyHeaderBlock(apsb_raw.data(), omr.paddr)) + return false; + + memcpy(&apsb, apsb_raw.data(), sizeof(apfs_superblock_t)); + + return true; +} + + +bool ApfsContainer::ReadBlocks(uint8_t * data, paddr_t paddr, uint64_t blkcnt) const +{ + uint64_t offs; + uint64_t size; + + //if ((paddr + blkcnt) > m_nx.nx_block_count) + // return false; + + offs = m_nx.nx_block_size * paddr; + size = m_nx.nx_block_size * blkcnt; + + if (offs & FUSION_TIER2_DEVICE_BYTE_ADDR) + { + if (!m_tier2_disk) + return false; + + offs = offs - FUSION_TIER2_DEVICE_BYTE_ADDR + m_tier2_part_start; + return m_tier2_disk->Read(data, offs, size); + } + else + { + if (!m_main_disk) + return false; + + offs = offs + m_main_part_start; + return m_main_disk->Read(data, offs, size); + } +} + +bool ApfsContainer::ReadAndVerifyHeaderBlock(uint8_t * data, paddr_t paddr) const +{ + if (!ReadBlocks(data, paddr)) + return false; + + if (!VerifyBlock(data, m_nx.nx_block_size)) { + if (g_debug & Dbg_Errors) { + std::cerr << "ReadAndVerifyHeaderBlock checksum error." << std::endl; + DumpHex(std::cerr, data, m_nx.nx_block_size); + } + return false; + } + + return true; +} + +bool ApfsContainer::GetVolumeKey(uint8_t *key, const apfs_uuid_t & vol_uuid, const char *password) +{ + if (!m_keymgr.IsValid()) + return false; + + if (password) + { + return m_keymgr.GetVolumeKey(key, vol_uuid, password); + } + else + { + if (m_passphrase.empty()) + return false; + + return m_keymgr.GetVolumeKey(key, vol_uuid, m_passphrase.c_str()); + } +} + +bool ApfsContainer::GetPasswordHint(std::string & hint, const apfs_uuid_t & vol_uuid) +{ + return m_keymgr.GetPasswordHint(hint, vol_uuid); +} + +void ApfsContainer::dump(BlockDumper& bd) +{ + std::vector blk; + paddr_t paddr; + uint32_t index; + uint32_t last_index; + + bd.st() << "Dumping Container" << std::endl; + bd.st() << "-----------------" << std::endl; + bd.st() << std::endl; + bd.st() << std::endl; + + blk.resize(GetBlocksize()); + ReadAndVerifyHeaderBlock(blk.data(), 0); + + bd.DumpNode(blk.data(), 0); + +#if 1 + if (m_keymgr.IsValid()) + m_keymgr.dump(bd.st()); +#endif + + /* + if (m_keybag.size()) + bd.DumpNode(m_keybag.data(), m_sb.keybag_blk_start); + */ + +#if 0 + for (paddr = m_nx.nx_xp_desc_base; paddr < (m_nx.nx_xp_desc_base + m_nx.nx_xp_desc_blocks); paddr++) + { + ReadAndVerifyHeaderBlock(blk.data(), paddr); + bd.DumpNode(blk.data(), paddr); + } +#endif +#if 0 + for (paddr = m_nx.nx_xp_data_base; paddr < (m_nx.nx_xp_data_base + m_nx.nx_xp_data_blocks); paddr++) + { + ReadAndVerifyHeaderBlock(blk.data(), paddr); + bd.DumpNode(blk.data(), paddr); + } +#endif + +#if 1 + bd.st() << std::endl << "Dumping XP desc area (current SB):" << std::endl; + paddr = m_nx.nx_xp_desc_base; + last_index = m_nx.nx_xp_desc_index + m_nx.nx_xp_desc_len; + if (last_index >= m_nx.nx_xp_desc_blocks) + last_index -= m_nx.nx_xp_desc_blocks; + + for (index = m_nx.nx_xp_desc_index; index != last_index;) + { + ReadAndVerifyHeaderBlock(blk.data(), paddr + index); + bd.DumpNode(blk.data(), paddr + index); + index++; + if (index >= m_nx.nx_xp_desc_blocks) + index -= m_nx.nx_xp_desc_blocks; + } + + bd.st() << std::endl << "Dumping XP data area (current SB):" << std::endl; + paddr = m_nx.nx_xp_data_base; + last_index = m_nx.nx_xp_data_index + m_nx.nx_xp_data_len; + if (last_index >= m_nx.nx_xp_data_blocks) + last_index -= m_nx.nx_xp_data_blocks; + + for (index = m_nx.nx_xp_data_index; index != last_index;) + { + ReadAndVerifyHeaderBlock(blk.data(), paddr + index); + bd.DumpNode(blk.data(), paddr + index); + index++; + if (index >= m_nx.nx_xp_data_blocks) + index -= m_nx.nx_xp_data_blocks; + } +#endif + + if (m_nx.nx_efi_jumpstart) + { + ReadAndVerifyHeaderBlock(blk.data(), m_nx.nx_efi_jumpstart); + bd.DumpNode(blk.data(), m_nx.nx_efi_jumpstart); + } + + ReadAndVerifyHeaderBlock(blk.data(), m_nx.nx_omap_oid); + bd.DumpNode(blk.data(), m_nx.nx_omap_oid); + + { + size_t bs = bd.GetBlockSize(); + bd.SetBlockSize(m_sm_data.size()); + bd.DumpNode(m_sm_data.data(), m_nx.nx_spaceman_oid); + bd.SetBlockSize(bs); + } + + uint64_t oid; + size_t k; + + for (k = 0; k < m_sm->sm_ip_bm_block_count; k++) + { + oid = m_sm->sm_ip_bm_base + k; + + bd.st() << "Dumping IP Bitmap block " << k << std::endl; + + ReadBlocks(blk.data(), oid); + bd.DumpNode(blk.data(), oid); + + bd.st() << std::endl; + } + + m_omap.dump(bd); + // m_omap_tree.dump(bd); + m_fq_tree_mgr.dump(bd); + m_fq_tree_vol.dump(bd); + + const le_uint64_t *cxb_oid = reinterpret_cast(m_sm_data.data() + m_sm->sm_dev[SD_MAIN].sm_addr_offset); + uint32_t cib_cnt = m_sm->sm_dev[SD_MAIN].sm_cib_count; + uint32_t cab_cnt = m_sm->sm_dev[SD_MAIN].sm_cab_count; + + uint32_t cib_id; + uint32_t cab_id; + + std::vector cib_oid_list; + std::vector cib_data(GetBlocksize()); + + cib_oid_list.reserve(cib_cnt); + + if (cab_cnt != 0) + { + for (cab_id = 0; cab_id < cab_cnt; cab_id++) + { + ReadAndVerifyHeaderBlock(blk.data(), cxb_oid[cab_id]); + bd.DumpNode(blk.data(), cxb_oid[cab_id]); + + const cib_addr_block_t *cab = reinterpret_cast(blk.data()); + + for (cib_id = 0; cib_id < cab->cab_cib_count; cib_id++) + cib_oid_list.push_back(cab->cab_cib_addr[cib_id]); + } + } + else + { + for (cib_id = 0; cib_id < cib_cnt; cib_id++) + cib_oid_list.push_back(cxb_oid[cib_id]); + } + + for (cib_id = 0; cib_id < cib_cnt; cib_id++) + { + ReadAndVerifyHeaderBlock(blk.data(), cib_oid_list[cib_id]); + bd.DumpNode(blk.data(), cib_oid_list[cib_id]); + } +} diff --git a/vendor/apfs-fuse/ApfsLib/ApfsContainer.h b/vendor/apfs-fuse/ApfsLib/ApfsContainer.h new file mode 100644 index 0000000..49f12b8 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/ApfsContainer.h @@ -0,0 +1,85 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#pragma once + +#include "Global.h" +#include "BTree.h" +#include "DiskStruct.h" +#include "Device.h" +#include "CheckPointMap.h" +#include "ApfsNodeMapperBTree.h" +#include "KeyMgmt.h" + +#include +#include + +class ApfsVolume; +class BlockDumper; + +class ApfsContainer +{ +public: + ApfsContainer(Device *disk_main, uint64_t main_start, uint64_t main_len, Device *disk_tier2 = 0, uint64_t tier2_start = 0, uint64_t tier2_len = 0); + ~ApfsContainer(); + + bool Init(xid_t req_xid = 0); + + ApfsVolume *GetVolume(unsigned int fsid, const std::string &passphrase = std::string(), xid_t snap_xid = 0); + bool GetVolumeInfo(unsigned int fsid, apfs_superblock_t &apsb); + + bool ReadBlocks(uint8_t *data, paddr_t paddr, uint64_t blkcnt = 1) const; + bool ReadAndVerifyHeaderBlock(uint8_t *data, paddr_t paddr) const; + + uint32_t GetBlocksize() const { return m_nx.nx_block_size; } + uint64_t GetBlockCount() const { return m_nx.nx_block_count; } + uint64_t GetFreeBlocks() const { return m_sm->sm_dev[SD_MAIN].sm_free_count + m_sm->sm_dev[SD_TIER2].sm_free_count; } + + bool GetVolumeKey(uint8_t *key, const apfs_uuid_t &vol_uuid, const char *password = nullptr); + bool GetPasswordHint(std::string &hint, const apfs_uuid_t &vol_uuid); + bool IsUnencrypted() const { return m_keymgr.IsUnencrypted(); } + + void dump(BlockDumper& bd); + +private: + Device *m_main_disk; + const uint64_t m_main_part_start; + const uint64_t m_main_part_len; + + Device *m_tier2_disk; + const uint64_t m_tier2_part_start; + const uint64_t m_tier2_part_len; + + std::string m_passphrase; + + nx_superblock_t m_nx; + + CheckPointMap m_cpm; + ApfsNodeMapperBTree m_omap; + + std::vector m_sm_data; + const spaceman_phys_t *m_sm; + // Block_8_11 -> omap + + // BTree m_omap_tree; // see ApfsNodeMapperBTree + BTree m_fq_tree_mgr; + BTree m_fq_tree_vol; + + KeyManager m_keymgr; +}; diff --git a/vendor/apfs-fuse/ApfsLib/ApfsDir.cpp b/vendor/apfs-fuse/ApfsLib/ApfsDir.cpp new file mode 100644 index 0000000..3349ce5 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/ApfsDir.cpp @@ -0,0 +1,847 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#include +#include +#include + +#include +#include + +#include "ApfsDir.h" +#include "ApfsVolume.h" +#include "ApfsContainer.h" +#include "BTree.h" +#include "Util.h" + +#ifndef _MSC_VER +template +void strcpy_s(char (&dst)[L], const char *src) +{ + strncpy(dst, src, L - 1); + dst[L - 1] = 0; +} +#endif + +ApfsDir::Inode::Inode() +{ + obj_id = 0; + + parent_id = 0; + private_id = 0; + + create_time = 0; + mod_time = 0; + change_time = 0; + access_time = 0; + + internal_flags = 0; + + nchildren_nlink = 0; + + default_protection_class = 0; + write_generation_counter = 0; + bsd_flags = 0; + owner = 0; + group = 0; + mode = 0; + + uncompressed_size = 0; + + snap_xid = 0; + delta_tree_oid = 0; + prev_fsize = 0; + + ds_size = 0; + ds_alloced_size = 0; + ds_default_crypto_id = 0; + ds_total_bytes_written = 0; + ds_total_bytes_read = 0; + + memset(fs_uuid, 0, sizeof(fs_uuid)); + + sparse_bytes = 0; + document_id = 0; + rdev = 0; + + optional_present_flags = 0; + + /* + ino = nullptr; + */ +} + +ApfsDir::Inode::Inode(const ApfsDir::Inode& o) +{ + obj_id = o.obj_id; + + parent_id = o.parent_id; + private_id = o.private_id; + + create_time = o.create_time; + mod_time = o.mod_time; + change_time = o.change_time; + access_time = o.access_time; + + internal_flags = o.internal_flags; + + nchildren_nlink = o.nchildren_nlink; + + default_protection_class = o.default_protection_class; + write_generation_counter = o.write_generation_counter; + bsd_flags = o.bsd_flags; + owner = o.owner; + group = o.group; + mode = o.mode; + + uncompressed_size = o.uncompressed_size; + + snap_xid = o.snap_xid; + delta_tree_oid = o.delta_tree_oid; + prev_fsize = o.prev_fsize; + // FinderInfo + ds_size = o.ds_size; + ds_alloced_size = o.ds_alloced_size; + ds_default_crypto_id = o.ds_default_crypto_id; + ds_total_bytes_written = o.ds_total_bytes_written; + ds_total_bytes_read = o.ds_total_bytes_read; + // j_dir_stats_val_t dir_stats; + memcpy(fs_uuid, o.fs_uuid, sizeof(apfs_uuid_t)); + sparse_bytes = o.sparse_bytes; + document_id = o.document_id;; + rdev = o.rdev; + name = o.name; + + optional_present_flags = o.optional_present_flags; + + /* + ino_val_data = o.ino_val_data; + ino = reinterpret_cast(ino_val_data.data()); + */ +} + +ApfsDir::DirRec::DirRec() +{ + parent_id = 0; + hash = 0; + + file_id = 0; + date_added = 0; + + sibling_id = 0; + flags = 0; + has_sibling_id = false; +} + +ApfsDir::DirRec::DirRec(const ApfsDir::DirRec& o) +{ + parent_id = o.parent_id; + hash = o.hash; + name = o.name; + + file_id = o.file_id; + date_added = o.date_added; + + sibling_id = o.sibling_id; + flags = o.flags; + has_sibling_id = o.has_sibling_id; +} + +ApfsDir::ApfsDir(ApfsVolume &vol) : + m_vol(vol), + m_fs_tree(vol.fstree()) +{ + m_txt_fmt = vol.getTextFormat(); + + m_blksize = vol.getContainer().GetBlocksize(); + m_blksize_sh = log2(m_blksize); + m_blksize_mask_lo = m_blksize - 1; + m_blksize_mask_hi = ~m_blksize_mask_lo; + m_tmp_blk.resize(m_blksize); + // m_bt.EnableDebugOutput(); +} + +ApfsDir::~ApfsDir() +{ +} + +bool ApfsDir::GetInode(ApfsDir::Inode& res, uint64_t inode) +{ + BTreeEntry bte; + j_inode_key_t key; + bool rc; + + key.hdr.obj_id_and_type = APFS_TYPE_ID(APFS_TYPE_INODE, inode); + + rc = m_fs_tree.Lookup(bte, &key, sizeof(j_inode_key_t), CompareStdDirKey, this, true); + + if (!rc || (bte.val == nullptr)) + return false; + + // const uint8_t *idata = reinterpret_cast(bte.val); + const j_inode_val_t *obj = reinterpret_cast(bte.val); + + res.obj_id = inode; + + res.parent_id = obj->parent_id; + res.private_id = obj->private_id; + + res.create_time = obj->create_time; + res.mod_time = obj->mod_time; + res.change_time = obj->change_time; + res.access_time = obj->access_time; + + res.internal_flags = obj->internal_flags; + + res.nchildren_nlink = obj->nchildren; + + res.default_protection_class = obj->default_protection_class; + res.write_generation_counter = obj->write_generation_counter; + res.bsd_flags = obj->bsd_flags; + res.owner = obj->owner; + res.group = obj->group; + res.mode = obj->mode; + res.uncompressed_size = obj->uncompressed_size; + + // internal_flags & 0x00200000 => pad2 = uncompressed_size ? + + if (bte.val_len > sizeof(j_inode_val_t)) + { + const xf_blob_t *xf_hdr = reinterpret_cast(obj->xfields); + const x_field_t *xf = reinterpret_cast(xf_hdr->xf_data); + const uint8_t *xdata = obj->xfields + sizeof(xf_blob_t) + xf_hdr->xf_num_exts * sizeof(x_field_t); + uint16_t n; + + for (n = 0; n < xf_hdr->xf_num_exts; n++) + { + switch (xf[n].x_type) + { + case INO_EXT_TYPE_SNAP_XID: + assert(xf[n].x_size == sizeof(uint64_t)); + res.snap_xid = bswap_le(*reinterpret_cast(xdata)); + res.optional_present_flags |= Inode::INO_HAS_SNAP_XID; + break; + case INO_EXT_TYPE_DELTRA_TREE_OID: + assert(xf[n].x_size == sizeof(uint64_t)); + res.delta_tree_oid = bswap_le(*reinterpret_cast(xdata)); + res.optional_present_flags |= Inode::INO_HAS_DELTA_TREE_OID; + break; + case INO_EXT_TYPE_DOCUMENT_ID: + assert(xf[n].x_size == sizeof(uint32_t)); + res.document_id = bswap_le(*reinterpret_cast(xdata)); + res.optional_present_flags |= Inode::INO_HAS_DOCUMENT_ID; + break; + case INO_EXT_TYPE_NAME: + res.name.assign(reinterpret_cast(xdata), xf[n].x_size); + res.optional_present_flags |= Inode::INO_HAS_NAME; + break; + case INO_EXT_TYPE_PREV_FSIZE: + assert(xf[n].x_size == sizeof(uint64_t)); + res.prev_fsize = bswap_le(*reinterpret_cast(xdata)); + res.optional_present_flags |= Inode::INO_HAS_PREV_FSIZE; + break; + case INO_EXT_TYPE_FINDER_INFO: + // TODO + break; + case INO_EXT_TYPE_DSTREAM: + { + assert(xf[n].x_size == sizeof(j_dstream_t)); + const j_dstream_t *ds = reinterpret_cast(xdata); + res.ds_size = ds->size; + res.ds_alloced_size = ds->alloced_size; + res.ds_default_crypto_id = ds->default_crypto_id; + res.ds_total_bytes_written = ds->total_bytes_written; + res.ds_total_bytes_read = ds->total_bytes_read; + res.optional_present_flags |= Inode::INO_HAS_DSTREAM; + } + break; + case INO_EXT_TYPE_DIR_STATS_KEY: + // TODO + break; + case INO_EXT_TYPE_FS_UUID: + assert(xf[n].x_size == sizeof(apfs_uuid_t)); + memcpy(res.fs_uuid, xdata, sizeof(apfs_uuid_t)); + res.optional_present_flags |= Inode::INO_HAS_FS_UUID; + break; + case INO_EXT_TYPE_SPARSE_BYTES: + assert(xf[n].x_size == sizeof(uint64_t)); + res.sparse_bytes = bswap_le(*reinterpret_cast(xdata)); + res.optional_present_flags |= Inode::INO_HAS_SPARSE_BYTES; + break; + default: + std::cerr << "Warning: Unknown XF " << xf[n].x_type << " at inode " << inode << std::endl; + break; + } + + xdata += ((xf[n].x_size + 7) & ~7); + } + } + + return true; +} + +bool ApfsDir::ListDirectory(std::vector &dir, uint64_t inode) +{ + uint8_t skey_buf[0x500]; + + BTreeIterator it; + BTreeEntry bte; + bool rc; + uint64_t skey; + + const j_key_t *k; + const j_drec_val_t *v; + + skey = APFS_TYPE_ID(APFS_TYPE_DIR_REC, inode); + + dir.clear(); + + if (m_txt_fmt & 9) + { + j_drec_hashed_key_t *key = reinterpret_cast(skey_buf); + key->hdr.obj_id_and_type = skey; + key->name_len_and_hash = 0; + key->name[0] = 0; + + rc = m_fs_tree.GetIterator(it, key, sizeof(j_drec_hashed_key_t), CompareStdDirKey, this); + } + else + { + j_drec_key_t *key = reinterpret_cast(skey_buf); + key->hdr.obj_id_and_type = skey; + key->name_len = 0; + key->name[0] = 0; + + rc = m_fs_tree.GetIterator(it, key, sizeof(j_drec_key_t), CompareStdDirKey, this); + } + + if (!rc) + return false; + + for (;;) + { + DirRec e; + + rc = it.GetEntry(bte); + if (!rc) + break; + + if (g_debug & Dbg_Dir) + { + DumpBuffer(reinterpret_cast(bte.key), bte.key_len, "entry key"); + DumpBuffer(reinterpret_cast(bte.val), bte.val_len, "entry val"); + } + + k = reinterpret_cast(bte.key); + + if (k->obj_id_and_type != skey) + break; + + e.parent_id = k->obj_id_and_type & OBJ_ID_MASK; + + if (m_txt_fmt != 0) + { + const j_drec_hashed_key_t *hk = reinterpret_cast(bte.key); + e.hash = hk->name_len_and_hash; + e.name = reinterpret_cast(hk->name); + } + else + { + const j_drec_key_t *rk = reinterpret_cast(bte.key); + e.hash = 0; + e.name = reinterpret_cast(rk->name); + } + + // assert(res.val_len == sizeof(APFS_Name)); + + v = reinterpret_cast(bte.val); + + e.file_id = v->file_id; + e.date_added = v->date_added; + e.flags = v->flags; + + if (bte.val_len > sizeof(j_drec_val_t)) + { + const xf_blob_t *xf_hdr = reinterpret_cast(v->xfields); + const x_field_t *xf = reinterpret_cast(xf_hdr->xf_data); + const uint8_t *xdata = v->xfields + sizeof(xf_blob_t) + xf_hdr->xf_num_exts * sizeof(x_field_t); + uint16_t n; + + for (n = 0; n < xf_hdr->xf_num_exts; n++) + { + switch (xf[n].x_type) + { + case DREC_EXT_TYPE_SIBLING_ID: + assert(xf[n].x_size == sizeof(uint64_t)); + e.sibling_id = bswap_le(*reinterpret_cast(xdata)); + e.has_sibling_id = true; + break; + default: + std::cerr << "Warning: Unknown XF " << xf[n].x_type << " at drec " << e.file_id << std::endl; + break; + } + + xdata += ((xf[n].x_size + 7) & ~7); + } + } + + dir.push_back(e); + + it.next(); + } + + return true; +} + +bool ApfsDir::LookupName(ApfsDir::DirRec& res, uint64_t parent_id, const char* name) +{ + bool rc; + BTreeEntry e; + uint8_t srch_key_buf[0x500]; + size_t name_len = strlen(name) + 1; + + if (name_len > 0x400) + return false; + + res.parent_id = parent_id; + res.name = name; + + if (m_txt_fmt & 9) + { + j_drec_hashed_key_t *skey = reinterpret_cast(srch_key_buf); + + skey->hdr.obj_id_and_type = APFS_TYPE_ID(APFS_TYPE_DIR_REC, parent_id); + skey->name_len_and_hash = HashFilename(reinterpret_cast(name), static_cast(name_len), (m_txt_fmt & APFS_INCOMPAT_CASE_INSENSITIVE) != 0); + memcpy(skey->name, name, name_len); + if (g_debug & Dbg_Dir) + { + std::cout << "Lookup hashed key: key=" << skey->hdr.obj_id_and_type << " hash=" << skey->name_len_and_hash << " name='" << reinterpret_cast(skey->name) << "'" << std::endl; + dump_utf8(std::cout, skey->name); + } + res.hash = skey->name_len_and_hash; + + rc = m_fs_tree.Lookup(e, skey, sizeof(j_drec_hashed_key_t) + (skey->name_len_and_hash & J_DREC_LEN_MASK), CompareStdDirKey, this, true); + } + else + { + j_drec_key_t *skey = reinterpret_cast(srch_key_buf); + skey->hdr.obj_id_and_type = parent_id | (static_cast(APFS_TYPE_DIR_REC) << OBJ_TYPE_SHIFT); + skey->name_len = static_cast(name_len); + memcpy(skey->name, name, name_len); + if (g_debug & Dbg_Dir) + { + std::cout << "Lookup key: key=" << skey->hdr.obj_id_and_type << " name_len=" << skey->name_len << " name='" << reinterpret_cast(skey->name) << "'" << std::endl; + dump_utf8(std::cout, skey->name); + } + res.hash = 0; + + rc = m_fs_tree.Lookup(e, skey, sizeof(j_drec_key_t) + skey->name_len, CompareStdDirKey, this, true); + } + + if (!rc) + { + if (g_debug & Dbg_Dir) + std::cout << "Lookup failed!" << std::endl; + return false; + } + + const j_drec_val_t *v = reinterpret_cast(e.val); + + res.file_id = v->file_id; + res.date_added = v->date_added; + // v->unk ? + // TODO: SIBLING-ID! XF! + + if (g_debug & Dbg_Dir) + std::cout << "Lookup: id = " << res.file_id << std::endl; + + return true; +} + +bool ApfsDir::ReadFile(void* data, uint64_t inode, uint64_t offs, size_t size) +{ + BTreeEntry e; + bool rc; + + uint8_t *bdata = reinterpret_cast(data); + + size_t cur_size; + uint64_t blk_idx; + uint64_t blk_offs; + + uint64_t extent_size; + uint64_t extent_offs; + paddr_t extent_paddr; + uint64_t extent_crypto_id; + + while (size > 0) + { + if (m_vol.isSealed()) { + fext_tree_key_t key; + const fext_tree_key_t *fext_key = nullptr; + const fext_tree_val_t *fext_val = nullptr; + + key.private_id = inode; + key.logical_addr = offs; + + rc = m_vol.fexttree().Lookup(e, &key, sizeof(key), CompareFextKey, this, false); + if (!rc) return false; + + fext_key = reinterpret_cast(e.key); + fext_val = reinterpret_cast(e.val); + + extent_offs = offs - fext_key->logical_addr; + + extent_size = fext_val->len_and_flags & J_FILE_EXTENT_LEN_MASK; + extent_paddr = fext_val->phys_block_num; + extent_crypto_id = 0; /* TODO: Crypto on sealed volumes? Need later beta for that ... */ + } else { + j_file_extent_key_t key; + const j_file_extent_key_t *ext_key = nullptr; + const j_file_extent_val_t *ext_val = nullptr; + + key.hdr.obj_id_and_type = APFS_TYPE_ID(APFS_TYPE_FILE_EXTENT, inode); + key.logical_addr = offs; + + if (g_debug & Dbg_Dir) + std::cout << "ReadFile(inode=" << inode << ",offs=" << offs << ",size=" << size << ")" << std::endl; + + rc = m_fs_tree.Lookup(e, &key, sizeof(key), CompareStdDirKey, this, false); + + if (!rc) + return false; + + ext_key = reinterpret_cast(e.key); + ext_val = reinterpret_cast(e.val); + + if (g_debug & Dbg_Dir) + { + std::cout << "FileExtent " << ext_key->hdr.obj_id_and_type << " " << ext_key->logical_addr << " => "; + std::cout << ext_val->len_and_flags << " " << ext_val->phys_block_num << " " << ext_val->crypto_id << std::endl; + } + + if (ext_key->hdr.obj_id_and_type != key.hdr.obj_id_and_type) + return false; + + // Remove flags from length member + extent_offs = offs - ext_key->logical_addr; + + extent_size = ext_val->len_and_flags & J_FILE_EXTENT_LEN_MASK; + extent_paddr = ext_val->phys_block_num; + extent_crypto_id = ext_val->crypto_id; + } + + blk_idx = extent_offs >> m_blksize_sh; + blk_offs = extent_offs & m_blksize_mask_lo; + + cur_size = size; + + if ((extent_offs + cur_size) > extent_size) + cur_size = extent_size - extent_offs; + + if (cur_size == 0) + break; + + if (extent_paddr != 0) + { + if (blk_offs == 0 && cur_size > m_blksize) + cur_size &= m_blksize_mask_hi; + + if (blk_offs == 0 && (cur_size & m_blksize_mask_lo) == 0) + { + if (g_debug & Dbg_Dir) + std::cout << "Full read blk " << extent_paddr + blk_idx << " cnt " << (cur_size >> m_blksize_sh) << std::endl; + m_vol.ReadBlocks(bdata, extent_paddr + blk_idx, cur_size >> m_blksize_sh, extent_crypto_id + blk_idx); + } + else + { + if (g_debug & Dbg_Dir) + std::cout << "Partial read blk " << extent_paddr + blk_idx << " cnt 1" << std::endl; + + m_vol.ReadBlocks(m_tmp_blk.data(), extent_paddr + blk_idx, 1, extent_crypto_id + blk_idx); + + if (blk_offs + cur_size > m_blksize) + cur_size = m_blksize - blk_offs; + + if (g_debug & Dbg_Dir) + std::cout << "Partial copy off " << blk_offs << " size " << cur_size << std::endl; + + memcpy(bdata, m_tmp_blk.data() + blk_offs, cur_size); + } + } + else + memset(bdata, 0, cur_size); + bdata += cur_size; + offs += cur_size; + size -= cur_size; + // printf("ReadFile: offs=%016lX size=%016lX\n", offs, size); + } + + return true; +} + +bool ApfsDir::ListAttributes(std::vector& names, uint64_t inode) +{ + j_inode_key_t skey; + const j_xattr_key_t *ekey; + BTreeIterator it; + BTreeEntry res; + bool rc; + + skey.hdr.obj_id_and_type = APFS_TYPE_ID(APFS_TYPE_INODE, inode); + + rc = m_fs_tree.GetIterator(it, &skey, sizeof(j_inode_key_t), CompareStdDirKey, this); + if (!rc) + return false; + + for (;;) + { + rc = it.GetEntry(res); + if (!rc) + break; + + ekey = reinterpret_cast(res.key); + + if ((ekey->hdr.obj_id_and_type & OBJ_ID_MASK) != inode) + break; + + if ((ekey->hdr.obj_id_and_type >> OBJ_TYPE_SHIFT) < APFS_TYPE_XATTR) + { + it.next(); + continue; + } + + if ((ekey->hdr.obj_id_and_type >> OBJ_TYPE_SHIFT) > APFS_TYPE_XATTR) + break; + + names.push_back(reinterpret_cast(ekey->name)); + + it.next(); + } + + return true; +} + +bool ApfsDir::GetAttribute(std::vector& data, uint64_t inode, const char* name) +{ + uint8_t skey_buf[0x500]; + j_xattr_key_t *skey = reinterpret_cast(skey_buf); + const j_xattr_val_t *attr; + const j_xattr_dstream_t *xstm = nullptr; + + // const uint8_t *adata; + BTreeEntry res; + bool rc; + + size_t name_len = strlen(name) + 1; + if (name_len > 0x400) + return false; + + skey->hdr.obj_id_and_type = APFS_TYPE_ID(APFS_TYPE_XATTR, inode); + skey->name_len = static_cast(name_len); + memcpy(skey->name, name, skey->name_len); + + rc = m_fs_tree.Lookup(res, skey, sizeof(j_xattr_key_t) + skey->name_len, CompareStdDirKey, this, true); + if (!rc) + return false; + + attr = reinterpret_cast(res.val); + // adata = reinterpret_cast(res.val) + sizeof(APFS_Xattr_Val); + + if (g_debug & Dbg_Dir) + std::cout << "GetAttribute: type=" << attr->flags << std::endl; + + if (attr->flags & XATTR_DATA_STREAM) + { + // Attribute contents are stored in a file + assert(attr->xdata_len == sizeof(j_xattr_dstream_t)); + xstm = reinterpret_cast(attr->xdata); + + if (g_debug & Dbg_Dir) + { + std::cout << "Attribute is link:" << std::endl; + std::cout << " obj_id : " << xstm->xattr_obj_id << std::endl; + std::cout << " size : " << xstm->dstream.size << std::endl; + std::cout << " alloced_size : " << xstm->dstream.alloced_size << std::endl; + std::cout << " default_crypto_id : " << xstm->dstream.default_crypto_id << std::endl; + std::cout << " total_bytes_written : " << xstm->dstream.total_bytes_written << std::endl; + std::cout << " total_bytes_read : " << xstm->dstream.total_bytes_read << std::endl; + } + + data.resize(xstm->dstream.alloced_size); + ReadFile(data.data(), xstm->xattr_obj_id, 0, data.size()); // Read must be multiple of 4K ... + data.resize(xstm->dstream.size); + + if (g_debug & Dbg_Dir) + { + size_t dmpsize = 0x40; + if (dmpsize > data.size()) + dmpsize = data.size(); + DumpBuffer(data.data(), dmpsize, "start of attribute content"); + } + } + else if (attr->flags & XATTR_DATA_EMBEDDED) + { + // Attribute contents are stored in the btree + data.assign(attr->xdata, attr->xdata + attr->xdata_len); + } + + return true; +} + +bool ApfsDir::GetAttributeInfo(ApfsDir::XAttr& attr, uint64_t inode, const char* name) +{ + uint8_t skey_buf[0x500]; + j_xattr_key_t *skey = reinterpret_cast(skey_buf); + const j_xattr_val_t *xv; + const j_xattr_dstream_t *xs = nullptr; + + BTreeEntry res; + bool rc; + + size_t name_len = strlen(name) + 1; + if (name_len > 0x400) + return false; + + skey->hdr.obj_id_and_type = APFS_TYPE_ID(APFS_TYPE_XATTR, inode); + skey->name_len = static_cast(name_len); + memcpy(skey->name, name, skey->name_len); + + rc = m_fs_tree.Lookup(res, skey, sizeof(j_xattr_key_t) + skey->name_len, CompareStdDirKey, this, true); + if (!rc) + return false; + + xv = reinterpret_cast(res.val); + attr.flags = xv->flags; + attr.xdata_len = xv->xdata_len; + + if (xv->flags & XATTR_DATA_STREAM) + { + xs = reinterpret_cast(xv->xdata); + attr.xstrm = *xs; + } + + return true; +} + +int ApfsDir::CompareStdDirKey(const void *skey, size_t skey_len, const void *ekey, size_t ekey_len, void *context) +{ + // assert(skey_len == 8); + // assert(ekey_len == 8); + + (void)ekey_len; + + ApfsDir *dir = reinterpret_cast(context); + + uint64_t ks = *reinterpret_cast(skey); + uint64_t ke = *reinterpret_cast(ekey); + + // std::cout << std::hex << std::uppercase << std::setfill('0'); + + ks = (ks << 4) | (ks >> 60); + ke = (ke << 4) | (ke >> 60); + + // std::cout << std::setw(16) << ks << " : " << std::setw(16) << ke << std::endl; + + if (ke < ks) + return -1; + if (ke > ks) + return 1; + + if (skey_len > 8) + { + switch (ks & 0xF) + { + case APFS_TYPE_DIR_REC: + if (dir->m_txt_fmt & 9) + { + const j_drec_hashed_key_t *s = reinterpret_cast(skey); + const j_drec_hashed_key_t *e = reinterpret_cast(ekey); + + /* TODO: Is this correct? Yes. TODO check _apfs_cstrncmp. */ + if ((e->name_len_and_hash & J_DREC_HASH_MASK) < (s->name_len_and_hash & J_DREC_HASH_MASK)) + return -1; + if ((e->name_len_and_hash & J_DREC_HASH_MASK) > (s->name_len_and_hash & J_DREC_HASH_MASK)) + return 1; + + return apfs_strncmp(e->name, e->name_len_and_hash & J_DREC_LEN_MASK, s->name, s->name_len_and_hash & J_DREC_LEN_MASK); + } + else + { + const j_drec_key_t *s = reinterpret_cast(skey); + const j_drec_key_t *e = reinterpret_cast(ekey); + + return apfs_strncmp(e->name, e->name_len, s->name, s->name_len); + } + break; + case APFS_TYPE_FILE_EXTENT: + { + const j_file_extent_key_t *s = reinterpret_cast(skey); + const j_file_extent_key_t *e = reinterpret_cast(ekey); + + assert(skey_len == sizeof(j_file_extent_key_t)); + assert(ekey_len == sizeof(j_file_extent_key_t)); + + if (e->logical_addr < s->logical_addr) + return -1; + if (e->logical_addr > s->logical_addr) + return 1; + } + break; + case APFS_TYPE_XATTR: + { + const j_xattr_key_t *s = reinterpret_cast(skey); + const j_xattr_key_t *e = reinterpret_cast(ekey); + + return apfs_strncmp(e->name, e->name_len, s->name, s->name_len); + } + break; + case APFS_TYPE_FILE_INFO: + { + const j_file_info_key_t *s = reinterpret_cast(skey); + const j_file_info_key_t *e = reinterpret_cast(ekey); + + if (e->info_and_lba < s->info_and_lba) + return -1; + if (e->info_and_lba > s->info_and_lba) + return 1; + + break; + } + } + } + + return 0; +} + +int ApfsDir::CompareFextKey(const void* skey, size_t skey_len, const void* ekey, size_t ekey_len, void* context) +{ + const fext_tree_key_t *s = reinterpret_cast(skey); + const fext_tree_key_t *e = reinterpret_cast(ekey); + + if (e->private_id < s->private_id) + return -1; + if (e->private_id > s->private_id) + return 1; + if (e->logical_addr < s->logical_addr) + return -1; + if (e->logical_addr > s->logical_addr) + return 1; + return 0; +} diff --git a/vendor/apfs-fuse/ApfsLib/ApfsDir.h b/vendor/apfs-fuse/ApfsLib/ApfsDir.h new file mode 100644 index 0000000..e8273a9 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/ApfsDir.h @@ -0,0 +1,147 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#pragma once + +#include +#include + +#include "DiskStruct.h" + +class BTree; +class ApfsVolume; + +class ApfsDir +{ +public: + struct Inode + { + Inode(); + Inode(const Inode &other); + + /* TODO */ + uint64_t obj_id; + + uint64_t parent_id; + uint64_t private_id; + + uint64_t create_time; + uint64_t mod_time; + uint64_t change_time; + uint64_t access_time; + + uint64_t internal_flags; + + uint64_t nchildren_nlink; + + cp_key_class_t default_protection_class; + uint32_t write_generation_counter; + uint32_t bsd_flags; + uint32_t owner; + uint32_t group; + uint16_t mode; + + uint64_t uncompressed_size; + + uint64_t snap_xid; + uint64_t delta_tree_oid; + uint64_t prev_fsize; + // FinderInfo + uint64_t ds_size; + uint64_t ds_alloced_size; + uint64_t ds_default_crypto_id; + uint64_t ds_total_bytes_written; + uint64_t ds_total_bytes_read; + // j_dir_stats_val_t dir_stats; + apfs_uuid_t fs_uuid; + uint64_t sparse_bytes; + uint32_t document_id; + uint32_t rdev; + std::string name; + + uint32_t optional_present_flags; + + enum PresentFlags { + INO_HAS_SNAP_XID = 1, + INO_HAS_DELTA_TREE_OID = 2, + INO_HAS_DOCUMENT_ID = 4, + INO_HAS_NAME = 8, + INO_HAS_PREV_FSIZE = 16, + INO_HAS_FINDER_INFO = 64, + INO_HAS_DSTREAM = 128, + INO_HAS_DIR_STATS = 512, + INO_HAS_FS_UUID = 1024, + INO_HAS_SPARSE_BYTES = 4096, + INO_HAS_RDEV = 8192 + }; + }; + + struct DirRec + { + DirRec(); + DirRec(const DirRec &other); + + uint64_t parent_id; + uint32_t hash; + std::string name; + + uint64_t file_id; + uint64_t date_added; + + uint64_t sibling_id; + uint16_t flags; + bool has_sibling_id; + }; + + struct XAttr + { + XAttr(); + XAttr(const XAttr &other); + + uint16_t flags; + uint16_t xdata_len; + j_xattr_dstream_t xstrm; + }; + + + ApfsDir(ApfsVolume &vol); + ~ApfsDir(); + + bool GetInode(Inode &res, uint64_t inode); + + bool ListDirectory(std::vector &dir, uint64_t inode); + bool LookupName(DirRec &res, uint64_t parent_id, const char *name); + bool ReadFile(void *data, uint64_t inode, uint64_t offs, size_t size); + bool ListAttributes(std::vector &names, uint64_t inode); + bool GetAttribute(std::vector &data, uint64_t inode, const char *name); + bool GetAttributeInfo(XAttr &attr, uint64_t inode, const char *name); + +private: + static int CompareStdDirKey(const void *skey, size_t skey_len, const void *ekey, size_t ekey_len, void *context); + static int CompareFextKey(const void *skey, size_t skey_len, const void *ekey, size_t ekey_len, void *context); + + ApfsVolume &m_vol; + BTree &m_fs_tree; + uint32_t m_txt_fmt; + uint32_t m_blksize; + uint64_t m_blksize_mask_hi; + uint64_t m_blksize_mask_lo; + int m_blksize_sh; + std::vector m_tmp_blk; +}; diff --git a/vendor/apfs-fuse/ApfsLib/ApfsNodeMapper.cpp b/vendor/apfs-fuse/ApfsLib/ApfsNodeMapper.cpp new file mode 100644 index 0000000..8080257 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/ApfsNodeMapper.cpp @@ -0,0 +1,29 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#include "ApfsNodeMapper.h" + +ApfsNodeMapper::ApfsNodeMapper() +{ +} + + +ApfsNodeMapper::~ApfsNodeMapper() +{ +} diff --git a/vendor/apfs-fuse/ApfsLib/ApfsNodeMapper.h b/vendor/apfs-fuse/ApfsLib/ApfsNodeMapper.h new file mode 100644 index 0000000..b7835f7 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/ApfsNodeMapper.h @@ -0,0 +1,40 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#pragma once + +#include "ApfsTypes.h" + +struct omap_res_t +{ + oid_t oid; + xid_t xid; + uint32_t flags; + uint32_t size; + paddr_t paddr; +}; + +class ApfsNodeMapper +{ +public: + ApfsNodeMapper(); + virtual ~ApfsNodeMapper(); + + virtual bool Lookup(omap_res_t &res, oid_t oid, xid_t xid) = 0; +}; diff --git a/vendor/apfs-fuse/ApfsLib/ApfsNodeMapperBTree.cpp b/vendor/apfs-fuse/ApfsLib/ApfsNodeMapperBTree.cpp new file mode 100644 index 0000000..2b620c5 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/ApfsNodeMapperBTree.cpp @@ -0,0 +1,137 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#include +#include +#include + +#include "ApfsNodeMapperBTree.h" +#include "ApfsContainer.h" + +static int CompareOMapKey(const void *skey, size_t skey_len, const void *ekey, size_t ekey_len, void *context) +{ + (void)context; + (void)skey_len; + (void)ekey_len; + + assert(skey_len == sizeof(omap_key_t)); + assert(ekey_len == sizeof(omap_val_t)); + + const omap_key_t *skey_map = reinterpret_cast(skey); + const omap_key_t *ekey_map = reinterpret_cast(ekey); + + if (ekey_map->ok_oid < skey_map->ok_oid) + return -1; + if (ekey_map->ok_oid > skey_map->ok_oid) + return 1; + if (ekey_map->ok_xid < skey_map->ok_xid) + return -1; + if (ekey_map->ok_xid > skey_map->ok_xid) + return 1; + return 0; +} + +ApfsNodeMapperBTree::ApfsNodeMapperBTree(ApfsContainer &container) : + m_tree(container), + m_container(container) +{ +} + +ApfsNodeMapperBTree::~ApfsNodeMapperBTree() +{ +} + +bool ApfsNodeMapperBTree::Init(oid_t omap_oid, xid_t xid) +{ + std::vector blk; + + blk.resize(m_container.GetBlocksize()); + + if (!m_container.ReadAndVerifyHeaderBlock(blk.data(), omap_oid)) + { + std::cerr << "ERROR: Invalid omap block @ oid 0x" << std::hex << omap_oid << std::endl; + return false; + } + + memcpy(&m_omap, blk.data(), sizeof(omap_phys_t)); + + if ((m_omap.om_o.o_type & OBJECT_TYPE_MASK) == OBJECT_TYPE_BTREE) + { + std::cerr << "ERROR: Wrong omap type 0x" << std::hex << m_omap.om_o.o_type << std::endl; + return false; + } + + return m_tree.Init(m_omap.om_tree_oid, xid); +} + +bool ApfsNodeMapperBTree::Lookup(omap_res_t &omr, oid_t oid, xid_t xid) +{ + omap_key_t key; + + const omap_key_t *res_key = nullptr; + const omap_val_t *res_val = nullptr; + + BTreeEntry res; + + key.ok_oid = oid; + key.ok_xid = xid; + + // std::cout << std::hex << "Omap Lookup: oid = " << oid << ", xid = " << xid << " => "; + + if (!m_tree.Lookup(res, &key, sizeof(key), CompareOMapKey, this, false)) + { + // std::cout << "NOT FOUND" << std::endl; + std::cerr << std::hex << "oid " << oid << " xid " << xid << " NOT FOUND!!!" << std::endl; + return false; + } + + assert(res.val_len == sizeof(omap_val_t)); + + res_key = reinterpret_cast(res.key); + res_val = reinterpret_cast(res.val); + +#if 0 + if (g_debug & Dbg_Info) { + std::cout << std::hex << "Omap Lookup: oid=" << oid << " xid=" << xid << ": "; + std::cout << "oid=" << res_key->ok_oid << " xid=" << res_key->ok_xid << " => flags=" << res_val->ov_flags << " size=" << res_val->ov_size << " paddr=" << res_val->ov_paddr << std::endl; + } +#endif + + if (key.ok_oid != res_key->ok_oid) + { + // std::cout << "NOT FOUND" << std::endl; + std::cerr << std::hex << "oid " << oid << " xid " << xid << " NOT FOUND!!!" << std::endl; + omr.oid = oid; + omr.xid = xid; + omr.flags = 0; + omr.size = 0; + omr.paddr = 0; + return false; + } + + // std::cout << val->blockid << std::endl; + + omr.oid = res_key->ok_oid; + omr.xid = res_key->ok_xid; + omr.flags = res_val->ov_flags; + omr.size = res_val->ov_size; + omr.paddr = res_val->ov_paddr; + + return true; +} diff --git a/vendor/apfs-fuse/ApfsLib/ApfsNodeMapperBTree.h b/vendor/apfs-fuse/ApfsLib/ApfsNodeMapperBTree.h new file mode 100644 index 0000000..6466c22 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/ApfsNodeMapperBTree.h @@ -0,0 +1,45 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#pragma once + +#include "DiskStruct.h" + +#include "ApfsNodeMapper.h" +#include "BTree.h" + +class BlockDumper; + +class ApfsNodeMapperBTree : public ApfsNodeMapper +{ +public: + ApfsNodeMapperBTree(ApfsContainer &container); + virtual ~ApfsNodeMapperBTree(); + + bool Init(oid_t omap_oid, xid_t xid); + bool Lookup(omap_res_t & omr, oid_t oid, xid_t xid) override; + + void dump(BlockDumper &bd) { m_tree.dump(bd); } + +private: + omap_phys_t m_omap; + BTree m_tree; + + ApfsContainer &m_container; +}; diff --git a/vendor/apfs-fuse/ApfsLib/ApfsTypes.h b/vendor/apfs-fuse/ApfsLib/ApfsTypes.h new file mode 100644 index 0000000..878e565 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/ApfsTypes.h @@ -0,0 +1,29 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#pragma once + +#include + +// Commonly used FS types +typedef unsigned char apfs_uuid_t[16]; +typedef uint64_t paddr_t; // Apple: int64_t +typedef uint64_t oid_t; +typedef uint64_t xid_t; + diff --git a/vendor/apfs-fuse/ApfsLib/ApfsVolume.cpp b/vendor/apfs-fuse/ApfsLib/ApfsVolume.cpp new file mode 100644 index 0000000..6c6ffa5 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/ApfsVolume.cpp @@ -0,0 +1,394 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#include +#include +#include + +#include "Global.h" + +#include "ApfsContainer.h" +#include "ApfsVolume.h" +#include "BlockDumper.h" +#include "Util.h" + +ApfsVolume::ApfsVolume(ApfsContainer &container) : + m_container(container), + m_omap(container), + m_fs_tree(container, this), + m_extentref_tree(container, this), + m_snap_meta_tree(container, this), + m_fext_tree(container, this) +{ + m_apsb_paddr = 0; + m_is_encrypted = false; +} + +ApfsVolume::~ApfsVolume() +{ +} + +bool ApfsVolume::Init(paddr_t apsb_paddr) +{ + std::vector blk; + + m_apsb_paddr = apsb_paddr; + + blk.resize(m_container.GetBlocksize()); + + if (!ReadBlocks(blk.data(), apsb_paddr, 1, 0)) + return false; + + if (!VerifyBlock(blk.data(), blk.size())) + return false; + + memcpy(&m_sb, blk.data(), sizeof(m_sb)); + + if (m_sb.apfs_magic != APFS_MAGIC) + return false; + + if (!m_omap.Init(m_sb.apfs_omap_oid, m_sb.apfs_o.o_xid)) { + std::cerr << "WARNING: Volume omap tree init failed." << std::endl; + return false; + } + + if ((m_sb.apfs_fs_flags & 3) != APFS_FS_UNENCRYPTED && !m_container.IsUnencrypted()) + { + uint8_t vek[0x20]; + std::string str; + + std::cout << "Volume " << m_sb.apfs_volname << " is encrypted." << std::endl; + + if (!m_container.GetVolumeKey(vek, m_sb.apfs_vol_uuid)) + { + if (m_container.GetPasswordHint(str, m_sb.apfs_vol_uuid)) + std::cout << "Hint: " << str << std::endl; + + std::cout << "Enter Password: "; + GetPassword(str); + + if (!m_container.GetVolumeKey(vek, m_sb.apfs_vol_uuid, str.c_str())) + { + std::cout << "Wrong password!" << std::endl; + return false; + } + } + + m_aes.SetKey(vek, vek + 0x10); + m_is_encrypted = true; + } + + if (!m_fs_tree.Init(m_sb.apfs_root_tree_oid, m_sb.apfs_o.o_xid, &m_omap)) + std::cerr << "ERROR: root tree init failed" << std::endl; + + if (!m_extentref_tree.Init(m_sb.apfs_extentref_tree_oid, m_sb.apfs_o.o_xid)) + std::cerr << "WARNING: extentref tree init failed" << std::endl; + + if (!m_snap_meta_tree.Init(m_sb.apfs_snap_meta_tree_oid, m_sb.apfs_o.o_xid)) + std::cerr << "WARNING: snap meta tree init failed" << std::endl; + + if (m_sb.apfs_incompatible_features & APFS_INCOMPAT_SEALED_VOLUME) + { + if (!m_fext_tree.Init(m_sb.apfs_fext_tree_oid, m_sb.apfs_o.o_xid)) + std::cerr << "ERROR: fext tree init failed" << std::endl; + } + + return true; +} + +bool ApfsVolume::MountSnapshot(paddr_t apsb_paddr, xid_t snap_xid) +{ + BTree snap_btree(m_container); + BTreeEntry snap_entry; + j_snap_metadata_key_t snap_key; + const j_snap_metadata_val_t *snap_val = nullptr; + std::vector blk; + + m_apsb_paddr = apsb_paddr; + + blk.resize(m_container.GetBlocksize()); + + if (!ReadBlocks(blk.data(), apsb_paddr, 1, 0)) + return false; + + if (!VerifyBlock(blk.data(), blk.size())) + return false; + + memcpy(&m_sb, blk.data(), sizeof(m_sb)); + + if (m_sb.apfs_magic != APFS_MAGIC) + return false; + + if (m_sb.apfs_snap_meta_tree_oid == 0) + return false; + + if (!snap_btree.Init(m_sb.apfs_snap_meta_tree_oid, m_sb.apfs_o.o_xid)) { + std::cerr << "snap meta tree init failed" << std::endl; + return false; + } + + snap_key.hdr.obj_id_and_type = APFS_TYPE_ID(APFS_TYPE_SNAP_METADATA, snap_xid); + if (!snap_btree.Lookup(snap_entry, &snap_key, sizeof(snap_key), CompareSnapMetaKey, nullptr, true)) { + std::cerr << "snap xid not found" << std::endl; + return false; + } + + snap_val = reinterpret_cast(snap_entry.val); + + if (!m_omap.Init(m_sb.apfs_omap_oid, m_sb.apfs_o.o_xid)) { + std::cerr << "WARNING: Volume omap tree init failed." << std::endl; + return false; + } + + if (!ReadBlocks(blk.data(), snap_val->sblock_oid, 1, 0)) { + std::cerr << "failed to read snapshot superblock" << std::endl; + return false; + } + if (!VerifyBlock(blk.data(), blk.size())) { + std::cerr << "snap superblock checksum error" << std::endl; + return false; + } + + memcpy(&m_sb, blk.data(), sizeof(m_sb)); + + if (m_sb.apfs_magic != APFS_MAGIC) + return false; + + if ((m_sb.apfs_fs_flags & 3) != APFS_FS_UNENCRYPTED) + { + uint8_t vek[0x20]; + std::string str; + + std::cout << "Volume " << m_sb.apfs_volname << " is encrypted." << std::endl; + + if (!m_container.GetVolumeKey(vek, m_sb.apfs_vol_uuid)) + { + if (m_container.GetPasswordHint(str, m_sb.apfs_vol_uuid)) + std::cout << "Hint: " << str << std::endl; + + std::cout << "Enter Password: "; + GetPassword(str); + + if (!m_container.GetVolumeKey(vek, m_sb.apfs_vol_uuid, str.c_str())) + { + std::cout << "Wrong password!" << std::endl; + return false; + } + } + + m_aes.SetKey(vek, vek + 0x10); + m_is_encrypted = true; + } + + if (!m_fs_tree.Init(m_sb.apfs_root_tree_oid, m_sb.apfs_o.o_xid, &m_omap)) + std::cerr << "WARNING: root tree init failed" << std::endl; + + if (!m_extentref_tree.Init(m_sb.apfs_extentref_tree_oid, m_sb.apfs_o.o_xid)) + std::cerr << "WARNING: extentref tree init failed" << std::endl; + + if (!m_snap_meta_tree.Init(m_sb.apfs_snap_meta_tree_oid, m_sb.apfs_o.o_xid)) + std::cerr << "WARNING: snap meta tree init failed" << std::endl; + + if (m_sb.apfs_incompatible_features & APFS_INCOMPAT_SEALED_VOLUME) + { + if (!m_fext_tree.Init(m_sb.apfs_fext_tree_oid, m_sb.apfs_o.o_xid)) + std::cerr << "ERROR: fext tree init failed" << std::endl; + } + + return true; +} + +void ApfsVolume::dump(BlockDumper& bd) +{ + std::vector blk; + omap_res_t om; + oid_t omap_snapshot_tree_oid = 0; + + blk.resize(m_container.GetBlocksize()); + + if (!ReadBlocks(blk.data(), m_apsb_paddr, 1, 0)) + return; + + if (!VerifyBlock(blk.data(), blk.size())) + return; + + bd.SetTextFlags(m_sb.apfs_incompatible_features & 0xFF); + + bd.DumpNode(blk.data(), m_apsb_paddr); + + ReadBlocks(blk.data(), m_sb.apfs_omap_oid, 1, 0); + bd.DumpNode(blk.data(), m_sb.apfs_omap_oid); + + { + const omap_phys_t *om = reinterpret_cast(blk.data()); + omap_snapshot_tree_oid = om->om_snapshot_tree_oid; + + ReadBlocks(blk.data(), omap_snapshot_tree_oid, 1, 0); + bd.DumpNode(blk.data(), omap_snapshot_tree_oid); + } + + if (m_sb.apfs_er_state_oid) { + ReadBlocks(blk.data(), m_sb.apfs_er_state_oid, 1, 0); + bd.DumpNode(blk.data(), m_sb.apfs_er_state_oid); + } + + m_omap.dump(bd); + m_fs_tree.dump(bd); + // m_extentref_tree.dump(bd); + m_snap_meta_tree.dump(bd); + + if (m_sb.apfs_integrity_meta_oid != 0) { + if (m_omap.Lookup(om, m_sb.apfs_integrity_meta_oid, m_sb.apfs_o.o_xid)) + { + ReadBlocks(blk.data(), om.paddr, 1, 0); + bd.DumpNode(blk.data(), om.paddr); + } + } + + if (m_sb.apfs_snap_meta_ext_oid != 0) { + if (m_omap.Lookup(om, m_sb.apfs_snap_meta_ext_oid, m_sb.apfs_o.o_xid)) + { + ReadBlocks(blk.data(), om.paddr, 1, 0); + bd.DumpNode(blk.data(), om.paddr); + } + } + +#if 1 + if (m_sb.apfs_fext_tree_oid != 0) { + BTree fxtree(m_container, this); + fxtree.Init(m_sb.apfs_fext_tree_oid, m_sb.apfs_o.o_xid); + fxtree.dump(bd); + } +#endif + + BTreeEntry bte; +#if 0 + BTreeIterator it; + const j_snap_metadata_key_t *sm_key; + const j_snap_metadata_val_t *sm_val; + + if (m_snap_meta_tree.GetIteratorBegin(it)) { + for (;;) { + if (!it.GetEntry(bte)) break; + + sm_key = reinterpret_cast(bte.key); + sm_val = reinterpret_cast(bte.val); + + if ((sm_key->hdr.obj_id_and_type >> OBJ_TYPE_SHIFT) != APFS_TYPE_SNAP_METADATA) break; + + ReadBlocks(blk.data(), sm_val->sblock_oid, 1, 0); + bd.DumpNode(blk.data(), sm_val->sblock_oid); + + apfs_superblock_t apsb; + memcpy(&apsb, blk.data(), sizeof(apfs_superblock_t)); + + if (apsb.apfs_omap_oid) { + ReadBlocks(blk.data(), apsb.apfs_omap_oid, 1, 0); + bd.DumpNode(blk.data(), apsb.apfs_omap_oid); + } + + if (!it.next()) break; + } + } + + bte.clear(); +#endif + +#if 0 + { + BTree omap_tree(m_container, this); + BTreeIterator oit; + omap_phys_t om; + + ReadBlocks(blk.data(), m_sb.apfs_omap_oid, 1, 0); + memcpy(&om, blk.data(), sizeof(om)); + + omap_tree.Init(om.om_tree_oid, om.om_o.o_xid); + if (omap_tree.GetIteratorBegin(oit)) { + const omap_key_t *ok; + const omap_val_t *ov; + for (;;) { + if (!oit.GetEntry(bte)) break; + ok = reinterpret_cast(bte.key); + ov = reinterpret_cast(bte.val); + + bd.st() << "omap: " << ok->ok_oid << " " << ok->ok_xid << " => " << ov->ov_flags << " " << ov->ov_size << " " << ov->ov_paddr << std::endl; + if (ov->ov_flags & OMAP_VAL_NOHEADER) { + ReadBlocks(blk.data(), ov->ov_paddr, 1, 0); + bd.DumpNode(blk.data(), ov->ov_paddr); + } + + if (!oit.next()) break; + } + } else { + bd.st() << "Failed getting omap iterator" << std::endl; + } + } +#endif +} + +bool ApfsVolume::ReadBlocks(uint8_t * data, paddr_t paddr, uint64_t blkcnt, uint64_t xts_tweak) +{ + constexpr int encryption_block_size = 0x200; + + if (!m_container.ReadBlocks(data, paddr, blkcnt)) + return false; + + if (!m_is_encrypted || (xts_tweak == 0)) + return true; + + uint64_t cs_factor = m_container.GetBlocksize() / encryption_block_size; + uint64_t uno = xts_tweak * cs_factor; + size_t size = blkcnt * m_container.GetBlocksize(); + size_t k; + + for (k = 0; k < size; k += encryption_block_size) + { + m_aes.Decrypt(data + k, data + k, encryption_block_size, uno); + uno++; + } + + return true; +} + +int ApfsVolume::CompareSnapMetaKey(const void* skey, size_t skey_len, const void* ekey, size_t ekey_len, void* context) +{ + const j_key_t *ks = reinterpret_cast(skey); + const j_key_t *ke = reinterpret_cast(ekey); + const j_snap_name_key_t *sks; + const j_snap_name_key_t *ske; + + if (ke->obj_id_and_type < ks->obj_id_and_type) + return -1; + if (ke->obj_id_and_type > ks->obj_id_and_type) + return 1; + + switch (ks->obj_id_and_type >> OBJ_TYPE_SHIFT) + { + case APFS_TYPE_SNAP_METADATA: + break; + case APFS_TYPE_SNAP_NAME: + sks = reinterpret_cast(skey); + ske = reinterpret_cast(ekey); + return apfs_strncmp(ske->name, ske->name_len, sks->name, sks->name_len); + break; + } + + return 0; +} diff --git a/vendor/apfs-fuse/ApfsLib/ApfsVolume.h b/vendor/apfs-fuse/ApfsLib/ApfsVolume.h new file mode 100644 index 0000000..a9a8448 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/ApfsVolume.h @@ -0,0 +1,71 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#pragma once + +#include + +#include "DiskStruct.h" +#include "ApfsNodeMapperBTree.h" +#include "BTree.h" +#include + +class ApfsContainer; +class BlockDumper; + +class ApfsVolume +{ +public: + ApfsVolume(ApfsContainer &container); + ~ApfsVolume(); + + bool Init(paddr_t apsb_paddr); + bool MountSnapshot(paddr_t apsb_paddr, xid_t snap_xid); + + const char *name() const { return reinterpret_cast(m_sb.apfs_volname); } + + void dump(BlockDumper &bd); + + BTree &fstree() { return m_fs_tree; } + BTree &fexttree() { return m_fext_tree; } + uint32_t getTextFormat() const { return m_sb.apfs_incompatible_features & 0x9; } + + ApfsContainer &getContainer() const { return m_container; } + + bool ReadBlocks(uint8_t *data, paddr_t paddr, uint64_t blkcnt, uint64_t xts_tweak); + bool isSealed() const { return (m_sb.apfs_incompatible_features & APFS_INCOMPAT_SEALED_VOLUME) != 0; } + +private: + static int CompareSnapMetaKey(const void *skey, size_t skey_len, const void *ekey, size_t ekey_len, void *context); + + ApfsContainer &m_container; + + apfs_superblock_t m_sb; + + ApfsNodeMapperBTree m_omap; + BTree m_fs_tree; + BTree m_extentref_tree; + BTree m_snap_meta_tree; + BTree m_fext_tree; + + paddr_t m_apsb_paddr; + + bool m_is_encrypted; + AesXts m_aes; +}; diff --git a/vendor/apfs-fuse/ApfsLib/BTree.cpp b/vendor/apfs-fuse/ApfsLib/BTree.cpp new file mode 100644 index 0000000..10c5639 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/BTree.cpp @@ -0,0 +1,759 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#include +#include +#include +#include + +#include +#include + +#include "ApfsContainer.h" +#include "ApfsVolume.h" +#include "BTree.h" +#include "Util.h" +#include "BlockDumper.h" + +int CompareStdKey(const void *skey, size_t skey_len, const void *ekey, size_t ekey_len, void *context) +{ + // assert(skey_len == 8); + // assert(ekey_len == 8); + + (void)skey_len; + (void)ekey_len; + (void)context; + + uint64_t ks = *reinterpret_cast(skey); + uint64_t ke = *reinterpret_cast(ekey); + + if (ke < ks) + return -1; + if (ke > ks) + return 1; + return 0; +} + +BTreeEntry::BTreeEntry() +{ + key = nullptr; + val = nullptr; + key_len = 0; + val_len = 0; +} + +BTreeEntry::~BTreeEntry() +{ +} + +void BTreeEntry::clear() +{ + key = nullptr; + val = nullptr; + key_len = 0; + val_len = 0; + + m_node.reset(); +} + +BTreeNode::BTreeNode(BTree &tree, const uint8_t *block, size_t blocksize, paddr_t paddr, const std::shared_ptr &parent, uint32_t parent_index) : + m_tree(tree), + m_parent_index(parent_index), + m_parent(parent), + m_paddr(paddr) +{ + m_block.assign(block, block + blocksize); + m_btn = reinterpret_cast(m_block.data()); + + assert(m_btn->btn_table_space.off == 0); + + m_keys_start = sizeof(btree_node_phys_t) + m_btn->btn_table_space.len; + if (m_btn->btn_flags & BTNODE_ROOT) + m_vals_start = blocksize - sizeof(btree_info_t); + else + m_vals_start = blocksize; +} + +std::shared_ptr BTreeNode::CreateNode(BTree & tree, const uint8_t * block, size_t blocksize, paddr_t paddr, const std::shared_ptr &parent, uint32_t parent_index) +{ + const btree_node_phys_t *btn = reinterpret_cast(block); + + if (btn->btn_flags & BTNODE_FIXED_KV_SIZE) + return std::make_shared(tree, block, blocksize, paddr, parent, parent_index); + else + return std::make_shared(tree, block, blocksize, paddr, parent, parent_index); +} + +BTreeNode::~BTreeNode() +{ +} + +BTreeNodeFix::BTreeNodeFix(BTree &tree, const uint8_t *block, size_t blocksize, paddr_t paddr, const std::shared_ptr &parent, uint32_t parent_index) : + BTreeNode(tree, block, blocksize, paddr, parent, parent_index) +{ + m_entries = reinterpret_cast(m_block.data() + sizeof(btree_node_phys_t)); +} + +bool BTreeNodeFix::GetEntry(BTreeEntry & result, uint32_t index) const +{ + result.clear(); + + if (index >= m_btn->btn_nkeys) + return false; + + result.key = m_block.data() + m_keys_start + m_entries[index].k; + result.key_len = m_tree.GetKeyLen(); + + if (m_entries[index].v != BTOFF_INVALID) + { + result.val = m_block.data() + m_vals_start - m_entries[index].v; + result.val_len = (m_btn->btn_flags & BTNODE_LEAF) ? m_tree.GetValLen() : sizeof(oid_t); + } + else + { + result.val = nullptr; + result.val_len = 0; + } + + return true; +} + +BTreeNodeVar::BTreeNodeVar(BTree &tree, const uint8_t *block, size_t blocksize, paddr_t paddr, const std::shared_ptr &parent, uint32_t parent_index) : + BTreeNode(tree, block, blocksize, paddr, parent, parent_index) +{ + m_entries = reinterpret_cast(m_block.data() + sizeof(btree_node_phys_t)); +} + +bool BTreeNodeVar::GetEntry(BTreeEntry & result, uint32_t index) const +{ + result.clear(); + + if (index >= m_btn->btn_nkeys) + return false; + + result.key = m_block.data() + m_keys_start + m_entries[index].k.off; + result.key_len = m_entries[index].k.len; + + if (m_entries[index].v.off != BTOFF_INVALID) + { + result.val = m_block.data() + m_vals_start - m_entries[index].v.off; + result.val_len = m_entries[index].v.len; + } + else + { + result.val = nullptr; + result.val_len = 0; + } + + return true; +} + +BTree::BTree(ApfsContainer &container, ApfsVolume *volume) : + m_container(container) +{ + m_volume = volume; + + m_root_node = nullptr; + m_omap = nullptr; + m_xid = 0; + m_debug = false; +} + +BTree::~BTree() +{ +#ifdef BTREE_USE_MAP + m_nodes.clear(); +#endif +} + +bool BTree::Init(oid_t oid_root, xid_t xid, ApfsNodeMapper *omap) +{ + std::shared_ptr dummy; + + m_omap = omap; + m_oid = oid_root; + m_xid = xid; + + if (oid_root == 0) return false; + + m_root_node = GetNode(oid_root, dummy, 0); + + if (m_root_node) + { + memcpy(&m_treeinfo, m_root_node->block().data() + m_root_node->block().size() - sizeof(btree_info_t), sizeof(btree_info_t)); + return true; + } + else + { + std::cerr << "ERROR: BTree Init: Unable to get root node " << oid_root << std::endl; + return false; + } +} + +bool BTree::Lookup(BTreeEntry &result, const void *key, size_t key_size, BTCompareFunc func, void *context, bool exact) +{ + if (!m_root_node) + return false; + + oid_t oid; + oid_t oid_parent; + int index; + + std::shared_ptr node(m_root_node); + BTreeEntry e; + + if (m_debug) + { + // std::cout << std::hex << "BTree::Lookup: key=" << *reinterpret_cast(key) << " root=" << node->nodeid() << std::endl; + std::cout << "BTree::Lookup: "; + DumpHex(std::cout, reinterpret_cast(key), key_size, key_size); + } + + while (node->level() > 0) + { + index = FindBin(node, key, key_size, func, context, FindMode::LE); + + if (index < 0) + return false; + + node->GetEntry(e, index); + // DumpHex(std::cout, reinterpret_cast(e.val), e.val_len, 32); + + oid_parent = node->nodeid(); + + if (node->flags() & BTNODE_HASHED) { + const btn_index_node_val_t *binv = reinterpret_cast(e.val); + oid = binv->binv_child_oid + m_oid; + } else { + assert(e.val_len == sizeof(oid_t)); + oid = *reinterpret_cast(e.val); + } + + node = GetNode(oid, node, index); + + if (!node) + { + std::cerr << "BTree::Lookup: Node " << oid << " with parent " << oid_parent << " not found." << std::endl; + return false; + } + } + + index = FindBin(node, key, key_size, func, context, exact ? FindMode::EQ : FindMode::LE); + + if (m_debug) + std::cout << "Result = " << node->nodeid() << ":" << index << std::endl; + + if (index < 0) + return false; + + node->GetEntry(result, index); + result.m_node = node; + + return true; +} + +bool BTree::GetIterator(BTreeIterator& it, const void* key, size_t key_size, BTCompareFunc func, void *context) +{ + oid_t oid; + int index; + + std::shared_ptr node(m_root_node); + BTreeEntry e; + + if (m_debug) + std::cout << std::hex << "BTree::GetIterator: key=" << *reinterpret_cast(key) << " root=" << node->nodeid() << std::endl; + + while (node->level() > 0) + { + index = FindBin(node, key, key_size, func, context, FindMode::LE); + + if (index < 0) + index = 0; + + node->GetEntry(e, index); + + if (node->flags() & BTNODE_HASHED) { + const btn_index_node_val_t *binv = reinterpret_cast(e.val); + oid = binv->binv_child_oid + m_oid; + } else { + assert(e.val_len == sizeof(oid_t)); + oid = *reinterpret_cast(e.val); + } + + node = GetNode(oid, node, index); + } + + index = FindBin(node, key, key_size, func, context, FindMode::GE); + + if (m_debug) + std::cout << "Result = " << node->nodeid() << ":" << index << std::endl; + + if (index < 0) + { + index = node->entries_cnt() - 1; + it.Setup(this, node, index); + it.next(); + if (m_debug) + std::cout << "Iterator next entry" << std::endl; + } + else + { + it.Setup(this, node, index); + } + +#if 0 + it.GetEntry(e); + + if (func(key, key_size, e.key, e.key_len) < 0) + it.next(); +#endif + + return true; +} + +bool BTree::GetIteratorBegin(BTreeIterator& it) +{ + oid_t oid; + + std::shared_ptr node(m_root_node); + BTreeEntry e; + + while (node->level() > 0) + { + if (!node->GetEntry(e, 0)) + return false; + + if (node->flags() & BTNODE_HASHED) { + const btn_index_node_val_t *binv = reinterpret_cast(e.val); + oid = binv->binv_child_oid + m_oid; + } else { + oid = *reinterpret_cast(e.val); + } + node = GetNode(oid, node, 0); + } + + it.Setup(this, node, 0); + + return true; +} + +void BTree::dump(BlockDumper& out) +{ + if (m_root_node) + DumpTreeInternal(out, m_root_node); +} + +void BTree::DumpTreeInternal(BlockDumper& out, const std::shared_ptr &node) +{ + size_t k; + size_t cnt; + BTreeEntry e; + std::shared_ptr child; + oid_t oid_child; + oid_t oid_parent; + + if (!node) + return; + + out.DumpNode(node->block().data(), node->paddr()); + + if (node->level() > 0) + { + cnt = node->entries_cnt(); + + for (k = 0; k < cnt; k++) + { + node->GetEntry(e, k); + + /* + if (e.val_len != sizeof(uint64_t)) + continue; // assert ... + */ + + oid_parent = node->nodeid(); + + if (node->flags() & BTNODE_HASHED) { + const btn_index_node_val_t *binv = reinterpret_cast(e.val); + oid_child = binv->binv_child_oid + m_oid; + } else { + oid_child = *reinterpret_cast(e.val); + } + + if (m_omap) { + omap_res_t omr; + m_omap->Lookup(omr, oid_child, m_xid); + // out.st() << "omap: " << omr.oid << " " << omr.xid << " => " << omr.flags << " " << omr.size << " " << omr.paddr << std::endl; + + if (omr.flags & OMAP_VAL_DELETED) { + out.st() << "Omap val " << oid_child << " deleted." << std::endl; + continue; + } + } + + child = GetNode(oid_child, node, k); + + if (child) + DumpTreeInternal(out, child); + else + out.st() << "Child node " << oid_child << " of parent " << oid_parent << " not found!" << std::endl; + } + } +} + +std::shared_ptr BTree::GetNode(oid_t oid, const std::shared_ptr &parent, uint32_t parent_index) +{ + std::shared_ptr node; + bool rc; + + // printf("GetNode oid=%" PRIx64 "\n", oid); + +#ifdef BTREE_USE_MAP + m_mutex.lock(); + auto it = m_nodes.find(oid); + + if (it != m_nodes.end()) + node = it->second; + + m_mutex.unlock(); + + if (!node) +#endif + { + omap_res_t omr; + + omr.oid = oid; + omr.xid = m_xid; + omr.flags = 0; + omr.size = m_treeinfo.bt_fixed.bt_node_size; + omr.paddr = oid; + + std::vector blk; + + if (m_omap) + { + rc = m_omap->Lookup(omr, oid, m_xid); + + if (g_debug & Dbg_Info) { + std::cout << "omap: oid=" << omr.oid << " xid=" << omr.xid << " flags=" << omr.flags << " size=" << omr.size << " paddr=" << omr.paddr << std::endl; + } + + if (!rc) + { + std::cerr << "ERROR: GetNode: omap entry oid " << std::hex << oid << " xid " << m_xid << " not found." << std::endl; + return node; + } + } + + blk.resize(m_container.GetBlocksize()); + + if (m_volume) + { + // TODO: is the crypto_id always equal to the block ID here? + // I think so, the xts id and the block id only differ when the + // volume has been converted from a HFS/FileVault volume, which + // used CoreStorage. After conversions, the block numbers do not + // match anymore, since the CoreStorage data has been removed + // and assigned to the apfs volume. But the metadata is always + // fresh and therefore the ids should match. + if (!m_volume->ReadBlocks(blk.data(), omr.paddr, 1, (omr.flags & OMAP_VAL_ENCRYPTED) ? omr.paddr : 0)) + { + std::cerr << "ERROR: GetNode: ReadBlocks failed!" << std::endl; + return node; + } + + if (!(omr.flags & OMAP_VAL_NOHEADER)) { + if (!VerifyBlock(blk.data(), blk.size())) + { + std::cerr << "ERROR: GetNode: VerifyBlock failed!" << std::endl; + if (g_debug & Dbg_Errors) + DumpHex(std::cerr, blk.data(), blk.size()); + return node; + } + } else { + /* + std::cout << "BTNode @ " << omr.paddr << ":" << std::endl; + DumpHex(std::cout, blk.data(), blk.size()); + std::cout << std::endl; + */ + } + } + else + { + if (!m_container.ReadAndVerifyHeaderBlock(blk.data(), omr.paddr)) + { + std::cerr << "ERROR: GetNode: ReadAndVerifyHeaderBlock failed!" << std::endl; + return node; + } + } + + node = BTreeNode::CreateNode(*this, blk.data(), blk.size(), omr.paddr, parent, parent_index); +#ifdef BTREE_USE_MAP + m_mutex.lock(); + + if (m_nodes.size() > BTREE_MAP_MAX_NODES) + { +#if 0 + m_nodes.clear(); // TODO: Make this somewhat more intelligent ... +#else + // This might be somewhat more intelligent ... + for (it = m_nodes.begin(); it != m_nodes.end();) + { + if (it->second.use_count() == 1) + it = m_nodes.erase(it); + else + ++it; + } +#endif + } + + m_nodes[oid] = node; + + m_mutex.unlock(); +#endif + } + + return node; +} + +uint32_t BTree::Find(const std::shared_ptr &node, const void *key, size_t key_size, BTCompareFunc func, void *context) +{ + uint32_t k; + uint32_t cnt; + int res_cur = 0; + int res_prev = 0; + BTreeEntry e; + + cnt = node->entries_cnt(); + + if (cnt == 0) + return 0; + + // Erst mal linear suchen ... + for (k = 0; k < cnt; k++) + { + node->GetEntry(e, k); + + res_cur = func(key, key_size, e.key, e.key_len, context); + + if (res_cur > 0 && res_prev <= 0) + break; + + res_prev = res_cur; + } + + return k; +} + +int BTree::FindBin(const std::shared_ptr& node, const void* key, size_t key_size, BTCompareFunc func, void *context, FindMode mode) +{ + static const char resstr[3] = { '<', '=', '>' }; + + int beg; + int end; + int mid = -1; + int cnt = node->entries_cnt(); + int res; + + BTreeEntry e; + int rc; + + if (cnt <= 0) + return -1; + + beg = 0; + end = cnt - 1; + + if (m_debug) + { + std::cout << "FindBin : "; + DumpHex(std::cout, reinterpret_cast(key), key_size, key_size); + } + + while (beg <= end) + { + mid = (beg + end) / 2; + + node->GetEntry(e, mid); + rc = func(key, key_size, e.key, e.key_len, context); + + if (m_debug) + { + std::cout << std::dec << std::setfill(' '); + std::cout << std::setw(2) << beg << " [" << std::setw(2) << mid << "] " << std::setw(2) << end << " : " << resstr[rc + 1] << " : "; + DumpHex(std::cout, reinterpret_cast(e.key), e.key_len, e.key_len); + } + + if (rc == 0) + break; + + if (rc == -1) + beg = mid + 1; + else if (rc == 1) + end = mid - 1; + } + + if (m_debug) + { + std::cout << std::dec << std::setfill(' '); + std::cout << " => " << resstr[rc + 1] << ", " << mid; + } + + switch (mode) + { + case FindMode::EQ: + res = (rc == 0) ? mid : -1; + break; + case FindMode::LE: + res = (rc <= 0) ? mid : (mid - 1); + break; + case FindMode::LT: + res = (rc < 0) ? mid : (mid - 1); + break; + case FindMode::GE: + res = (rc >= 0) ? mid : (mid + 1); + break; + case FindMode::GT: + res = (rc > 0) ? mid : (mid + 1); + break; + default: + assert(false); + res = -1; + break; + } + + if (res == cnt) + res = -1; + + if (m_debug) + std::cout << " => " << res << std::endl; + + return res; +} + +BTreeIterator::BTreeIterator() +{ + m_tree = nullptr; + m_index = 0; +} + +BTreeIterator::BTreeIterator(BTree *tree, const std::shared_ptr &node, uint32_t index) +{ + m_tree = tree; + m_node = node; + m_index = index; +} + +BTreeIterator::~BTreeIterator() +{ +} + +void BTreeIterator::Setup(BTree* tree, const std::shared_ptr& node, uint32_t index) +{ + m_tree = tree; + m_node = node; + m_index = index; +} + + +bool BTreeIterator::next() +{ + if (!m_node) + return false; + + m_index++; + + if (m_index < m_node->entries_cnt()) + return true; + else { + std::shared_ptr node = next_node(); + if (node) { + m_index = 0; + m_node = node; + return true; + } + } + return false; +} + +void BTreeIterator::reset() +{ + m_tree = nullptr; + m_node.reset(); + m_index = 0; +} + +bool BTreeIterator::GetEntry(BTreeEntry& res) const +{ + if (!m_node) + return false; + + return m_node->GetEntry(res, m_index); +} + +#undef BTITDBG + +std::shared_ptr BTreeIterator::next_node() +{ + std::shared_ptr node; + uint32_t pidx; + BTreeEntry e; + oid_t oid; + + node = m_node; + +#ifdef BTITDBG + std::cout << "======== ******** BTreeIterator::next_node() ******** ========" << std::endl; + std::cout << " Current node: " << node->nodeid() << std::endl; +#endif + + do { + pidx = node->parent_index(); + node = node->parent(); +#ifdef BTITDBG + std::cout << " Navigating up to node " << node->nodeid() << " index " << pidx << std::endl; +#endif + pidx++; + } while (node && pidx >= node->entries_cnt()); + + if (!node) + return std::shared_ptr(); + + while (node->level() > 0) { + node->GetEntry(e, pidx); + + if (node->flags() & BTNODE_HASHED) { + const btn_index_node_val_t *binv = reinterpret_cast(e.val); + oid = binv->binv_child_oid + m_tree->m_oid; + } else { + oid = *reinterpret_cast(e.val); + } + +#ifdef BTITDBG + std::cout << " Navigating down to node " << oid << std::endl; +#endif + node = m_tree->GetNode(oid, node, pidx); + + if (!node) + std::cerr << "Failed to load btree node oid " << oid << std::endl; + + pidx = 0; + } + + return node; +} diff --git a/vendor/apfs-fuse/ApfsLib/BTree.h b/vendor/apfs-fuse/ApfsLib/BTree.h new file mode 100644 index 0000000..c517ece --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/BTree.h @@ -0,0 +1,209 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#pragma once + +#include +#include +#include +#include + +#include "Global.h" +#include "DiskStruct.h" + +#include "ApfsNodeMapper.h" + +class BTree; +class BTreeNode; +class BTreeIterator; +class BlockDumper; + +class ApfsContainer; +class ApfsVolume; + +// This enables a rudimentary disk cache ... +#define BTREE_USE_MAP +// TODO: Think about a better solution. +// 8192 will take max. 32 MB of RAM. Higher may be faster, but use more RAM. +#define BTREE_MAP_MAX_NODES 8192 + +// ekey < skey: -1, ekey > skey: 1, ekey == skey: 0 +typedef int(*BTCompareFunc)(const void *skey, size_t skey_len, const void *ekey, size_t ekey_len, void *context); + +int CompareStdKey(const void *skey, size_t skey_len, const void *ekey, size_t ekey_len, void *context); + +class BTreeEntry +{ + friend class BTree; +public: + BTreeEntry(); + ~BTreeEntry(); + + BTreeEntry(const BTreeEntry &o) = delete; + BTreeEntry &operator=(const BTreeEntry &o) = delete; + + void clear(); + + const void *key; + const void *val; + size_t key_len; + size_t val_len; + +private: + std::shared_ptr m_node; +}; + +class BTreeNode +{ +protected: + BTreeNode(BTree &tree, const uint8_t *block, size_t blocksize, paddr_t paddr, const std::shared_ptr &parent, uint32_t parent_index); + +public: + static std::shared_ptr CreateNode(BTree &tree, const uint8_t *block, size_t blocksize, paddr_t paddr, const std::shared_ptr &parent, uint32_t parent_index); + + virtual ~BTreeNode(); + + uint64_t nodeid() const { return m_btn->btn_o.o_oid; } + uint32_t entries_cnt() const { return m_btn->btn_nkeys; } + uint16_t level() const { return m_btn->btn_level; } + uint16_t flags() const { return m_btn->btn_flags; } + paddr_t paddr() const { return m_paddr; } + + const std::shared_ptr &parent() const { return m_parent; } + uint16_t parent_index() const { return m_parent_index; } + + virtual bool GetEntry(BTreeEntry &result, uint32_t index) const = 0; + // virtual uint32_t Find(const void *key, size_t key_size, BTCompareFunc func) const = 0; + + const std::vector &block() const { return m_block; } + +protected: + std::vector m_block; + BTree &m_tree; + + uint16_t m_keys_start; // Up + uint16_t m_vals_start; // Dn + + const uint32_t m_parent_index; + const std::shared_ptr m_parent; + + const paddr_t m_paddr; + + const btree_node_phys_t *m_btn; +}; + +class BTreeNodeFix : public BTreeNode +{ +public: + BTreeNodeFix(BTree &tree, const uint8_t *block, size_t blocksize, paddr_t paddr, const std::shared_ptr &parent, uint32_t parent_index); + + bool GetEntry(BTreeEntry &result, uint32_t index) const override; + // uint32_t Find(const void *key, size_t key_size, BTCompareFunc func) const override; + +private: + const kvoff_t *m_entries; +}; + +class BTreeNodeVar : public BTreeNode +{ +public: + BTreeNodeVar(BTree &tree, const uint8_t *block, size_t blocksize, paddr_t paddr, const std::shared_ptr &parent, uint32_t parent_index); + + bool GetEntry(BTreeEntry &result, uint32_t index) const override; + // uint32_t Find(const void *key, size_t key_size, BTCompareFunc func) const override; + +private: + const kvloc_t *m_entries; +}; + +class BTree +{ + enum class FindMode + { + EQ, + LE, + LT, + GE, + GT + }; + + friend class BTreeIterator; +public: + BTree(ApfsContainer &container, ApfsVolume *vol = nullptr); + ~BTree(); + + bool Init(oid_t oid_root, xid_t xid, ApfsNodeMapper *omap = nullptr); + + bool Lookup(BTreeEntry &result, const void *key, size_t key_size, BTCompareFunc func, void *context, bool exact); + bool GetIterator(BTreeIterator &it, const void *key, size_t key_size, BTCompareFunc func, void *context); + bool GetIteratorBegin(BTreeIterator &it); + + uint16_t GetKeyLen() const { return m_treeinfo.bt_fixed.bt_key_size; } + uint16_t GetValLen() const { return m_treeinfo.bt_fixed.bt_val_size; } + + void dump(BlockDumper &out); + + void EnableDebugOutput() { m_debug = true; } + +private: + void DumpTreeInternal(BlockDumper &out, const std::shared_ptr &node); + uint32_t Find(const std::shared_ptr &node, const void *key, size_t key_size, BTCompareFunc func, void *context); + int FindBin(const std::shared_ptr &node, const void *key, size_t key_size, BTCompareFunc func, void *context, FindMode mode); + + std::shared_ptr GetNode(oid_t oid, const std::shared_ptr &parent, uint32_t parent_index); + + ApfsContainer &m_container; + ApfsVolume *m_volume; + + std::shared_ptr m_root_node; + ApfsNodeMapper *m_omap; + + btree_info_t m_treeinfo; + + oid_t m_oid; + xid_t m_xid; + bool m_debug; + +#ifdef BTREE_USE_MAP + std::map> m_nodes; + std::mutex m_mutex; +#endif +}; + +class BTreeIterator +{ +public: + BTreeIterator(); + BTreeIterator(BTree *tree, const std::shared_ptr &node, uint32_t index); + ~BTreeIterator(); + + bool next(); + void reset(); + + bool GetEntry(BTreeEntry &res) const; + + void Setup(BTree *tree, const std::shared_ptr &node, uint32_t index); + +private: + BTree *m_tree; + std::shared_ptr m_node; + uint32_t m_index; + + std::shared_ptr next_node(); +}; diff --git a/vendor/apfs-fuse/ApfsLib/BlockDumper.cpp b/vendor/apfs-fuse/ApfsLib/BlockDumper.cpp new file mode 100644 index 0000000..2e41f4c --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/BlockDumper.cpp @@ -0,0 +1,2203 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#include "BlockDumper.h" +#include "Decmpfs.h" +#include "Util.h" + +#include +#include +#include +#include +#include +#include + +#undef DUMP_COMPRESSED + +static const FlagDesc fd_obj_type[] = { + { OBJ_EPHEMERAL, "OBJ_EPHEMERAL" }, + { OBJ_PHYSICAL, "OBJ_PHYSICAL" }, + { OBJ_NOHEADER, "OBJ_NOHEADER" }, + { OBJ_ENCRYPTED, "OBJ_ENCRYPTED" }, + { OBJ_NONPERSISTENT, "OBJ_NONPERSISTENT" }, + { 0, 0 } +}; + +static const FlagDesc fd_nx_feat[] = { + { NX_FEATURE_DEFRAG, "NX_FEATURE_DEFRAG" }, + { NX_FEATURE_LCFD, "NX_FEATURE_LCFD" }, + { 0, 0 } +}; + +static const FlagDesc fd_nx_rocompat[] = { + { 0, 0 } +}; + +static const FlagDesc fd_nx_incompat[] = { + { NX_INCOMPAT_VERSION1, "NX_INCOMPAT_VERSION1" }, + { NX_INCOMPAT_VERSION2, "NX_INCOMPAT_VERSION2" }, + { NX_INCOMPAT_FUSION, "NX_INCOMPAT_FUSION" }, + { 0, 0 } +}; + +static const FlagDesc fd_nx_flags[] = { + { NX_RESERVED_1, "NX_RESERVED_1" }, + { NX_RESERVED_2, "NX_RESERVED_2" }, + { NX_CRYPTO_SW, "NX_CRYPTO_SW" }, + { 0, 0 } +}; + +static const FlagDesc fd_cpm_flags[] = { + { CHECKPOINT_MAP_LAST, "CHECKPOINT_MAP_LAST" }, + { 0, 0 } +}; + +static const FlagDesc fd_om_flags[] = { + { OMAP_MANUALLY_MANAGED, "OMAP_MANUALLY_MANAGED" }, + { OMAP_ENCRYPTING, "OMAP_ENCRYPTING" }, + { OMAP_DECRYPTING, "OMAP_DECRYPTING" }, + { OMAP_KEYROLLING, "OMAP_KEYROLLING" }, + { OMAP_CRYPTO_GENERATION, "OMAP_CRYPTO_GENERATION" }, + { 0, 0 } +}; + +static const FlagDesc fd_ov_flags[] = { + { OMAP_VAL_DELETED, "OMAP_VAL_DELETED" }, + { OMAP_VAL_SAVED, "OMAP_VAL_SAVED" }, + { OMAP_VAL_ENCRYPTED, "OMAP_VAL_ENCRYPTED" }, + { OMAP_VAL_NOHEADER, "OMAP_VAL_NOHEADER" }, + { OMAP_VAL_CRYPTO_GENERATION, "OMAP_VAL_CRYPTO_GENERATION" }, + { 0, 0 } +}; + +static const FlagDesc fd_oms_flags[] = { + { OMAP_SNAPSHOT_DELETED, "OMAP_SNAPSHOT_DELETED" }, + { OMAP_SNAPSHOT_REVERTED, "OMAP_SNAPSHOT_REVERTED" }, + { 0, 0 } +}; + +static const FlagDesc fd_apfs_fs_flags[] = { + { APFS_FS_UNENCRYPTED, "APFS_FS_UNENCRYPTED" }, + { APFS_FS_RESERVED_2, "APFS_FS_RESERVED_2" }, + { APFS_FS_RESERVED_4, "APFS_FS_RESERVED_4" }, + { APFS_FS_ONEKEY, "APFS_FS_ONEKEY" }, + { APFS_FS_SPILLEDOVER, "APFS_FS_SPILLEDOVER" }, + { APFS_FS_RUN_SPILLOVER_CLEANER, "APFS_FS_RUN_SPILLOVER_CLEANER" }, + { APFS_FS_ALWAYS_CHECK_EXTENTREF, "APFS_FS_ALWAYS_CHECK_EXTENTREF" }, + { APFS_FS_RESERVED_80, "APFS_FS_RESERVED_80" }, + { APFS_FS_RESERVED_100, "APFS_FS_RESERVED_100" }, + { 0, 0 } +}; + +static const FlagDesc fd_apfs_features[] = { + { APFS_FEATURE_DEFRAG_PRERELEASE, "APFS_FEATURE_DEFRAG_PRERELEASE" }, + { APFS_FEATURE_HARDLINK_MAP_RECORDS, "APFS_FEATURE_HARDLINK_MAP_RECORDS" }, + { APFS_FEATURE_DEFRAG, "APFS_FEATURE_DEFRAG" }, + { APFS_FEATURE_STRICTATIME, "APFS_FEATURE_STRICTATIME" }, + { APFS_FEATURE_VOLGRP_SYSTEM_INO_SPACE, "APFS_FEATURE_VOLGRP_SYSTEM_INO_SPACE" }, + { 0, 0 } +}; + +static const FlagDesc fd_apfs_rocompat[] = { + { 0, 0 } +}; + +static const FlagDesc fd_apfs_incompat[] = { + { APFS_INCOMPAT_CASE_INSENSITIVE, "APFS_INCOMPAT_CASE_INSENSITIVE" }, + { APFS_INCOMPAT_DATALESS_SNAPS, "APFS_INCOMPAT_DATALESS_SNAPS" }, + { APFS_INCOMPAT_ENC_ROLLED, "APFS_INCOMPAT_ENC_ROLLED" }, + { APFS_INCOMPAT_NORMALIZATION_INSENSITIVE, "APFS_INCOMPAT_NORMALIZATION_INSENSITIVE" }, + { APFS_INCOMPAT_INCOMPLETE_RESTORE, "APFS_INCOMPAT_INCOMPLETE_RESTORE" }, + { APFS_INCOMPAT_SEALED_VOLUME, "APFS_INCOMPAT_SEALED_VOLUME" }, + { APFS_INCOMPAT_RESERVED_40, "APFS_INCOMPAT_RESERVED_40" }, + { 0, 0 } +}; + +static const FlagDesc fd_j_inode_flags[] = { + { INODE_IS_APFS_PRIVATE, "INODE_IS_APFS_PRIVATE" }, + { INODE_MAINTAIN_DIR_STATS, "INODE_MAINTAIN_DIR_STATS" }, + { INODE_DIR_STATS_ORIGIN, "INODE_DIR_STATS_ORIGIN" }, + { INODE_PROT_CLASS_EXPLICIT, "INODE_PROT_CLASS_EXPLICIT" }, + { INODE_WAS_CLONED, "INODE_WAS_CLONED" }, + { INODE_FLAGS_UNUSED, "INODE_FLAGS_UNUSED" }, + { INODE_HAS_SECURITY_EA, "INODE_HAS_SECURITY_EA" }, + { INODE_BEING_TRUNCATED, "INODE_BEING_TRUNCATED" }, + { INODE_HAS_FINDER_INFO, "INODE_HAS_FINDER_INFO" }, + { INODE_IS_SPARSE, "INODE_IS_SPARSE" }, + { INODE_WAS_EVER_CLONED, "INODE_WAS_EVER_CLONED" }, + { INODE_ACTIVE_FILE_TRIMMED, "INODE_ACTIVE_FILE_TRIMMED" }, + { INODE_PINNED_TO_MAIN, "INODE_PINNED_TO_MAIN" }, + { INODE_PINNED_TO_TIER2, "INODE_PINNED_TO_TIER2" }, + { INODE_HAS_RSRC_FORK, "INODE_HAS_RSRC_FORK" }, + { INODE_NO_RSRC_FORK, "INODE_NO_RSRC_FORK" }, + { INODE_ALLOCATION_SPILLEDOVER, "INODE_ALLOCATION_SPILLEDOVER" }, + { INODE_FAST_PROMOTE, "INODE_FAST_PROMOTE" }, + { INODE_HAS_UNCOMPRESSED_SIZE, "INODE_HAS_UNCOMPRESSED_SIZE" }, + { INODE_IS_PURGEABLE, "INODE_IS_PURGEABLE" }, + { INODE_WANTS_TO_BE_PURGEABLE, "INODE_WANTS_TO_BE_PURGEABLE" }, + { INODE_IS_SYNC_ROOT, "INODE_IS_SYNC_ROOT" }, + { INODE_SNAPSHOT_COW_EXEMPTION, "INODE_SNAPSHOT_COW_EXEMPTION" }, + { 0, 0 } +}; + +static const FlagDesc fd_j_xattr_flags[] = { + { XATTR_DATA_STREAM, "XATTR_DATA_STREAM" }, + { XATTR_DATA_EMBEDDED, "XATTR_DATA_EMBEDDED" }, + { XATTR_FILE_SYSTEM_OWNED, "XATTR_FILE_SYSTEM_OWNED" }, + { XATTR_RESERVED_8, "XATTR_RESERVED_8" }, + { 0, 0 } +}; + +static const FlagDesc fd_j_inode_mode[] = { + { MODE_S_IFIFO, "S_IFIFO" }, + { MODE_S_IFCHR, "S_IFCHR" }, + { MODE_S_IFDIR, "S_IFDIR" }, + { MODE_S_IFBLK, "S_IFBLK" }, + { MODE_S_IFREG, "S_IFREG" }, + { MODE_S_IFLNK, "S_IFLNK" }, + { MODE_S_IFSOCK, "S_IFSOCK" }, + { MODE_S_IFWHT, "S_IFWHT" }, + { MODE_S_IFMT, "S_IFMT" }, + { 0, 0 } +}; + +static const FlagDesc fd_j_inode_bsd_flags[] = { + { APFS_UF_NODUMP, "UF_NODUMP" }, + { APFS_UF_IMMUTABLE, "UF_IMMUTABLE" }, + { APFS_UF_APPEND, "UF_APPEND" }, + { APFS_UF_OPAQUE, "UF_OPAQUE" }, + { APFS_UF_NOUNLINK, "UF_NOUNLINK" }, + { APFS_UF_COMPRESSED, "UF_COMPRESSED" }, + { APFS_UF_TRACKED, "UF_TRACKED" }, + { APFS_UF_DATAVAULT, "UF_DATAVAULT" }, + { APFS_UF_HIDDEN, "UF_HIDDEN" }, + { APFS_SF_ARCHIVED, "SF_ARCHIVED" }, + { APFS_SF_IMMUTABLE, "SF_IMMUTABLE" }, + { APFS_SF_APPEND, "SF_APPEND" }, + { APFS_SF_RESTRICTED, "SF_RESTRICTED" }, + { APFS_SF_NOUNLINK, "SF_NOUNLINK" }, + { APFS_SF_SNAPSHOT, "SF_SNAPSHOT" }, + { APFS_SF_FIRMLINK, "SF_FIRMLINK" }, + { APFS_SF_DATALESS, "SF_DATALESS" }, + { 0, 0 } +}; + +static const FlagDesc fd_j_drec_flags[] = { + { DT_UNKNOWN, "DT_UNKNOWN" }, + { DT_FIFO, "DT_FIFO" }, + { DT_CHR, "DT_CHR" }, + { DT_DIR, "DT_DIR" }, + { DT_BLK, "DT_BLK" }, + { DT_REG, "DT_REG" }, + { DT_LNK, "DT_LNK" }, + { DT_SOCK, "DT_SOCK" }, + { DT_WHT, "DT_WHT" }, + { 0, 0 } +}; + +static const FlagDesc fd_x_flags[] = { + { XF_DATA_DEPENDENT, "XF_DATA_DEPENDENT" }, + { XF_DO_NOT_COPY, "XF_DO_NOT_COPY" }, + { XF_RESERVED_4, "XF_RESERVED_4" }, + { XF_CHILDREN_INHERIT, "XF_CHILDREN_INHERIT" }, + { XF_USER_FIELD, "XF_USER_FIELD" }, + { XF_SYSTEM_FIELD, "XF_SYSTEM_FIELD" }, + { XF_RESERVED_40, "XF_RESERVED_40" }, + { XF_RESERVED_80, "XF_RESERVED_80" }, + { 0, 0 } +}; + +static const FlagDesc fd_snap_meta_flags[] = { + { SNAP_META_PENDING_DATALESS, "SNAP_META_PENDING_DATALESS" }, + { SNAP_META_MERGE_IN_PROGRESS, "SNAP_META_MERGE_IN_PROGRESS" }, + { 0, 0 } +}; + +static const FlagDesc fd_bt_flags[] = { + { BTREE_UINT64_KEYS, "BTREE_UINT64_KEYS" }, + { BTREE_SEQUENTIAL_INSERT, "BTREE_SEQUENTIAL_INSERT" }, + { BTREE_ALLOW_GHOSTS, "BTREE_ALLOW_GHOSTS" }, + { BTREE_EPHEMERAL, "BTREE_EPHEMERAL" }, + { BTREE_PHYSICAL, "BTREE_PHYSICAL" }, + { BTREE_NONPERSISTENT, "BTREE_NONPERSISTENT" }, + { BTREE_KV_NONALIGNED, "BTREE_KV_NONALIGNED" }, + { BTREE_HASHED, "BTREE_HASHED" }, + { BTREE_NOHEADER, "BTREE_NOHEADER" }, + { 0, 0 } +}; + +static const FlagDesc fd_btn_flags[] = { + { BTNODE_ROOT, "BTNODE_ROOT" }, + { BTNODE_LEAF, "BTNODE_LEAF" }, + { BTNODE_FIXED_KV_SIZE, "BTNODE_FIXED_KV_SIZE" }, + { BTNODE_HASHED, "BTNODE_HASHED" }, + { BTNODE_NOHEADER, "BTNODE_NOHEADER" }, + { BTNODE_CHECK_KOFF_INVAL, "BTNODE_CHECK_KOFF_INVAL" }, + { 0, 0 } +}; + +static const FlagDesc fd_mt_flags[] = { + { FUSION_MT_DIRTY, "FUSION_MT_DIRTY" }, + { FUSION_MT_TENANT, "FUSION_MT_TENANT" }, + { 0, 0 } +}; + +using namespace std; + +BlockDumper::BlockDumper(std::ostream &os, size_t blocksize) : + m_os(os) +{ + m_block = nullptr; + m_blocksize = blocksize; + m_text_flags = 0x08; // Standard: Case-sensitive +} + +BlockDumper::~BlockDumper() +{ +} + +void BlockDumper::DumpNode(const uint8_t *block, uint64_t blk_nr) +{ + using namespace std; + + m_block = block; + + m_os << hex << uppercase << setfill('0'); + + const obj_phys_t * const obj = reinterpret_cast(m_block); + + if (IsEmptyBlock(m_block, m_blocksize)) + { + m_os << setw(16) << blk_nr << " [Empty]" << endl; + m_block = nullptr; + return; + } + + /* + if (!VerifyBlock(m_block, m_blocksize)) + { + m_os << setw(16) << blk_nr << ":" << endl; + DumpBlockHex(); + return; + } + */ + + DumpNodeHeader(obj, blk_nr); + + switch (obj->o_type & OBJECT_TYPE_MASK) + { + case OBJECT_TYPE_NX_SUPERBLOCK: // NXSB Block + DumpBlk_NXSB(); + break; + case OBJECT_TYPE_BTREE: // BTree Root + case OBJECT_TYPE_BTREE_NODE: // BTree Node + DumpBTNode_0(); + break; + // MTREE ? + case OBJECT_TYPE_SPACEMAN: + DumpBlk_SM(); + break; + case OBJECT_TYPE_SPACEMAN_CAB: + DumpBlk_CAB(); + break; + case OBJECT_TYPE_SPACEMAN_CIB: // Bitmap Block List + DumpBlk_CIB(); + break; + /* 8 - A */ + case OBJECT_TYPE_OMAP: // Pointer to Header (?) + DumpBlk_OM(); + break; + case OBJECT_TYPE_CHECKPOINT_MAP: // Another Mapping + DumpBlk_CPM(); + break; + case OBJECT_TYPE_FS: // APSB Block + DumpBlk_APSB(); + break; + /* E - 10 */ + case OBJECT_TYPE_NX_REAPER: + DumpBlk_NR(); + break; + case OBJECT_TYPE_NX_REAP_LIST: + DumpBlk_NRL(); + break; + + // case OBJECT_TYPE_OMAP_SNAPSHOT: + + case OBJECT_TYPE_EFI_JUMPSTART: + DumpBlk_JSDR(); + break; + + // case OBJECT_TYPE_FUSION_MIDDLE_TREE: + + case OBJECT_TYPE_NX_FUSION_WBC: + DumpBlk_WBC(); + break; + + case OBJECT_TYPE_NX_FUSION_WBC_LIST: + DumpBlk_WBCL(); + break; + + case OBJECT_TYPE_ER_STATE: + DumpBlk_ER(); + break; + + case OBJECT_TYPE_SNAP_META_EXT: + DumpBlk_SnapMetaExt(); + break; + + case OBJECT_TYPE_INTEGRITY_META: + DumpBlk_IntegrityMeta(); + break; + + // case OBJECT_TYPE_GBITMAP: + + // case OBJECT_TYPE_GBITMAP_TREE: + // case OBJECT_TYPE_GBITMAP_BLOCK: + + case 0: + DumpBTNode(&BlockDumper::DumpBTEntry_APFS_Root); + break; + + default: + // assert(false); + std::cerr << "!!! UNKNOWN NODE TYPE " << hex << setw(8) << obj->o_type << " in block " << setw(16) << blk_nr << " !!!" << endl; + m_os << "!!! UNKNOWN NODE TYPE !!!" << endl; + DumpBlockHex(); + break; + } + + m_os << endl; + m_os << "===========================================================================================================================" << endl; + m_os << endl; + + m_block = nullptr; +} + +void BlockDumper::DumpNodeHeader(const obj_phys_t *blk, uint64_t blk_nr) +{ + m_os << "[paddr] | cksum | oid | xid | type | subtype | description" << endl; + m_os << "-----------------+------------------+------------------+------------------+----------+----------+-----------------------" << endl; + + m_os << setw(16) << blk_nr << " | "; + m_os << setw(16) << hexstr(blk->o_cksum, MAX_CKSUM_SIZE) << " | "; + m_os << setw(16) << blk->o_oid << " | "; + m_os << setw(16) << blk->o_xid << " | "; + m_os << setw(8) << blk->o_type << " | "; + m_os << setw(8) << blk->o_subtype << " | "; + m_os << GetNodeType(blk->o_type, blk->o_subtype) << endl; + m_os << endl; +} + +#undef BT_VERBOSE + +void BlockDumper::DumpBTNode(DumpFunc func, uint16_t keys_size, uint16_t values_size) +{ + const btree_node_phys_t * const btn = reinterpret_cast(m_block); + + const uint8_t *key_ptr = nullptr; + const uint8_t *val_ptr = nullptr; + size_t key_len = 0; + size_t val_len = 0; + + uint16_t base; + uint16_t end; + size_t k; + +#ifdef BT_VERBOSE + // TODO -> nloc_t + struct FreeListEntry + { + uint16_t next_offs; + uint16_t free_size; + }; + + const FreeListEntry *fle; + uint16_t flo; + + uint16_t tot_key_free = 0; + uint16_t tot_val_free = 0; +#endif + + // DumpHex(m_block, 0x1000); + + DumpBTHeader(); + + base = btn->btn_table_space.off + btn->btn_table_space.len + sizeof(btree_node_phys_t); + if (btn->btn_flags & BTNODE_ROOT) + end = static_cast(m_blocksize - sizeof(btree_info_t)); + else + end = static_cast(m_blocksize); + +#ifdef BT_VERBOSE + if (btn->btn_key_free_list.off != BTOFF_INVALID) + { + m_os << "Key Free List Space (" << setw(4) << (base + btn->btn_key_free_list.off) << ") :" << endl; + // DumpHex(m_block + base + bt->key_free_list_space_offset, bt->key_free_list_space_length); + flo = btn->btn_key_free_list.off; + while (flo != BTOFF_INVALID) + { + fle = reinterpret_cast(m_block + base + flo); + m_os << setw(4) << flo << ":" << setw(4) << fle->next_offs << "/" << setw(4) << fle->free_size << endl; + DumpHex(m_block + base + flo, fle->free_size); + flo = fle->next_offs; + tot_key_free += fle->free_size; + } + } + + if (btn->btn_val_free_list.off != BTOFF_INVALID) + { + m_os << "Val Free List Space (" << setw(4) << (end - btn->btn_val_free_list.off) << ") :" << endl; + // DumpHex(m_block + end - bt->val_free_list_space_offset, bt->val_free_list_space_length); + flo = btn->btn_val_free_list.off; + while (flo != BTOFF_INVALID) + { + fle = reinterpret_cast(m_block + end - flo); + m_os << setw(4) << flo << ":" << setw(4) << fle->next_offs << "/" << setw(4) << fle->free_size << endl; + DumpHex(m_block + end - flo, fle->free_size); + flo = fle->next_offs; + tot_val_free += fle->free_size; + } + } + + m_os << "Free List: Tot Key = " << tot_key_free << ", Tot Val = " << tot_val_free << endl; + if (tot_key_free != btn->btn_key_free_list.len) + m_os << "[!!!]" << endl; + if (tot_val_free != btn->btn_val_free_list.len) + m_os << "[!!!]" << endl; +#endif + + if (btn->btn_flags & BTNODE_FIXED_KV_SIZE) + { + if (keys_size == 0 || values_size == 0) + { + m_os << "!!! UNKNOWN FIXED KEY / VALUE SIZE !!!" << endl << endl; + + if ((btn->btn_o.o_type & OBJECT_TYPE_MASK) == OBJECT_TYPE_BTREE) + DumpBTreeInfo(); + + return; + } + + const kvoff_t * const entry = reinterpret_cast(m_block + (btn->btn_table_space.off + sizeof(btree_node_phys_t))); + + for (k = 0; k < btn->btn_nkeys; k++) + { +#ifdef BT_VERBOSE + m_os << endl << k << ": " << setw(4) << entry[k].k << " / " << setw(4) << entry[k].v << endl; +#endif + + assert(entry[k].k != BTOFF_INVALID); + + if (entry[k].k == BTOFF_INVALID) + continue; + + key_ptr = m_block + base + entry[k].k; + key_len = keys_size; + val_ptr = (entry[k].v == BTOFF_INVALID) ? nullptr : m_block + end - entry[k].v; + val_len = (btn->btn_flags & BTNODE_LEAF) ? values_size : sizeof(oid_t); + + (*this.*func)(key_ptr, key_len, val_ptr, val_len, !(btn->btn_flags & BTNODE_LEAF)); + } + } + else + { + const kvloc_t * const entry = reinterpret_cast(m_block + (btn->btn_table_space.off + sizeof(btree_node_phys_t))); + + for (k = 0; k < btn->btn_nkeys; k++) + { +#ifdef BT_VERBOSE + m_os << endl << k << ": " << setw(4) << entry[k].k.off << " L " << setw(4) << entry[k].k.len; + m_os << " / " << setw(4) << entry[k].v.off << " / " << setw(4) << entry[k].v.len << endl; +#endif + + assert(entry[k].k.off != BTOFF_INVALID); + + if (entry[k].k.off == BTOFF_INVALID) + continue; + + key_ptr = m_block + base + entry[k].k.off; + key_len = entry[k].k.len; + val_ptr = (entry[k].v.off == BTOFF_INVALID) ? nullptr : m_block + end - entry[k].v.off; + val_len = entry[k].v.len; + + (*this.*func)(key_ptr, key_len, val_ptr, val_len, !(btn->btn_flags & BTNODE_LEAF)); + } + } + + m_os << endl; + + if (btn->btn_flags & BTNODE_ROOT) + DumpBTreeInfo(); +} + +void BlockDumper::DumpBTHeader(bool dump_offsets) +{ + const btree_node_phys_t *bt = reinterpret_cast(m_block); + + m_os << "Flgs | Levl | Key Cnt | Table Area | Free Area | Key Free L | Val Free L" << endl; + m_os << "-----+------+----------+-------------+-------------+-------------+------------" << endl; + + m_os << setw(4) << bt->btn_flags << " | "; + m_os << setw(4) << bt->btn_level << " | "; + m_os << setw(8) << bt->btn_nkeys << " | "; + m_os << setw(4) << bt->btn_table_space.off << " L " << setw(4) << bt->btn_table_space.len << " | "; + m_os << setw(4) << bt->btn_free_space.off << " L " << setw(4) << bt->btn_free_space.len << " | "; + m_os << setw(4) << bt->btn_key_free_list.off << " L " << setw(4) << bt->btn_key_free_list.len << " | "; + m_os << setw(4) << bt->btn_val_free_list.off << " L " << setw(4) << bt->btn_val_free_list.len; + m_os << " [" << flagstr(bt->btn_flags, fd_btn_flags) << "]" << endl; + m_os << endl; + + if (dump_offsets) + { + m_os << "Index: " << setw(4) << bt->btn_table_space.off << " L " << setw(4) << bt->btn_table_space.len; + m_os << " [" << setw(4) << (0x38 + bt->btn_table_space.off) << " - " << setw(4) << (0x38 + bt->btn_table_space.off + bt->btn_table_space.len) << "]" << endl; + m_os << "Free: " << setw(4) << bt->btn_free_space.off << " L " << setw(4) << bt->btn_free_space.len; + m_os << " [" << setw(4) << (0x38 + bt->btn_table_space.off + bt->btn_table_space.len + bt->btn_free_space.off) << " - " << setw(4) << (0x38 + bt->btn_table_space.off + bt->btn_table_space.len + bt->btn_free_space.off + bt->btn_free_space.len) << "]" << endl; + m_os << "K Free: " << setw(4) << bt->btn_key_free_list.off << " L " << setw(4) << bt->btn_key_free_list.len << endl; + m_os << "V Free: " << setw(4) << bt->btn_val_free_list.off << " L " << setw(4) << bt->btn_val_free_list.len << endl; + m_os << endl; + + size_t cnt; + size_t k; + + if (bt->btn_flags & BTNODE_FIXED_KV_SIZE) + { + const kvoff_t *e = reinterpret_cast(m_block + sizeof(btree_node_phys_t)); + + cnt = bt->btn_table_space.len / 4; + + for (k = 0; k < bt->btn_nkeys; k++) + m_os << setw(2) << k << " : " << setw(4) << e[k].k << " => " << setw(4) << e[k].v << endl; + m_os << "-----------------" << endl; + for (; k < cnt; k++) + { + if (e[k].k != 0 || e[k].v != 0) + { + m_os << setw(2) << k << " : " << setw(4) << e[k].k << " => " << setw(4) << e[k].v << endl; + } + } + } + else + { + const kvloc_t *e = reinterpret_cast(m_block + sizeof(btree_node_phys_t)); + + cnt = bt->btn_table_space.len / 8; + + for (k = 0; k < bt->btn_nkeys; k++) + { + m_os << setw(2) << k << " : "; + m_os << setw(4) << e[k].k.off << " L " << setw(4) << e[k].k.len << " => "; + m_os << setw(4) << e[k].v.off << " L " << setw(4) << e[k].v.len << endl; + } + m_os << "-------------------------------" << endl; + for (; k < cnt; k++) + { + if (e[k].k.off != 0 || e[k].k.len != 0 || e[k].v.off != 0 || e[k].v.len != 0) + { + m_os << setw(2) << k << " : "; + m_os << setw(4) << e[k].k.off << " L " << setw(4) << e[k].k.len << " => "; + m_os << setw(4) << e[k].v.off << " L " << setw(4) << e[k].v.len << endl; + } + } + } + + m_os << endl; + } +} + +void BlockDumper::DumpBTreeInfo() +{ + const btree_info_t * const info = reinterpret_cast(m_block + (m_blocksize - sizeof(btree_info_t))); + + m_os << endl; + m_os << "Flags | Nodesize | Key Size | Val Size | Key Max | Val Max | Key Count | Node Count " << endl; + m_os << "---------+----------+----------+----------+----------+----------+------------------+-----------------" << endl; + m_os << setw(8) << info->bt_fixed.bt_flags << " | "; + m_os << setw(8) << info->bt_fixed.bt_node_size << " | "; + m_os << setw(8) << info->bt_fixed.bt_key_size << " | "; + m_os << setw(8) << info->bt_fixed.bt_val_size << " | "; + m_os << setw(8) << info->bt_longest_key << " | "; + m_os << setw(8) << info->bt_longest_val << " | "; + m_os << setw(16) << info->bt_key_count << " | "; + m_os << setw(16) << info->bt_node_count; + m_os << " [" << flagstr(info->bt_fixed.bt_flags, fd_bt_flags) << "]" << endl; +} + +void BlockDumper::DumpBTEntry_APFS_Root(const void *key_ptr, size_t key_len, const void *val_ptr, size_t val_len, bool index) +{ + // TODO: Sauber implementieren ... + + uint64_t key; + // uint16_t nlen; + uint8_t type; + + static const char *typestr[16] = { + "Any ", + "SnapMeta", + "Extent ", + "Inode ", + "XAttr ", + "SibLnk ", + "DStmID ", + "Crypto ", + "FileExt ", + "DirRec ", + "DirStats", + "SnapName", + "SibMap ", + "FileInfo", + "Undef-14", + "Undef-15" + }; + + if (key_len < 8) + { + m_os << "!!! KEY LENGTH TOO SHORT : " << key_len << endl; + DumpBTEntry_Unk(key_ptr, key_len, val_ptr, val_len, index); + return; + } + + key = *reinterpret_cast(key_ptr); + type = key >> OBJ_TYPE_SHIFT; + key &= OBJ_ID_MASK; + + m_os << typestr[type] << ' '; + + switch (type) + { + // 1 = SnapMetadata + // 2 = PhysExtent + case APFS_TYPE_INODE: + assert(key_len == 8); + m_os << key << " => "; + + if (index) + { + DumpBTIndex(val_ptr, val_len); + } + else + { + assert(val_len >= sizeof(j_inode_val_t)); + + // m_os << "[" << setw(4) << val_len << "] "; + + const j_inode_val_t *obj = reinterpret_cast(val_ptr); + + m_os << obj->parent_id << " "; + m_os << obj->private_id << " "; +#if 0 + m_os << obj->birthtime << " "; + m_os << obj->mtime << " "; + m_os << obj->ctime << " "; + m_os << obj->atime << " "; +#else + m_os << "[TS] "; +#endif + m_os << obj->internal_flags << " [" << flagstr(obj->internal_flags, fd_j_inode_flags) << "] "; + m_os << obj->nchildren << " "; + m_os << obj->default_protection_class << " "; + m_os << obj->write_generation_counter << " "; + m_os << obj->bsd_flags << " [" << flagstr(obj->bsd_flags, fd_j_inode_bsd_flags) << "] "; + m_os << dec; + m_os << obj->owner << " "; + m_os << obj->group << " "; + m_os << oct << obj->mode << hex << " "; + m_os << obj->pad1 << " "; + m_os << obj->uncompressed_size; + + if (val_len > sizeof(j_inode_val_t)) + Dump_XF(obj->xfields, val_len - sizeof(j_inode_val_t), false); + + m_os << " [" << flagstr(obj->internal_flags, fd_j_inode_flags) << "]"; + + m_os << endl; + } + + break; + + case APFS_TYPE_XATTR: + { + assert(key_len >= sizeof(j_xattr_key_t)); + const j_xattr_key_t *xk = reinterpret_cast(key_ptr); + + m_os << key << " "; + m_os << '\'' << xk->name << '\''; + m_os << " => "; + + if (index) + { + DumpBTIndex(val_ptr, val_len); + } + else + { + const j_xattr_val_t *xv = reinterpret_cast(val_ptr); + + assert(xv->xdata_len + 4U == val_len); + + m_os << xv->flags << " [" << flagstr(xv->flags, fd_j_xattr_flags) << "] " << xv->xdata_len; + + if ((xv->flags & XATTR_DATA_STREAM) && xv->xdata_len == sizeof(j_xattr_dstream_t)) + { + const j_xattr_dstream_t *attrlnk = reinterpret_cast(xv->xdata); + + m_os << " : " << attrlnk->xattr_obj_id << " " << attrlnk->dstream.size << " " << attrlnk->dstream.alloced_size << " "; + m_os << attrlnk->dstream.default_crypto_id << " " << attrlnk->dstream.total_bytes_written << " " << attrlnk->dstream.total_bytes_read << endl; + } + else if (xv->flags & XATTR_DATA_EMBEDDED) + { + const char *attr_name = reinterpret_cast(xk->name); + + if (!strcmp(attr_name, "com.apple.fs.symlink")) + m_os << " : " << '\'' << xv->xdata << '\'' << endl; + else if (!strcmp(attr_name, "com.apple.quarantine")) + { + // Limit length ... + std::string str(reinterpret_cast(xv->xdata), val_len - 4); + m_os << " : '" << str << '\'' << endl; + } + else if (!strcmp(attr_name, "com.apple.decmpfs")) + { + if (xv->xdata_len >= sizeof(CompressionHeader)) + { + const CompressionHeader *cmpf = reinterpret_cast(xv->xdata); + if (cmpf->signature == 0x636D7066) + m_os << " : 'cmpf' " << cmpf->algo << ' ' << cmpf->size; + else + m_os << " : [!!! Compression Header Invalid !!!]"; +#ifdef DUMP_COMPRESSED + if (xv->xdata_len > sizeof(CompressionHeader)) + { + m_os << endl; + DumpHex(xv->xdata + sizeof(CompressionHeader), xv->xdata_len - sizeof(CompressionHeader)); + } +#else + if (xv->xdata_len > sizeof(CompressionHeader)) + m_os << " ..."; + m_os << endl; +#endif + } + } +#ifndef DUMP_COMPRESSED + else if (!strncmp(attr_name, "com.apple.metadata:", 19)) + { + m_os << endl; + // Not really interested in e-mail metadata ... + } +#endif + else + { + m_os << endl; + DumpHex(xv->xdata, xv->xdata_len); + } + } + } + + break; + } + + case APFS_TYPE_SIBLING_LINK: + { + assert(key_len == 0x10); + + const j_sibling_key_t *k = reinterpret_cast(key_ptr); + + m_os << key << " "; + m_os << k->sibling_id << " => "; + + if (index) + { + DumpBTIndex(val_ptr, val_len); + } + else + { + const j_sibling_val_t * const v = reinterpret_cast(val_ptr); + + m_os << v->parent_id << " "; + m_os << "'" << v->name << "'"; + m_os << endl; + } + break; + } + + case APFS_TYPE_DSTREAM_ID: + assert(key_len == 8); + m_os << key << " => "; + if (index) + { + DumpBTIndex(val_ptr, val_len); + } + else + { + assert(val_len == sizeof(j_dstream_id_val_t)); + const j_dstream_id_val_t *v = reinterpret_cast(val_ptr); + + m_os << v->refcnt << endl; + } + + break; + + case APFS_TYPE_CRYPTO_STATE: + assert(key_len == 8); + + if (index) + { + DumpBTIndex(val_ptr, val_len); + } + else + { + const j_crypto_val_t *c = reinterpret_cast(val_ptr); + + m_os << key << " => "; + m_os << c->refcnt << " : "; + m_os << c->state.major_version << " "; + m_os << c->state.minor_version << " "; + m_os << c->state.cpflags << " "; + m_os << c->state.persistent_class << " "; + m_os << c->state.key_os_version << " "; + m_os << c->state.key_revision << " "; + m_os << c->state.key_len << endl; + + if (val_len > sizeof(j_crypto_val_t)) + DumpHex(reinterpret_cast(val_ptr) + sizeof(j_crypto_val_t), val_len - sizeof(j_crypto_val_t)); + } + + break; + + case APFS_TYPE_FILE_EXTENT: + { + assert(key_len == 16); + const j_file_extent_key_t *k = reinterpret_cast(key_ptr); + + m_os << key << " " << k->logical_addr << " => "; + + if (index) + { + DumpBTIndex(val_ptr, val_len); + } + else + { + assert(val_len == sizeof(j_file_extent_val_t)); + + const j_file_extent_val_t *ext = reinterpret_cast(val_ptr); + uint16_t flags = ext->len_and_flags >> J_FILE_EXTENT_FLAG_SHIFT; + uint64_t length = ext->len_and_flags & J_FILE_EXTENT_LEN_MASK; + if (flags != 0) + m_os << flags << "/"; + m_os << length << " " << ext->phys_block_num << " " << ext->crypto_id << endl; + } + + break; + } + + case APFS_TYPE_DIR_REC: + { + assert(key_len >= 10); + + m_os << key << " "; + + if (m_text_flags & (APFS_INCOMPAT_CASE_INSENSITIVE | APFS_INCOMPAT_NORMALIZATION_INSENSITIVE)) + { + const j_drec_hashed_key_t *k = reinterpret_cast(key_ptr); + + m_os << setw(8) << k->name_len_and_hash << " "; + +#if 0 // Hash verify + uint32_t hash_calc = HashFilename(k->name, k->name_len_and_hash & J_DREC_LEN_MASK, (m_text_flags & APFS_INCOMPAT_CASE_INSENSITIVE) != 0); + m_os << "[" << setw(8) << hash_calc << "] "; + + if (hash_calc != k->name_len_and_hash) + { + cerr << hex << "Hash not matching at name " << key << " : stored " << k->name_len_and_hash << ", calc " << hash_calc << " '"; + cerr << k->name << "'" << endl; + + m_os << endl; + DumpHex(key_data, key_len, key_len); + } +#endif + + m_os << '\'' << k->name << '\''; + } + else + { + const j_drec_key_t *k = reinterpret_cast(key_ptr); + + m_os << '\'' << k->name << '\''; + } + m_os << " => "; + + if (index) + { + DumpBTIndex(val_ptr, val_len); + } + else + { + const j_drec_val_t *ptr = reinterpret_cast(val_ptr); + m_os << ptr->file_id << " [" << tstamp(ptr->date_added) << "] " << ptr->flags; + + if (val_len > sizeof(j_drec_val_t)) + Dump_XF(ptr->xfields, val_len - sizeof(j_drec_val_t), true); + + m_os << " [" << enumstr(ptr->flags & DREC_TYPE_MASK, fd_j_drec_flags) << "]"; + + m_os << endl; + } + +#if 0 + DumpHex(key_data + 12, key_len - 12); +#endif + + break; + } + + // TODO: A = DirSize + // B = SnapName + + case APFS_TYPE_SIBLING_MAP: + m_os << key << " => "; + if (index) + DumpBTIndex(val_ptr, val_len); + else + m_os << *reinterpret_cast(val_ptr) << endl; + break; + + case APFS_TYPE_FILE_INFO: + { + const j_file_info_key_t *k = reinterpret_cast(key_ptr); + + m_os << key << " " << k->info_and_lba << " => "; + if (index) { + DumpBTIndex(val_ptr, val_len); + } else { + const j_file_info_val_t *v = reinterpret_cast(val_ptr); + m_os << v->dhash.hashed_len << " " << static_cast(v->dhash.hash_size) << " "; + for (uint8_t k = 0; k < v->dhash.hash_size; k++) + m_os << setw(2) << static_cast(v->dhash.hash[k]); + m_os << endl; + } + + break; + } + + default: + m_os << "KEY TYPE UNKNOWN" << endl; + + DumpBTEntry_Unk(key_ptr, key_len, val_ptr, val_len, index); + break; + } + + m_os << hex; +} + +void BlockDumper::DumpBTEntry_OMap(const void *key_ptr, size_t key_len, const void *val_ptr, size_t val_len, bool index) +{ + (void)key_len; + (void)val_len; + + const omap_key_t *ekey = reinterpret_cast(key_ptr); + + m_os << ekey->ok_oid << " "; + m_os << ekey->ok_xid << " => "; + + if (index) + { + m_os << *reinterpret_cast(val_ptr) << endl; + } + else + { + if (!val_ptr) + { + m_os << "(NULL)" << endl; + } + else + { + const omap_val_t *val = reinterpret_cast(val_ptr); + + m_os << val->ov_flags << " "; + m_os << "[" << flagstr(val->ov_flags, fd_ov_flags) << "] "; + m_os << val->ov_size << " "; + m_os << val->ov_paddr << endl; + } + } +} + +void BlockDumper::DumpBTEntry_APFS_ExtentRef(const void *key_ptr, size_t key_len, const void *val_ptr, size_t val_len, bool index) +{ + (void)key_len; + (void)val_len; + + assert(key_len == sizeof(uint64_t)); + uint64_t key = *reinterpret_cast(key_ptr); + uint16_t type = key >> OBJ_TYPE_SHIFT; + + key &= OBJ_ID_MASK; + + if (type != APFS_TYPE_EXTENT) + { + m_os << "!!! type != APFS_TYPE_EXTENT !!!" << endl; + DumpBTEntry_Unk(key_ptr, key_len, val_ptr, val_len, index); + return; + } + + m_os << "Extent " << key << " => "; + + if (index) + { + assert(val_len == sizeof(oid_t)); + m_os << *reinterpret_cast(val_ptr) << endl; + } + else + { + if (!val_ptr) + { + m_os << "(NULL)" << endl; + } + else + { + assert(val_len == sizeof(j_phys_ext_val_t)); + const j_phys_ext_val_t *val = reinterpret_cast(val_ptr); + uint64_t len; + uint16_t kind; + + kind = val->len_and_kind >> PEXT_KIND_SHIFT; + len = val->len_and_kind & PEXT_LEN_MASK; + + m_os << kind << "/" << len << " "; + m_os << val->owning_obj_id << " "; + m_os << val->refcnt << endl; + } + } +} + +void BlockDumper::DumpBTEntry_APFS_SnapMeta(const void *key_ptr, size_t key_len, const void *val_ptr, size_t val_len, bool index) +{ + (void)key_len; + (void)val_len; + + uint64_t key = *reinterpret_cast(key_ptr); + + switch (key >> OBJ_TYPE_SHIFT) + { + case APFS_TYPE_SNAP_METADATA: + { + m_os << "SnapMeta " << key; + break; + } + case APFS_TYPE_SNAP_NAME: + { + const j_snap_name_key_t *k = reinterpret_cast(key_ptr); + m_os << "SnapName " << key << " '" << k->name << "'"; + break; + } + } + + m_os << " => "; + + if (!val_ptr) + { + m_os << "(NULL)" << endl; + } + else + { + if (index) + { + assert(val_len == sizeof(oid_t)); + m_os << setw(16) << *reinterpret_cast(val_ptr) << endl; + } + else + { + switch (key >> OBJ_TYPE_SHIFT) + { + case APFS_TYPE_SNAP_METADATA: + { + const j_snap_metadata_val_t *val = reinterpret_cast(val_ptr); + m_os << setw(16) << val->extentref_tree_oid << " "; + m_os << setw(16) << val->sblock_oid << " "; + // m_os << setw(16) << val->change_time << " "; + // m_os << setw(16) << val->create_time << " "; + m_os << "[" << tstamp(val->change_time) << "] "; + m_os << "[" << tstamp(val->create_time) << "] "; + m_os << setw(16) << val->inum << " "; + m_os << setw(8) << val->extentref_tree_type << " "; + m_os << setw(8) << val->flags << " "; + m_os << "'" << val->name << "'"; + break; + } + case APFS_TYPE_SNAP_NAME: + { + const j_snap_name_val_t *val = reinterpret_cast(val_ptr); + m_os << setw(16) << val->snap_xid; + break; + } + } + } + } + m_os << endl; +} + +void BlockDumper::DumpBTEntry_OMap_Snapshot(const void *key_ptr, size_t key_len, const void *val_ptr, size_t val_len, bool index) +{ + // OMAP-Snapshot ? + + (void)key_len; + (void)val_len; + (void)index; + + assert(key_len == sizeof(uint64_t)); + + m_os << *reinterpret_cast(key_ptr) << " => "; + + if (index) + { + assert(val_len == sizeof(oid_t)); + m_os << *reinterpret_cast(val_ptr); + } + else + { + assert(val_len == sizeof(omap_snapshot_t)); + const omap_snapshot_t *oms = reinterpret_cast(val_ptr); + + m_os << oms->oms_flags << " [" << flagstr(oms->oms_flags, fd_oms_flags) << "] " << oms->oms_pad << " " << oms->oms_oid; + } + + // DumpHex(key_ptr, 0x08); + // DumpHex(val_ptr, 0x10); + m_os << endl; +} + +void BlockDumper::DumpBTEntry_FreeList(const void *key_ptr, size_t key_len, const void *val_ptr, size_t val_len, bool index) +{ + (void)key_len; + (void)val_len; + (void)index; + + const spaceman_free_queue_key_t *key = reinterpret_cast(key_ptr); + + assert(key_len == sizeof(spaceman_free_queue_key_t)); + assert(val_len == sizeof(uint64_t)); + + m_os << key->sfqk_xid << " "; + m_os << key->sfqk_paddr << " => "; + + if (!val_ptr) + m_os << "1/NULL" << endl; + else + m_os << *reinterpret_cast(val_ptr) << endl; +} + +void BlockDumper::DumpBTEntry_GBitmap(const void *key_ptr, size_t key_len, const void *val_ptr, size_t val_len, bool index) +{ + const uint64_t *k; + const uint64_t *v; + + (void)key_len; + (void)val_len; + (void)index; + + assert(key_len == sizeof(uint64_t)); + assert(val_len == sizeof(uint64_t)); + + k = reinterpret_cast(key_ptr); + v = reinterpret_cast(val_ptr); + + m_os << *k << " => " << *v << std::endl; +} + +void BlockDumper::DumpBTEntry_FusionMT(const void* key_ptr, size_t key_len, const void* val_ptr, size_t val_len, bool index) +{ + const fusion_mt_key_t *key = reinterpret_cast(key_ptr); + + m_os << key->paddr << " => "; + + if (index) + { + m_os << *reinterpret_cast(val_ptr) << endl; + } + else + { + const fusion_mt_val_t *val = reinterpret_cast(val_ptr); + + m_os << val->fmv_lba << ' ' << val->fmv_length << ' ' << val->fmv_flags << " [" << flagstr(val->fmv_flags, fd_mt_flags) << "]" << endl; + } +} + +void BlockDumper::DumpBTEntry_FExtTree(const void* key_ptr, size_t key_len, const void* val_ptr, size_t val_len, bool index) +{ + const fext_tree_key_t* key = reinterpret_cast(key_ptr); + + m_os << key->private_id << ' ' << key->logical_addr << " => "; + + if (index) + m_os << *reinterpret_cast(val_ptr) << endl; + else { + const fext_tree_val_t* val = reinterpret_cast(val_ptr); + m_os << val->len_and_flags << ' ' << val->phys_block_num << endl; + } +} + +void BlockDumper::DumpBTEntry_Unk(const void *key_ptr, size_t key_len, const void *val_ptr, size_t val_len, bool index) +{ + (void)index; + + m_os << "Key: " << std::endl; + DumpHex(reinterpret_cast(key_ptr), key_len); + m_os << "Value: " << std::endl; + DumpHex(reinterpret_cast(val_ptr), val_len); + m_os << std::endl; +} + +void BlockDumper::DumpBTIndex(const void* val_ptr, uint16_t val_len) +{ + const btn_index_node_val_t *binv = reinterpret_cast(val_ptr); + + m_os << binv->binv_child_oid; + if (val_len > 8) { + m_os << " "; + for (uint16_t n = 8; n < val_len; n++) + m_os << setw(2) << static_cast(binv->binv_child_hash[n - 8]); + } + m_os << endl; +} + +void BlockDumper::Dump_XF(const uint8_t * xf_data, size_t xf_size, bool drec) +{ + const xf_blob_t *h = reinterpret_cast(xf_data); + const x_field_t *e = reinterpret_cast(h->xf_data); + uint16_t entry_base = h->xf_num_exts * sizeof(x_field_t) + sizeof(xf_blob_t); + uint16_t k; + const uint8_t *data; + + if (xf_size < 4) + { + m_os << " [!!!XF size too small!!!]" << endl; + return; + } + + m_os << " XF: " << h->xf_num_exts << " " << h->xf_used_data << " : "; + + for (k = 0; k < h->xf_num_exts; k++) + { + m_os << setw(2) << static_cast(e[k].x_type) << " "; + m_os << setw(2) << static_cast(e[k].x_flags) << " "; + m_os << setw(4) << e[k].x_size << " : "; + } + + for (k = 0; k < h->xf_num_exts; k++) + { + data = xf_data + entry_base; + + if (drec) + { + switch (e[k].x_type) + { + case DREC_EXT_TYPE_SIBLING_ID: + m_os << "[SIB_ID] " << *reinterpret_cast(data); + break; + default: + m_os << "[!!!UNKNOWN!!!] "; + DumpHex(data, e[k].x_size, e[k].x_size); + } + } + else + { + switch (e[k].x_type) + { + case INO_EXT_TYPE_SNAP_XID: + m_os << "[SNAP_XID] " << *reinterpret_cast(data); + break; + + case INO_EXT_TYPE_DELTRA_TREE_OID: + m_os << "[DELTA_TREE_OID] " << *reinterpret_cast(data); + break; + + case INO_EXT_TYPE_DOCUMENT_ID: + m_os << "[DOC_ID] " << *reinterpret_cast(data); + break; + + case INO_EXT_TYPE_NAME: + m_os << "[NAME] '" << data << "'"; + break; + + case INO_EXT_TYPE_PREV_FSIZE: + m_os << "[PREV_FSIZE] " << *reinterpret_cast(data); + break; + + case INO_EXT_TYPE_FINDER_INFO: + m_os << "[FINDER_INFO] ... "; // TODO + break; + + case INO_EXT_TYPE_DSTREAM: + { + const j_dstream_t *ft = reinterpret_cast(data); + m_os << "[DSTREAM] "; + m_os << ft->size << " "; + m_os << ft->alloced_size << " "; + m_os << ft->default_crypto_id << " "; + m_os << ft->total_bytes_written << " "; + m_os << ft->total_bytes_read; + break; + } + + case INO_EXT_TYPE_DIR_STATS_KEY: + m_os << "[DIR_STATS] !!! " << *reinterpret_cast(data); + break; + + case INO_EXT_TYPE_FS_UUID: + m_os << "[FS_UUID] " << uuidstr(*reinterpret_cast(data)); + break; + + case INO_EXT_TYPE_SPARSE_BYTES: + m_os << "[SPARSE] " << *reinterpret_cast(data); + break; + + case INO_EXT_TYPE_RDEV: + m_os << "[RDEV] " << *reinterpret_cast(data); + break; + + default: + m_os << "[!!!UNKNOWN!!!] "; + DumpHex(data, e[k].x_size, e[k].x_size); + } + + entry_base += ((e[k].x_size + 7) & 0xFFF8); + + if (k < (h->xf_num_exts - 1)) + m_os << " : "; + } + } +} + +void BlockDumper::DumpBlk_APSB() +{ + const apfs_superblock_t * const sb = reinterpret_cast(m_block); + int k; + + m_os << hex; + m_os << "magic : " << setw(8) << sb->apfs_magic << endl; + m_os << "fs_index : " << setw(8) << sb->apfs_fs_index << endl; + m_os << "features : " << setw(16) << sb->apfs_features << " [" << flagstr(sb->apfs_features, fd_apfs_features) << "]" << endl; + m_os << "ro_compat_feat : " << setw(16) << sb->apfs_readonly_compatible_features << " [" << flagstr(sb->apfs_readonly_compatible_features, fd_apfs_rocompat) << "]" << endl; + m_os << "incompat_feat : " << setw(16) << sb->apfs_incompatible_features << " [" << flagstr(sb->apfs_incompatible_features, fd_apfs_incompat) << "]" << endl; + m_os << "unmount_time : " << tstamp(sb->apfs_unmount_time) << endl; + m_os << "reserve_blk_cnt : " << setw(16) << sb->apfs_fs_reserve_block_count << endl; + m_os << "quota_blk_cnt : " << setw(16) << sb->apfs_fs_quota_block_count << endl; + m_os << "alloc_count : " << setw(16) << sb->apfs_fs_alloc_count << endl; + m_os << " major_ver : " << setw(4) << sb->apfs_meta_crypto.major_version << endl; + m_os << " minor_ver : " << setw(4) << sb->apfs_meta_crypto.minor_version << endl; + m_os << " cpflags : " << setw(8) << sb->apfs_meta_crypto.cpflags << endl; + m_os << " persistent_cls : " << setw(8) << sb->apfs_meta_crypto.persistent_class << endl; + m_os << " key_os_ver : " << setw(8) << sb->apfs_meta_crypto.key_os_version << endl; + m_os << " key_os_rev : " << setw(4) << sb->apfs_meta_crypto.key_revision << endl; + m_os << " unused : " << setw(4) << sb->apfs_meta_crypto.unused << endl; + m_os << "root_tree_type : " << setw(8) << sb->apfs_root_tree_type << endl; + m_os << "extentref_tree_t : " << setw(8) << sb->apfs_extentref_tree_type << endl; + m_os << "snap_meta_tree_t : " << setw(8) << sb->apfs_snap_meta_tree_type << endl; + m_os << "omap_oid : " << setw(16) << sb->apfs_omap_oid << endl; + m_os << "root_tree_oid : " << setw(16) << sb->apfs_root_tree_oid << endl; + m_os << "extentref_tree_o : " << setw(16) << sb->apfs_extentref_tree_oid << endl; + m_os << "snap_meta_tree_o : " << setw(16) << sb->apfs_snap_meta_tree_oid << endl; + m_os << "revert_to_xid : " << setw(16) << sb->apfs_revert_to_xid << endl; + m_os << "revert_to_sb_oid : " << setw(16) << sb->apfs_revert_to_sblock_oid << endl; + m_os << "next_obj_id : " << setw(16) << sb->apfs_next_obj_id << endl; + m_os << "num_files : " << setw(16) << sb->apfs_num_files << endl; + m_os << "num_directories : " << setw(16) << sb->apfs_num_directories << endl; + m_os << "num_symlinks : " << setw(16) << sb->apfs_num_symlinks << endl; + m_os << "num_other_fsobjs : " << setw(16) << sb->apfs_num_other_fsobjects << endl; + m_os << "num_snapshots : " << setw(16) << sb->apfs_num_snapshots << endl; + m_os << "total_blocks_alc : " << setw(16) << sb->apfs_total_blocks_alloced << endl; + m_os << "total_blocks_frd : " << setw(16) << sb->apfs_total_blocks_freed << endl; + m_os << "vol_uuid : " << uuidstr(sb->apfs_vol_uuid) << endl; + m_os << "last_mod_time : " << tstamp(sb->apfs_last_mod_time) << endl; + m_os << "fs_flags : " << setw(16) << sb->apfs_fs_flags << " [" << flagstr(sb->apfs_fs_flags, fd_apfs_fs_flags) << "]" << endl; + m_os << "formatted_by id : " << sb->apfs_formatted_by.id << endl; + m_os << " timestamp : " << tstamp(sb->apfs_formatted_by.timestamp) << endl; + m_os << " last_xid : " << setw(16) << sb->apfs_formatted_by.last_xid << endl; + for (k = 0; k < 8; k++) + { + m_os << "modified_by id : " << sb->apfs_modified_by[k].id << endl; + m_os << " timestamp : " << tstamp(sb->apfs_modified_by[k].timestamp) << endl; + m_os << " last_xid : " << setw(16) << sb->apfs_modified_by[k].last_xid << endl; + } + m_os << "volname : " << sb->apfs_volname << endl; + m_os << "next_doc_id : " << setw(8) << sb->apfs_next_doc_id << endl; + m_os << "role : " << setw(4) << sb->apfs_role << endl; + m_os << "reserved : " << setw(4) << sb->reserved << endl; + m_os << "root_to_xid : " << setw(16) << sb->apfs_root_to_xid << endl; + m_os << "er_state_oid : " << setw(16) << sb->apfs_er_state_oid << endl; + m_os << "cloneinfo_epoch : " << setw(16) << sb->apfs_cloneinfo_id_epoch << endl; + m_os << "cloneinfo_xid : " << setw(16) << sb->apfs_cloneinfo_xid << endl; + m_os << "snap_meta_ext_oid: " << setw(16) << sb->apfs_snap_meta_ext_oid << endl; + m_os << "volume_group_id : " << uuidstr(sb->apfs_volume_group_id) << endl; + m_os << "integrity_meta_oi: " << setw(16) << sb->apfs_integrity_meta_oid << endl; + m_os << "fext_tree_oid : " << setw(16) << sb->apfs_fext_tree_oid << endl; + m_os << "fext_tree_type : " << setw(8) << sb->apfs_fext_tree_type << endl; + m_os << "reserved_type : " << setw(8) << sb->reserved_type << endl; + m_os << "reserved_oid : " << setw(16) << sb->reserved_oid << endl; + + m_os << endl; + + if (!IsZero(m_block + sizeof(apfs_superblock_t), m_blocksize - sizeof(apfs_superblock_t))) + { + m_os << "!!! ADDITIONAL DATA !!!" << endl; + DumpBlockHex(); + } +} + +void BlockDumper::DumpBlk_CAB() +{ + const cib_addr_block_t * const cab = reinterpret_cast(m_block); + + size_t k; + size_t cnt; + + dumpm("index ", cab, cab->cab_index); + dumpm("cib_count ", cab, cab->cab_cib_count); + + m_os << endl; + + cnt = cab->cab_cib_count; + + for (k = 0; k < cnt; k++) + dumpm("cib_addr ", cab, cab->cab_cib_addr[k]); +} + +void BlockDumper::DumpBlk_CIB() +{ + const chunk_info_block_t * const cib = reinterpret_cast(m_block); + + size_t k; + + dumpm("index ", cib, cib->cib_index); + dumpm("chunk_count", cib, cib->cib_chunk_info_count); + m_os << endl; + + m_os << "Xid | Offset | Bits Tot | Bits Avl | Block" << endl; + m_os << "-----------------+------------------+----------+----------+-----------------" << endl; + + for (k = 0; k < cib->cib_chunk_info_count; k++) + { + m_os << setw(16) << cib->cib_chunk_info[k].ci_xid << " | "; + m_os << setw(16) << cib->cib_chunk_info[k].ci_addr << " | "; + m_os << setw(8) << cib->cib_chunk_info[k].ci_block_count << " | "; + m_os << setw(8) << cib->cib_chunk_info[k].ci_free_count << " | "; + m_os << setw(16) << cib->cib_chunk_info[k].ci_bitmap_addr << endl; + } +} + +void BlockDumper::DumpBlk_OM() +{ + const omap_phys_t * const om = reinterpret_cast(m_block); + + dumpm("flags ", om, om->om_flags); + dumpm("snap_count ", om, om->om_snap_count); + dumpm("tree_type ", om, om->om_tree_type); + dumpm("snap_tree_type ", om, om->om_snapshot_tree_type); + dumpm("tree_oid ", om, om->om_tree_oid); + dumpm("snap_tree_oid ", om, om->om_snapshot_tree_oid); + dumpm("most_recent_snp", om, om->om_most_recent_snap); + dumpm("pending_rev_min", om, om->om_pending_revert_min); + dumpm("pending_rev_max", om, om->om_pending_revert_max); + /* AA98B -> tree = AA9A6, unk_38=AAF85, unk_40=0650 */ + /* AAF85 -> oms btree */ + + /* + + m_os << "Type 1 | Type 2 | Block" << endl; + m_os << "---------+----------+-----------------" << endl; + + m_os << setw(8) << blk->entry[0].type_1 << " | "; + m_os << setw(8) << blk->entry[0].type_2 << " | "; + m_os << setw(16) << blk->entry[0].blk << endl; + */ + + m_os << endl; + + if (!IsZero(m_block + sizeof(omap_phys_t), m_blocksize - sizeof(omap_phys_t))) + { + m_os << "!!! ADDITIONAL DATA !!!" << endl; + DumpBlockHex(); + } +} + +void BlockDumper::DumpBlk_CPM() +{ + const checkpoint_map_phys_t * const cpm = reinterpret_cast(m_block); + + size_t k; + + dumpm("cpm_flags", cpm, cpm->cpm_flags); + dumpm("cpm_count", cpm, cpm->cpm_count); + m_os << endl; + + m_os << "Type | Subtype | Size | Pad | FS-OID | OID | PAddr" << endl; + m_os << "---------+----------+----------+----------+------------------+------------------+-----------------" << endl; + + for (k = 0; k < cpm->cpm_count; k++) + { + m_os << setw(8) << cpm->cpm_map[k].cpm_type << " | "; + m_os << setw(8) << cpm->cpm_map[k].cpm_subtype << " | "; + m_os << setw(8) << cpm->cpm_map[k].cpm_size << " | "; + m_os << setw(8) << cpm->cpm_map[k].cpm_pad << " | "; + m_os << setw(16) << cpm->cpm_map[k].cpm_fs_oid << " | "; + m_os << setw(16) << cpm->cpm_map[k].cpm_oid << " | "; + m_os << setw(16) << cpm->cpm_map[k].cpm_paddr << endl; + } +} + +void BlockDumper::DumpBlk_NXSB() +{ + const nx_superblock_t * const nx = reinterpret_cast(m_block); + size_t k; + + m_os << hex; + dumpm("magic ", nx, nx->nx_magic); + dumpm("block_size ", nx, nx->nx_block_size); + dumpm("block_count ", nx, nx->nx_block_count); + dumpm("features ", nx, nx->nx_features, false); + m_os << " [" << flagstr(nx->nx_features, fd_nx_feat) << "]" << endl; + dumpm("ro_compat_feat's", nx, nx->nx_readonly_compatible_features, false); + m_os << " [" << flagstr(nx->nx_readonly_compatible_features, fd_nx_rocompat) << "]" << endl; + dumpm("incompat_feat's ", nx, nx->nx_incompatible_features, false); + m_os << " [" << flagstr(nx->nx_incompatible_features, fd_nx_incompat) << "]" << endl; + dumpm("uuid ", nx, nx->nx_uuid); + dumpm("next_oid ", nx, nx->nx_next_oid); + dumpm("next_xid ", nx, nx->nx_next_xid); + dumpm("xp_desc_blocks ", nx, nx->nx_xp_desc_blocks); + dumpm("xp_data_blocks ", nx, nx->nx_xp_data_blocks); + dumpm("xp_desc_base ", nx, nx->nx_xp_desc_base); + dumpm("xp_data_base ", nx, nx->nx_xp_data_base); + dumpm("xp_desc_next ", nx, nx->nx_xp_desc_next); + dumpm("xp_data_next ", nx, nx->nx_xp_data_next); + dumpm("xp_desc_index ", nx, nx->nx_xp_desc_index); + dumpm("xp_desc_len ", nx, nx->nx_xp_desc_len); + dumpm("xp_data_index ", nx, nx->nx_xp_data_index); + dumpm("xp_data_len ", nx, nx->nx_xp_data_len); + dumpm("spaceman_oid ", nx, nx->nx_spaceman_oid); + dumpm("omap_oid ", nx, nx->nx_omap_oid); + dumpm("reaper_oid ", nx, nx->nx_reaper_oid); + dumpm("test_type ", nx, nx->nx_test_type); + dumpm("max_file_systems", nx, nx->nx_max_file_systems); + m_os << endl; + + for (k = 0; k < nx->nx_max_file_systems; k++) + if (nx->nx_fs_oid[k] != 0) + dumpm("fs_oid ", nx, nx->nx_fs_oid[k]); + + m_os << endl; + + for (k = 0; k < NX_NUM_COUNTERS; k++) + dumpm("nx_counter ", nx, nx->nx_counters[k]); + + dumpm("blocked_out_base", nx, nx->nx_blocked_out_prange.pr_start_addr); + dumpm("blocked_out_blks", nx, nx->nx_blocked_out_prange.pr_block_count); + dumpm("evict_map_tree ", nx, nx->nx_evict_mapping_tree_oid); + dumpm("flags ", nx, nx->nx_flags, false); + m_os << " [" << flagstr(nx->nx_flags, fd_nx_flags) << "]" << endl; + dumpm("efi_js_paddr ", nx, nx->nx_efi_jumpstart); + dumpm("fusion_uuid ", nx, nx->nx_fusion_uuid); + dumpm("keybag_base ", nx, nx->nx_keylocker.pr_start_addr); + dumpm("keybag_blocks ", nx, nx->nx_keylocker.pr_block_count); + dumpm("eph_info[0] ", nx, nx->nx_ephemeral_info[0]); + dumpm("eph_info[1] ", nx, nx->nx_ephemeral_info[1]); + dumpm("eph_info[2] ", nx, nx->nx_ephemeral_info[2]); + dumpm("eph_info[3] ", nx, nx->nx_ephemeral_info[3]); + dumpm("test_oid ", nx, nx->nx_test_oid); + dumpm("fusion_mt_oid ", nx, nx->nx_fusion_mt_oid); + dumpm("fusion_wbc_oid ", nx, nx->nx_fusion_wbc_oid); + dumpm("fusion_wbc.paddr", nx, nx->nx_fusion_wbc.pr_start_addr); + dumpm("fusion_wbc.cnt ", nx, nx->nx_fusion_wbc.pr_block_count); + dumpm("newest_mounted_v", nx, nx->nx_newest_mounted_version); + dumpm("mkb_locker.base ", nx, nx->nx_mkb_locker.pr_start_addr); + dumpm("mkb_locker.count", nx, nx->nx_mkb_locker.pr_block_count); + + m_os << endl; + + if (!IsZero(m_block + sizeof(nx_superblock_t), m_blocksize - sizeof(nx_superblock_t))) + { + m_os << "!!! ADDITIONAL DATA !!!" << endl; + DumpBlockHex(); + } + + DumpBlockHex(); +} + +void BlockDumper::DumpBlk_SM() +{ + size_t d; + size_t k; + size_t cnt; + const spaceman_phys_t *b = reinterpret_cast(m_block); + + static const char *devstr[2] = { "SD_MAIN", "SD_TIER2" }; + static const char *fqstr[3] = { "SFQ_IP", "SFQ_MAIN", "SFQ_TIER2" }; + + m_os << hex; + dumpm("block_size ", b, b->sm_block_size); + dumpm("blocks_per_chunk ", b, b->sm_blocks_per_chunk); + dumpm("chunks_per_cib ", b, b->sm_chunks_per_cib); + dumpm("cibs_per_cab ", b, b->sm_cibs_per_cab); + for (k = 0; k < SD_COUNT; k++) + { + m_os << "sm_dev[" << devstr[k] << "] {" << endl; + dumpm(" block_count ", b, b->sm_dev[k].sm_block_count); + dumpm(" chunk_count ", b, b->sm_dev[k].sm_chunk_count); + dumpm(" cib_count ", b, b->sm_dev[k].sm_cib_count); + dumpm(" cab_count ", b, b->sm_dev[k].sm_cab_count); + dumpm(" free_count ", b, b->sm_dev[k].sm_free_count); + dumpm(" addr_offset ", b, b->sm_dev[k].sm_addr_offset); + dumpm(" reserved ", b, b->sm_dev[k].sm_reserved); + dumpm(" reserved2 ", b, b->sm_dev[k].sm_reserved2); + m_os << "}" << endl; + } + dumpm("flags ", b, b->sm_flags); + dumpm("ip_bm_tx_multiplier ", b, b->sm_ip_bm_tx_multiplier); + dumpm("ip_block_count ", b, b->sm_ip_block_count); + dumpm("ip_bm_size_in_blocks", b, b->sm_ip_bm_size_in_blocks); + dumpm("ip_bm_block_count ", b, b->sm_ip_bm_block_count); + dumpm("ip_bm_base ", b, b->sm_ip_bm_base); + dumpm("ip_base ", b, b->sm_ip_base); + dumpm("fs_reserve_blk_cnt ", b, b->sm_fs_reserve_block_count); + dumpm("fs_reserve_alloc_cnt", b, b->sm_fs_reserve_alloc_count); + for (k = 0; k < SFQ_COUNT; k++) + { + m_os << "sm_fq[" << fqstr[k] << "] {" << endl; + dumpm(" count ", b, b->sm_fq[k].sfq_count); + dumpm(" tree_oid ", b, b->sm_fq[k].sfq_tree_oid); + dumpm(" oldest_xid ", b, b->sm_fq[k].sfq_oldest_xid); + dumpm(" tree_node_limit ", b, b->sm_fq[k].sfq_tree_node_limit); + dumpm(" pad16 ", b, b->sm_fq[k].sfq_pad16); + dumpm(" pad32 ", b, b->sm_fq[k].sfq_pad32); + dumpm(" reserved ", b, b->sm_fq[k].sfq_reserved); + m_os << "}" << endl; + } + + dumpm("ip_bm_free_head ", b, b->sm_ip_bm_free_head); + dumpm("ip_bm_free_tail ", b, b->sm_ip_bm_free_tail); + dumpm("ip_bm_xid_offset ", b, b->sm_ip_bm_xid_offset); + dumpm("ip_bitmap_offset ", b, b->sm_ip_bitmap_offset); + dumpm("ip_bm_free_next_offs", b, b->sm_ip_bm_free_next_offset); + dumpm("version ", b, b->sm_version); + dumpm("struct_size ", b, b->sm_struct_size); + m_os << endl; + + for (k = 0; k < SD_COUNT; k++) { + for (d = 0; d < SM_DATAZONE_ALLOCZONE_COUNT; d++) { + const spaceman_allocation_zone_info_phys_t &azip = b->sm_datazone.sdz_allocation_zones[k][d]; + m_os << "sdz_allocation_zones[" << k << "][" << d << "]:" << endl; + m_os << " saz_current_boundaries : " << setw(16) << azip.saz_current_boundaries.saz_zone_start << " " << setw(16) << azip.saz_current_boundaries.saz_zone_end << endl; + for (int n = 0; n < SM_ALLOCZONE_NUM_PREVIOUS_BOUNDARIES; n++) + m_os << " saz_previous_boundaries: " << setw(16) << azip.saz_previous_boundaries[n].saz_zone_start << " " << setw(16) << azip.saz_previous_boundaries[n].saz_zone_end << endl; + m_os << " saz_zone_id : " << setw(4) << azip.saz_zone_id << endl; + m_os << " saz_prev_boundary_idx : " << setw(4) << azip.saz_previous_boundary_index << endl; + m_os << " saz_reserved : " << setw(4) << azip.saz_reserved << endl; + } + } + + /* + for (k = 0; k < 0x10; k++) + dumpm("? ", b, b->unk_160[k]); + m_os << endl; + */ + + /* + const le *unk_arr = reinterpret_cast *>(m_block + b->sm_struct_size); + for (k = 0; k < b->ip_bitmap_block_count; k++) + dumpm("? ", b, unk_arr[k]); + */ + + for (d = 0; d < SD_COUNT; d++) + { + m_os << "Device " << devstr[d] << " blocks:" << std::endl; + if (b->sm_dev[d].sm_cab_count > 0) + cnt = b->sm_dev[d].sm_cab_count; + else + cnt = b->sm_dev[d].sm_cib_count; + + if (b->sm_dev[d].sm_addr_offset != 0 && cnt != 0) + { + const le_uint64_t *addr = reinterpret_cast(m_block + b->sm_dev[d].sm_addr_offset); + + for (k = 0; k < cnt; k++) + dumpm("addr ", b, addr[k]); + } + + m_os << endl; + } + +#if 0 + if (b->unk_144 != 0) + { + const le_uint64_t &unk_xid = *reinterpret_cast(m_block + b->unk_144); + dumpm("unk_144->? ", b, unk_xid); + } + + if (b->unk_148 != 0) + { + const le_uint64_t &unk_148 = *reinterpret_cast(m_block + b->unk_148); + dumpm("unk_148->? ", b, unk_148); + } + + // unk_14C and unk_154 also point to some data ... + + /* + + m_os << "0180 BID BmpHdr : " << setw(16) << b->blockid_vol_bitmap_hdr << endl; + m_os << "..." << endl; + m_os << "09D8 Some XID : " << setw(16) << b->some_xid_9D8 << endl; + m_os << "09E0 : " << setw(16) << b->unk_9E0 << endl; + for (k = 0; k < 0x10; k++) + m_os << setw(4) << (0x9E8 + 2 * k) << " : " << setw(4) << b->unk_9E8[k] << endl; + for (k = 0; k < 0xBF && b->bid_bmp_hdr_list[k] != 0; k++) + m_os << setw(4) << (0xA08 + 8 * k) << "cib_oid : " << setw(16) << b->bid_bmp_hdr_list[k] << endl; + + */ +#endif + + m_os << endl; + DumpBlockHex(); +} + +void BlockDumper::DumpBlk_NR() +{ + const nx_reaper_phys_t *nr = reinterpret_cast(m_block); + + m_os << hex; + + dumpm("next_reap_id ", nr, nr->nr_next_reap_id); + dumpm("completed_id ", nr, nr->nr_completed_id); + dumpm("head ", nr, nr->nr_head); + dumpm("tail ", nr, nr->nr_tail); + dumpm("flags ", nr, nr->nr_flags); + dumpm("rlcount ", nr, nr->nr_rlcount); + dumpm("type ", nr, nr->nr_type); + dumpm("size ", nr, nr->nr_size); + dumpm("oid ", nr, nr->nr_oid); + dumpm("xid ", nr, nr->nr_xid); + dumpm("nrle_flags ", nr, nr->nr_nrle_flags); + dumpm("state_buf_size ", nr, nr->nr_state_buffer_size); + + m_os << endl; + DumpBlockHex(); +} + +void BlockDumper::DumpBlk_NRL() +{ + const nx_reap_list_phys_t *nrl = reinterpret_cast(m_block); + uint32_t k; + + m_os << hex; + m_os << "next : " << setw(16) << nrl->nrl_next << endl; + m_os << "flags : " << setw(8) << nrl->nrl_flags << endl; + m_os << "max : " << setw(8) << nrl->nrl_max << endl; + m_os << "count : " << setw(8) << nrl->nrl_count << endl; + m_os << "first : " << setw(8) << nrl->nrl_first << endl; + m_os << "last : " << setw(8) << nrl->nrl_last << endl; + m_os << "free : " << setw(8) << nrl->nrl_free << endl; + m_os << endl; + + m_os << "next | flags | type | size | fs_oid | oid | xid" << endl; + m_os << "---------+----------+----------+----------+------------------+------------------+-----------------" << endl; + for (k = 0; k < nrl->nrl_max; k++) + { + m_os << setw(8) << nrl->nrl_entries[k].nrle_next << " | "; + m_os << setw(8) << nrl->nrl_entries[k].nrle_flags << " | "; + m_os << setw(8) << nrl->nrl_entries[k].nrle_type << " | "; + m_os << setw(8) << nrl->nrl_entries[k].nrle_size << " | "; + m_os << setw(16) << nrl->nrl_entries[k].nrle_fs_oid << " | "; + m_os << setw(16) << nrl->nrl_entries[k].nrle_oid << " | "; + m_os << setw(16) << nrl->nrl_entries[k].nrle_xid << endl; + } + + m_os << endl; + DumpBlockHex(); +} + +void BlockDumper::DumpBlk_JSDR() +{ + const nx_efi_jumpstart_t *js = reinterpret_cast(m_block); + + dumpm("magic ", js, js->nej_magic); + dumpm("version ", js, js->nej_version); + dumpm("efi_file_len ", js, js->nej_efi_file_len); + dumpm("num_extents ", js, js->nej_num_extents); + + for (size_t k = 0; k < js->nej_num_extents; k++) + { + dumpm("apfs.efi base ", js, js->nej_rec_extents[k].pr_start_addr); + dumpm("apfs.efi blocks ", js, js->nej_rec_extents[k].pr_block_count); + } + + m_os << endl; + + DumpBlockHex(); +} + +void BlockDumper::DumpBlk_ER() +{ + const er_state_phys_t *er = reinterpret_cast(m_block); + + dumpm("magic ", er, er->ersb_header.ersb_magic); + dumpm("version ", er, er->ersb_header.ersb_version); + + if (er->ersb_header.ersb_version == 1) + { + const er_state_phys_v1_t *er1 = reinterpret_cast(m_block); + + dumpm("flags ", er1, er1->ersb_flags); + dumpm("snap_xid ", er1, er1->ersb_snap_xid); + dumpm("cur_fext_obj_id ", er1, er1->ersb_current_fext_obj_id); + dumpm("file_offset ", er1, er1->ersb_file_offset); + dumpm("fext_pbn ", er1, er1->ersb_fext_pbn); + dumpm("paddr ", er1, er1->ersb_paddr); + dumpm("progress ", er1, er1->ersb_progress); + dumpm("total_blk_to_encr", er1, er1->ersb_total_blk_to_encrypt); + dumpm("blockmap_oid ", er1, er1->ersb_blockmap_oid); + dumpm("checksum_count ", er1, er1->ersb_checksum_count); + dumpm("reserved ", er1, er1->ersb_reserved); + dumpm("fext_cid ", er1, er1->ersb_fext_cid); + m_os << "checksum : " << hexstr(er1->ersb_checksum, 8) << std::endl; + } + else + { + dumpm("flags ", er, er->ersb_flags); + dumpm("snap_xid ", er, er->ersb_snap_xid); + dumpm("cur_fext_obj_id ", er, er->ersb_current_fext_obj_id); + dumpm("file_offset ", er, er->ersb_file_offset); + dumpm("progress ", er, er->ersb_progress); + dumpm("total_blk_to_encr", er, er->ersb_total_blk_to_encrypt); + dumpm("blockmap_oid ", er, er->ersb_blockmap_oid); + dumpm("tidemark_obj_id ", er, er->ersb_tidemark_obj_id); + dumpm("rec_extents_count", er, er->ersb_recovery_extents_count); + dumpm("recovery_list_oid", er, er->ersb_recovery_list_oid); + dumpm("recovery_length ", er, er->ersb_recovery_length); + } + + // dumpm(" ", er, er->ersb_checksum); + + DumpBlockHex(); +} + +void BlockDumper::DumpBlk_WBC() +{ + const fusion_wbc_phys_t *wbc = reinterpret_cast(m_block); + + dumpm("version ", wbc, wbc->fwp_version); + dumpm("listHeadOid ", wbc, wbc->fwp_listHeadOid); + dumpm("listTailOid ", wbc, wbc->fwp_listTailOid); + dumpm("stableHeadOffset", wbc, wbc->fwp_stableHeadOffset); + dumpm("stableTailOffset", wbc, wbc->fwp_stableTailOffset); + dumpm("listBlocksCount ", wbc, wbc->fwp_listBlocksCount); + dumpm("reserved ", wbc, wbc->fwp_reserved); + dumpm("usedByRC ", wbc, wbc->fwp_usedByRC); + dumpm("rcStash base ", wbc, wbc->fwp_rcStash.pr_start_addr); + dumpm("rcStash count ", wbc, wbc->fwp_rcStash.pr_block_count); + + // DumpBlockHex(); +} + +void BlockDumper::DumpBlk_WBCL() +{ + const fusion_wbc_list_phys_t *wbcl = reinterpret_cast(m_block); + uint32_t k; + + dumpm("version ", wbcl, wbcl->fwlp_version); + dumpm("tailOffset ", wbcl, wbcl->fwlp_tailOffset); + dumpm("indexBegin ", wbcl, wbcl->fwlp_indexBegin); + dumpm("indexEnd ", wbcl, wbcl->fwlp_indexEnd); + dumpm("indexMax ", wbcl, wbcl->fwlp_indexMax); + dumpm("reserved ", wbcl, wbcl->fwlp_reserved); + m_os << endl; + m_os << "wbcLba | targetLba | length" << endl; + m_os << "-----------------+------------------+-----------------" << endl; + + for (k = wbcl->fwlp_indexBegin; k < wbcl->fwlp_indexEnd; k++) + { + m_os << setw(16) << wbcl->fwlp_listEntries[k].fwle_wbcLba << " | "; + m_os << setw(16) << wbcl->fwlp_listEntries[k].fwle_targetLba << " | "; + m_os << setw(16) << wbcl->fwlp_listEntries[k].fwle_length << endl; + } + + // DumpBlockHex(); +} + +void BlockDumper::DumpBlk_SnapMetaExt() +{ + const snap_meta_ext_obj_phys_t* sme = reinterpret_cast(m_block); + + dumpm("version ", sme, sme->smeop_sme.sme_version); + dumpm("flags ", sme, sme->smeop_sme.sme_flags); + dumpm("snap_xid ", sme, sme->smeop_sme.sme_snap_xid); + dumpm("uuid ", sme, sme->smeop_sme.sme_uuid); + dumpm("token ", sme, sme->smeop_sme.sme_token); + + DumpBlockHex(); +} + +void BlockDumper::DumpBlk_IntegrityMeta() +{ + const integrity_meta_phys_t* const im = reinterpret_cast(m_block); + + dumpm("version ", im, im->im_version); + dumpm("flags ", im, im->im_flags); + dumpm("hash_type ", im, im->im_hash_type); + dumpm("root_hash_offset", im, im->im_root_hash_offset); + dumpm("broken_xid ", im, im->im_broken_xid); + + DumpBlockHex(); +} + +void BlockDumper::DumpBTNode_0() +{ + const obj_phys_t * const hdr = reinterpret_cast(m_block); + + switch (hdr->o_subtype) + { + case OBJECT_TYPE_SPACEMAN_FREE_QUEUE: + DumpBTNode(&BlockDumper::DumpBTEntry_FreeList, 0x10, 0x08); + break; + + case OBJECT_TYPE_OMAP: + DumpBTNode(&BlockDumper::DumpBTEntry_OMap, 0x10, 0x10); + break; + + case OBJECT_TYPE_FSTREE: + DumpBTNode(&BlockDumper::DumpBTEntry_APFS_Root); + break; + + case OBJECT_TYPE_BLOCKREFTREE: + DumpBTNode(&BlockDumper::DumpBTEntry_APFS_ExtentRef); + break; + + case OBJECT_TYPE_SNAPMETATREE: + DumpBTNode(&BlockDumper::DumpBTEntry_APFS_SnapMeta); + break; + + case OBJECT_TYPE_OMAP_SNAPSHOT: + DumpBTNode(&BlockDumper::DumpBTEntry_OMap_Snapshot, 0x8, 0x10); + break; + + case OBJECT_TYPE_GBITMAP_TREE: + DumpBTNode(&BlockDumper::DumpBTEntry_GBitmap, 8, 8); + break; + + case OBJECT_TYPE_FUSION_MIDDLE_TREE: + DumpBTNode(&BlockDumper::DumpBTEntry_FusionMT, 8, 16); + break; + + case OBJECT_TYPE_FEXT_TREE: + DumpBTNode(&BlockDumper::DumpBTEntry_FExtTree, 16, 16); + break; + + default: + DumpBTNode(&BlockDumper::DumpBTEntry_Unk); + break; + } +} + +void BlockDumper::DumpBlockHex() +{ + unsigned int sz = 0xFFF; + + while (sz > 0 && m_block[sz] == 0) + sz--; + + sz = (sz + 0x10) & 0xFFFFFFF0; + + ::DumpHex(m_os, m_block, sz); +} + +void BlockDumper::DumpHex(const uint8_t * data, size_t size, size_t line_size) +{ + ::DumpHex(m_os, data, size, line_size); +} + +std::string BlockDumper::flagstr(uint64_t flag, const FlagDesc *desc) +{ + size_t k; + bool notfirst = false; + std::string st; + + for (k = 0; desc[k].desc != 0; k++) + { + if (flag & desc[k].flag) + { + if (notfirst) + st += ", "; + st += desc[k].desc; + notfirst = true; + } + } + + return st; +} + +std::string BlockDumper::enumstr(uint64_t flag, const FlagDesc *desc) +{ + size_t k; + std::string st; + + for (k = 0; desc[k].desc != 0; k++) + { + if (flag == desc[k].flag) + { + st = desc[k].desc; + break; + } + } + + return st; +} + +const char * BlockDumper::GetNodeType(uint32_t type, uint32_t subtype) +{ + const char *typestr = "Unknown"; + const char *names[0x21] = { + "0", + "Container Superblock", + "B-Tree", + "B-Tree Node", + "M-Tree", + "Spaceman", + "Spaceman CIB Address Block", + "Spaceman Chunk Info Block", + "Spaceman Bitmap", + "Spaceman Free Queue", + "Extent List Tree", + "Object Map", + "Checkpoint Map", + "Volume Superblock", + "Filesystem Tree", + "Block Ref Tree", + "Snap Meta Tree", + "NX Reaper", + "NX Reap List", + "OMap Snapshot", + "EFI Jumpstart", + "Fusion Middle Tree", + "Fusion WBC", + "Fusion WBC List", + "ER State", + "G Bitmap", + "G Bitmap Tree", + "G Bitmap Block", + "ER Recovery Block", + "Snap Meta Ext", + "Integrity Meta", + "Fext Tree", + "Reserved 20" + }; + + int t = type & OBJECT_TYPE_MASK; + + if (t == OBJECT_TYPE_BTREE || t == OBJECT_TYPE_BTREE_NODE) + t = subtype; + + if (t < 0x21) + typestr = names[t]; + + return typestr; +} + +std::string BlockDumper::tstamp(uint64_t apfs_time) +{ + struct tm gmt; + time_t secs; + uint32_t nanos; + std::stringstream st; + + nanos = apfs_time % 1000000000ULL; + secs = apfs_time / 1000000000ULL; + +#ifdef _MSC_VER + gmtime_s(&gmt, &secs); +#else + gmtime_r(&secs, &gmt); + // Unix variant ... +#endif + + st << setfill('0'); + st << setw(4) << (gmt.tm_year + 1900) << "-"; + st << setw(2) << (gmt.tm_mon + 1) << "-"; + st << setw(2) << gmt.tm_mday << " "; + st << setw(2) << gmt.tm_hour << ":"; + st << setw(2) << gmt.tm_min << ":"; + st << setw(2) << gmt.tm_sec << "."; + st << setw(9) << nanos; + + return st.str(); +} + +#define DUMP_OT + +void BlockDumper::dumpm(const char* name, const void *base, const uint8_t& v, bool lf) +{ +#ifdef DUMP_OT + ptrdiff_t d = reinterpret_cast(&v) - reinterpret_cast(base); + m_os << setw(4) << d << " u8 "; +#endif + + m_os << name << " : " << setw(2) << static_cast(v); + if (lf) + m_os << endl; +} + +void BlockDumper::dumpm(const char* name, const void *base, const le_uint16_t& v, bool lf) +{ +#ifdef DUMP_OT + ptrdiff_t d = reinterpret_cast(&v) - reinterpret_cast(base); + m_os << setw(4) << d << " u16 "; +#endif + + m_os << name << " : " << setw(4) << v; + if (lf) + m_os << endl; +} + +void BlockDumper::dumpm(const char* name, const void *base, const le_uint32_t& v, bool lf) +{ +#ifdef DUMP_OT + ptrdiff_t d = reinterpret_cast(&v) - reinterpret_cast(base); + m_os << setw(4) << d << " u32 "; +#endif + + m_os << name << " : " << setw(8) << v; + if (lf) + m_os << endl; +} + +void BlockDumper::dumpm(const char* name, const void *base, const le_uint64_t& v, bool lf) +{ +#ifdef DUMP_OT + ptrdiff_t d = reinterpret_cast(&v) - reinterpret_cast(base); + m_os << setw(4) << d << " u64 "; +#endif + + m_os << name << " : " << setw(16) << v; + if (lf) + m_os << endl; +} + +void BlockDumper::dumpm(const char* name, const void* base, const apfs_uuid_t& uuid) +{ +#ifdef DUMP_OT + ptrdiff_t d = reinterpret_cast(&uuid) - reinterpret_cast(base); + m_os << setw(4) << d << " uid "; +#endif + + m_os << name << " : " << setw(16) << uuidstr(uuid) << endl; +} diff --git a/vendor/apfs-fuse/ApfsLib/BlockDumper.h b/vendor/apfs-fuse/ApfsLib/BlockDumper.h new file mode 100644 index 0000000..0f9d0d1 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/BlockDumper.h @@ -0,0 +1,117 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#pragma once + +#include +#include +#include + +#include "Global.h" +#include "DiskStruct.h" + +struct FlagDesc +{ + uint64_t flag; + const char *desc; +}; + +class BlockDumper +{ +public: + BlockDumper(std::ostream &os, size_t blocksize); + ~BlockDumper(); + + void SetTextFlags(uint32_t flags) { m_text_flags = flags; } + size_t GetBlockSize() const { return m_blocksize; } + void SetBlockSize(size_t size) { m_blocksize = size; } + + void DumpNode(const uint8_t *block, uint64_t blk_nr); + + std::ostream &st() { return m_os; } + +private: + typedef void(BlockDumper::*DumpFunc)(const void *key_ptr, size_t key_len, const void *val_ptr, size_t val_len, bool index); + + void DumpNodeHeader(const obj_phys_t *blk, uint64_t blk_nr); + void DumpBTNode(DumpFunc func, uint16_t key_size = 0, uint16_t value_size = 0); + void DumpBTHeader(bool dump_offsets = false); + void DumpBTreeInfo(); + // void DumpTableHeader(const APFS_TableHeader &tbl); + + void DumpBlk_APSB(); + void DumpBlk_CAB(); + void DumpBlk_CIB(); + void DumpBlk_OM(); + void DumpBlk_CPM(); + void DumpBlk_NXSB(); + void DumpBlk_SM(); + void DumpBlk_NR(); + void DumpBlk_NRL(); + void DumpBlk_JSDR(); + void DumpBlk_ER(); + + void DumpBlk_WBC(); + void DumpBlk_WBCL(); + + void DumpBlk_SnapMetaExt(); + void DumpBlk_IntegrityMeta(); + + void DumpBTNode_0(); + + void DumpBTEntry_APFS_Root(const void *key_ptr, size_t key_len, const void *val_ptr, size_t val_len, bool index); + void DumpBTEntry_OMap(const void *key_ptr, size_t key_len, const void *val_ptr, size_t val_len, bool index); + void DumpBTEntry_APFS_ExtentRef(const void *key_ptr, size_t key_len, const void *val_ptr, size_t val_len, bool index); + void DumpBTEntry_APFS_SnapMeta(const void *key_ptr, size_t key_len, const void *val_ptr, size_t val_len, bool index); + void DumpBTEntry_OMap_Snapshot(const void *key_ptr, size_t key_len, const void *val_ptr, size_t val_len, bool index); + void DumpBTEntry_FreeList(const void *key_ptr, size_t key_len, const void *val_ptr, size_t val_len, bool index); + void DumpBTEntry_GBitmap(const void *key_ptr, size_t key_len, const void *val_ptr, size_t val_len, bool index); + void DumpBTEntry_FusionMT(const void *key_ptr, size_t key_len, const void *val_ptr, size_t val_len, bool index); + void DumpBTEntry_FExtTree(const void* key_ptr, size_t key_len, const void* val_ptr, size_t val_len, bool index); + + void DumpBTEntry_Unk(const void *key_ptr, size_t key_len, const void *val_ptr, size_t val_len, bool index); + + void DumpBTIndex(const void* val_ptr, uint16_t val_len); + + void Dump_XF(const uint8_t *xf_data, size_t xf_size, bool drec); + + void DumpBlockHex(); + void DumpHex(const uint8_t *data, size_t size, size_t line_size = 16); + + static std::string flagstr(uint64_t flag, const FlagDesc *desc); + static std::string enumstr(uint64_t flag, const FlagDesc *desc); + +public: + static const char * GetNodeType(uint32_t type, uint32_t subtype); + +private: + static std::string tstamp(uint64_t apfs_time); + + void dumpm(const char *name, const void *base, const uint8_t &v, bool lf = true); + void dumpm(const char *name, const void *base, const le_uint16_t &v, bool lf = true); + void dumpm(const char *name, const void *base, const le_uint32_t &v, bool lf = true); + void dumpm(const char *name, const void *base, const le_uint64_t &v, bool lf = true); + void dumpm(const char *name, const void *base, const apfs_uuid_t &uuid); + + uint32_t m_text_flags; // 00 - Alt, 01 - insensitive, 08 - sensitive + + std::ostream &m_os; + const uint8_t *m_block; + size_t m_blocksize; +}; diff --git a/vendor/apfs-fuse/ApfsLib/CheckPointMap.cpp b/vendor/apfs-fuse/ApfsLib/CheckPointMap.cpp new file mode 100644 index 0000000..2494324 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/CheckPointMap.cpp @@ -0,0 +1,86 @@ +#include + +#include "ApfsContainer.h" +#include "DiskStruct.h" +#include "BlockDumper.h" +#include "CheckPointMap.h" + +CheckPointMap::CheckPointMap(ApfsContainer& container) : m_container(container) +{ + m_cpm_oid = 0; + m_blksize = 0; +} + +CheckPointMap::~CheckPointMap() +{ +} + +bool CheckPointMap::Init(oid_t root_oid, uint32_t blk_count) +{ + uint32_t n; + const checkpoint_map_phys_t *cpm; + + m_blksize = m_container.GetBlocksize(); + m_cpm_data.resize(m_blksize * blk_count); + + for (n = 0; n < blk_count; n++) + { + if (!m_container.ReadAndVerifyHeaderBlock(m_cpm_data.data() + n * m_blksize, root_oid + n)) + { + m_cpm_data.clear(); + return false; + } + + cpm = reinterpret_cast(m_cpm_data.data() + m_blksize * n); + + assert((cpm->cpm_o.o_type & OBJECT_TYPE_MASK) == OBJECT_TYPE_CHECKPOINT_MAP); + + if ((cpm->cpm_o.o_type & OBJECT_TYPE_MASK) != OBJECT_TYPE_CHECKPOINT_MAP) + { + m_cpm_data.clear(); + return false; + } + } + + m_cpm_oid = root_oid; + + return true; +} + +bool CheckPointMap::Lookup(omap_res_t & res, oid_t oid, xid_t xid) +{ + uint32_t blk_offs; + uint32_t k; + uint32_t cnt; + const checkpoint_map_phys_t * cpm; + + (void)xid; + + for (blk_offs = 0; blk_offs < m_cpm_data.size(); blk_offs += m_blksize) + { + cpm = reinterpret_cast(m_cpm_data.data() + blk_offs); + + cnt = cpm->cpm_count; + + for (k = 0; k < cnt; k++) + { + if (oid == cpm->cpm_map[k].cpm_oid) + { + res.oid = cpm->cpm_map[k].cpm_oid; + res.xid = cpm->cpm_o.o_xid; + res.flags = 0; + res.size = cpm->cpm_map[k].cpm_size; + res.paddr = cpm->cpm_map[k].cpm_paddr; + + return true; + } + } + } + + return false; +} + +void CheckPointMap::dump(BlockDumper& bd) +{ + bd.DumpNode(m_cpm_data.data(), m_cpm_oid); +} diff --git a/vendor/apfs-fuse/ApfsLib/CheckPointMap.h b/vendor/apfs-fuse/ApfsLib/CheckPointMap.h new file mode 100644 index 0000000..37919be --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/CheckPointMap.h @@ -0,0 +1,27 @@ +#pragma once + +#include + +#include "DiskStruct.h" +#include "ApfsNodeMapper.h" + +class ApfsContainer; +class BlockDumper; + +class CheckPointMap : public ApfsNodeMapper +{ +public: + CheckPointMap(ApfsContainer &container); + virtual ~CheckPointMap(); + + bool Init(oid_t root_oid, uint32_t blk_count); + bool Lookup(omap_res_t & res, oid_t oid, xid_t xid) override; + + void dump(BlockDumper &bd); + +private: + ApfsContainer &m_container; + std::vector m_cpm_data; + oid_t m_cpm_oid; + uint32_t m_blksize; +}; diff --git a/vendor/apfs-fuse/ApfsLib/Crc32.cpp b/vendor/apfs-fuse/ApfsLib/Crc32.cpp new file mode 100644 index 0000000..fa9b7f2 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/Crc32.cpp @@ -0,0 +1,97 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#include "Crc32.h" + +Crc32::Crc32(bool reflect, uint32_t poly) +{ + unsigned int i; + uint32_t r; + unsigned int b; + + m_reflect = reflect; + m_crc = 0; + + if (reflect) { + poly = ((poly << 16) & 0xFFFF0000) | ((poly >> 16) & 0x0000FFFF); + poly = ((poly << 8) & 0xFF00FF00) | ((poly >> 8) & 0x00FF00FF); + poly = ((poly << 4) & 0xF0F0F0F0) | ((poly >> 4) & 0x0F0F0F0F); + poly = ((poly << 2) & 0xCCCCCCCC) | ((poly >> 2) & 0x33333333); + poly = ((poly << 1) & 0xAAAAAAAA) | ((poly >> 1) & 0x55555555); + + for (i = 0; i < 256; i++) { + r = i; + for (b = 0; b < 8; b++) { + if (r & 1) + r = (r >> 1) ^ poly; + else + r = (r >> 1); + } + m_table[i] = r; + } + } + else { + for (i = 0; i < 256; i++) { + r = (i << 24); + for (b = 0; b < 8; b++) { + if (r & 0x80000000) + r = (r << 1) ^ poly; + else + r = (r << 1); + } + m_table[i] = r; + } + } +} + +Crc32::~Crc32() +{ + +} + +void Crc32::Calc(const uint8_t *data, size_t size) +{ + size_t i; + + for (i = 0; i < size; i++) { + if (m_reflect) { + CalcLE(data[i]); + } + else { + CalcBE(data[i]); + } + } +} + +void Crc32::CalcLE(uint8_t b) +{ + m_crc = m_table[b ^ (m_crc & 0xFF)] ^ (m_crc >> 8); +} + +void Crc32::CalcBE(uint8_t b) +{ + m_crc = m_table[b ^ ((m_crc >> 24) & 0xFF)] ^ (m_crc << 8); +} + +uint32_t Crc32::GetDataCRC(const uint8_t *data, size_t size, uint32_t initialXor, uint32_t finalXor) +{ + m_crc = initialXor; + Calc(data, size); + return m_crc ^ finalXor; +} diff --git a/vendor/apfs-fuse/ApfsLib/Crc32.h b/vendor/apfs-fuse/ApfsLib/Crc32.h new file mode 100644 index 0000000..21d0fbb --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/Crc32.h @@ -0,0 +1,47 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#pragma once + +#include +#include +#include "Global.h" + +class Crc32 +{ +public: + Crc32(bool reflect, uint32_t poly = 0x04C11DB7); + ~Crc32(); + + void SetCRC(uint32_t crc) { m_crc = crc; } + uint32_t GetCRC() const { return m_crc; } + void Calc(const uint8_t *data, size_t size); + + uint32_t GetDataCRC(const uint8_t *data, size_t size, uint32_t initialXor, uint32_t finalXor); + +private: + void CalcLE(uint8_t b); + void CalcBE(uint8_t b); + + uint32_t m_table[256]; + uint32_t m_crc; + + bool m_reflect; +}; + diff --git a/vendor/apfs-fuse/ApfsLib/Decmpfs.cpp b/vendor/apfs-fuse/ApfsLib/Decmpfs.cpp new file mode 100644 index 0000000..b59d72b --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/Decmpfs.cpp @@ -0,0 +1,341 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#include +#include +#include +#include + +#include "Decmpfs.h" +#include "Endian.h" + +#include "Global.h" +#include "Util.h" + + +struct RsrcForkHeader +{ + be_uint32_t data_offset; + be_uint32_t mgmt_offset; + be_uint32_t data_size; + be_uint32_t mgmt_size; +}; + +struct CmpfRsrcEntry +{ + // 1 64K-Block + le_uint32_t off; + le_uint32_t size; +}; + +struct CmpfRsrc +{ + le_uint32_t entries; + CmpfRsrcEntry entry[32]; +}; + +bool IsDecompAlgoSupported(uint16_t algo) +{ + switch (algo) + { + case 3: + case 4: + case 7: + case 8: + case 9: + case 10: + case 11: + case 12: + case 13: + case 14: + return true; + default: + return false; + } +} + +bool IsDecompAlgoInRsrc(uint16_t algo) +{ + switch (algo) + { + case 4: + case 8: + case 10: + case 12: + case 14: + return true; + default: + return false; + } +} + +bool DecompressFile(ApfsDir &dir, uint64_t ino, std::vector &decompressed, const std::vector &compressed) +{ + if (compressed.size() < sizeof(CompressionHeader)) + return false; + + const CompressionHeader *hdr = reinterpret_cast(compressed.data()); + const uint8_t *cdata = compressed.data() + sizeof(CompressionHeader); + size_t csize = compressed.size() - sizeof(CompressionHeader); + size_t decoded_bytes = 0; + +#if 1 // Disable to get compressed data + if (g_debug & Dbg_Cmpfs) + { + std::cout << "DecompressFile " << compressed.size() << " => " << hdr->size << ", algo = " << hdr->algo; + + switch (hdr->algo) + { + case 3: std::cout << " (Zlib, Attr)"; break; + case 4: std::cout << " (Zlib, Rsrc)"; break; + case 7: std::cout << " (LZVN, Attr)"; break; + case 8: std::cout << " (LZVN, Rsrc)"; break; + case 9: std::cout << " (Uncompressed, Attr)"; break; + case 10: std::cout << " (Uncompressed, Rsrc)"; break; + case 11: std::cout << " (LZFSE, Attr)"; break; + case 12: std::cout << " (LZFSE, Rsrc)"; break; + case 13: std::cout << " (LZBITMAP, Attr)"; break; + case 14: std::cout << " (LZBITMAP, Rsrc)"; break; + default: std::cout << " (Unknown)"; break; + } + + std::cout << std::endl; + } + + if (!IsDecompAlgoSupported(hdr->algo)) + { + if (g_debug & Dbg_Errors) { + std::cout << "Unsupported decompression algorithm." << std::endl; + DumpHex(std::cout, compressed.data(), compressed.size()); + } + return false; + } + + if (IsDecompAlgoInRsrc(hdr->algo)) + { + std::vector rsrc; + size_t k; + + bool rc = dir.GetAttribute(rsrc, ino, "com.apple.ResourceFork"); + + if (!rc) + { + if (g_debug & Dbg_Errors) + std::cout << "Decmpfs: Could not find resource fork " << ino << std::endl; + decompressed.clear(); + return false; + } + + if (hdr->algo == 4) // Zlib, rsrc + { + RsrcForkHeader rsrc_hdr; + + memcpy(&rsrc_hdr, rsrc.data(), sizeof(rsrc_hdr)); + + if (rsrc_hdr.data_offset > rsrc.size()) + { + if (g_debug & Dbg_Errors) + std::cout << "Decmpfs: Invalid data offset in rsrc header." << std::endl; + return false; + } + + const uint8_t *cmpf_rsrc_base = rsrc.data() + rsrc_hdr.data_offset + sizeof(uint32_t); + const CmpfRsrc *cmpf_rsrc = reinterpret_cast(cmpf_rsrc_base); + + decompressed.resize((hdr->size + 0xFFFF) & 0xFFFF0000); + + for (k = 0; k < cmpf_rsrc->entries; k++) + { + size_t src_offset = cmpf_rsrc->entry[k].off; + const uint8_t *src = cmpf_rsrc_base + src_offset; + size_t src_len = cmpf_rsrc->entry[k].size; + uint8_t *dst = decompressed.data() + 0x10000 * k; + size_t expected_len = hdr->size - (0x10000 * k); + if (expected_len > 0x10000) + expected_len = 0x10000; + + if (src_len > 0x10001) + { + if (g_debug & Dbg_Errors) + std::cout << "Decmpfs: In rsrc, src_len too big (" << src_len << ")" << std::endl; + return false; + } + + if (src[0] == 0x78) + { + decoded_bytes = DecompressZLib(dst, 0x10000, src, src_len); + } + else if ((src[0] & 0x0F) == 0x0F) + { + memcpy(dst, src + 1, src_len - 1); + decoded_bytes = src_len - 1; + } + else + { + if (g_debug & Dbg_Errors) + std::cout << "Decmpfs: Something wrong with zlib data." << std::endl; + decompressed.clear(); + return false; + } + + if (expected_len != decoded_bytes) + { + if (g_debug & Dbg_Errors) + std::cout << "Decmpfs: Expected len != decompressed len: " << expected_len << " != " << decoded_bytes << std::endl; + return false; + } + } + } + else + { + const uint32_t *off_list = reinterpret_cast(rsrc.data()); + + decompressed.resize((hdr->size + 0xFFFF) & 0xFFFF0000); + + for (k = 0; (k << 16) < decompressed.size(); k++) { + size_t expected_len = hdr->size - (0x10000 * k); + if (expected_len > 0x10000) + expected_len = 0x10000; + const uint8_t *src = rsrc.data() + off_list[k]; + size_t src_len = off_list[k + 1] - off_list[k]; + + if (src_len > 0x10001) { + if (g_debug & Dbg_Errors) + std::cout << "Decmpfs: In rsrc, src_len too big (" << src_len << ")" << std::endl; + return false; + } + + switch (hdr->algo) { + case 8: + if (src[0] == 0x06) { + memcpy(decompressed.data() + (k << 16), src + 1, src_len - 1); + decoded_bytes = src_len - 1; + } + else + decoded_bytes = DecompressLZVN(decompressed.data() + (k << 16), expected_len, src, src_len); + break; + case 10: + // Assuming ... + memcpy(decompressed.data() + (k << 16), src + 1, src_len - 1); + decoded_bytes = src_len - 1; + break; + case 12: + // Assuming ... + decoded_bytes = DecompressLZFSE(decompressed.data() + (k << 16), expected_len, src, src_len); + // TODO is there also an uncompressed variant? + break; + case 14: + if (src[0] == 0xFF) { + memcpy(decompressed.data() + (k << 16), src + 1, src_len - 1); + decoded_bytes = src_len - 1; + } + else + decoded_bytes = DecompressLZBITMAP(decompressed.data() + (k << 16), expected_len, src, src_len); + break; + default: + decoded_bytes = 0; + break; + } + + if (decoded_bytes != expected_len) { + if (g_debug & Dbg_Errors) + std::cout << "Decmpfs: Expected length != decompressed length: " << expected_len << " != " << decoded_bytes << " [k = " << k << "]" << std::endl; + + return false; + } + } + } + + decompressed.resize(hdr->size); + } + else + { + decompressed.resize(hdr->size); + + switch (hdr->algo) { + case 3: + if (cdata[0] == 0x78) + decoded_bytes = DecompressZLib(decompressed.data(), decompressed.size(), cdata, csize); + else if (cdata[0] == 0xFF) { + assert(hdr->size == csize - 1); + decompressed.assign(cdata + 1, cdata + csize); + decoded_bytes = decompressed.size(); + } + else + return false; + break; + + case 7: + if (cdata[0] == 0x06) { + assert(hdr->size == csize - 1); + decompressed.assign(cdata + 1, cdata + csize); + decoded_bytes = decompressed.size(); + } + else + decoded_bytes = DecompressLZVN(decompressed.data(), decompressed.size(), cdata, csize); + break; + + case 9: + assert(cdata[0] == 0xCC); + assert(hdr->size == csize - 1); + decompressed.assign(cdata + 1, cdata + csize); + decoded_bytes = decompressed.size(); + break; + + case 11: + // TODO uncompressed variant? + decoded_bytes = DecompressLZFSE(decompressed.data(), decompressed.size(), cdata, csize); + break; + + case 13: + if (cdata[0] == 0xFF) { + assert(hdr->size == csize - 1); + decompressed.assign(cdata + 1, cdata + csize); + decoded_bytes = decompressed.size(); + } + else + decoded_bytes = DecompressLZBITMAP(decompressed.data(), decompressed.size(), cdata, csize); + break; + + default: + decoded_bytes = 0; + break; + } + + if (decoded_bytes != hdr->size) + { + if (g_debug & Dbg_Errors) + std::cout << "Decmpfs: In attr, expected len != decoded len: " << hdr->size << " != " << decoded_bytes << std::endl; + return false; + } + } + +#else + if (IsDecompAlgoInRsrc(hdr->algo)) + { + dir.GetAttribute(decompressed, ino, "com.apple.ResourceFork"); + } + else + { + decompressed = compressed; + } +#endif + + return true; +} diff --git a/vendor/apfs-fuse/ApfsLib/Decmpfs.h b/vendor/apfs-fuse/ApfsLib/Decmpfs.h new file mode 100644 index 0000000..a29871e --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/Decmpfs.h @@ -0,0 +1,36 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#pragma once + +#include + +#include "ApfsDir.h" + +struct CompressionHeader +{ + le_uint32_t signature; + le_uint32_t algo; + le_uint64_t size; +}; + +bool IsDecompAlgoSupported(uint16_t algo); +bool IsDecompAlgoInRsrc(uint16_t algo); + +bool DecompressFile(ApfsDir &dir, uint64_t ino, std::vector &decompressed, const std::vector &compressed); diff --git a/vendor/apfs-fuse/ApfsLib/Device.cpp b/vendor/apfs-fuse/ApfsLib/Device.cpp new file mode 100644 index 0000000..2934c12 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/Device.cpp @@ -0,0 +1,111 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#include + +#include "Device.h" + +#include "DeviceWinFile.h" +#include "DeviceWinPhys.h" +#include "DeviceLinux.h" +#include "DeviceMac.h" +#include "DeviceDMG.h" +#include "DeviceSparseImage.h" +#include "DeviceVDI.h" + +Device::Device() +{ + m_sector_size = 0x200; +} + +Device::~Device() +{ +} + +Device * Device::OpenDevice(const char * name) +{ + Device *dev = nullptr; + bool rc; + const char *ext; + +#ifdef _WIN32 + if (!strncmp(name, "\\\\.\\PhysicalDrive", 17)) + { + dev = new DeviceWinPhys(); + rc = dev->Open(name); + if (rc) + return dev; + else + { + dev->Close(); + delete dev; + dev = nullptr; + } + } +#endif + + ext = strrchr(name, '.'); + if (ext) + { + if (!strcmp(ext, ".dmg")) + { + DeviceDMG *dmg; + dmg = new DeviceDMG(); + rc = dmg->Open(name); + if (rc) + return dmg; + dmg->Close(); + delete dmg; + } + + if (!strcmp(ext, ".sparseimage")) + { + DeviceSparseImage *sprs; + sprs = new DeviceSparseImage(); + rc = sprs->Open(name); + if (rc) + return sprs; + sprs->Close(); + delete sprs; + } + } + + if (!dev) + { +#ifdef _WIN32 + dev = new DeviceWinFile(); +#endif +#ifdef __linux__ + dev = new DeviceLinux(); +#endif +#ifdef __APPLE__ + dev = new DeviceMac(); +#endif + rc = dev->Open(name); + + if (!rc) + { + dev->Close(); + delete dev; + dev = nullptr; + } + } + + return dev; +} diff --git a/vendor/apfs-fuse/ApfsLib/Device.h b/vendor/apfs-fuse/ApfsLib/Device.h new file mode 100644 index 0000000..5feea5e --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/Device.h @@ -0,0 +1,45 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#pragma once + +#include + +class Device +{ +protected: + Device(); + +public: + virtual ~Device(); + + virtual bool Open(const char *name) = 0; + virtual void Close() = 0; + + virtual bool Read(void *data, uint64_t offs, uint64_t len) = 0; + virtual uint64_t GetSize() const = 0; + + unsigned int GetSectorSize() const { return m_sector_size; } + void SetSectorSize(unsigned int size) { m_sector_size = size; } + + static Device *OpenDevice(const char *name); + +private: + unsigned int m_sector_size; +}; diff --git a/vendor/apfs-fuse/ApfsLib/DeviceDMG.cpp b/vendor/apfs-fuse/ApfsLib/DeviceDMG.cpp new file mode 100644 index 0000000..4fe7259 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/DeviceDMG.cpp @@ -0,0 +1,527 @@ +/* +This file is part of apfs-fuse, a read-only implementation of APFS +(Apple File System) for FUSE. +Copyright (C) 2017 Simon Gander + +Apfs-fuse is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +Apfs-fuse is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with apfs-fuse. If not, see . +*/ + +#include +#include +#include +#include + +#include "Endian.h" +#include "Util.h" +#include "PList.h" +#include "DeviceDMG.h" + +#pragma pack(push, 1) + +struct KolyHeader +{ + char signature[4]; + be_uint32_t version; + be_uint32_t headersize; + be_uint32_t flags; + be_uint64_t running_data_fork_offset; + be_uint64_t data_fork_offset; + be_uint64_t data_fork_length; + be_uint64_t rsrc_fork_offset; + be_uint64_t rsrc_fork_length; + be_uint32_t segment_number; + be_uint32_t segment_count; + uint8_t segment_id[0x10]; + // 0x50 + be_uint32_t data_fork_checksum_type; + be_uint32_t data_fork_unknown; + be_uint32_t data_fork_checksum_data; + uint8_t unk_05C[0x7C]; + // 0xD8 + be_uint64_t xml_offset; + be_uint64_t xml_length; + // ??? + // uint64_t codesign_offset; + // uint64_t codesign_length; + uint8_t unk_0E8[0x78]; + // 0x160 + be_uint32_t master_checksum_type; + be_uint32_t master_checksum_unknown; + be_uint32_t master_checksum_data; + uint8_t unk_16C[0x7C]; + // 0x1E8 + be_uint32_t image_variant; + be_uint64_t sector_count; + // 0x1F4 + uint8_t unk_1F4[12]; +}; + +static_assert(sizeof(KolyHeader) == 0x200, "Wrong Koly Header Size"); + +struct MishHeader +{ + char signature[4]; + be_uint32_t unk; + be_uint64_t sector_start; + be_uint64_t sector_count; + be_uint64_t dmg_offset; + be_uint32_t unk_20; + be_uint32_t part_id; + be_uint8_t unk_28[0x18]; + be_uint32_t checksum_type; + be_uint32_t checksum_unk; + be_uint32_t checksum_data; + be_uint8_t unk_4C[0x7C]; + be_uint32_t entry_count; +}; + +static_assert(sizeof(MishHeader) == 0xCC, "Wrong Mish Header Size"); + +struct MishEntry +{ + be_uint32_t method; + be_uint32_t comment; + be_uint64_t sector_start; + be_uint64_t sector_count; + be_uint64_t dmg_offset; + be_uint64_t dmg_length; +}; + +static_assert(sizeof(MishEntry) == 0x28, "Wrong Mish Entry Size"); + +#pragma pack(pop) + +DeviceDMG::DmgSection::DmgSection() +{ + method = 0; + comment = 0; + disk_offset = 0; + disk_length = 0; + dmg_offset = 0; + dmg_length = 0; + cache = nullptr; +} + +DeviceDMG::DmgSection::~DmgSection() +{ + delete[] cache; +} + +DeviceDMG::DeviceDMG() : m_crc(true) +{ + m_size = 0; + m_offset = 0; + + m_is_raw = false; + +#ifdef DMG_CACHE + m_cache_base = 0; + m_cache_size = 0; + m_cache_data = 0; +#endif +} + +DeviceDMG::~DeviceDMG() +{ + Close(); + +#ifdef DMG_CACHE + delete[] m_cache_data; +#endif +} + +bool DeviceDMG::Open(const char * name) +{ + Close(); + + if (!m_img.Open(name)) + return false; + + if (!m_img.CheckSetupEncryption()) + { + m_img.Close(); + return false; + } + + KolyHeader koly; + + m_img.Read(m_img.GetContentSize() - 0x200, &koly, sizeof(koly)); + + if (memcmp(koly.signature, "koly", 4)) + { + m_is_raw = true; + m_size = m_img.GetContentSize(); + return true; + } + +#ifdef DMG_DEBUG + m_dbg.open("mish_ev1.txt"); + m_dbg.flags(m_dbg.hex | m_dbg.uppercase); + m_dbg.fill('0'); + + m_dbg << "Signature : " << std::setw(8) << koly.signature << std::endl; + m_dbg << "Version : " << std::setw(8) << koly.version << std::endl; + m_dbg << "Header size : " << std::setw(8) << koly.headersize << std::endl; + m_dbg << "Flags : " << std::setw(8) << koly.flags << std::endl; + m_dbg << "Running DF Off : " << std::setw(16) << koly.running_data_fork_offset << std::endl; + m_dbg << "Data Fork Offs : " << std::setw(16) << koly.data_fork_offset << std::endl; + m_dbg << "Data Fork Len : " << std::setw(16) << koly.data_fork_length << std::endl; + m_dbg << "Rsrc Fork Off : " << std::setw(16) << koly.rsrc_fork_offset << std::endl; + m_dbg << "Rsrc Fork Len : " << std::setw(16) << koly.rsrc_fork_length << std::endl; + m_dbg << "Segment Number : " << std::setw(8) << koly.segment_number << std::endl; + m_dbg << "Segment Count : " << std::setw(8) << koly.segment_count << std::endl; + m_dbg << "..." << std::endl; + m_dbg << "XML Offset : " << std::setw(16) << koly.xml_offset << std::endl; + m_dbg << "XML Length : " << std::setw(16) << koly.xml_length << std::endl; + m_dbg << "..." << std::endl; + DumpHex(m_dbg, reinterpret_cast(&koly), sizeof(koly), 0x20); + m_dbg << std::endl << std::endl; +#endif + + m_size = koly.sector_count * 0x200; + m_offset = koly.data_fork_offset; + + if (koly.xml_offset != 0) + { + if (g_debug & Dbg_Info) + printf("Loading DMG using XML plist.\n"); + + if (!ProcessHeaderXML(koly.xml_offset, koly.xml_length)) + { + fprintf(stderr, "Error parsing propertiy list.\n"); + m_img.Close(); + m_img.Reset(); + m_size = 0; + return false; + } + } + else if (koly.rsrc_fork_offset != 0) + { + if (g_debug & Dbg_Info) + printf("Loading DMG using RSRC fork.\n"); + + if (!ProcessHeaderRsrc(koly.rsrc_fork_offset, koly.rsrc_fork_length)) + { + fprintf(stderr, "Error processing resource fork.\n"); + m_img.Close(); + m_img.Reset(); + m_size = 0; + return false; + } + } + else + { + m_img.Close(); + m_img.Reset(); + m_size = 0; + return false; + } + +#ifdef DMG_DEBUG + m_dbg.close(); +#endif + + return true; +} + +void DeviceDMG::Close() +{ + m_img.Close(); + m_size = 0; + m_sections.clear(); + m_is_raw = false; +} + +bool DeviceDMG::Read(void * data, uint64_t offs, uint64_t len) +{ + if (m_is_raw) + { + m_img.Read(offs + m_offset, data, len); + return true; + // TODO: Error handling ... + } + + // Binary search start sector in m_sections + // Get data if necessary + // Decompress: cache data + + size_t entry_idx = m_sections.size(); + size_t rd_offs; + size_t rd_size; + char *bdata = reinterpret_cast(data); + bool compressed = false; + + ptrdiff_t beg = 0; + ptrdiff_t end = m_sections.size() - 1; + ptrdiff_t mid; + + while (beg <= end) + { + mid = (beg + end) / 2; + + if (offs >= m_sections[mid].disk_offset && offs < (m_sections[mid].disk_offset + m_sections[mid].disk_length)) + { + entry_idx = mid; + break; + } + else if (offs < m_sections[mid].disk_offset) + end = mid - 1; + else + beg = mid + 1; + } + + if (entry_idx == m_sections.size()) + return false; + + while (len > 0) + { + if (entry_idx >= m_sections.size()) + return false; + + DmgSection § = m_sections[entry_idx]; + + rd_offs = offs - sect.disk_offset; + rd_size = len; + if (rd_offs + rd_size > sect.disk_length) + rd_size = sect.disk_length - rd_offs; + + compressed = false; + + switch (sect.method) + { + case 1: // raw + m_img.Read(rd_offs + sect.dmg_offset + m_offset, bdata, rd_size); + break; + case 0: // unsure ... + case 2: // ignore + memset(bdata, 0, rd_size); + break; + case 0x80000004: // adc + case 0x80000005: // zlib + case 0x80000006: // bzip2 + case 0x80000007: // lzfse + compressed = true; + break; + default: + std::cerr << "DMG: unknown compression method " << sect.method << std::endl; + return false; + } + + if (compressed) + { +#ifdef DMG_CACHE + if (m_cache_data == 0 || m_cache_base != sect.disk_offset || m_cache_size != sect.disk_length) + { + delete[] m_cache_data; + m_cache_data = 0; + + uint8_t *compr_buf = new uint8_t[sect.dmg_length]; + + m_cache_data = new uint8_t[sect.disk_length]; + m_cache_base = sect.disk_offset; + m_cache_size = sect.disk_length; + + m_img.Read(sect.dmg_offset + m_offset, compr_buf, sect.dmg_length); + + switch (sect.method) + { + case 0x80000004: + DecompressADC(m_cache_data, sect.disk_length, compr_buf, sect.dmg_length); + break; + case 0x80000005: + DecompressZLib(m_cache_data, sect.disk_length, compr_buf, sect.dmg_length); + break; + case 0x80000006: + DecompressBZ2(m_cache_data, sect.disk_length, compr_buf, sect.dmg_length); + break; + case 0x80000007: + DecompressLZFSE(m_cache_data, sect.disk_length, compr_buf, sect.dmg_length); + break; + default: + std::cerr << "DMG: invalid compression method " << sect.method << std::endl; + return false; + break; + } + + delete[] compr_buf; + } + + memcpy(bdata, m_cache_data + rd_offs, rd_size); +#else + + if (sect.cache == nullptr) + { + uint8_t *compr_buf = new uint8_t[sect.dmg_length]; + + sect.cache = new uint8_t[sect.disk_length]; + + m_img.Read(sect.dmg_offset + m_offset, compr_buf, sect.dmg_length); + + switch (sect.method) + { + case 0x80000004: + DecompressADC(sect.cache, sect.disk_length, compr_buf, sect.dmg_length); + break; + case 0x80000005: + DecompressZLib(sect.cache, sect.disk_length, compr_buf, sect.dmg_length); + break; + case 0x80000006: + DecompressBZ2(sect.cache, sect.disk_length, compr_buf, sect.dmg_length); + break; + case 0x80000007: + DecompressLZFSE(sect.cache, sect.disk_length, compr_buf, sect.dmg_length); + break; + default: + std::cerr << "DMG: invalid compression method " << sect.method << std::endl; + return false; + break; + } + + delete[] compr_buf; + + memcpy(bdata, sect.cache + rd_offs, rd_size); + +#if 1 // TODO: figure out some intelligent cache mechanism ... + delete[] sect.cache; + sect.cache = nullptr; +#endif + } +#endif + } + + + bdata += rd_size; + offs += rd_size; + len -= rd_size; + entry_idx++; + } + + return true; +} + +uint64_t DeviceDMG::GetSize() const +{ + return m_size; +} + +bool DeviceDMG::ProcessHeaderXML(uint64_t off, uint64_t size) +{ + std::vector xmldata; + + xmldata.resize(size, 0); + + m_img.Read(off, xmldata.data(), size); + + PListXmlParser parser(xmldata.data(), xmldata.size()); + const PLDict *plist = parser.Parse()->toDict(); + + if (!plist) + return false; + + const PLDict *rsrc_fork = plist->get("resource-fork")->toDict(); + + if (!rsrc_fork) + return false; + + const PLArray *blkx = rsrc_fork->get("blkx")->toArray(); + + if (!blkx) + return false; + + const PLDict *entry; + const PLData *mish; + size_t k; + + for (k = 0; k < blkx->size(); k++) + { + entry = blkx->get(k)->toDict(); + + if (!entry) + return false; + + mish = entry->get("Data")->toData(); + + if (!mish) + return false; + + ProcessMish(mish->data(), mish->size()); + } + + return true; +} + +bool DeviceDMG::ProcessHeaderRsrc(uint64_t off, uint64_t size) +{ + return false; +} + +void DeviceDMG::ProcessMish(const uint8_t * data, size_t size) +{ + (void)size; + + const MishHeader *mish = reinterpret_cast(data); + const MishEntry *entry = reinterpret_cast(data + 0xCC); + DmgSection section; + + uint64_t partition_start; + uint32_t cnt; + uint32_t k; + + if (memcmp(mish->signature, "mish", 4)) + return; + +#ifdef DMG_DEBUG + DumpHex(m_dbg, data, 0xCC, 0x20); + + m_dbg << std::setw(8) << mish->signature << ' '; + m_dbg << std::setw(8) << mish->unk << ' '; + m_dbg << std::setw(16) << mish->sector_start << ' '; + m_dbg << std::setw(16) << mish->sector_count << ' '; + m_dbg << std::setw(16) << mish->dmg_offset << ' '; + m_dbg << std::setw(8) << mish->unk_20 << ' '; + m_dbg << std::setw(8) << mish->part_id << " - "; + m_dbg << std::setw(8) << mish->checksum_data << std::endl; +#endif + + partition_start = mish->sector_start; + + cnt = mish->entry_count; + + for (k = 0; k < cnt; k++) + { +#ifdef DMG_DEBUG + m_dbg << std::setw(8) << entry[k].method << ' '; + m_dbg << std::setw(8) << entry[k].comment << ' '; + m_dbg << std::setw(16) << entry[k].sector_start << ' '; + m_dbg << std::setw(16) << entry[k].sector_count << ' '; + m_dbg << std::setw(16) << entry[k].dmg_offset << ' '; + m_dbg << std::setw(16) << entry[k].dmg_length << std::endl; +#endif + + section.method = entry[k].method; + section.comment = entry[k].comment; + section.disk_offset = (entry[k].sector_start + partition_start) * 0x200; + section.disk_length = entry[k].sector_count * 0x200; + section.dmg_offset = entry[k].dmg_offset + mish->dmg_offset; + section.dmg_length = entry[k].dmg_length; + section.cache = nullptr; + + if (section.method != 0xFFFFFFFF && section.method != 0x7FFFFFFE) + m_sections.push_back(section); + } + +#ifdef DMG_DEBUG + m_dbg << std::endl; +#endif +} diff --git a/vendor/apfs-fuse/ApfsLib/DeviceDMG.h b/vendor/apfs-fuse/ApfsLib/DeviceDMG.h new file mode 100644 index 0000000..c59c168 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/DeviceDMG.h @@ -0,0 +1,85 @@ +/* +This file is part of apfs-fuse, a read-only implementation of APFS +(Apple File System) for FUSE. +Copyright (C) 2017 Simon Gander + +Apfs-fuse is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +Apfs-fuse is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with apfs-fuse. If not, see . +*/ + +#pragma once + +#include +#include +#include +#include + +#include "Device.h" +#include "DiskImageFile.h" + +#include "Crc32.h" + +#undef DMG_DEBUG +#define DMG_CACHE + +class DeviceDMG : public Device +{ + struct DmgSection + { + DmgSection(); + ~DmgSection(); + + uint32_t method; + uint32_t comment; + uint64_t disk_offset; + uint64_t disk_length; + uint64_t dmg_offset; + uint64_t dmg_length; + uint8_t *cache; + }; + +public: + DeviceDMG(); + ~DeviceDMG(); + + bool Open(const char *name) override; + void Close() override; + + bool Read(void *data, uint64_t offs, uint64_t len) override; + uint64_t GetSize() const override; + +private: + bool ProcessHeaderXML(uint64_t off, uint64_t size); + bool ProcessHeaderRsrc(uint64_t off, uint64_t size); + + void ProcessMish(const uint8_t *data, size_t size); + + DiskImageFile m_img; + uint64_t m_size; + uint64_t m_offset; + + bool m_is_raw; + + Crc32 m_crc; + + std::vector m_sections; + +#ifdef DMG_DEBUG + std::ofstream m_dbg; +#endif +#ifdef DMG_CACHE + uint64_t m_cache_base; + uint64_t m_cache_size; + uint8_t *m_cache_data; +#endif +}; diff --git a/vendor/apfs-fuse/ApfsLib/DeviceLinux.cpp b/vendor/apfs-fuse/ApfsLib/DeviceLinux.cpp new file mode 100644 index 0000000..1ddd47d --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/DeviceLinux.cpp @@ -0,0 +1,94 @@ +/* +This file is part of apfs-fuse, a read-only implementation of APFS +(Apple File System) for FUSE. +Copyright (C) 2017 Simon Gander + +Apfs-fuse is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +Apfs-fuse is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with apfs-fuse. If not, see . +*/ + +#ifdef __linux__ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "DeviceLinux.h" +#include "Global.h" + +DeviceLinux::DeviceLinux() +{ + m_device = -1; + m_size = 0; +} + +DeviceLinux::~DeviceLinux() +{ + Close(); +} + +bool DeviceLinux::Open(const char* name) +{ + m_device = open(name, O_RDONLY | O_LARGEFILE); + + if (m_device == -1) + { + std::cout << "Opening device " << name << " failed with error " << strerror(errno) << std::endl; + return false; + } + + struct stat64 st; + + fstat64(m_device, &st); + + if (S_ISREG(st.st_mode)) + { + m_size = st.st_size; + } + else if (S_ISBLK(st.st_mode)) + { + // Hmmm ... + ioctl(m_device, BLKGETSIZE64, &m_size); + } + + if (g_debug & Dbg_Info) + std::cout << "Device " << name << " opened. Size is " << m_size << std::endl; + + return m_device != -1; +} + +void DeviceLinux::Close() +{ + if (m_device != -1) + close(m_device); + m_device = -1; + m_size = 0; +} + +bool DeviceLinux::Read(void* data, uint64_t offs, uint64_t len) +{ + size_t nread; + + nread = pread64(m_device, data, len, offs); + + // TODO: Better error handling ... + return nread == len; +} + +#endif diff --git a/vendor/apfs-fuse/ApfsLib/DeviceLinux.h b/vendor/apfs-fuse/ApfsLib/DeviceLinux.h new file mode 100644 index 0000000..5a5bc45 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/DeviceLinux.h @@ -0,0 +1,45 @@ +/* +This file is part of apfs-fuse, a read-only implementation of APFS +(Apple File System) for FUSE. +Copyright (C) 2017 Simon Gander + +Apfs-fuse is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +Apfs-fuse is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with apfs-fuse. If not, see . +*/ + +#pragma once + +#ifdef __linux__ + +#include +#include "Device.h" + +class DeviceLinux : public Device +{ +public: + DeviceLinux(); + ~DeviceLinux(); + + bool Open(const char *name) override; + void Close() override; + + bool Read(void *data, uint64_t offs, uint64_t len) override; + + uint64_t GetSize() const override { return m_size; } + +private: + int m_device; + uint64_t m_size; +}; + +#endif diff --git a/vendor/apfs-fuse/ApfsLib/DeviceMac.cpp b/vendor/apfs-fuse/ApfsLib/DeviceMac.cpp new file mode 100644 index 0000000..3d31bc0 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/DeviceMac.cpp @@ -0,0 +1,109 @@ +/* +This file is part of apfs-fuse, a read-only implementation of APFS +(Apple File System) for FUSE. +Copyright (C) 2017 Simon Gander + +Apfs-fuse is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +Apfs-fuse is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with apfs-fuse. If not, see . +*/ + +#ifdef __APPLE__ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "DeviceMac.h" +#include "Global.h" + +DeviceMac::DeviceMac() +{ + m_device = -1; + m_size = 0; +} + +DeviceMac::~DeviceMac() +{ + Close(); +} + +bool DeviceMac::Open(const char* name) +{ + m_device = open(name, O_RDONLY); + + if (m_device == -1) + { + std::cout << "Opening device " << name << " failed with error " << strerror(errno) << std::endl; + return false; + } + + struct stat st; + + fstat(m_device, &st); + + std::cout << "st_mode = " << st.st_mode << std::endl; + + if (S_ISREG(st.st_mode)) + { + m_size = st.st_size; + } + else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)) + { + uint64_t sector_count = 0; + uint32_t sector_size = 0; + + ioctl(m_device, DKIOCGETBLOCKCOUNT, §or_count); + ioctl(m_device, DKIOCGETBLOCKSIZE, §or_size); + + m_size = sector_size * sector_count; + + std::cout << "Sector count = " << sector_count << std::endl; + std::cout << "Sector size = " << sector_size << std::endl; + } + else + { + std::cout << "File mode unknown!" << std::endl; + } + + if (g_debug & Dbg_Info) + std::cout << "Device " << name << " opened. Size is " << m_size << std::endl; + + return m_device != -1; +} + +void DeviceMac::Close() +{ + if (m_device != -1) + close(m_device); + m_device = -1; + m_size = 0; +} + +bool DeviceMac::Read(void* data, uint64_t offs, uint64_t len) +{ + size_t nread; + + nread = pread(m_device, data, len, offs); + + // TODO: Better error handling ... + return nread == len; +} + +#endif diff --git a/vendor/apfs-fuse/ApfsLib/DeviceMac.h b/vendor/apfs-fuse/ApfsLib/DeviceMac.h new file mode 100644 index 0000000..48aeb47 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/DeviceMac.h @@ -0,0 +1,44 @@ +/* +This file is part of apfs-fuse, a read-only implementation of APFS +(Apple File System) for FUSE. +Copyright (C) 2017 Simon Gander + +Apfs-fuse is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +Apfs-fuse is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with apfs-fuse. If not, see . +*/ + +#pragma once + +#ifdef __APPLE__ + +#include "Device.h" + +class DeviceMac : public Device +{ +public: + DeviceMac(); + ~DeviceMac(); + + bool Open(const char *name) override; + void Close() override; + + bool Read(void *data, uint64_t offs, uint64_t len) override; + + uint64_t GetSize() const override { return m_size; } + +private: + int m_device; + uint64_t m_size; +}; + +#endif diff --git a/vendor/apfs-fuse/ApfsLib/DeviceSparseImage.cpp b/vendor/apfs-fuse/ApfsLib/DeviceSparseImage.cpp new file mode 100644 index 0000000..28ebbdb --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/DeviceSparseImage.cpp @@ -0,0 +1,188 @@ +/* +This file is part of apfs-fuse, a read-only implementation of APFS +(Apple File System) for FUSE. +Copyright (C) 2017 Simon Gander + +Apfs-fuse is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +Apfs-fuse is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with apfs-fuse. If not, see . +*/ + +#include + +#include "Endian.h" + +#include "DeviceSparseImage.h" + +#ifdef _MSC_VER +#pragma pack(push, 4) +#define __attribute__(x) +#endif + +struct HeaderNode +{ + be_uint32_t signature; + be_uint32_t version; + be_uint32_t sectors_per_band; + be_uint32_t flags; + be_uint32_t total_sectors_low; + be_uint64_t next_node_offset; + be_uint64_t total_sectors; + be_uint32_t padding[7]; + be_uint32_t band_id[0x3F0]; +} __attribute__((packed, aligned(4))); + +struct IndexNode +{ + be_uint32_t magic; + be_uint32_t index_node_number; + be_uint32_t flags; + be_uint64_t next_node_offset; + be_uint32_t padding[9]; + be_uint32_t band_id[0x3F2]; +} __attribute__((packed, aligned(4))); + +#ifdef _MSC_VER +#pragma pack(pop) +#undef __attribute__ +#endif + +constexpr int SECTOR_SIZE = 0x200; +constexpr size_t NODE_SIZE = 0x1000; +constexpr size_t BAND_SIZE = 0x100000; +constexpr uint32_t SPRS_SIGNATURE = 0x73707273; + +DeviceSparseImage::DeviceSparseImage() +{ + m_band_size = 0; + m_size = 0; +} + +DeviceSparseImage::~DeviceSparseImage() +{ + m_img.Close(); + m_img.Reset(); +} + +bool DeviceSparseImage::Open(const char * name) +{ + if (!m_img.Open(name)) + return false; + + if (!m_img.CheckSetupEncryption()) + { + m_img.Close(); + m_img.Reset(); + return false; + } + + HeaderNode hdr; + IndexNode idx; + uint32_t off; + uint64_t base; + uint64_t next; + size_t k; + + m_img.Read(0, &hdr, sizeof(hdr)); + + if (hdr.signature != SPRS_SIGNATURE) + { + m_img.Close(); + m_img.Reset(); + return false; + } + + m_size = hdr.total_sectors * SECTOR_SIZE; + m_band_size = hdr.sectors_per_band * SECTOR_SIZE; + + m_band_offset.resize((m_size + m_band_size - 1) / m_band_size, 0); + + base = 0x1000; + + for (k = 0; k < 0x3F0; k++) + { + off = hdr.band_id[k]; + if (off) + m_band_offset[off - 1] = base + m_band_size * k; + } + + next = hdr.next_node_offset; + base = next + NODE_SIZE; + + while (next) + { + m_img.Read(next, &idx, sizeof(idx)); + + if (hdr.signature != SPRS_SIGNATURE) + { + m_img.Close(); + m_img.Reset(); + return false; + } + + for (k = 0; k < 0x3F2; k++) + { + off = idx.band_id[k]; + if (off) + m_band_offset[off - 1] = base + m_band_size * k; + } + + next = idx.next_node_offset; + base = next + NODE_SIZE; + } + + return true; +} + +void DeviceSparseImage::Close() +{ + m_img.Close(); + m_img.Reset(); +} + +bool DeviceSparseImage::Read(void * data, uint64_t offs, uint64_t len) +{ + uint32_t chunk; + uint32_t chunk_offs; + uint64_t chunk_base; + size_t read_size; + uint8_t *bdata = reinterpret_cast(data); + + while (len > 0) + { + chunk = offs >> 20; // TODO + chunk_offs = offs & (m_band_size - 1); + + read_size = len; + + if ((chunk_offs + read_size) > m_band_size) + read_size = m_band_size - chunk_offs; + + chunk_base = m_band_offset[chunk]; + + if (chunk_base) + m_img.Read(chunk_base + chunk_offs, bdata, read_size); + else + memset(bdata, 0, read_size); + + len -= read_size; + offs += read_size; + bdata += read_size; + } + + return true; +} + +uint64_t DeviceSparseImage::GetSize() const +{ + return m_size; +} diff --git a/vendor/apfs-fuse/ApfsLib/DeviceSparseImage.h b/vendor/apfs-fuse/ApfsLib/DeviceSparseImage.h new file mode 100644 index 0000000..8715685 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/DeviceSparseImage.h @@ -0,0 +1,26 @@ +#pragma once + +#include + +#include "Device.h" +#include "DiskImageFile.h" + +class DeviceSparseImage : public Device +{ +public: + DeviceSparseImage(); + ~DeviceSparseImage(); + + bool Open(const char *name) override; + void Close() override; + + bool Read(void *data, uint64_t offs, uint64_t len) override; + uint64_t GetSize() const override; + +private: + std::vector m_band_offset; + uint64_t m_size; + uint64_t m_band_size; + + DiskImageFile m_img; +}; diff --git a/vendor/apfs-fuse/ApfsLib/DeviceVDI.cpp b/vendor/apfs-fuse/ApfsLib/DeviceVDI.cpp new file mode 100644 index 0000000..494c41f --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/DeviceVDI.cpp @@ -0,0 +1,154 @@ +#include "DeviceVDI.h" + +#pragma pack(1) + +struct VdiDiskGeometry +{ + le_uint32_t cylinders; + le_uint32_t heads; + le_uint32_t sectors; + le_uint32_t sector_size; +}; + +struct VdiPreHeader +{ + char file_info[0x40]; + le_uint32_t signature; // 0xBEDA107F + le_uint32_t version; // 0x00010001 +}; + +struct VdiHeader1Plus +{ + le_uint32_t struct_size; + le_uint32_t image_type; + le_uint32_t flags; + char comment[0x100]; + le_uint32_t off_blocks; + le_uint32_t off_data; + VdiDiskGeometry legacy_geometry; + le_uint32_t dummy; + le_uint64_t disk_size; + le_uint32_t block_size; + le_uint32_t block_extra; + le_uint32_t blocks_total; + le_uint32_t blocks_allocated; + char uuid_create[0x10]; + char uuid_modify[0x10]; + char uuid_linkage[0x10]; + char uuid_parent_modify[0x10]; + VdiDiskGeometry lchs_geometry; +}; + +#pragma pack() + + +DeviceVDI::DeviceVDI() +{ + m_disk_size = 0; + m_block_size = 0; + m_block_count = 0; + m_data_offset = 0; + + m_vdi = nullptr; +} + +DeviceVDI::~DeviceVDI() +{ +} + +bool DeviceVDI::Open(const char * name) +{ + VdiPreHeader phdr; + VdiHeader1Plus hdr; + + Close(); + + fopen_s(&m_vdi, name, "rb"); + if (m_vdi) + { + if (fread(&phdr, sizeof(phdr), 1, m_vdi) == 1) + { + if (fread(&hdr, sizeof(hdr), 1, m_vdi) == 1) + { + if (phdr.signature == 0xBEDA107F && phdr.version == 0x00010001) + { + m_disk_size = hdr.disk_size; + m_block_size = hdr.block_size; + m_block_count = hdr.blocks_total; + m_data_offset = hdr.off_data; + + m_block_map.resize(m_block_count, 0xFF); + + fseek(m_vdi, hdr.off_blocks, SEEK_SET); + + fread(m_block_map.data(), sizeof(uint32_t), m_block_count, m_vdi); + + fseek(m_vdi, 0, SEEK_END); + + return true; + } + } + } + } + + Close(); + + return false; +} + +void DeviceVDI::Close() +{ + if (m_vdi) + { + fclose(m_vdi); + m_vdi = nullptr; + } +} + +bool DeviceVDI::Read(void * data, uint64_t offs, uint64_t len) +{ + uint64_t block_nr; + uint64_t block_offs; + uint64_t vdi_offs; + uint32_t map_entry; + + uint64_t read_size; + uint8_t *pdata = reinterpret_cast(data); + + if (!m_vdi) + return false; + + while (len > 0) + { + block_nr = offs >> 20; + block_offs = offs & 0xFFFFF; + + map_entry = m_block_map[block_nr]; + + read_size = m_block_size - block_offs; + + if (read_size > len) + read_size = len; + + if (map_entry == 0xFFFFFFFF) + { + memset(pdata, 0, read_size); + } + else + { + fseek(m_vdi, map_entry * m_block_size + m_data_offset + block_offs, SEEK_SET); + fread(pdata, 1, read_size, m_vdi); + } + + pdata += read_size; + offs += read_size; + len -= read_size; + } + + return true; +} + +uint64_t DeviceVDI::GetSize() const +{ + return m_disk_size; +} diff --git a/vendor/apfs-fuse/ApfsLib/DeviceVDI.h b/vendor/apfs-fuse/ApfsLib/DeviceVDI.h new file mode 100644 index 0000000..50cb8ec --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/DeviceVDI.h @@ -0,0 +1,30 @@ +#pragma once + +#include + +#include "Endian.h" +#include "Device.h" + + +class DeviceVDI : public Device +{ +public: + DeviceVDI(); + ~DeviceVDI(); + + bool Open(const char *name) override; + void Close() override; + + bool Read(void *data, uint64_t offs, uint64_t len) override; + uint64_t GetSize() const override; + +private: + uint64_t m_disk_size; + uint32_t m_block_size; + uint32_t m_block_count; + uint32_t m_data_offset; + + std::vector m_block_map; + + FILE *m_vdi; +}; diff --git a/vendor/apfs-fuse/ApfsLib/DeviceWinFile.cpp b/vendor/apfs-fuse/ApfsLib/DeviceWinFile.cpp new file mode 100644 index 0000000..86f4004 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/DeviceWinFile.cpp @@ -0,0 +1,62 @@ +/* +This file is part of apfs-fuse, a read-only implementation of APFS +(Apple File System) for FUSE. +Copyright (C) 2017 Simon Gander + +Apfs-fuse is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +Apfs-fuse is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with apfs-fuse. If not, see . +*/ + +#ifdef _WIN32 + +#include "DeviceWinFile.h" + +DeviceWinFile::DeviceWinFile() +{ + m_size = 0; +} + +DeviceWinFile::~DeviceWinFile() +{ + Close(); +} + +bool DeviceWinFile::Open(const char * name) +{ + m_vol.open(name, std::ios::binary); + + if (!m_vol.is_open()) + return false; + + m_vol.seekg(0, std::ios::end); + m_size = m_vol.tellg(); + m_vol.seekg(0); + + return m_vol.is_open(); +} + +void DeviceWinFile::Close() +{ + m_vol.close(); +} + +bool DeviceWinFile::Read(void *data, uint64_t offs, uint64_t len) +{ + m_vol.seekg(offs); + m_vol.read(reinterpret_cast(data), len); + + // TODO: Fehlerbehandlung ... + return true; +} + +#endif diff --git a/vendor/apfs-fuse/ApfsLib/DeviceWinFile.h b/vendor/apfs-fuse/ApfsLib/DeviceWinFile.h new file mode 100644 index 0000000..1cc2af3 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/DeviceWinFile.h @@ -0,0 +1,46 @@ +/* +This file is part of apfs-fuse, a read-only implementation of APFS +(Apple File System) for FUSE. +Copyright (C) 2017 Simon Gander + +Apfs-fuse is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +Apfs-fuse is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with apfs-fuse. If not, see . +*/ + +#pragma once + +#ifdef _WIN32 + +#include + +#include "Device.h" + +class DeviceWinFile : public Device +{ +public: + DeviceWinFile(); + ~DeviceWinFile(); + + bool Open(const char *name) override; + void Close() override; + + bool Read(void *data, uint64_t offs, uint64_t len) override; + + uint64_t GetSize() const override { return m_size; } + +private: + std::ifstream m_vol; + uint64_t m_size; +}; + +#endif diff --git a/vendor/apfs-fuse/ApfsLib/DeviceWinPhys.cpp b/vendor/apfs-fuse/ApfsLib/DeviceWinPhys.cpp new file mode 100644 index 0000000..6a55edd --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/DeviceWinPhys.cpp @@ -0,0 +1,91 @@ +/* +This file is part of apfs-fuse, a read-only implementation of APFS +(Apple File System) for FUSE. +Copyright (C) 2017 Simon Gander + +Apfs-fuse is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +Apfs-fuse is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with apfs-fuse. If not, see . +*/ + +#ifdef _WIN32 + +#include "DeviceWinPhys.h" + +DeviceWinPhys::DeviceWinPhys() +{ + m_drive = INVALID_HANDLE_VALUE; + m_size = 0; +} + +DeviceWinPhys::~DeviceWinPhys() +{ + Close(); +} + +bool DeviceWinPhys::Open(const char *name) +{ + TCHAR path[MAX_PATH]; + uint8_t buf[0x1000]; + DWORD bytes_ret = 0; + + // _stprintf_s(path, _T("\\\\.\\PhysicalDrive%d"), disk); + + MultiByteToWideChar(CP_UTF8, 0, name, -1, path, MAX_PATH); + + m_drive = CreateFile(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, nullptr); + if (m_drive == INVALID_HANDLE_VALUE) + return false; + + DeviceIoControl(m_drive, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, nullptr, 0, &buf, sizeof(buf), &bytes_ret, nullptr); + + if (bytes_ret == 0) + return false; + + const DISK_GEOMETRY_EX *geom = reinterpret_cast(buf); + + m_size = geom->DiskSize.QuadPart; + + return true; +} + +void DeviceWinPhys::Close() +{ + if (m_drive != INVALID_HANDLE_VALUE) + CloseHandle(m_drive); + m_drive = INVALID_HANDLE_VALUE; + m_size = 0; +} + +bool DeviceWinPhys::Read(void * data, uint64_t offs, uint64_t len) +{ + DWORD read_bytes = 0; + LARGE_INTEGER off; + BOOL rc; + + if (m_drive == INVALID_HANDLE_VALUE) + return false; + + off.QuadPart = offs; + + SetFilePointerEx(m_drive, off, nullptr, FILE_BEGIN); + rc = ReadFile(m_drive, data, len, &read_bytes, nullptr); + + return rc == TRUE; +} + +uint64_t DeviceWinPhys::GetSize() const +{ + return m_size; +} + +#endif diff --git a/vendor/apfs-fuse/ApfsLib/DeviceWinPhys.h b/vendor/apfs-fuse/ApfsLib/DeviceWinPhys.h new file mode 100644 index 0000000..644942c --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/DeviceWinPhys.h @@ -0,0 +1,47 @@ +/* +This file is part of apfs-fuse, a read-only implementation of APFS +(Apple File System) for FUSE. +Copyright (C) 2017 Simon Gander + +Apfs-fuse is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +Apfs-fuse is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with apfs-fuse. If not, see . +*/ + +#pragma once + +#ifdef _WIN32 + +#include "Device.h" + +// #define WIN32_LEAN_AND_MEAN +#include +#include + +class DeviceWinPhys : public Device +{ +public: + DeviceWinPhys(); + ~DeviceWinPhys(); + + bool Open(const char *name) override; + void Close() override; + + bool Read(void *data, uint64_t offs, uint64_t len) override; + uint64_t GetSize() const override; + +private: + HANDLE m_drive; + uint64_t m_size; +}; + +#endif diff --git a/vendor/apfs-fuse/ApfsLib/DiskImageFile.cpp b/vendor/apfs-fuse/ApfsLib/DiskImageFile.cpp new file mode 100644 index 0000000..192b609 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/DiskImageFile.cpp @@ -0,0 +1,518 @@ +#include + +#include +#include + +#include "Global.h" +#include "Endian.h" +#include +#include +#include "Util.h" + +#include "DiskImageFile.h" + +#pragma pack(push, 1) + +struct DmgCryptHeaderV1 +{ + uint8_t uuid[0x10]; + be_uint32_t block_size; + be_uint32_t unk_14; + be_uint32_t unk_18; + be_uint32_t unk_1C; + be_uint32_t unk_20; + be_uint32_t unk_24; + be_uint32_t kdf_algorithm; + be_uint32_t kdf_prng_algorithm; + be_uint32_t kdf_iteration_count; + be_uint32_t kdf_salt_len; + uint8_t kdf_salt[0x20]; + be_uint32_t unk_58; + be_uint32_t unk_5C; + be_uint32_t unk_60; + be_uint32_t unk_64; + uint8_t unwrap_iv[0x20]; + be_uint32_t wrapped_aes_key_len; + uint8_t wrapped_aes_key[0x100]; + be_uint32_t unk_18C; + be_uint32_t unk_190; + uint8_t unk_194[0x20]; + be_uint32_t wrapped_hmac_sha1_key_len; + uint8_t wrapped_hmac_sha1_key[0x100]; + be_uint32_t unk_2B8; + be_uint32_t unk_2BC; + uint8_t unk_2C0[0x20]; + be_uint32_t wrapped_integrity_key_len; + uint8_t wrapped_integrity_key[0x100]; + be_uint32_t unk_3E8_len; + uint8_t unk_3E8[0x100]; + be_uint64_t decrypted_data_length; + be_uint32_t unk_4F0; + char signature[8]; +}; + +static_assert(sizeof(DmgCryptHeaderV1) == 0x4FC, "DmgCryptHeaderV1 invalid length."); + +struct DmgCryptHeaderV2 +{ + char signature[8]; + be_uint32_t maybe_version; + be_uint32_t unk_0C; + be_uint32_t unk_10; + be_uint32_t unk_14; + be_uint32_t key_bits; + be_uint32_t unk_1C; + be_uint32_t unk_20; + uint8_t uuid[0x10]; + be_uint32_t block_size; + be_uint64_t encrypted_data_length; + be_uint64_t encrypted_data_offset; + be_uint32_t no_of_keys; +}; + +struct DmgKeyPointer +{ + be_uint32_t key_type; + be_uint64_t key_offset; + be_uint64_t key_length; +}; + +struct DmgKeyData +{ + be_uint32_t kdf_algorithm; + be_uint32_t prng_algorithm; + be_uint32_t iteration_count; + be_uint32_t salt_len; + uint8_t salt[0x20]; + be_uint32_t blob_enc_iv_size; + uint8_t blob_enc_iv[0x20]; + be_uint32_t blob_enc_key_bits; + be_uint32_t blob_enc_algorithm; + be_uint32_t blob_unk_5C; + be_uint32_t blob_enc_mode; + be_uint32_t encr_key_blob_size; + uint8_t encr_key_blob[0x200]; +}; + +#pragma pack(pop) + +DiskImageFile::DiskImageFile() +{ + m_is_encrypted = false; + + m_crypt_offset = 0; + m_crypt_size = 0; + m_crypt_blocksize = 0; +} + +DiskImageFile::~DiskImageFile() +{ +} + +bool DiskImageFile::Open(const char * name) +{ + m_image.open(name, std::ios::binary); + + return m_image.is_open(); +} + +void DiskImageFile::Close() +{ + m_image.close(); + + m_crypt_blocksize = 0; + m_crypt_size = 0; + m_crypt_offset = 0; +} + +void DiskImageFile::Reset() +{ + m_is_encrypted = false; + m_crypt_offset = 0; + m_crypt_size = 0; + m_crypt_blocksize = 0; + memset(m_hmac_key, 0, sizeof(m_hmac_key)); + m_aes.CleanUp(); +} + +bool DiskImageFile::CheckSetupEncryption() +{ + char signature[8]; + + m_image.seekg(0, std::ios::end); + + m_is_encrypted = false; + m_crypt_offset = 0; + m_crypt_size = m_image.tellg(); + + m_image.seekg(-8, std::ios::end); + m_image.read(signature, 8); + + if (!memcmp(signature, "cdsaencr", 8)) + { + m_is_encrypted = true; + + if (!SetupEncryptionV1()) + { + m_image.close(); + fprintf(stderr, "Error setting up decryption V1.\n"); + return false; + } + } + + m_image.seekg(0); + m_image.read(signature, 8); + + if (!memcmp(signature, "encrcdsa", 8)) + { + m_is_encrypted = true; + + if (!SetupEncryptionV2()) + { + m_image.close(); + fprintf(stderr, "Error setting up decryption V2.\n"); + return false; + } + } + + return true; +} + +void DiskImageFile::Read(uint64_t off, void * data, size_t size) +{ + if (!m_is_encrypted) + { + m_image.seekg(off); + m_image.read(reinterpret_cast(data), size); + } + else + { + uint8_t buffer[0x1000]; + uint64_t mask = m_crypt_blocksize - 1; + uint32_t blkid; + uint8_t iv[0x14]; + size_t rd_len; + uint8_t *bdata = reinterpret_cast(data); + + if (off & mask) + { + blkid = static_cast(off / m_crypt_blocksize); + blkid = bswap_be(blkid); + + m_image.seekg(m_crypt_offset + (off & ~mask)); + m_image.read(reinterpret_cast(buffer), m_crypt_blocksize); + + HMAC_SHA1(m_hmac_key, 0x14, reinterpret_cast(&blkid), sizeof(uint32_t), iv); + + m_aes.SetIV(iv); + m_aes.DecryptCBC(buffer, buffer, m_crypt_blocksize); + + rd_len = m_crypt_blocksize - (off & mask); + if (rd_len > size) + rd_len = size; + + memcpy(bdata, buffer + (off & mask), rd_len); + + bdata += rd_len; + off += rd_len; + size -= rd_len; + } + + while (size > 0) + { + blkid = static_cast(off / m_crypt_blocksize); + blkid = bswap_be(blkid); + + m_image.seekg(m_crypt_offset + (off & ~mask)); + m_image.read(reinterpret_cast(buffer), m_crypt_blocksize); + + HMAC_SHA1(m_hmac_key, 0x14, reinterpret_cast(&blkid), sizeof(uint32_t), iv); + + m_aes.SetIV(iv); + m_aes.DecryptCBC(buffer, buffer, m_crypt_blocksize); + + rd_len = m_crypt_blocksize; + if (rd_len > size) + rd_len = size; + + memcpy(bdata, buffer, rd_len); + + bdata += rd_len; + off += rd_len; + size -= rd_len; + } + } +} + +bool DiskImageFile::SetupEncryptionV1() +{ + DmgCryptHeaderV1 hdr; + TripleDES des; + std::string password; + uint8_t derived_key[0x18]; + size_t n; + uint64_t total_size; + + static const uint8_t des_iv[8] = { 0x4A, 0xDD, 0xA2, 0x2C, 0x79, 0xE8, 0x21, 0x05 }; + // uint8_t aes_key[0x10]; + // uint8_t hmac_key[0x14]; + // uint8_t integrity_key[0x100]; + uint8_t tmp_1[0x100]; + uint8_t tmp_2[0x100]; + uint8_t tmp_3[0x100]; + size_t len; + + int64_t hdrsize = sizeof(DmgCryptHeaderV1); + + m_image.seekg(-hdrsize, std::ios::end); + total_size = m_image.tellg(); + m_image.read(reinterpret_cast(&hdr), sizeof(hdr)); + + if (g_debug & Dbg_Crypto) + { + std::cout.flags(std::cout.hex | std::cout.uppercase); + std::cout.fill('0'); + } + + m_crypt_size = hdr.decrypted_data_length; + m_crypt_blocksize = hdr.block_size; + m_crypt_offset = 0; + m_is_encrypted = true; + + std::cout << "Password: "; + GetPassword(password); + + PBKDF2_HMAC_SHA1(reinterpret_cast(password.c_str()), password.size(), hdr.kdf_salt, hdr.kdf_salt_len, hdr.kdf_iteration_count, derived_key, 0x18); + + if (g_debug & Dbg_Crypto) + { + std::cout << "Salt: " << hexstr(hdr.kdf_salt, hdr.kdf_salt_len) << std::endl; + std::cout << "Iter: " << hdr.kdf_iteration_count << std::endl; + std::cout << "DKey: " << hexstr(derived_key, sizeof(derived_key)) << std::endl; + + std::cout << "IV : " << hexstr(hdr.unwrap_iv, sizeof(hdr.unwrap_iv)) << std::endl; + std::cout << "U3E8: " << hexstr(hdr.unk_3E8, hdr.unk_3E8_len) << std::endl; + std::cout << std::endl; + + std::cout << "AES Key:" << std::endl; + } + + des.SetKey(derived_key); + + len = hdr.wrapped_aes_key_len; + + des.SetIV(des_iv); + des.DecryptCBC(tmp_1, hdr.wrapped_aes_key, len); + + if (g_debug & Dbg_Crypto) + DumpHex(std::cout, tmp_1, len); + + if (tmp_1[len - 1] > 0x8) + return false; + + len = PkcsUnpad(tmp_1, len); + + for (n = 0; n < len; n++) + tmp_2[len - n - 1] = tmp_1[n]; + + if (g_debug & Dbg_Crypto) + DumpHex(std::cout, tmp_2, len); + + des.SetIV(nullptr); + + des.DecryptCBC(tmp_3, tmp_2, len); + + if (g_debug & Dbg_Crypto) + { + DumpHex(std::cout, tmp_3, len); + std::cout << std::endl; + } + + if (tmp_3[len - 1] > 0x8) + return false; + + len = PkcsUnpad(tmp_3, len); + + // memcpy(aes_key, tmp_3 + 12, 0x10); + m_aes.SetKey(tmp_3 + 12, AES::AES_128); + + if (g_debug & Dbg_Crypto) + std::cout << "HMAC SHA1 Key:" << std::endl; + + len = hdr.wrapped_hmac_sha1_key_len; + + des.SetIV(des_iv); + des.DecryptCBC(tmp_1, hdr.wrapped_hmac_sha1_key, len); + + if (g_debug & Dbg_Crypto) + DumpHex(std::cout, tmp_1, len); + + if (tmp_1[len - 1] > 0x8) + return false; + + len = PkcsUnpad(tmp_1, len); + + for (n = 0; n < len; n++) + tmp_2[len - n - 1] = tmp_1[n]; + + if (g_debug & Dbg_Crypto) + DumpHex(std::cout, tmp_2, len); + + des.SetIV(nullptr); + + des.DecryptCBC(tmp_3, tmp_2, len); + + if (g_debug & Dbg_Crypto) + { + DumpHex(std::cout, tmp_3, len); + std::cout << std::endl; + } + + if (tmp_3[len - 1] > 0x8) + return false; + + len = PkcsUnpad(tmp_3, len); + + // memcpy(hmac_key, tmp_3 + 12, 0x14); + memcpy(m_hmac_key, tmp_3 + 12, 0x14); + + if (g_debug & Dbg_Crypto) + std::cout << "Integrity Key:" << std::endl; + + len = hdr.wrapped_integrity_key_len; + des.SetIV(des_iv); + des.DecryptCBC(tmp_1, hdr.wrapped_integrity_key, len); + + if (g_debug & Dbg_Crypto) + DumpHex(std::cout, tmp_1, len); + + if (tmp_1[len - 1] > 0x8) + return false; + + len = PkcsUnpad(tmp_1, len); + + for (n = 0; n < len; n++) + tmp_2[len - n - 1] = tmp_1[n]; + + if (g_debug & Dbg_Crypto) + DumpHex(std::cout, tmp_2, len); + + des.SetIV(tmp_2); + len -= 8; + + des.DecryptCBC(tmp_3, tmp_2 + 8, len); + + if (g_debug & Dbg_Crypto) + { + DumpHex(std::cout, tmp_3, len); + std::cout << std::endl; + + std::cout << "Unknown Key:" << std::endl; + DumpHex(std::cout, hdr.unk_3E8, hdr.unk_3E8_len); + } + + return true; +} + +bool DiskImageFile::SetupEncryptionV2() +{ + std::vector data; + std::vector kdata; + + const DmgCryptHeaderV2 *hdr; + const DmgKeyPointer *keyptr; + const DmgKeyData *keydata; + std::string password; + uint32_t no_of_keys; + uint32_t key_id; + + uint8_t derived_key[0x18]; + uint8_t blob[0x200]; + uint32_t blob_len; + + TripleDES des; + + bool key_ok = false; + + data.resize(0x1000); + + m_image.seekg(0); + m_image.read(reinterpret_cast(data.data()), data.size()); + + hdr = reinterpret_cast(data.data()); + + if (memcmp(hdr->signature, "encrcdsa", 8)) + return false; + + m_crypt_offset = hdr->encrypted_data_offset; + m_crypt_size = hdr->encrypted_data_length; + m_crypt_blocksize = hdr->block_size; + + std::cout << "Encryped DMG detected." << std::endl; + std::cout << "Password: "; + GetPassword(password); + + no_of_keys = hdr->no_of_keys; + + for (key_id = 0; key_id < no_of_keys; key_id++) + { + keyptr = reinterpret_cast(data.data() + sizeof(DmgCryptHeaderV2) + key_id * sizeof(DmgKeyPointer)); + + kdata.resize(keyptr->key_length); + m_image.seekg(keyptr->key_offset.get()); + m_image.read(reinterpret_cast(kdata.data()), kdata.size()); + + keydata = reinterpret_cast(kdata.data()); + + PBKDF2_HMAC_SHA1(reinterpret_cast(password.c_str()), password.size(), keydata->salt, keydata->salt_len, keydata->iteration_count, derived_key, sizeof(derived_key)); + + des.SetKey(derived_key); + des.SetIV(keydata->blob_enc_iv); + + blob_len = keydata->encr_key_blob_size; + + des.DecryptCBC(blob, keydata->encr_key_blob, blob_len); + + if (blob[blob_len - 1] < 1 || blob[blob_len - 1] > 8) + continue; + blob_len -= blob[blob_len - 1]; + + if (g_debug & Dbg_Crypto) + { + std::cout << "Salt: " << hexstr(keydata->salt, keydata->salt_len) << std::endl; + std::cout << "Iter: " << keydata->iteration_count << std::endl; + std::cout << "DKey: " << hexstr(derived_key, sizeof(derived_key)) << std::endl; + std::cout << "Blob: " << hexstr(keydata->encr_key_blob, keydata->encr_key_blob_size) << std::endl; + std::cout << "DBlb: " << hexstr(blob, keydata->encr_key_blob_size) << std::endl; + std::cout << "Key : " << hexstr(blob, hdr->key_bits / 8) << std::endl; + std::cout << "HMAC: " << hexstr(blob + hdr->key_bits / 8, 0x14) << std::endl; + } + + if (memcmp(blob + blob_len - 5, "CKIE", 4)) + continue; + + if (hdr->key_bits == 128) + { + m_aes.SetKey(blob, AES::AES_128); + memcpy(m_hmac_key, blob + 0x10, 0x14); + key_ok = true; + break; + } + else if (hdr->key_bits == 256) + { + m_aes.SetKey(blob, AES::AES_256); + memcpy(m_hmac_key, blob + 0x20, 0x14); + key_ok = true; + break; + } + } + + return key_ok; +} + +size_t DiskImageFile::PkcsUnpad(const uint8_t *data, size_t size) +{ + if (data[size - 1] >= 1 && data[size - 1] <= 8) + size -= data[size - 1]; + return size; +} diff --git a/vendor/apfs-fuse/ApfsLib/DiskImageFile.h b/vendor/apfs-fuse/ApfsLib/DiskImageFile.h new file mode 100644 index 0000000..6bdfa68 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/DiskImageFile.h @@ -0,0 +1,39 @@ +#pragma once + +#include +#include + +#include +#include "Device.h" + +class DiskImageFile +{ +public: + DiskImageFile(); + ~DiskImageFile(); + + bool Open(const char *name); + void Close(); + void Reset(); + + void Read(uint64_t off, void *data, size_t size); + + uint64_t GetContentSize() const { return m_crypt_size; } + + bool CheckSetupEncryption(); + +private: + bool SetupEncryptionV1(); + bool SetupEncryptionV2(); + size_t PkcsUnpad(const uint8_t *data, size_t size); + + std::ifstream m_image; + + bool m_is_encrypted; + uint64_t m_crypt_offset; + uint64_t m_crypt_size; + uint32_t m_crypt_blocksize; + uint8_t m_hmac_key[0x14]; + + AES m_aes; +}; diff --git a/vendor/apfs-fuse/ApfsLib/DiskStruct.h b/vendor/apfs-fuse/ApfsLib/DiskStruct.h new file mode 100644 index 0000000..aac7020 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/DiskStruct.h @@ -0,0 +1,1515 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#pragma once + +#include + +#include "ApfsTypes.h" +#include "Endian.h" + +#pragma pack(push) +#pragma pack(1) + +#ifdef _MSC_VER +#pragma warning(disable: 4200) +#endif + +typedef le_uint64_t le_paddr_t; +typedef le_uint64_t le_oid_t; +typedef le_uint64_t le_xid_t; + +struct prange_t { + le_paddr_t pr_start_addr; + le_uint64_t pr_block_count; +}; + +typedef uint32_t crypto_flags_t; +typedef uint32_t cp_key_class_t; +typedef uint32_t cp_key_os_version_t; +typedef uint16_t cp_key_revision_t; +typedef struct cpx* cpx_t; + +typedef le_uint32_t le_crypto_flags_t; +typedef le_uint32_t le_cp_key_class_t; +typedef le_uint32_t le_cp_key_os_version_t; +typedef le_uint16_t le_cp_key_revision_t; + +struct wrapped_crypto_state_t { + le_uint16_t major_version; + le_uint16_t minor_version; + le_crypto_flags_t cpflags; + le_cp_key_class_t persistent_class; + le_cp_key_os_version_t key_os_version; + le_cp_key_revision_t key_revision; + le_uint16_t key_len; + uint8_t persistent_key[0]; +}; + +struct wrapped_meta_crypto_state_t { + le_uint16_t major_version; + le_uint16_t minor_version; + le_crypto_flags_t cpflags; + le_cp_key_class_t persistent_class; + le_cp_key_os_version_t key_os_version; + le_cp_key_revision_t key_revision; + le_uint16_t unused; +}; + + +constexpr size_t MAX_CKSUM_SIZE = 8; + +struct obj_phys_t { + uint8_t o_cksum[MAX_CKSUM_SIZE]; + le_oid_t o_oid; + le_xid_t o_xid; + le_uint32_t o_type; + le_uint32_t o_subtype; +}; + +constexpr uint64_t OID_NX_SUPERBLOCK = 1; + +constexpr uint64_t OID_INVALID = 0; +constexpr uint64_t OID_RESERVED_COUNT = 1024; + +constexpr uint32_t OBJECT_TYPE_MASK = 0x0000FFFF; +constexpr uint32_t OBJECT_TYPE_FLAGS_MASK = 0xFFFF0000; + +constexpr uint32_t OBJECT_TYPE_FLAGS_DEFINED_MASK = 0xF8000000; + +constexpr uint32_t OBJECT_TYPE_NX_SUPERBLOCK = 0x00000001; + +constexpr uint32_t OBJECT_TYPE_BTREE = 0x00000002; +constexpr uint32_t OBJECT_TYPE_BTREE_NODE = 0x00000003; + +constexpr uint32_t OBJECT_TYPE_SPACEMAN = 0x00000005; +constexpr uint32_t OBJECT_TYPE_SPACEMAN_CAB = 0x00000006; +constexpr uint32_t OBJECT_TYPE_SPACEMAN_CIB = 0x00000007; +constexpr uint32_t OBJECT_TYPE_SPACEMAN_BITMAP = 0x00000008; +constexpr uint32_t OBJECT_TYPE_SPACEMAN_FREE_QUEUE = 0x00000009; + +constexpr uint32_t OBJECT_TYPE_EXTENT_LIST_TREE = 0x0000000A; +constexpr uint32_t OBJECT_TYPE_OMAP = 0x0000000B; +constexpr uint32_t OBJECT_TYPE_CHECKPOINT_MAP = 0x0000000C; +constexpr uint32_t OBJECT_TYPE_FS = 0x0000000D; +constexpr uint32_t OBJECT_TYPE_FSTREE = 0x0000000E; +constexpr uint32_t OBJECT_TYPE_BLOCKREFTREE = 0x0000000F; +constexpr uint32_t OBJECT_TYPE_SNAPMETATREE = 0x00000010; + +constexpr uint32_t OBJECT_TYPE_NX_REAPER = 0x00000011; +constexpr uint32_t OBJECT_TYPE_NX_REAP_LIST = 0x00000012; +constexpr uint32_t OBJECT_TYPE_OMAP_SNAPSHOT = 0x00000013; +constexpr uint32_t OBJECT_TYPE_EFI_JUMPSTART = 0x00000014; +constexpr uint32_t OBJECT_TYPE_FUSION_MIDDLE_TREE = 0x00000015; +constexpr uint32_t OBJECT_TYPE_NX_FUSION_WBC = 0x00000016; +constexpr uint32_t OBJECT_TYPE_NX_FUSION_WBC_LIST = 0x00000017; +constexpr uint32_t OBJECT_TYPE_ER_STATE = 0x00000018; + +constexpr uint32_t OBJECT_TYPE_GBITMAP = 0x00000019; +constexpr uint32_t OBJECT_TYPE_GBITMAP_TREE = 0x0000001A; +constexpr uint32_t OBJECT_TYPE_GBITMAP_BLOCK = 0x0000001B; + +constexpr uint32_t OBJECT_TYPE_ER_RECOVERY_BLOCK = 0x0000001C; +constexpr uint32_t OBJECT_TYPE_SNAP_META_EXT = 0x0000001D; +constexpr uint32_t OBJECT_TYPE_INTEGRITY_META = 0x0000001E; +constexpr uint32_t OBJECT_TYPE_FEXT_TREE = 0x0000001F; +constexpr uint32_t OBJECT_TYPE_RESERVED_20 = 0x00000020; + +constexpr uint32_t OBJECT_TYPE_INVALID = 0; +constexpr uint32_t OBJECT_TYPE_TEST = 0x000000FF; + +constexpr uint32_t OBJECT_TYPE_CONTAINER_KEYBAG = 0x7379656B; // 'keys' +constexpr uint32_t OBJECT_TYPE_VOLUME_KEYBAG = 0x73636572; // 'recs' +constexpr uint32_t OBJECT_TYPE_MEDIA_KEYBAG = 0x79656B6D; // 'mkey' + +constexpr uint32_t OBJ_STORAGETYPE_MASK = 0xC0000000; + +constexpr uint32_t OBJ_VIRTUAL = 0x00000000; +constexpr uint32_t OBJ_PHYSICAL = 0x40000000; +constexpr uint32_t OBJ_EPHEMERAL = 0x80000000; + +constexpr uint32_t OBJ_NOHEADER = 0x20000000; +constexpr uint32_t OBJ_ENCRYPTED = 0x10000000; +constexpr uint32_t OBJ_NONPERSISTENT = 0x08000000; + + +constexpr uint32_t NX_EFI_JUMPSTART_MAGIC = 0x5244534A; // RDSJ +constexpr uint32_t NX_EFI_JUMPSTART_VERSION = 1; + +struct nx_efi_jumpstart_t { + obj_phys_t nej_o; + le_uint32_t nej_magic; + le_uint32_t nej_version; + le_uint32_t nej_efi_file_len; + le_uint32_t nej_num_extents; + uint64_t nej_reserved[16]; + prange_t nej_rec_extents[]; +}; + + +constexpr uint32_t NX_MAGIC = 0x4253584E; // BSXN +constexpr int NX_MAX_FILE_SYSTEMS = 100; + +constexpr int NX_EPH_INFO_COUNT = 4; +constexpr int NX_EPH_MIN_BLOCK_COUNT = 8; +constexpr int NX_MAX_FILE_SYSTEM_EPH_STRUCTS = 4; +constexpr int NX_TX_MIN_CHECKPOINT_COUNT = 4; +constexpr int NX_EPH_INFO_VERSION_1 = 1; + +constexpr uint64_t NX_RESERVED_1 = 1; +constexpr uint64_t NX_RESERVED_2 = 2; +constexpr uint64_t NX_CRYPTO_SW = 4; + +constexpr uint64_t NX_FEATURE_DEFRAG = 1; +constexpr uint64_t NX_FEATURE_LCFD = 2; +constexpr uint64_t NX_SUPPORTED_FEATURES_MASK = NX_FEATURE_DEFRAG | NX_FEATURE_LCFD; + +constexpr uint64_t NX_SUPPORTED_ROCOMPAT_MASK = 0; + +constexpr uint64_t NX_INCOMPAT_VERSION1 = 1; +constexpr uint64_t NX_INCOMPAT_VERSION2 = 2; +constexpr uint64_t NX_INCOMPAT_FUSION = 0x100; +constexpr uint64_t NX_SUPPORTED_INCOMPAT_MASK = NX_INCOMPAT_VERSION2 | NX_INCOMPAT_FUSION; + +constexpr int NX_MINIMUM_BLOCK_SIZE = 4096; +constexpr int NX_DEFAULT_BLOCK_SIZE = 4096; +constexpr int NX_MAXIMUM_BLOCK_SIZE = 65536; + +constexpr uint64_t NX_MINIMUM_CONTAINER_SIZE = 1048576; + +enum nx_counter_id_t { + NX_CNTR_OBJ_CKSUM_SET = 0, + NX_CNTR_OBJ_CKSUM_FAIL = 1, + + NX_NUM_COUNTERS = 32 +}; + +struct nx_superblock_t { + obj_phys_t nx_o; + le_uint32_t nx_magic; + le_uint32_t nx_block_size; + le_uint64_t nx_block_count; + + le_uint64_t nx_features; + le_uint64_t nx_readonly_compatible_features; + le_uint64_t nx_incompatible_features; + + apfs_uuid_t nx_uuid; + + le_oid_t nx_next_oid; + le_xid_t nx_next_xid; + + le_uint32_t nx_xp_desc_blocks; + le_uint32_t nx_xp_data_blocks; + le_paddr_t nx_xp_desc_base; + le_paddr_t nx_xp_data_base; + le_uint32_t nx_xp_desc_next; + le_uint32_t nx_xp_data_next; + le_uint32_t nx_xp_desc_index; + le_uint32_t nx_xp_desc_len; + le_uint32_t nx_xp_data_index; + le_uint32_t nx_xp_data_len; + + le_oid_t nx_spaceman_oid; + le_oid_t nx_omap_oid; + le_oid_t nx_reaper_oid; + + le_uint32_t nx_test_type; + + le_uint32_t nx_max_file_systems; + le_oid_t nx_fs_oid[NX_MAX_FILE_SYSTEMS]; + le_uint64_t nx_counters[NX_NUM_COUNTERS]; + prange_t nx_blocked_out_prange; + le_oid_t nx_evict_mapping_tree_oid; + le_uint64_t nx_flags; + le_paddr_t nx_efi_jumpstart; + apfs_uuid_t nx_fusion_uuid; + prange_t nx_keylocker; + le_uint64_t nx_ephemeral_info[NX_EPH_INFO_COUNT]; + + le_oid_t nx_test_oid; + + le_oid_t nx_fusion_mt_oid; + le_oid_t nx_fusion_wbc_oid; + prange_t nx_fusion_wbc; + + le_uint64_t nx_newest_mounted_version; + + prange_t nx_mkb_locker; +}; + + +constexpr uint32_t CHECKPOINT_MAP_LAST = 0x00000001; + + +struct checkpoint_mapping_t { + le_uint32_t cpm_type; + le_uint32_t cpm_subtype; + le_uint32_t cpm_size; + le_uint32_t cpm_pad; + le_oid_t cpm_fs_oid; + le_oid_t cpm_oid; + le_oid_t cpm_paddr; +}; + +struct checkpoint_map_phys_t { + obj_phys_t cpm_o; + le_uint32_t cpm_flags; + le_uint32_t cpm_count; + checkpoint_mapping_t cpm_map[]; +}; + +struct evict_mapping_val_t { + le_paddr_t dst_paddr; + le_uint64_t len; +}; + + +constexpr uint32_t OMAP_VAL_DELETED = 0x00000001; +constexpr uint32_t OMAP_VAL_SAVED = 0x00000002; +constexpr uint32_t OMAP_VAL_ENCRYPTED = 0x00000004; +constexpr uint32_t OMAP_VAL_NOHEADER = 0x00000008; +constexpr uint32_t OMAP_VAL_CRYPTO_GENERATION = 0x00000010; + +constexpr uint32_t OMAP_SNAPSHOT_DELETED = 0x00000001; +constexpr uint32_t OMAP_SNAPSHOT_REVERTED = 0x00000002; + +constexpr uint32_t OMAP_MANUALLY_MANAGED = 0x00000001; +constexpr uint32_t OMAP_ENCRYPTING = 0x00000002; +constexpr uint32_t OMAP_DECRYPTING = 0x00000004; +constexpr uint32_t OMAP_KEYROLLING = 0x00000008; +constexpr uint32_t OMAP_CRYPTO_GENERATION = 0x00000010; + +constexpr uint32_t OMAP_VALID_FLAGS = 0x0000001F; + +constexpr uint32_t OMAP_MAX_SNAP_COUNT = UINT32_MAX; + +constexpr int OMAP_REAP_PHASE_MAP_TREE = 1; +constexpr int OMAP_REAP_PHASE_SNAPSHOT_TREE = 2; + +struct omap_phys_t { + obj_phys_t om_o; + le_uint32_t om_flags; + le_uint32_t om_snap_count; + le_uint32_t om_tree_type; + le_uint32_t om_snapshot_tree_type; + le_oid_t om_tree_oid; + le_oid_t om_snapshot_tree_oid; + le_xid_t om_most_recent_snap; + le_xid_t om_pending_revert_min; + le_xid_t om_pending_revert_max; +}; + +struct omap_key_t { + le_oid_t ok_oid; + le_oid_t ok_xid; +}; + +struct omap_val_t { + le_uint32_t ov_flags; + le_uint32_t ov_size; + le_paddr_t ov_paddr; +}; + +struct omap_snapshot_t { + le_uint32_t oms_flags; + le_uint32_t oms_pad; + le_oid_t oms_oid; +}; + + +constexpr uint32_t APFS_MAGIC = 0x42535041; // BSPA +constexpr int APFS_MAX_HIST = 8; +constexpr int APFS_VOLNAME_LEN = 256; + + +constexpr uint64_t APFS_FS_UNENCRYPTED = 0x01; +constexpr uint64_t APFS_FS_RESERVED_2 = 0x02; // APFS_FS_EFFACEABLE in older specs +constexpr uint64_t APFS_FS_RESERVED_4 = 0x04; +constexpr uint64_t APFS_FS_ONEKEY = 0x08; +constexpr uint64_t APFS_FS_SPILLEDOVER = 0x10; +constexpr uint64_t APFS_FS_RUN_SPILLOVER_CLEANER = 0x20; +constexpr uint64_t APFS_FS_ALWAYS_CHECK_EXTENTREF = 0x40; +constexpr uint64_t APFS_FS_RESERVED_80 = 0x80; +constexpr uint64_t APFS_FS_RESERVED_100 = 0x100; + + +constexpr uint64_t APFS_FS_FLAGS_VALID_MASK = APFS_FS_UNENCRYPTED | APFS_FS_RESERVED_2 | APFS_FS_RESERVED_4 | APFS_FS_ONEKEY + | APFS_FS_SPILLEDOVER | APFS_FS_RUN_SPILLOVER_CLEANER | APFS_FS_ALWAYS_CHECK_EXTENTREF | APFS_FS_RESERVED_80 | APFS_FS_RESERVED_100; + +constexpr uint64_t APFS_FS_CRYPTOFLAGS = APFS_FS_UNENCRYPTED | APFS_FS_ONEKEY; + +constexpr int APFS_VOLUME_ENUM_SHIFT = 6; + +constexpr uint16_t APFS_VOL_ROLE_NONE = 0x0000; + +constexpr uint16_t APFS_VOL_ROLE_SYSTEM = 0x0001; +constexpr uint16_t APFS_VOL_ROLE_USER = 0x0002; +constexpr uint16_t APFS_VOL_ROLE_RECOVERY = 0x0004; +constexpr uint16_t APFS_VOL_ROLE_VM = 0x0008; + +constexpr uint16_t APFS_VOL_ROLE_PREBOOT = 0x0010; +constexpr uint16_t APFS_VOL_ROLE_INSTALLER = 0x0020; +constexpr uint16_t APFS_VOL_ROLE_DATA = 1 << APFS_VOLUME_ENUM_SHIFT; +constexpr uint16_t APFS_VOL_ROLE_BASEBAND = 2 << APFS_VOLUME_ENUM_SHIFT; +constexpr uint16_t APFS_VOL_ROLE_UPDATE = 3 << APFS_VOLUME_ENUM_SHIFT; +constexpr uint16_t APFS_VOL_ROLE_XART = 4 << APFS_VOLUME_ENUM_SHIFT; +constexpr uint16_t APFS_VOL_ROLE_HARDWARE = 5 << APFS_VOLUME_ENUM_SHIFT; +constexpr uint16_t APFS_VOL_ROLE_BACKUP = 6 << APFS_VOLUME_ENUM_SHIFT; +constexpr uint16_t APFS_VOL_ROLE_RESERVED_7 = 7 << APFS_VOLUME_ENUM_SHIFT; +constexpr uint16_t APFS_VOL_ROLE_RESERVED_8 = 8 << APFS_VOLUME_ENUM_SHIFT; +constexpr uint16_t APFS_VOL_ROLE_ENTERPRISE = 9 << APFS_VOLUME_ENUM_SHIFT; +constexpr uint16_t APFS_VOL_ROLE_RESERVED_10 = 10 << APFS_VOLUME_ENUM_SHIFT; +constexpr uint16_t APFS_VOL_ROLE_PRELOGIN = 11 << APFS_VOLUME_ENUM_SHIFT; + +constexpr uint16_t APFS_VOL_ROLE_RESERVED_200 = 0x0200; + + +constexpr uint64_t APFS_FEATURE_DEFRAG_PRERELEASE = 1; +constexpr uint64_t APFS_FEATURE_HARDLINK_MAP_RECORDS = 2; +constexpr uint64_t APFS_FEATURE_DEFRAG = 4; +constexpr uint64_t APFS_FEATURE_STRICTATIME = 8; +constexpr uint64_t APFS_FEATURE_VOLGRP_SYSTEM_INO_SPACE = 0x10; + +constexpr uint64_t APFS_SUPPORTED_FEATURES_MASK = APFS_FEATURE_DEFRAG | APFS_FEATURE_DEFRAG_PRERELEASE | APFS_FEATURE_HARDLINK_MAP_RECORDS | APFS_FEATURE_STRICTATIME | APFS_FEATURE_VOLGRP_SYSTEM_INO_SPACE; + + +constexpr uint64_t APFS_SUPPORTED_ROCOMPAT_MASK = 0; + + +constexpr uint64_t APFS_INCOMPAT_CASE_INSENSITIVE = 1; +constexpr uint64_t APFS_INCOMPAT_DATALESS_SNAPS = 2; +constexpr uint64_t APFS_INCOMPAT_ENC_ROLLED = 4; +constexpr uint64_t APFS_INCOMPAT_NORMALIZATION_INSENSITIVE = 8; +constexpr uint64_t APFS_INCOMPAT_INCOMPLETE_RESTORE = 0x10; +constexpr uint64_t APFS_INCOMPAT_SEALED_VOLUME = 0x20; +constexpr uint64_t APFS_INCOMPAT_RESERVED_40 = 0x40; + +constexpr uint64_t APFS_SUPPORTED_INCOMPAT_MASK = APFS_INCOMPAT_CASE_INSENSITIVE | APFS_INCOMPAT_DATALESS_SNAPS | APFS_INCOMPAT_ENC_ROLLED + | APFS_INCOMPAT_NORMALIZATION_INSENSITIVE | APFS_INCOMPAT_INCOMPLETE_RESTORE | APFS_INCOMPAT_SEALED_VOLUME | APFS_INCOMPAT_RESERVED_40; + + +constexpr int APFS_MODIFIED_NAMELEN = 32; + +struct apfs_modified_by_t { + uint8_t id[APFS_MODIFIED_NAMELEN]; + le_uint64_t timestamp; + le_xid_t last_xid; +}; + +struct apfs_superblock_t { + obj_phys_t apfs_o; + + le_uint32_t apfs_magic; + le_uint32_t apfs_fs_index; + + le_uint64_t apfs_features; + le_uint64_t apfs_readonly_compatible_features; + le_uint64_t apfs_incompatible_features; + + le_uint64_t apfs_unmount_time; + + le_uint64_t apfs_fs_reserve_block_count; + le_uint64_t apfs_fs_quota_block_count; + le_uint64_t apfs_fs_alloc_count; + + wrapped_meta_crypto_state_t apfs_meta_crypto; + + le_uint32_t apfs_root_tree_type; + le_uint32_t apfs_extentref_tree_type; + le_uint32_t apfs_snap_meta_tree_type; + + le_oid_t apfs_omap_oid; + le_oid_t apfs_root_tree_oid; + le_oid_t apfs_extentref_tree_oid; + le_oid_t apfs_snap_meta_tree_oid; + + le_xid_t apfs_revert_to_xid; + le_oid_t apfs_revert_to_sblock_oid; + + le_uint64_t apfs_next_obj_id; + + le_uint64_t apfs_num_files; + le_uint64_t apfs_num_directories; + le_uint64_t apfs_num_symlinks; + le_uint64_t apfs_num_other_fsobjects; + le_uint64_t apfs_num_snapshots; + + le_uint64_t apfs_total_blocks_alloced; + le_uint64_t apfs_total_blocks_freed; + + apfs_uuid_t apfs_vol_uuid; + le_uint64_t apfs_last_mod_time; + + le_uint64_t apfs_fs_flags; + + apfs_modified_by_t apfs_formatted_by; + apfs_modified_by_t apfs_modified_by[APFS_MAX_HIST]; + + uint8_t apfs_volname[APFS_VOLNAME_LEN]; + le_uint32_t apfs_next_doc_id; + + le_uint16_t apfs_role; + le_uint16_t reserved; + + le_xid_t apfs_root_to_xid; + le_oid_t apfs_er_state_oid; + + le_uint64_t apfs_cloneinfo_id_epoch; + le_uint64_t apfs_cloneinfo_xid; + + le_oid_t apfs_snap_meta_ext_oid; + + apfs_uuid_t apfs_volume_group_id; + + le_oid_t apfs_integrity_meta_oid; + + le_oid_t apfs_fext_tree_oid; + le_uint32_t apfs_fext_tree_type; + + le_uint32_t reserved_type; + le_oid_t reserved_oid; +}; + + + +struct j_key_t { + le_uint64_t obj_id_and_type; +}; + +constexpr uint64_t OBJ_ID_MASK = 0x0FFFFFFFFFFFFFFFULL; +constexpr uint64_t OBJ_TYPE_MASK = 0xF000000000000000ULL; +constexpr int OBJ_TYPE_SHIFT = 60; + + +struct j_inode_key_t { + j_key_t hdr; +}; + +struct j_inode_val_t { + le_uint64_t parent_id; + le_uint64_t private_id; + + le_uint64_t create_time; + le_uint64_t mod_time; + le_uint64_t change_time; + le_uint64_t access_time; + + le_uint64_t internal_flags; + + union { + le_int32_t nchildren; + le_int32_t nlink; + }; + + le_cp_key_class_t default_protection_class; + + le_uint32_t write_generation_counter; + le_uint32_t bsd_flags; + le_uint32_t owner; + le_uint32_t group; + le_uint16_t mode; + le_uint16_t pad1; + le_uint64_t uncompressed_size; + uint8_t xfields[]; +}; + + +constexpr uint32_t J_DREC_LEN_MASK = 0x000003FF; +constexpr uint32_t J_DREC_HASH_MASK = 0xFFFFFC00; +constexpr int J_DREC_HASH_SHIFT = 10; + +struct j_drec_key_t { + j_key_t hdr; + le_uint16_t name_len; + uint8_t name[]; +}; + +struct j_drec_hashed_key_t { + j_key_t hdr; + le_uint32_t name_len_and_hash; + uint8_t name[]; +}; + +struct j_drec_val_t { + le_uint64_t file_id; + le_uint64_t date_added; + le_uint16_t flags; + uint8_t xfields[]; +}; + + +struct j_dir_stats_key_t { + j_key_t hdr; +}; + +struct j_dir_stats_val_t { + le_uint64_t num_children; + le_uint64_t total_size; + le_uint64_t chained_key; + le_uint64_t gen_count; +}; + + +struct j_xattr_key_t { + j_key_t hdr; + le_uint16_t name_len; + uint8_t name[0]; +}; + +struct j_xattr_val_t { + le_uint16_t flags; + le_uint16_t xdata_len; + uint8_t xdata[0]; +}; + + +enum j_obj_types { + APFS_TYPE_ANY = 0, + + APFS_TYPE_SNAP_METADATA = 1, + APFS_TYPE_EXTENT = 2, + APFS_TYPE_INODE = 3, + APFS_TYPE_XATTR = 4, + APFS_TYPE_SIBLING_LINK = 5, + APFS_TYPE_DSTREAM_ID = 6, + APFS_TYPE_CRYPTO_STATE = 7, + APFS_TYPE_FILE_EXTENT = 8, + APFS_TYPE_DIR_REC = 9, + APFS_TYPE_DIR_STATS = 10, + APFS_TYPE_SNAP_NAME = 11, + APFS_TYPE_SIBLING_MAP = 12, + APFS_TYPE_FILE_INFO = 13, + + APFS_TYPE_MAX_VALID = 13, + APFS_TYPE_MAX = 15, + + APFS_TYPE_INVALID = 15 +}; + +enum j_obj_kinds { + APFS_KIND_ANY = 0, + APFS_KIND_NEW = 1, + APFS_KIND_UPDATE = 2, + APFS_KIND_DEAD = 3, + APFS_KIND_UPDATE_REFCNT = 4, + + APFS_KIND_INVALID = 255 +}; + +enum j_inode_flags { + INODE_IS_APFS_PRIVATE = 0x00000001, + INODE_MAINTAIN_DIR_STATS = 0x00000002, + INODE_DIR_STATS_ORIGIN = 0x00000004, + INODE_PROT_CLASS_EXPLICIT = 0x00000008, + INODE_WAS_CLONED = 0x00000010, + INODE_FLAGS_UNUSED = 0x00000020, + INODE_HAS_SECURITY_EA = 0x00000040, + INODE_BEING_TRUNCATED = 0x00000080, + INODE_HAS_FINDER_INFO = 0x00000100, + INODE_IS_SPARSE = 0x00000200, + INODE_WAS_EVER_CLONED = 0x00000400, + INODE_ACTIVE_FILE_TRIMMED = 0x00000800, + INODE_PINNED_TO_MAIN = 0x00001000, + INODE_PINNED_TO_TIER2 = 0x00002000, + INODE_HAS_RSRC_FORK = 0x00004000, + INODE_NO_RSRC_FORK = 0x00008000, + INODE_ALLOCATION_SPILLEDOVER = 0x00010000, + INODE_FAST_PROMOTE = 0x00020000, + INODE_HAS_UNCOMPRESSED_SIZE = 0x00040000, + INODE_IS_PURGEABLE = 0x00080000, + INODE_WANTS_TO_BE_PURGEABLE = 0x00100000, + INODE_IS_SYNC_ROOT = 0x00200000, + INODE_SNAPSHOT_COW_EXEMPTION = 0x00400000, + + INODE_INHERITED_INTERNAL_FLAGS = INODE_MAINTAIN_DIR_STATS | INODE_SNAPSHOT_COW_EXEMPTION, + INODE_CLONED_INTERNAL_FLAGS = INODE_HAS_RSRC_FORK | INODE_NO_RSRC_FORK | INODE_HAS_FINDER_INFO | INODE_SNAPSHOT_COW_EXEMPTION +}; + +enum j_inode_bsd_flags { + APFS_UF_NODUMP = 0x1, + APFS_UF_IMMUTABLE = 0x2, + APFS_UF_APPEND = 0x4, + APFS_UF_OPAQUE = 0x8, + APFS_UF_NOUNLINK = 0x10, // Reserved on macOS + APFS_UF_COMPRESSED = 0x20, + APFS_UF_TRACKED = 0x40, + APFS_UF_DATAVAULT = 0x80, + // 0x100 - 0x4000 reserved + APFS_UF_HIDDEN = 0x8000, + APFS_SF_ARCHIVED = 0x10000, + APFS_SF_IMMUTABLE = 0x20000, + APFS_SF_APPEND = 0x40000, + APFS_SF_RESTRICTED = 0x80000, + APFS_SF_NOUNLINK = 0x100000, + APFS_SF_SNAPSHOT = 0x200000, // Reserved on macOS + APFS_SF_FIRMLINK = 0x800000, + APFS_SF_DATALESS = 0x40000000 +}; + +constexpr uint64_t APFS_VALID_INTERNAL_INODE_FLAGS = +INODE_IS_APFS_PRIVATE | +INODE_MAINTAIN_DIR_STATS | +INODE_DIR_STATS_ORIGIN | +INODE_PROT_CLASS_EXPLICIT | +INODE_WAS_CLONED | +INODE_HAS_SECURITY_EA | +INODE_BEING_TRUNCATED | +INODE_HAS_FINDER_INFO | +INODE_IS_SPARSE | +INODE_WAS_EVER_CLONED | +INODE_ACTIVE_FILE_TRIMMED | +INODE_PINNED_TO_MAIN | +INODE_PINNED_TO_TIER2 | +INODE_HAS_RSRC_FORK | +INODE_NO_RSRC_FORK | +INODE_ALLOCATION_SPILLEDOVER | +INODE_FAST_PROMOTE | +INODE_HAS_UNCOMPRESSED_SIZE | +INODE_IS_PURGEABLE | +INODE_WANTS_TO_BE_PURGEABLE | +INODE_IS_SYNC_ROOT | +INODE_SNAPSHOT_COW_EXEMPTION; + +enum j_xattr_flags { + XATTR_DATA_STREAM = 0x0001, + XATTR_DATA_EMBEDDED = 0x0002, + XATTR_FILE_SYSTEM_OWNED = 0x0004, + XATTR_RESERVED_8 = 0x0008 +}; + +enum dir_rec_flags { + DREC_TYPE_MASK = 0x000F, + RESERVED_10 = 0x0010 +}; + +constexpr uint64_t INVALID_INO_NUM = 0; +constexpr uint64_t ROOT_DIR_PARENT = 1; +constexpr uint64_t ROOT_DIR_INO_NUM = 2; +constexpr uint64_t PRIV_DIR_INO_NUM = 3; +constexpr uint64_t SNAP_DIR_INO_NUM = 6; +constexpr uint64_t PURGEABLE_DIR_INO_NUM = 7; +constexpr uint64_t MIN_USER_INO_NUM = 16; + +constexpr uint64_t UNIFIED_ID_SPACE_MARK = 0x0800000000000000; + + +constexpr uint16_t XATTR_MAX_EMBEDDED_SIZE = 3804; +constexpr const char* SYMLINK_EA_NAME = "com.apple.fs.symlink"; +constexpr const char* FIRMLINK_EA_NAME = "com.apple.fs.firmlink"; +constexpr const char* APFS_COW_EXEMPT_COUNT_NAME = "com.apple.fs.cow-exempt-file-count"; + +constexpr uint64_t OWNING_OBJ_ID_INVALID = ~0ULL; +constexpr uint64_t OWNING_OBJ_ID_UNKNOWN = ~1ULL; + +constexpr uint16_t JOBJ_MAX_KEY_SIZE = 832; +constexpr uint16_t JOBJ_MAX_VALUE_SIZE = 3808; + +constexpr uint32_t MIN_DOC_ID = 3; + + +constexpr uint32_t FEXT_CRYPTO_ID_IS_TWEAK = 0x01; + +constexpr uint16_t MODE_S_IFMT = 0170000; +constexpr uint16_t MODE_S_IFIFO = 0010000; +constexpr uint16_t MODE_S_IFCHR = 0020000; +constexpr uint16_t MODE_S_IFDIR = 0040000; +constexpr uint16_t MODE_S_IFBLK = 0060000; +constexpr uint16_t MODE_S_IFREG = 0100000; +constexpr uint16_t MODE_S_IFLNK = 0120000; +constexpr uint16_t MODE_S_IFSOCK = 0140000; +constexpr uint16_t MODE_S_IFWHT = 0160000; + +constexpr uint16_t DT_UNKNOWN = 0; +constexpr uint16_t DT_FIFO = 1; +constexpr uint16_t DT_CHR = 2; +constexpr uint16_t DT_DIR = 4; +constexpr uint16_t DT_BLK = 6; +constexpr uint16_t DT_REG = 8; +constexpr uint16_t DT_LNK = 10; +constexpr uint16_t DT_SOCK = 12; +constexpr uint16_t DT_WHT = 14; + + +struct j_phys_ext_key_t { + j_key_t hdr; +}; + +struct j_phys_ext_val_t { + le_uint64_t len_and_kind; + le_uint64_t owning_obj_id; + le_uint32_t refcnt; +}; + +constexpr uint64_t PEXT_LEN_MASK = 0x0FFFFFFFFFFFFFFFULL; +constexpr uint64_t PEXT_KIND_MASK = 0xF000000000000000ULL; +constexpr int PEXT_KIND_SHIFT = 60; + + +struct j_file_extent_key_t { + j_key_t hdr; + le_uint64_t logical_addr; +}; + +struct j_file_extent_val_t { + le_uint64_t len_and_flags; + le_uint64_t phys_block_num; + le_uint64_t crypto_id; +}; + +constexpr uint64_t J_FILE_EXTENT_LEN_MASK = 0x00FFFFFFFFFFFFFFULL; +constexpr uint64_t J_FILE_EXTENT_FLAG_MASK = 0xFF00000000000000ULL; +constexpr int J_FILE_EXTENT_FLAG_SHIFT = 56; + + +struct j_dstream_id_key_t { + j_key_t hdr; +}; + +struct j_dstream_id_val_t { + le_uint32_t refcnt; +}; + + +struct j_dstream_t { + le_uint64_t size; + le_uint64_t alloced_size; + le_uint64_t default_crypto_id; + le_uint64_t total_bytes_written; + le_uint64_t total_bytes_read; +}; + +struct j_xattr_dstream_t { + le_uint64_t xattr_obj_id; + j_dstream_t dstream; +}; + + +struct xf_blob_t { + le_uint16_t xf_num_exts; + le_uint16_t xf_used_data; + uint8_t xf_data[]; +}; + +struct x_field_t { + uint8_t x_type; + uint8_t x_flags; + le_uint16_t x_size; +}; + +enum { + DREC_EXT_TYPE_SIBLING_ID = 1, + + INO_EXT_TYPE_SNAP_XID = 1, + INO_EXT_TYPE_DELTRA_TREE_OID = 2, + INO_EXT_TYPE_DOCUMENT_ID = 3, + INO_EXT_TYPE_NAME = 4, + INO_EXT_TYPE_PREV_FSIZE = 5, + INO_EXT_TYPE_RESERVED_6 = 6, + INO_EXT_TYPE_FINDER_INFO = 7, + INO_EXT_TYPE_DSTREAM = 8, + INO_EXT_TYPE_RESERVED_9 = 9, + INO_EXT_TYPE_DIR_STATS_KEY = 10, + INO_EXT_TYPE_FS_UUID = 11, + INO_EXT_TYPE_RESERVED_12 = 12, + INO_EXT_TYPE_SPARSE_BYTES = 13, + INO_EXT_TYPE_RDEV = 14, + INO_EXT_TYPE_PURGEABLE_FLAGS = 15, + INO_EXT_TYPE_ORIG_SYNC_ROOT_ID = 16 +}; + +constexpr uint8_t XF_DATA_DEPENDENT = 0x01; +constexpr uint8_t XF_DO_NOT_COPY = 0x02; +constexpr uint8_t XF_RESERVED_4 = 0x04; +constexpr uint8_t XF_CHILDREN_INHERIT = 0x08; +constexpr uint8_t XF_USER_FIELD = 0x10; +constexpr uint8_t XF_SYSTEM_FIELD = 0x20; +constexpr uint8_t XF_RESERVED_40 = 0x40; +constexpr uint8_t XF_RESERVED_80 = 0x80; + + +struct j_sibling_key_t { + j_key_t hdr; + le_uint64_t sibling_id; +}; + +struct j_sibling_val_t { + le_uint64_t parent_id; + le_uint16_t name_len; + uint8_t name[0]; +}; + + +struct j_sibling_map_key_t { + j_key_t hdr; +}; + +struct j_sibling_map_val_t { + le_uint64_t file_id; +}; + + +struct j_snap_metadata_key_t { + j_key_t hdr; +}; + +struct j_snap_metadata_val_t { + le_oid_t extentref_tree_oid; + le_oid_t sblock_oid; + le_uint64_t create_time; + le_uint64_t change_time; + le_uint64_t inum; + le_uint32_t extentref_tree_type; + le_uint32_t flags; + le_uint16_t name_len; + uint8_t name[0]; +}; + + +struct j_snap_name_key_t { + j_key_t hdr; + le_uint16_t name_len; + uint8_t name[0]; +}; + +struct j_snap_name_val_t { + le_xid_t snap_xid; +}; + + +enum snap_meta_flags { + SNAP_META_PENDING_DATALESS = 0x00000001, + SNAP_META_MERGE_IN_PROGRESS = 0x00000002 +}; + + +struct snap_meta_ext_t { + le_uint32_t sme_version; + + le_uint32_t sme_flags; + le_xid_t sme_snap_xid; + apfs_uuid_t sme_uuid; + + le_uint64_t sme_token; +}; + +struct snap_meta_ext_obj_phys_t { + obj_phys_t smeop_o; + snap_meta_ext_t smeop_sme; +}; + + +struct nloc_t { + le_uint16_t off; + le_uint16_t len; +}; + +struct btree_node_phys_t { + obj_phys_t btn_o; + le_uint16_t btn_flags; + le_uint16_t btn_level; + le_uint32_t btn_nkeys; + nloc_t btn_table_space; + nloc_t btn_free_space; + nloc_t btn_key_free_list; + nloc_t btn_val_free_list; + // le_uint64_t btn_data[]; + uint8_t btn_data[]; +}; + +struct btree_info_fixed_t { + le_uint32_t bt_flags; + le_uint32_t bt_node_size; + le_uint32_t bt_key_size; + le_uint32_t bt_val_size; +}; + +struct btree_info_t { + btree_info_fixed_t bt_fixed; + le_uint32_t bt_longest_key; + le_uint32_t bt_longest_val; + le_uint64_t bt_key_count; + le_uint64_t bt_node_count; +}; + +constexpr uint16_t BTREE_NODE_HASH_SIZE_MAX = 64; + +struct btn_index_node_val_t { + le_oid_t binv_child_oid; + uint8_t binv_child_hash[BTREE_NODE_HASH_SIZE_MAX]; +}; + +constexpr uint16_t BTOFF_INVALID = 0xFFFF; + +struct kvloc_t { + nloc_t k; + nloc_t v; +}; + +struct kvoff_t { + le_uint16_t k; + le_uint16_t v; +}; + + +constexpr uint32_t BTREE_UINT64_KEYS = 0x00000001; +constexpr uint32_t BTREE_SEQUENTIAL_INSERT = 0x00000002; +constexpr uint32_t BTREE_ALLOW_GHOSTS = 0x00000004; +constexpr uint32_t BTREE_EPHEMERAL = 0x00000008; +constexpr uint32_t BTREE_PHYSICAL = 0x00000010; +constexpr uint32_t BTREE_NONPERSISTENT = 0x00000020; +constexpr uint32_t BTREE_KV_NONALIGNED = 0x00000040; +constexpr uint32_t BTREE_HASHED = 0x00000080; +constexpr uint32_t BTREE_NOHEADER = 0x00000100; + +constexpr int BTREE_TOC_ENTRY_INCREMENT = 8; +constexpr int BTREE_TOC_ENTRY_MAX_UNUSED = 2 * BTREE_TOC_ENTRY_INCREMENT; + +constexpr uint16_t BTNODE_ROOT = 0x0001; +constexpr uint16_t BTNODE_LEAF = 0x0002; +constexpr uint16_t BTNODE_FIXED_KV_SIZE = 0x0004; +constexpr uint16_t BTNODE_HASHED = 0x0008; +constexpr uint16_t BTNODE_NOHEADER = 0x0010; + +constexpr uint16_t BTNODE_CHECK_KOFF_INVAL = 0x8000; + + +constexpr uint16_t BTREE_NODE_SIZE_DEFAULT = 4096; +constexpr uint32_t BTREE_NODE_MIN_ENTRY_COUNT = 4; + +enum { + INTEGRITY_META_VERSION_INVALID = 0, + INTEGRITY_META_VERSION_1 = 1, + INTEGRITY_META_VERSION_2 = 2, + INTEGRITY_META_VERSION_HIGHEST = INTEGRITY_META_VERSION_2 +}; + +constexpr uint32_t APFS_SEAL_BROKEN = 1; + +enum apfs_hash_type_t { + APFS_HASH_INVALID = 0, + APFS_HASH_SHA256 = 1, + APFS_HASH_SHA512_256 = 2, + APFS_HASH_SHA384 = 3, + APFS_HASH_SHA512 = 4, + + APFS_HASH_MIN = APFS_HASH_SHA256, + APFS_HASH_MAX = APFS_HASH_SHA512, + + APFS_HASH_DEFAULT = APFS_HASH_SHA256 +}; + +constexpr int APFS_HASH_CCSHA256_SIZE = 32; +constexpr int APFS_HASH_CCSHA512_256_SIZE = 32; +constexpr int APFS_HASH_CCSHA384_SIZE = 48; +constexpr int APFS_HASH_CCSHA512_SIZE = 64; + +constexpr int APFS_HASH_MAX_SIZE = 64; + +struct integrity_meta_phys_t { + obj_phys_t im_o; + le_uint32_t im_version; + le_uint32_t im_flags; + le_uint32_t im_hash_type; + le_uint32_t im_root_hash_offset; + le_xid_t im_broken_xid; + le_uint64_t im_reserved[9]; +}; + +struct fext_tree_key_t { + le_uint64_t private_id; + le_uint64_t logical_addr; +}; + +struct fext_tree_val_t { + le_uint64_t len_and_flags; + le_uint64_t phys_block_num; +}; + +struct j_file_info_key_t { + j_key_t hdr; + le_uint64_t info_and_lba; +}; + +constexpr uint64_t J_FILE_INFO_LBA_MASK = 0x00FFFFFFFFFFFFFF; +constexpr uint64_t J_FILE_INFO_TYPE_MASK = 0xFF00000000000000; +constexpr int J_FILE_INFO_TYPE_SHIFT = 56; + +struct j_file_data_hash_val_t { + le_uint16_t hashed_len; + uint8_t hash_size; + uint8_t hash[0]; +}; + +struct j_file_info_val_t { + union { + j_file_data_hash_val_t dhash; + }; +}; + +enum j_obj_file_info_type { + APFS_FILE_INFO_DATA_HASH = 1 +}; + + +struct chunk_info_t { + le_uint64_t ci_xid; + le_uint64_t ci_addr; + le_uint32_t ci_block_count; + le_uint32_t ci_free_count; + le_paddr_t ci_bitmap_addr; +}; + +struct chunk_info_block_t { + obj_phys_t cib_o; + le_uint32_t cib_index; + le_uint32_t cib_chunk_info_count; + chunk_info_t cib_chunk_info[]; +}; + +struct cib_addr_block_t { + obj_phys_t cab_o; + le_uint32_t cab_index; + le_uint32_t cab_cib_count; + le_paddr_t cab_cib_addr[]; +}; + +struct spaceman_free_queue_key_t { + le_xid_t sfqk_xid; + le_paddr_t sfqk_paddr; +}; + +typedef le_uint64_t spaceman_free_queue_val_t; + +struct spaceman_free_queue_entry_t { + spaceman_free_queue_key_t sfqe_key; + spaceman_free_queue_val_t sfqe_count; +}; + +struct spaceman_free_queue_t { + le_uint64_t sfq_count; + le_oid_t sfq_tree_oid; + le_xid_t sfq_oldest_xid; + le_uint16_t sfq_tree_node_limit; + le_uint16_t sfq_pad16; + le_uint32_t sfq_pad32; + le_uint64_t sfq_reserved; +}; + +struct spaceman_device_t { + le_uint64_t sm_block_count; + le_uint64_t sm_chunk_count; + le_uint32_t sm_cib_count; + le_uint32_t sm_cab_count; + le_uint64_t sm_free_count; + le_uint32_t sm_addr_offset; + le_uint32_t sm_reserved; + le_uint64_t sm_reserved2; +}; + +constexpr int SM_ALLOCZONE_INVALID_END_BOUNDARY = 0; +constexpr int SM_ALLOCZONE_NUM_PREVIOUS_BOUNDARIES = 7; +constexpr int SM_DATAZONE_ALLOCZONE_COUNT = 8; + +enum sfq { + SFQ_IP = 0, + SFQ_MAIN = 1, + SFQ_TIER2 = 2, + SFQ_COUNT = 3 +}; + +enum smdev { + SD_MAIN = 0, + SD_TIER2 = 1, + SD_COUNT = 2 +}; + +struct spaceman_allocation_zone_boundaries_t { + le_uint64_t saz_zone_start; + le_uint64_t saz_zone_end; +}; + +struct spaceman_allocation_zone_info_phys_t { + spaceman_allocation_zone_boundaries_t saz_current_boundaries; + spaceman_allocation_zone_boundaries_t saz_previous_boundaries[SM_ALLOCZONE_NUM_PREVIOUS_BOUNDARIES]; + le_uint16_t saz_zone_id; + le_uint16_t saz_previous_boundary_index; + le_uint32_t saz_reserved; +}; + +struct spaceman_datazone_info_phys_t { + spaceman_allocation_zone_info_phys_t sdz_allocation_zones[SD_COUNT][SM_DATAZONE_ALLOCZONE_COUNT]; +}; + +struct spaceman_phys_t { + obj_phys_t sm_o; + le_uint32_t sm_block_size; + le_uint32_t sm_blocks_per_chunk; + le_uint32_t sm_chunks_per_cib; + le_uint32_t sm_cibs_per_cab; + spaceman_device_t sm_dev[SD_COUNT]; + le_uint32_t sm_flags; + le_uint32_t sm_ip_bm_tx_multiplier; + le_uint64_t sm_ip_block_count; + le_uint32_t sm_ip_bm_size_in_blocks; + le_uint32_t sm_ip_bm_block_count; + le_paddr_t sm_ip_bm_base; + le_paddr_t sm_ip_base; + le_uint64_t sm_fs_reserve_block_count; + le_uint64_t sm_fs_reserve_alloc_count; + spaceman_free_queue_t sm_fq[SFQ_COUNT]; + le_uint16_t sm_ip_bm_free_head; + le_uint16_t sm_ip_bm_free_tail; + le_uint32_t sm_ip_bm_xid_offset; + le_uint32_t sm_ip_bitmap_offset; + le_uint32_t sm_ip_bm_free_next_offset; + le_uint32_t sm_version; + le_uint32_t sm_struct_size; + spaceman_datazone_info_phys_t sm_datazone; +}; + +constexpr uint32_t SM_FLAG_VERSIONED = 0x00000001; + +constexpr uint32_t CI_COUNT_MASK = 0x000FFFFF; +constexpr uint32_t CI_COUNT_RESERVED_MASK = 0xFFF00000; + + +constexpr uint32_t SPACEMAN_IP_BM_TX_MULTIPLIER = 16; +constexpr uint16_t SPACEMAN_IP_BM_INDEX_INVALID = 0xFFFF; +constexpr uint16_t SPACEMAN_IP_BM_BLOCK_COUNT_MAX = 0xFFFE; + + +struct nx_reaper_phys_t { + obj_phys_t nr_o; + le_uint64_t nr_next_reap_id; + le_uint64_t nr_completed_id; + le_oid_t nr_head; + le_oid_t nr_tail; + le_uint32_t nr_flags; + le_uint32_t nr_rlcount; + le_uint32_t nr_type; + le_uint32_t nr_size; + le_oid_t nr_fs_oid; + le_oid_t nr_oid; + le_xid_t nr_xid; + le_uint32_t nr_nrle_flags; + le_uint32_t nr_state_buffer_size; + uint8_t nr_state_buffer[]; +}; + +struct nx_reap_list_entry_t { + le_uint32_t nrle_next; + le_uint32_t nrle_flags; + le_uint32_t nrle_type; + le_uint32_t nrle_size; + le_oid_t nrle_fs_oid; + le_oid_t nrle_oid; + le_xid_t nrle_xid; +}; + +struct nx_reap_list_phys_t { + obj_phys_t nrl_o; + le_oid_t nrl_next; + le_uint32_t nrl_flags; + le_uint32_t nrl_max; + le_uint32_t nrl_count; + le_uint32_t nrl_first; + le_uint32_t nrl_last; + le_uint32_t nrl_free; + nx_reap_list_entry_t nrl_entries[]; +}; + +enum { + APFS_REAP_PHASE_START = 0, + APFS_REAP_PHASE_SNAPSHOTS = 1, + APFS_REAP_PHASE_ACTIVE_FS = 2, + APFS_REAP_PHASE_DESTROY_OMAP = 3, + APFS_REAP_PHASE_DONE = 4 +}; + +constexpr uint32_t NR_BHM_FLAG = 0x00000001; +constexpr uint32_t NR_CONTINUE = 0x00000002; + +constexpr uint32_t NRLE_VALID = 0x00000001; +constexpr uint32_t NRLE_REAP_ID_RECORD = 0x00000002; +constexpr uint32_t NRLE_CALL = 0x00000004; +constexpr uint32_t NRLE_COMPLETION = 0x00000008; +constexpr uint32_t NRLE_CLEANUP = 0x00000010; + +constexpr uint32_t NRL_INDEX_INVALID = 0xFFFFFFFF; + +struct omap_reap_state_t { + le_uint32_t omr_phase; + omap_key_t omr_ok; +}; + + +struct omap_cleanup_state_t { + le_uint32_t omc_cleaning; + le_uint32_t omc_omsflags; + le_xid_t omc_sxidprev; + le_xid_t omc_sxidstart; + le_xid_t omc_sxidend; + le_xid_t omc_sxidnext; + omap_key_t omc_curkey; +}; + +struct apfs_reap_state_t { + le_uint64_t last_pbn; + le_xid_t cur_snap_xid; + le_uint32_t phase; +}; + + +struct j_crypto_key_t { + j_key_t hdr; +}; + +struct j_crypto_val_t { + le_uint32_t refcnt; + wrapped_crypto_state_t state; +}; + + +constexpr uint32_t CP_EFFECTIVE_CLASSMASK = 0x0000001F; + +constexpr int CP_IV_KEYSIZE = 16; +constexpr int CP_MAX_KEYSIZE = 32; +constexpr int CP_MAX_CACHEBUFLEN = 64; + +constexpr int CP_INITIAL_WRAPPEDKEYSIZE = 40; +constexpr int CP_V2_WRAPPEDKEYSIZE = 40; +constexpr int CP_V4_RESERVEDBYTES = 16; + +constexpr int CP_MAX_WRAPPEDKEYSIZE = 128; + +constexpr int CP_VERS_4 = 4; +constexpr int CP_VERS_5 = 5; +constexpr int CP_MINOR_VERS = 0; +constexpr int CP_CURRENT_VERS = CP_VERS_5; + +constexpr int PROTECTION_CLASS_DIR_NONE = 0; +constexpr int PROTECTION_CLASS_A = 1; +constexpr int PROTECTION_CLASS_B = 2; +constexpr int PROTECTION_CLASS_C = 3; +constexpr int PROTECTION_CLASS_D = 4; +constexpr int PROTECTION_CLASS_F = 6; + +constexpr int CRYPTO_SW_ID = 4; +constexpr int CRYPTO_VOLKEY_ID = 5; + + +struct keybag_entry_t { + apfs_uuid_t ke_uuid; + le_uint16_t ke_tag; + le_uint16_t ke_keylen; + uint8_t _padding_[4]; + uint8_t ke_keydata[]; +}; + +struct kb_locker_t { + le_uint16_t kl_version; + le_uint16_t kl_nkeys; + le_uint32_t kl_nbytes; + uint8_t _padding_[8]; + uint8_t kl_entries[]; /* keybag_entry_t */ +}; + +struct mk_obj_t { + uint8_t o_cksum[MAX_CKSUM_SIZE]; + le_oid_t o_oid; + le_xid_t o_xid; + le_uint32_t o_type; + le_uint32_t o_subtype; +}; + +struct media_keybag_t { + mk_obj_t mk_obj; + kb_locker_t mk_locker; +}; + +constexpr uint16_t APFS_KEYBAG_VERSION = 2; +constexpr uint32_t APFS_KEYBAG_OBJ = 0x6B657973; // 'keys'; +constexpr uint32_t APFS_VOL_KEYBAG_OBJ = 0x72656373; // 'recs'; +constexpr uint16_t APFS_VOL_KEYBAG_ENTRY_MAX_SIZE = 512; +constexpr uint16_t APFS_ENTRY_ALIGN = 16; + +enum { + KB_TAG_UNKNOWN = 0, + KB_TAG_WRAPPING_KEY, + KB_TAG_VOLUME_KEY, + KB_TAG_VOLUME_UNLOCK_RECORDS, + KB_TAG_VOLUME_PASSPHRASE_HINT, + KB_TAG_USER_PAYLOAD +}; + +struct er_state_phys_header_t { + obj_phys_t ersb_o; + le_uint32_t ersb_magic; + le_uint32_t ersb_version; +}; + +struct er_state_phys_t { + er_state_phys_header_t ersb_header; + le_uint64_t ersb_flags; + le_uint64_t ersb_snap_xid; + le_uint64_t ersb_current_fext_obj_id; + le_uint64_t ersb_file_offset; + le_uint64_t ersb_progress; + le_uint64_t ersb_total_blk_to_encrypt; + le_oid_t ersb_blockmap_oid; + le_uint64_t ersb_tidemark_obj_id; + le_uint64_t ersb_recovery_extents_count; + le_oid_t ersb_recovery_list_oid; + le_uint64_t ersb_recovery_length; +}; + +struct er_state_phys_v1_t { + er_state_phys_header_t ersb_header; + le_uint64_t ersb_flags; + le_uint64_t ersb_snap_xid; + le_uint64_t ersb_current_fext_obj_id; + le_uint64_t ersb_file_offset; + le_uint64_t ersb_fext_pbn; + le_uint64_t ersb_paddr; + le_uint64_t ersb_progress; + le_uint64_t ersb_total_blk_to_encrypt; + le_uint64_t ersb_blockmap_oid; + le_uint32_t ersb_checksum_count; + le_uint32_t ersb_reserved; + le_uint64_t ersb_fext_cid; + uint8_t ersb_checksum[0]; +}; + +enum er_phase_t { + ER_PHASE_OMAP_ROLL = 1, + ER_PHASE_DATA_ROLL = 2, + ER_PHASE_SNAP_ROLL = 3 +}; + +struct er_recovery_block_phys_t { + obj_phys_t erb_o; + le_uint64_t erb_offset; + le_oid_t erb_next_oid; + uint8_t erb_data[0]; +}; + +struct gbitmap_block_phys_t { + obj_phys_t bmb_o; + le_uint64_t bmb_field[0]; +}; + +struct gbitmap_phys_t { + obj_phys_t bm_o; + le_oid_t bm_tree_oid; + le_uint64_t bm_bit_count; + le_uint64_t bm_flags; +}; + +enum { + ER_512B_BLOCKSIZE = 0, + ER_2KiB_BLOCKSIZE = 1, + ER_4KiB_BLOCKSIZE = 2, + ER_8KiB_BLOCKSIZE = 3, + ER_16KiB_BLOCKSIZE = 4, + ER_32KiB_BLOCKSIZE = 5, + ER_64KiB_BLOCKSIZE = 6 +}; + +constexpr uint32_t ERSB_FLAG_ENCRYPTING = 0x00000001; +constexpr uint32_t ERSB_FLAG_DECRYPTING = 0x00000002; +constexpr uint32_t ERSB_FLAG_KEYROLLING = 0x00000004; +constexpr uint32_t ERSB_FLAG_PAUSED = 0x00000008; +constexpr uint32_t ERSB_FLAG_FAILED = 0x00000010; +constexpr uint32_t ERSB_FLAG_CID_IS_TWEAK = 0x00000020; +constexpr uint32_t ERSB_FLAG_FREE_1 = 0x00000040; +constexpr uint32_t ERSB_FLAG_FREE_2 = 0x00000080; + +constexpr uint32_t ERSB_FLAG_CM_BLOCK_SIZE_MASK = 0x00000F00; +constexpr int ERSB_FLAG_CM_BLOCK_SIZE_SHIFT = 8; + +constexpr uint32_t ERSB_FLAG_ER_PHASE_MASK = 0x00003000; +constexpr int ERSB_FLAG_ER_PHASE_SHIFT = 12; +constexpr uint32_t ERSB_FLAG_FROM_ONEKEY = 0x00004000; + +constexpr int ER_CHECKSUM_LENGTH = 8; +constexpr uint32_t ER_MAGIC = 0x464C4142; // 'FLAB'; +constexpr uint32_t ER_VERSION = 1; + +constexpr int ER_MAX_CHECKSUM_COUNT_SHIFT = 16; +constexpr uint32_t ER_CUR_CHECKSUM_COUNT_MASK = 0x0000FFFF; + + +struct fusion_wbc_phys_t { + obj_phys_t fwp_objHdr; + le_uint64_t fwp_version; + le_oid_t fwp_listHeadOid; + le_oid_t fwp_listTailOid; + le_uint64_t fwp_stableHeadOffset; + le_uint64_t fwp_stableTailOffset; + le_uint32_t fwp_listBlocksCount; + le_uint32_t fwp_reserved; + le_uint64_t fwp_usedByRC; + prange_t fwp_rcStash; +}; + +struct fusion_wbc_list_entry_t { + le_paddr_t fwle_wbcLba; + le_paddr_t fwle_targetLba; + le_uint64_t fwle_length; +}; + +struct fusion_wbc_list_phys_t { + obj_phys_t fwlp_objHdr; + le_uint64_t fwlp_version; + le_uint64_t fwlp_tailOffset; + le_uint32_t fwlp_indexBegin; + le_uint32_t fwlp_indexEnd; + le_uint32_t fwlp_indexMax; + le_uint32_t fwlp_reserved; + fusion_wbc_list_entry_t fwlp_listEntries[]; +}; + +constexpr uint64_t FUSION_TIER2_DEVICE_BYTE_ADDR = 0x4000000000000000ULL; +// FUSION_TIER2_DEVICE_BLOCK_ADDR +// FUSION_BLKNO() + +struct fusion_mt_key_t { + le_paddr_t paddr; +}; + +struct fusion_mt_val_t { + le_paddr_t fmv_lba; + le_uint32_t fmv_length; + le_uint32_t fmv_flags; +}; + +constexpr uint32_t FUSION_MT_DIRTY = 1 << 0; +constexpr uint32_t FUSION_MT_TENANT = 1 << 1; + + + +#define APFS_TYPE_ID(t, o) ((static_cast(t) << OBJ_TYPE_SHIFT) | (o & OBJ_ID_MASK)) + + +#ifdef _MSC_VER +#pragma warning(default: 4200) +#endif + +#pragma pack(pop) diff --git a/vendor/apfs-fuse/ApfsLib/Endian.h b/vendor/apfs-fuse/ApfsLib/Endian.h new file mode 100644 index 0000000..5c461c8 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/Endian.h @@ -0,0 +1,217 @@ +/* +This file is part of apfs-fuse, a read-only implementation of APFS +(Apple File System) for FUSE. +Copyright (C) 2017 Simon Gander + +Apfs-fuse is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +Apfs-fuse is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with apfs-fuse. If not, see . +*/ +/* +This file is for handling the different endiannesses used by Apple. +Also helps making the driver run on big-endian architectures. +*/ +#pragma once + +#include + +// This should later be done by some configuration ... +#define APFS_LITTLE_ENDIAN +#undef APFS_BIG_ENDIAN + +#ifdef _MSC_VER +// Definitions for Visual Studio +#include + +#define bswap_16(x) _byteswap_ushort(x) +#define bswap_32(x) _byteswap_ulong(x) +#define bswap_64(x) _byteswap_uint64(x) + +#define be16toh(x) _byteswap_ushort(x) +#define be32toh(x) _byteswap_ulong(x) +#define be64toh(x) _byteswap_uint64(x) +#define htobe16(x) _byteswap_ushort(x) +#define htobe32(x) _byteswap_ulong(x) +#define htobe64(x) _byteswap_uint64(x) + +#define le16toh(x) (x) +#define le32toh(x) (x) +#define le64toh(x) (x) +#define htole16(x) (x) +#define htole32(x) (x) +#define htole64(x) (x) + +#define __attribute__(x) + +#endif +#ifdef __linux__ +// Definitions for Linux +#include +#include +#endif +#ifdef __APPLE__ +// Definitions for macOS +#include +#define bswap_16(x) _OSSwapInt16(x) +#define bswap_32(x) _OSSwapInt32(x) +#define bswap_64(x) _OSSwapInt64(x) +#define be16toh(x) OSSwapBigToHostInt16(x) +#define be32toh(x) OSSwapBigToHostInt32(x) +#define be64toh(x) OSSwapBigToHostInt64(x) +#define htobe16(x) OSSwapHostToBigInt16(x) +#define htobe32(x) OSSwapHostToBigInt32(x) +#define htobe64(x) OSSwapHostToBigInt64(x) +#define le16toh(x) OSSwapLittleToHostInt16(x) +#define le32toh(x) OSSwapLittleToHostInt32(x) +#define le64toh(x) OSSwapLittleToHostInt64(x) +#define htole16(x) OSSwapHostToLittleInt16(x) +#define htole32(x) OSSwapHostToLittleInt32(x) +#define htole64(x) OSSwapHostToLittleInt64(x) +#endif + +#ifdef APFS_LITTLE_ENDIAN +// Swap to/from little endian. +inline int16_t bswap_le(const int16_t &x) { return x; } +inline int32_t bswap_le(const int32_t &x) { return x; } +inline int64_t bswap_le(const int64_t &x) { return x; } +inline uint16_t bswap_le(const uint16_t &x) { return x; } +inline uint32_t bswap_le(const uint32_t &x) { return x; } +inline uint64_t bswap_le(const uint64_t &x) { return x; } +// Swap to/from big endian. +inline int16_t bswap_be(const int16_t& x) { return bswap_16(x); } +inline int32_t bswap_be(const int32_t& x) { return bswap_32(x); } +inline int64_t bswap_be(const int64_t& x) { return bswap_64(x); } +inline uint16_t bswap_be(const uint16_t& x) { return bswap_16(x); } +inline uint32_t bswap_be(const uint32_t& x) { return bswap_32(x); } +inline uint64_t bswap_be(const uint64_t& x) { return bswap_64(x); } + +typedef uint8_t le_uint8_t; +typedef uint16_t le_uint16_t; +typedef uint32_t le_uint32_t; +typedef uint64_t le_uint64_t; +typedef int8_t le_int8_t; +typedef int16_t le_int16_t; +typedef int32_t le_int32_t; +typedef int64_t le_int64_t; +typedef uint8_t be_uint8_t; +struct be_uint16_t { + void operator=(uint16_t v) { val = bswap_16(v); } + operator uint16_t() const { return bswap_16(val); } + uint16_t get() const { return bswap_16(val); } +private: + uint16_t val; +} __attribute__((packed)); +struct be_uint32_t { + void operator=(uint32_t v) { val = bswap_32(v); } + operator uint32_t() const { return bswap_32(val); } + uint32_t get() const { return bswap_32(val); } +private: + uint32_t val; +} __attribute__((packed)); +struct be_uint64_t { + void operator=(uint64_t v) { val = bswap_64(v); } + operator uint64_t() const { return bswap_64(val); } + uint64_t get() const { return bswap_64(val); } +private: + uint64_t val; +} __attribute__((packed)); + +#endif + +#ifdef APFS_BIG_ENDIAN +// Swap to/from little endian. +inline int16_t bswap_le(const int16_t &x) { return bswap_16(x); } +inline int32_t bswap_le(const int32_t &x) { return bswap_32(x); } +inline int64_t bswap_le(const int64_t &x) { return bswap_64(x); } +inline uint16_t bswap_le(const uint16_t &x) { return bswap_16(x); } +inline uint32_t bswap_le(const uint32_t &x) { return bswap_32(x); } +inline uint64_t bswap_le(const uint64_t &x) { return bswap_64(x); } +// Swap to/from big endian. +inline int16_t bswap_be(const int16_t &x) { return x; } +inline int32_t bswap_be(const int32_t &x) { return x; } +inline int64_t bswap_be(const int64_t &x) { return x; } +inline uint16_t bswap_be(const uint16_t &x) { return x; } +inline uint32_t bswap_be(const uint32_t &x) { return x; } +inline uint64_t bswap_be(const uint64_t &x) { return x; } + +typedef uint8_t le_uint8_t; +struct le_uint16_t { + void operator=(uint16_t v) { val = bswap_16(v); } + operator uint16_t() const { return bswap_16(val); } + uint16_t get() const { return bswap_16(val); } +private: + uint16_t val; +} __attribute__((packed)); +struct le_uint32_t { + void operator=(uint32_t v) { val = bswap_32(v); } + operator uint32_t() const { return bswap_32(val); } + uint32_t get() const { return bswap_32(val); } +private: + uint32_t val; +} __attribute__((packed)); +struct le_uint64_t { + void operator=(uint64_t v) { val = bswap_64(v); } + operator uint64_t() const { return bswap_64(val); } + uint64_t get() const { return bswap_64(val); } +private: + uint64_t val; +} __attribute__((packed)); +struct le_int64_t { + void operator=(int64_t v) { val = bswap_64(v); } + operator int64_t() const { return bswap_64(val); } + int64_t get() const { return bswap_64(val); } +private: + int64_t val; +} __attribute__((packed)); + +typedef uint8_t be_uint8_t; +typedef uint16_t be_uint16_t; +typedef uint32_t be_uint32_t; +typedef uint64_t be_uint64_t; +#endif + +#if 0 + +inline int8_t bswap_le(const int8_t &x) { return x; } +inline int8_t bswap_be(const int8_t &x) { return x; } +inline uint8_t bswap_le(const uint8_t &x) { return x; } +inline uint8_t bswap_be(const uint8_t &x) { return x; } + +// Big-endian template that does automatic swapping if necessary. +template +struct be +{ +public: + void operator=(T v) { val = bswap_be(v); } + operator T() const { return bswap_be(val); } + T get() const { return bswap_be(val); } +private: + T val; +}; + +// Little-endian template that does automatic swapping if necessary. +template +struct le +{ +public: + void operator=(T v) { val = bswap_le(v); } + operator T() const { return bswap_le(val); } + T get() const { return bswap_le(val); } +private: + T val; +}; + +#endif + +#ifdef _MSC_VER +#undef __attribute__ +#endif diff --git a/vendor/apfs-fuse/ApfsLib/Global.h b/vendor/apfs-fuse/ApfsLib/Global.h new file mode 100644 index 0000000..3a76a84 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/Global.h @@ -0,0 +1,36 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#pragma once + +#include + +// Debug level - defined in ApfsContainer.cpp +extern int g_debug; +// Lax mode - defined in ApfsContainer.cpp +extern bool g_lax; + +enum DbgFlags +{ + Dbg_Errors = 1, + Dbg_Info = 2, + Dbg_Dir = 4, + Dbg_Cmpfs = 8, + Dbg_Crypto = 16 +}; diff --git a/vendor/apfs-fuse/ApfsLib/GptPartitionMap.cpp b/vendor/apfs-fuse/ApfsLib/GptPartitionMap.cpp new file mode 100644 index 0000000..7a8c676 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/GptPartitionMap.cpp @@ -0,0 +1,200 @@ +#include +#include +#include + +#include "Device.h" +#include "GptPartitionMap.h" + +typedef uint8_t PM_GUID[0x10]; + +#pragma pack(1) + +struct PMAP_GptHeader +{ + le_uint64_t Signature; + le_uint32_t Revision; + le_uint32_t HeaderSize; + le_uint32_t HeaderCRC32; + le_uint32_t Reserved; + le_uint64_t MyLBA; + le_uint64_t AlternateLBA; + le_uint64_t FirstUsableLBA; + le_uint64_t LastUsableLBA; + PM_GUID DiskGUID; + le_uint64_t PartitionEntryLBA; + le_uint32_t NumberOfPartitionEntries; + le_uint32_t SizeOfPartitionEntry; + le_uint32_t PartitionEntryArrayCRC32; +}; + +struct PMAP_Entry +{ + PM_GUID PartitionTypeGUID; + PM_GUID UniquePartitionGUID; + le_uint64_t StartingLBA; + le_uint64_t EndingLBA; + le_uint64_t Attributes; + le_uint16_t PartitionName[36]; +}; + +static_assert(sizeof(PMAP_GptHeader) == 92, "PMAP GPT-Header wrong size"); +static_assert(sizeof(PMAP_Entry) == 128, "PMAP Entry wrong size"); + +#pragma pack() + +static const PM_GUID partitiontype_apfs = { 0xEF, 0x57, 0x34, 0x7C, 0x00, 0x00, 0xAA, 0x11, 0xAA, 0x11, 0x00, 0x30, 0x65, 0x43, 0xEC, 0xAC }; + +static void PrintGUID(const PM_GUID &guid) +{ + printf("%02X%02X%02X%02X-", guid[3], guid[2], guid[1], guid[0]); + printf("%02X%02X-", guid[5], guid[4]); + printf("%02X%02X-", guid[7], guid[6]); + printf("%02X%02X-", guid[9], guid[8]); + printf("%02X%02X%02X%02X%02X%02X", guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]); +} + +GptPartitionMap::GptPartitionMap() : m_crc(true) +{ + m_hdr = nullptr; + m_map = nullptr; + m_sector_size = 0x200; +} + +bool GptPartitionMap::LoadAndVerify(Device & dev) +{ + PMAP_GptHeader *hdr; + + m_hdr = nullptr; + m_map = nullptr; + m_entry_data.clear(); + m_hdr_data.clear(); + + m_sector_size = dev.GetSectorSize(); + + m_hdr_data.resize(m_sector_size); + + dev.Read(m_hdr_data.data(), m_sector_size, m_sector_size); + + hdr = reinterpret_cast(m_hdr_data.data()); + + if (hdr->Signature != 0x5452415020494645) { + m_hdr_data.resize(0x1000); + dev.Read(m_hdr_data.data(), 0x1000, 0x1000); + + hdr = reinterpret_cast(m_hdr_data.data()); + + if (hdr->Signature != 0x5452415020494645) + return false; + + m_sector_size = 0x1000; + } + + if (hdr->Revision != 0x00010000) + return false; + + if (hdr->HeaderSize > m_sector_size) + return false; + + if (hdr->SizeOfPartitionEntry != 0x80) + return false; + + uint32_t hdr_crc; + uint32_t calc_crc; + + hdr_crc = hdr->HeaderCRC32; + hdr->HeaderCRC32 = 0; + + m_crc.SetCRC(0xFFFFFFFF); + m_crc.Calc(m_hdr_data.data(), hdr->HeaderSize); + calc_crc = m_crc.GetCRC() ^ 0xFFFFFFFF; + + if (calc_crc != hdr_crc) { + m_hdr_data.clear(); + return false; + } + + size_t mapsize; + + mapsize = hdr->NumberOfPartitionEntries * hdr->SizeOfPartitionEntry; + mapsize = (mapsize + m_sector_size - 1) & ~(static_cast(m_sector_size) - 1); + + m_entry_data.resize(mapsize); + dev.Read(m_entry_data.data(), m_sector_size * hdr->PartitionEntryLBA, m_entry_data.size()); + + m_crc.SetCRC(0xFFFFFFFF); + m_crc.Calc(m_entry_data.data(), hdr->SizeOfPartitionEntry * hdr->NumberOfPartitionEntries); + calc_crc = m_crc.GetCRC() ^ 0xFFFFFFFF; + + if (calc_crc != hdr->PartitionEntryArrayCRC32) { + m_entry_data.clear(); + m_hdr_data.clear(); + return false; + } + + m_hdr = reinterpret_cast(m_hdr_data.data()); + m_map = reinterpret_cast(m_entry_data.data()); + + return true; +} + +int GptPartitionMap::FindFirstAPFSPartition() +{ + if (!m_hdr || !m_map) + return -1; + + unsigned int k; + int rc = -1; + + for (k = 0; k < m_hdr->NumberOfPartitionEntries; k++) + { + if (m_map[k].StartingLBA == 0 && m_map[k].EndingLBA == 0) + break; + + if (!memcmp(m_map[k].PartitionTypeGUID, partitiontype_apfs, sizeof(PM_GUID))) + { + rc = k; + break; + } + } + + return rc; +} + +bool GptPartitionMap::GetPartitionOffsetAndSize(int partnum, uint64_t & offset, uint64_t & size) +{ + if (!m_hdr || !m_map) + return false; + + offset = m_map[partnum].StartingLBA * m_sector_size; + size = (m_map[partnum].EndingLBA - m_map[partnum].StartingLBA + 1) * m_sector_size; + + return true; +} + +void GptPartitionMap::ListEntries() +{ + if (!m_hdr || !m_map) + return; + + size_t k; + size_t n; + + for (k = 0; k < m_hdr->NumberOfPartitionEntries; k++) + { + const PMAP_Entry &e = m_map[k]; + + if (e.StartingLBA == 0 && e.EndingLBA == 0) + break; + + PrintGUID(e.PartitionTypeGUID); + printf(" "); + PrintGUID(e.UniquePartitionGUID); + printf(" %016" PRIX64 " %016" PRIX64 " ", e.StartingLBA, e.EndingLBA); + printf("%016" PRIX64 " ", e.Attributes); + + for (n = 0; (n < 36) && (e.PartitionName[n] != 0); n++) + printf("%c", e.PartitionName[n]); + + printf("\n"); + } +} diff --git a/vendor/apfs-fuse/ApfsLib/GptPartitionMap.h b/vendor/apfs-fuse/ApfsLib/GptPartitionMap.h new file mode 100644 index 0000000..ec57460 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/GptPartitionMap.h @@ -0,0 +1,35 @@ +#pragma once + +#include +#include + +#include "Endian.h" +#include "Crc32.h" + +class Device; + +struct PMAP_GptHeader; +struct PMAP_Entry; + +class GptPartitionMap +{ +public: + GptPartitionMap(); + + bool LoadAndVerify(Device &dev); + + int FindFirstAPFSPartition(); + bool GetPartitionOffsetAndSize(int partnum, uint64_t &offset, uint64_t &size); + + void ListEntries(); + +private: + Crc32 m_crc; + + std::vector m_hdr_data; + std::vector m_entry_data; + + const PMAP_GptHeader *m_hdr; + const PMAP_Entry *m_map; + unsigned int m_sector_size; +}; diff --git a/vendor/apfs-fuse/ApfsLib/KeyMgmt.cpp b/vendor/apfs-fuse/ApfsLib/KeyMgmt.cpp new file mode 100644 index 0000000..b2af762 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/KeyMgmt.cpp @@ -0,0 +1,741 @@ +#include +#include +#include +#include + +#include "ApfsContainer.h" +#include +#include +#include +#include +#include "Util.h" +#include "BlockDumper.h" + +#include "KeyMgmt.h" + +struct bagdata_t +{ + const uint8_t *data; + size_t size; +}; + +struct key_info_t +{ + uint32_t flags; + uint8_t unk_04; + uint8_t unk_05; + uint8_t uuid[16]; +}; + +struct key_header_t +{ + uint64_t unk_0; + apfs_uuid_t uuid; + key_info_t info; +}; + +struct kek_entry_t +{ + key_header_t hdr; + uint8_t wrapped_kek[0x28]; + uint64_t iterations; + uint8_t salt[0x10]; +}; + +struct vek_entry_t +{ + key_header_t hdr; + uint8_t wrapped_vek[0x28]; +}; + +Keybag::Keybag() +{ + m_kl = nullptr; +} + +Keybag::~Keybag() +{ +} + +bool Keybag::Init(const media_keybag_t * mk, size_t size) +{ + (void)size; + + if (mk->mk_locker.kl_version != 2) + return false; + + const uint8_t *data = reinterpret_cast(&mk->mk_locker); + + m_data.assign(data, data + mk->mk_locker.kl_nbytes); + m_kl = reinterpret_cast(m_data.data()); + + return true; +} + +size_t Keybag::GetKeyCnt() +{ + if (m_kl) + return m_kl->kl_nkeys; + else + return 0; +} + +const keybag_entry_t * Keybag::GetKey(size_t nr) +{ + if (!m_kl) + return nullptr; + + if (nr >= m_kl->kl_nkeys) + return nullptr; + + const uint8_t *ptr; + const keybag_entry_t *kb; + size_t len; + size_t k; + + ptr = m_kl->kl_entries; + + for (k = 0; k < nr; k++) + { + kb = reinterpret_cast(ptr); + + len = (kb->ke_keylen + sizeof(keybag_entry_t) + 0x0F) & ~0xF; + ptr += len; + } + + kb = reinterpret_cast(ptr); + + return kb; +} + +const keybag_entry_t * Keybag::FindKey(const apfs_uuid_t & uuid, uint16_t type) +{ + if (!m_kl) + return nullptr; + + const uint8_t *ptr; + const keybag_entry_t *kb; + size_t len; + size_t k; + + ptr = m_kl->kl_entries; + + for (k = 0; k < m_kl->kl_nkeys; k++) + { + kb = reinterpret_cast(ptr); + + if (memcmp(uuid, kb->ke_uuid, sizeof(apfs_uuid_t)) == 0 && kb->ke_tag == type) + return kb; + + len = (kb->ke_keylen + sizeof(keybag_entry_t) + 0x0F) & ~0xF; + ptr += len; + } + + return nullptr; +} + +#undef DUMP_RAW_KEYS + +void Keybag::dump(std::ostream &st, Keybag *cbag, const apfs_uuid_t &vuuid) +{ + using namespace std; + + (void)vuuid; + + size_t s; + size_t k; + const keybag_entry_t *ke; + bagdata_t bd; + const char *typestr; + + st << "Dumping Keybag (" << (cbag ? "recs" : "keys") << ")" << endl; + st << endl; + + if (m_data.empty() || !m_kl) + { + st << "Keybag is empty." << endl; + return; + } + +#ifdef DUMP_RAW_KEYS + DumpHex(st, m_data.data(), m_data.size()); + st << endl; +#endif + + st << "Version : " << setw(4) << m_kl->kl_version << endl; + st << "Keys : " << setw(4) << m_kl->kl_nkeys << endl; + st << "Bytes : " << setw(8) << m_kl->kl_nbytes << endl; + st << endl; + + s = GetKeyCnt(); + + for (k = 0; k < s; k++) + { + ke = GetKey(k); + + typestr = "!!! Unknown !!!"; + + if (!cbag) + { + switch (ke->ke_tag) + { + case KB_TAG_VOLUME_KEY: typestr = "VEK"; break; + case KB_TAG_VOLUME_UNLOCK_RECORDS: typestr = "Keybag Ref"; break; + default: break; + } + } + else + { + switch (ke->ke_tag) + { + case KB_TAG_VOLUME_UNLOCK_RECORDS: typestr = "KEK"; break; + case KB_TAG_VOLUME_PASSPHRASE_HINT: typestr = "Password Hint"; break; + default: break; + } + } + + st << "Key " << k << ":" << endl; + st << "UUID : " << uuidstr(ke->ke_uuid) << endl; + st << "Type : " << setw(4) << ke->ke_tag << " [" << typestr << "]" << endl; + st << "Length : " << setw(4) << ke->ke_keylen << endl; + /* Skip the ke->_padding_ */ + + // DumpHex(st, kd.data.data, kd.data.size); +#ifdef DUMP_RAW_KEYS + DumpHex(st, ke->ke_keydata, ke->ke_keylen); + st << endl; +#endif + + if (!cbag) + { + switch (ke->ke_tag) + { + case 2: + der_dump(ke->ke_keydata, ke->ke_keylen); +#if 0 + bd.data = ke->ke_keydata; + bd.size = ke->ke_keylen; + + if (KeyManager::DecodeBlobHeader(bhdr, bd)) + { + st << "[Blob Header]" << endl; + st << "Unk 80 : " << bhdr.unk_80 << endl; + st << "HMAC : " << hexstr(bhdr.hmac, sizeof(bhdr.hmac)) << endl; + st << "Salt : " << hexstr(bhdr.salt, sizeof(bhdr.salt)) << endl; +#ifdef DUMP_RAW_KEYS + st << "Data :" << endl; + DumpHex(st, bhdr.blob.data, bhdr.blob.size); +#endif + st << endl; + + vek_blob_t vek; + + if (KeyManager::DecodeVEKBlob(vek, bhdr.blob)) + { + st << "[VEK]" << endl; + st << "Unk 80 : " << vek.unk_80 << endl; + st << "UUID : " << uuidstr(vek.uuid) << endl; + st << "Unk 82 : " << setw(8) << vek.unk_82.unk_00 << ' ' << setw(4) << vek.unk_82.unk_04 << ' '; + st << setw(2) << static_cast(vek.unk_82.unk_06) << ' ' << setw(2) << static_cast(vek.unk_82.unk_07) << endl; + st << "VEK Wrpd: " << hexstr(vek.wrapped_vek, sizeof(vek.wrapped_vek)) << endl; + st << endl; + } + else + { + st << "Invalid VEK Blob!!!" << endl; + } + } + else + { + st << "Invalid BLOB Header!!!" << endl; + } +#endif + break; + case 3: + { + const prange_t *pr = reinterpret_cast(ke->ke_keydata); + st << "Block : " << setw(16) << pr->pr_start_addr << endl; + st << "Count : " << setw(16) << pr->pr_block_count << endl; + } + break; + default: + st << "Unknown Type !!!" << endl; + break; + } + } + else + { + switch (ke->ke_tag) + { + case 3: + der_dump(ke->ke_keydata, ke->ke_keylen); +#if 0 + bd.data = ke->ke_keydata; + bd.size = ke->ke_keylen; + + if (KeyManager::DecodeBlobHeader(bhdr, bd)) + { + st << "[Blob Header]" << endl; + st << "Unk 80 : " << bhdr.unk_80 << endl; + st << "HMAC : " << hexstr(bhdr.hmac, sizeof(bhdr.hmac)) << endl; + st << "Salt : " << hexstr(bhdr.salt, sizeof(bhdr.salt)) << endl; +#ifdef DUMP_RAW_KEYS + st << "Data :" << endl; + DumpHex(st, bhdr.blob.data, bhdr.blob.size); +#endif + st << endl; + + kek_blob_t kek; + + if (KeyManager::DecodeKEKBlob(kek, bhdr.blob)) + { + st << "[KEK]" << endl; + st << "Unk 80 : " << kek.unk_80 << endl; + st << "UUID : " << uuidstr(kek.uuid) << endl; + st << "Unk 82 : " << setw(8) << kek.unk_82.unk_00 << ' ' << setw(4) << kek.unk_82.unk_04 << ' '; + st << setw(2) << static_cast(kek.unk_82.unk_06) << ' ' << setw(2) << static_cast(kek.unk_82.unk_07) << endl; + st << "KEK Wrpd: " << hexstr(kek.wrapped_kek, sizeof(kek.wrapped_kek)) << endl; + st << "Iterat's: " << dec << kek.iterations << hex << endl; + st << "Salt : " << hexstr(kek.salt, sizeof(kek.salt)) << endl; + st << endl; + +#if 0 // Test decryption of keys + string pw; + uint8_t dk[0x20] = { 0 }; + uint8_t kekk[0x20] = { 0 }; + uint64_t iv; + + cout << "Enter Password for KEK " << uuidstr(kd.header->uuid) << endl; + GetPassword(pw); + + st << "[Decryption Check]" << endl; + PBKDF2_HMAC_SHA256(reinterpret_cast(pw.c_str()), pw.size(), kek.salt, sizeof(kek.salt), kek.iterations, dk, sizeof(dk)); + st << "PW DKey : " << hexstr(dk, sizeof(dk)) << endl; + if (kek.unk_82.unk_00 == 0 || kek.unk_82.unk_00 == 0x10) + Rfc3394_KeyUnwrap(kekk, kek.wrapped_kek, 0x20, dk, AES::AES_256, &iv); + else if (kek.unk_82.unk_00 == 2) + Rfc3394_KeyUnwrap(kekk, kek.wrapped_kek, 0x10, dk, AES::AES_128, &iv); + else + { + st << "kek/82/00 = " << kek.unk_82.unk_00 << " ???" << endl; + iv = 0; + } + + st << "KEK Dec : " << hexstr(kekk, sizeof(kekk)) << endl; + st << "IV KEK : " << setw(16) << iv << " [" << (iv == 0xA6A6A6A6A6A6A6A6 ? "Correct" : "!!!INCORRECT!!!") << "]" << endl; + + if (iv != 0xA6A6A6A6A6A6A6A6) + cerr << "Warning: Wrong password." << endl; + else + { + vek_blob_t vek; + uint8_t vekk[0x20] = { 0 }; + + if (cbag->FindKey(vuuid, 2, kd)) + { + if (KeyManager::DecodeBlobHeader(bhdr, kd.data)) + { + if (KeyManager::DecodeVEKBlob(vek, bhdr.blob)) + { + if (vek.unk_82.unk_00 == 0) + Rfc3394_KeyUnwrap(vekk, vek.wrapped_vek, 0x20, kekk, AES::AES_256, &iv); + else if (vek.unk_82.unk_00 == 2) + Rfc3394_KeyUnwrap(vekk, vek.wrapped_vek, 0x10, kekk, AES::AES_128, &iv); + else + { + st << "vek/82/00 = " << vek.unk_82.unk_00 << " ???" << endl; + iv = 0; + } + + st << "VEK Dec : " << hexstr(vekk, sizeof(vekk)) << endl; + st << "IV VEK : " << setw(16) << iv << " [" << (iv == 0xA6A6A6A6A6A6A6A6 ? "Correct" : "!!!INCORRECT!!!") << "]" << endl; + if (iv != 0xA6A6A6A6A6A6A6A6) + cerr << "Error: Password correct, but KEK can't decrypt VEK ..." << endl; + } + } + } + } +#endif + } + else + { + st << "Invalid KEK Blob!!!" << endl; + } + } + else + { + st << "Invalid BLOB Header!!!" << endl; + } +#endif + break; + case 4: + st << "Hint : " << string(reinterpret_cast(ke->ke_keydata), ke->ke_keylen) << endl; + break; + } + } + + st << endl; + } + + st << endl; +} + +KeyManager::KeyManager(ApfsContainer& container) : m_container(container) +{ + memset(m_container_uuid, 0, sizeof(m_container_uuid)); + m_is_valid = false; + m_is_unencrypted = false; +} + +KeyManager::~KeyManager() +{ +} + +bool KeyManager::Init(uint64_t block, uint64_t blockcnt, const apfs_uuid_t& container_uuid) +{ + bool rc; + + rc = LoadKeybag(m_container_bag, 0x6B657973, block, blockcnt, container_uuid); + if (rc) + memcpy(m_container_uuid, container_uuid, sizeof(apfs_uuid_t)); + else + memset(m_container_uuid, 0, sizeof(m_container_uuid)); + m_is_valid = rc; + return rc; +} + +bool KeyManager::GetPasswordHint(std::string& hint, const apfs_uuid_t& volume_uuid) +{ + const keybag_entry_t *ke; + + ke = m_container_bag.FindKey(volume_uuid, KB_TAG_VOLUME_UNLOCK_RECORDS); + if (!ke) + return false; + + if (ke->ke_keylen != sizeof(prange_t)) + return false; + + const prange_t *pr = reinterpret_cast(ke->ke_keydata); + + Keybag recs_bag; + + if (!LoadKeybag(recs_bag, APFS_VOL_KEYBAG_OBJ, pr->pr_start_addr, pr->pr_block_count, volume_uuid)) + return false; + + ke = recs_bag.FindKey(volume_uuid, KB_TAG_VOLUME_PASSPHRASE_HINT); + if (!ke) + return false; + + hint.assign(reinterpret_cast(ke->ke_keydata), ke->ke_keylen); + + return true; +} + +bool KeyManager::GetVolumeKey(uint8_t* vek, const apfs_uuid_t& volume_uuid, const char* password) +{ + const keybag_entry_t *ke_recs; + + if (g_debug & Dbg_Crypto) + { + std::cout.setf(std::ios::hex | std::ios::uppercase); + std::cout.fill('0'); + + m_container_bag.dump(std::cout, nullptr, volume_uuid); + } + + ke_recs = m_container_bag.FindKey(volume_uuid, 3); + if (!ke_recs) + return false; + + if (ke_recs->ke_keylen != sizeof(prange_t)) + return false; + + const prange_t *pr = reinterpret_cast(ke_recs->ke_keydata); + + Keybag recs_bag; + + if (!LoadKeybag(recs_bag, APFS_VOL_KEYBAG_OBJ, pr->pr_start_addr, pr->pr_block_count, volume_uuid)) + return false; + + if (g_debug & Dbg_Crypto) + recs_bag.dump(std::cout, &m_container_bag, volume_uuid); + + uint8_t dk[0x20] = {}; + uint8_t kek[0x20] = {}; + uint64_t iv; + bool rc = false; + const keybag_entry_t *ke_kek; + const keybag_entry_t *ke_vek; + bagdata_t bd; + kek_entry_t keke; + vek_entry_t veke; + AES::Mode kek_mode = AES::AES_256; + + int cnt = recs_bag.GetKeyCnt(); + int k; + + // Check all KEKs for any valid KEK. + for (k = 0; k < cnt; k++) + { + ke_kek = recs_bag.GetKey(k); + if (!ke_kek) + continue; + + if (ke_kek->ke_tag != KB_TAG_VOLUME_UNLOCK_RECORDS) + continue; + + if (!DecodeKEK(keke, ke_kek->ke_keydata, ke_kek->ke_keydata + ke_kek->ke_keylen)) + continue; + + PBKDF2_HMAC_SHA256(reinterpret_cast(password), strlen(password), keke.salt, sizeof(keke.salt), keke.iterations, dk, sizeof(dk)); + + // There are more variants here ... 1 = hw_crypt + if (keke.hdr.info.flags & 2) { + rc = Rfc3394_KeyUnwrap(kek, keke.wrapped_kek, 0x10, dk, AES::AES_128, &iv); + kek_mode = AES::AES_128; + } else { + rc = Rfc3394_KeyUnwrap(kek, keke.wrapped_kek, 0x20, dk, AES::AES_256, &iv); + kek_mode = AES::AES_256; + } + + if (g_debug & Dbg_Crypto) + { + std::cout << "!!! WARNING !!!" << std::endl; + std::cout << "Do not post the output \"PW Key\", \"KEK\" or \"VEK\" on the internet !!!" << std::endl; + std::cout << "These contain your encryption keys." << std::endl << std::endl; + std::cout << "PW Key : " << hexstr(dk, sizeof(dk)) << std::endl; + std::cout << "KEK Wrpd: " << hexstr(keke.wrapped_kek, sizeof(keke.wrapped_kek)) << std::endl; + std::cout << "KEK : " << hexstr(kek, 0x20) << std::endl; + std::cout << "KEK IV : " << std::setw(16) << iv << std::endl; + std::cout << std::endl; + } + + if (rc) + break; + } + + if (!rc) + { + if (g_debug & Dbg_Crypto) + std::cout << "Password doesn't work for any key." << std::endl; + return false; + } + + ke_vek = m_container_bag.FindKey(volume_uuid, KB_TAG_VOLUME_KEY); + if (!ke_vek) + return false; + + if (!DecodeVEK(veke, ke_vek->ke_keydata, ke_vek->ke_keydata + ke_vek->ke_keylen)) + return false; + + memset(vek, 0, 0x20); + + // TODO 1 + if (veke.hdr.info.flags & 2) { + // AES-128. This method is used for FileVault and CoreStorage encrypted + // volumes that have been converted to APFS. + rc = Rfc3394_KeyUnwrap(vek, veke.wrapped_vek, 0x10, kek, kek_mode, &iv); + + if (rc) + { + SHA256 sha; + uint8_t sha_result[0x20]; + sha.Init(); + + // Use (VEK || vek_blob.uuid), then SHA256, then take the first 16 bytes + sha.Update(vek, 0x10); + sha.Update(veke.hdr.uuid, 0x10); + sha.Final(sha_result); + memcpy(vek + 0x10, sha_result, 0x10); + } + } else { + // AES-256. This method is used for wrapping the whole XTS-AES key, + // and applies to non-FileVault encrypted APFS volumes. + rc = Rfc3394_KeyUnwrap(vek, veke.wrapped_vek, 0x20, kek, kek_mode, &iv); + } + + if (g_debug & Dbg_Crypto) + { + std::cout << "VEK Wrpd: " << hexstr(veke.wrapped_vek, 0x28) << std::endl; + std::cout << "VEK : " << hexstr(vek, 0x20) << std::endl; + std::cout << "VEK IV : " << std::setw(16) << iv << std::endl; + } + + return rc; +} + +void KeyManager::dump(std::ostream &st) +{ + size_t k; + size_t s; + const keybag_entry_t *ke; + apfs_uuid_t dummy_uuid; + std::ios::fmtflags fl = st.setf(st.hex | st.uppercase); + char ch = st.fill('0'); + + memset(dummy_uuid, 0, sizeof(dummy_uuid)); + + m_container_bag.dump(st, nullptr, dummy_uuid); + + s = m_container_bag.GetKeyCnt(); + + for (k = 0; k < s; k++) + { + ke = m_container_bag.GetKey(k); + if (!ke) + continue; + + if ((ke->ke_tag == KB_TAG_VOLUME_UNLOCK_RECORDS) && (ke->ke_keylen == sizeof(prange_t))) + { + st << std::endl; + st << "---------------------------------------------------------------------------------------------------------------------------" << std::endl; + st << std::endl; + + const prange_t *pr = reinterpret_cast(ke->ke_keydata); + Keybag recs_bag; + + if (LoadKeybag(recs_bag, APFS_VOL_KEYBAG_OBJ, pr->pr_start_addr, pr->pr_block_count, ke->ke_uuid)) + recs_bag.dump(st, &m_container_bag, ke->ke_uuid); + } + } + + st << std::endl; + st << "===========================================================================================================================" << std::endl; + st << std::endl; + + st.fill(ch); + st.setf(fl); +} + +bool KeyManager::LoadKeybag(Keybag& bag, uint32_t type, uint64_t block, uint64_t blockcnt, const apfs_uuid_t& uuid) +{ + std::vector data; + size_t k; + const size_t blocksize = m_container.GetBlocksize(); + + if (g_debug & Dbg_Crypto) + std::cout << "starting LoadKeybag @ " << std::hex << block << std::endl; + + data.resize(blockcnt * blocksize); + const media_keybag_t *mk = reinterpret_cast(data.data()); + + m_container.ReadBlocks(data.data(), block, blockcnt); + if (mk->mk_obj.o_type == type) + m_is_unencrypted = true; + else + DecryptBlocks(data.data(), block, blockcnt, uuid); + + for (k = 0; k < blockcnt; k++) + { + if (!VerifyBlock(data.data(), blockcnt * blocksize)) + return false; + } + + if (g_debug & Dbg_Crypto) + std::cout << " all blocks verified" << std::endl; + + if (mk->mk_obj.o_type != type) + { + if (g_debug & Dbg_Errors) + { + std::cout << "Keybag block types not matching: " << mk->mk_obj.o_type << ", expected " << type << std::endl; + DumpHex(std::cout, data.data(), data.size()); + } + + return false; + } + + bag.Init(mk, data.size()); // TODO: This only works with one block ... + + return true; +} + +void KeyManager::DecryptBlocks(uint8_t* data, uint64_t block, uint64_t cnt, const uint8_t* key) +{ + AesXts xts; + size_t k; + size_t size; + uint64_t uno; + uint64_t cs_factor = m_container.GetBlocksize() / 0x200; + + xts.SetKey(key, key); + + uno = block * cs_factor; + size = m_container.GetBlocksize() * cnt; + + for (k = 0; k < size; k += 0x200) + { + xts.Decrypt(data + k, data + k, 0x200, uno); + uno++; + } +} + +const uint8_t* KeyManager::DecodeKeyHeader(key_header_t& hdr, const uint8_t*& end, const uint8_t* der, const uint8_t* der_end) +{ + static const uint8_t blob_cookie[6] = { 0x01, 0x16, 0x20, 0x17, 0x15, 0x05 }; + + const uint8_t* body_end; + uint64_t hmac_0; + uint8_t hmac_hash[32]; + uint8_t hmac_salt[8]; + size_t info_len; + + SHA256 sha256; + uint8_t hmac_key[32]; + uint8_t hmac_res[40]; + + der = der_decode_sequence_tl(body_end, der, der_end); + if (der == nullptr) return nullptr; + der = der_decode_uint64(0x8000000000000000U, hmac_0, der, body_end); + der = der_decode_octet_string_copy(0x8000000000000001U, hmac_hash, 32, der, body_end); + der = der_decode_octet_string_copy(0x8000000000000002U, hmac_salt, 8, der, body_end); + if (der == nullptr) { + log_error("Keybag: Parsing key header failed.\n"); + return nullptr; + } + + sha256.Init(); + sha256.Update(blob_cookie, 6); + sha256.Update(hmac_salt, 8); + sha256.Final(hmac_key); + + HMAC_SHA256(hmac_key, 32, der, body_end - der, hmac_res); + if (memcmp(hmac_hash, hmac_res, 32) != 0) + log_error("Keybag: HMAC verification failed.\n"); + // Ignoring for now ... + + der = der_decode_constructed_tl(0xA000000000000003U, end, der, body_end); + der = der_decode_uint64(0x8000000000000000U, hdr.unk_0, der, end); + der = der_decode_octet_string_copy(0x8000000000000001U, hdr.uuid, 16, der, end); + der = der_decode_tl(0x8000000000000002U, info_len, der, end); + if (info_len > 0x16) return nullptr; + memcpy(&hdr.info, der, info_len); + der += info_len; + hdr.info.flags = bswap_le(hdr.info.flags); + return der; +} + +bool KeyManager::DecodeKEK(kek_entry_t& kek, const uint8_t* der, const uint8_t* der_end) +{ + const uint8_t* end = nullptr; + + memset(&kek, 0, sizeof(kek)); + der = DecodeKeyHeader(kek.hdr, end, der, der_end); + if (der == nullptr) return false; + der = der_decode_octet_string_copy(0x8000000000000003U, kek.wrapped_kek, 40, der, end); + der = der_decode_uint64(0x8000000000000004U, kek.iterations, der, end); + der = der_decode_octet_string_copy(0x8000000000000005U, kek.salt, 16, der, end); + return der != nullptr; +} + +bool KeyManager::DecodeVEK(vek_entry_t& vek, const uint8_t* der, const uint8_t* der_end) +{ + const uint8_t* end = nullptr; + + memset(&vek, 0, sizeof(vek)); + der = DecodeKeyHeader(vek.hdr, end, der, der_end); + if (der == nullptr) return false; + der = der_decode_octet_string_copy(0x8000000000000003U, vek.wrapped_vek, 40, der, der_end); + // HW encrypt has more here ... TODO + return der != nullptr; +} diff --git a/vendor/apfs-fuse/ApfsLib/KeyMgmt.h b/vendor/apfs-fuse/ApfsLib/KeyMgmt.h new file mode 100644 index 0000000..d04b7db --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/KeyMgmt.h @@ -0,0 +1,68 @@ +#pragma once + +#include +#include +#include +#include + +#include "Global.h" + +class ApfsContainer; + +struct bagdata_t; + +struct key_header_t; +struct kek_entry_t; +struct vek_entry_t; + +class Keybag +{ +public: + Keybag(); + ~Keybag(); + + bool Init(const media_keybag_t *mk, size_t size); + + size_t GetKeyCnt(); + const keybag_entry_t * GetKey(size_t nr); + const keybag_entry_t * FindKey(const apfs_uuid_t &uuid, uint16_t type); + + void dump(std::ostream &st, Keybag *cbag, const apfs_uuid_t &vuuid); + +private: + std::vector m_data; + kb_locker_t *m_kl; +}; + +class KeyManager +{ + friend class Keybag; +public: + KeyManager(ApfsContainer &container); + ~KeyManager(); + + bool Init(uint64_t block, uint64_t blockcnt, const apfs_uuid_t &container_uuid); + + bool GetPasswordHint(std::string &hint, const apfs_uuid_t &volume_uuid); + bool GetVolumeKey(uint8_t *vek, const apfs_uuid_t &volume_uuid, const char *password = nullptr); + + bool IsValid() const { return m_is_valid; } + bool IsUnencrypted() const { return m_is_unencrypted; } + + void dump(std::ostream &st); + +private: + bool LoadKeybag(Keybag &bag, uint32_t type, uint64_t block, uint64_t blockcnt, const apfs_uuid_t &uuid); + void DecryptBlocks(uint8_t *data, uint64_t block, uint64_t cnt, const uint8_t *key); + + static const uint8_t* DecodeKeyHeader(key_header_t &hdr, const uint8_t*& end, const uint8_t* der, const uint8_t* der_end); + static bool DecodeKEK(kek_entry_t &kek, const uint8_t* der, const uint8_t* der_end); + static bool DecodeVEK(vek_entry_t &vek, const uint8_t* der, const uint8_t* der_end); + + ApfsContainer &m_container; + Keybag m_container_bag; + apfs_uuid_t m_container_uuid; + + bool m_is_valid; + bool m_is_unencrypted; +}; diff --git a/vendor/apfs-fuse/ApfsLib/PList.cpp b/vendor/apfs-fuse/ApfsLib/PList.cpp new file mode 100644 index 0000000..bb909d7 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/PList.cpp @@ -0,0 +1,476 @@ +#include + +#include "PList.h" + +PLObject::PLObject() +{ +} + +PLObject::~PLObject() +{ +} + +const PLInteger * PLObject::toInt() const +{ + return dynamic_cast(this); +} + +const PLString * PLObject::toString() const +{ + return dynamic_cast(this); +} + +const PLData * PLObject::toData() const +{ + return dynamic_cast(this); +} + +const PLArray * PLObject::toArray() const +{ + return dynamic_cast(this); +} + +const PLDict * PLObject::toDict() const +{ + return dynamic_cast(this); +} + +PLInteger::PLInteger() +{ +} + +PLInteger::~PLInteger() +{ +} + +PLType PLInteger::type() const +{ + return PLType_Integer; +} + +PLString::PLString() +{ +} + +PLString::~PLString() +{ +} + +PLType PLString::type() const +{ + return PLType_String; +} + +PLData::PLData() +{ +} + +PLData::~PLData() +{ +} + +PLType PLData::type() const +{ + return PLType_Data; +} + +PLArray::PLArray() +{ +} + +PLArray::~PLArray() +{ + for (std::vector::iterator it = m_array.begin(); it != m_array.end(); it++) + delete *it; + m_array.clear(); +} + +PLType PLArray::type() const +{ + return PLType_Array; +} + +PLObject * PLArray::get(size_t idx) const +{ + if (idx < m_array.size()) + return m_array[idx]; + else + return nullptr; +} + +PLDict::PLDict() +{ +} + +PLDict::~PLDict() +{ + for (std::map::iterator it = m_dict.begin(); it != m_dict.end(); it++) + delete it->second; + m_dict.clear(); +} + +PLType PLDict::type() const +{ + return PLType_Dict; +} + +PLObject * PLDict::get(const char * name) const +{ + std::map::const_iterator it = m_dict.find(name); + + if (it != m_dict.cend()) + return it->second; + else + return nullptr; +} + +PListXmlParser::PListXmlParser(const char * data, size_t size) + : + m_data(data), + m_size(size) +{ + m_idx = 0; +} + +PListXmlParser::~PListXmlParser() +{ + +} + +PLObject * PListXmlParser::Parse() +{ + std::string name; + std::string content; + TagType type; + PLObject *root = nullptr; + + while (FindTag(name, type)) + { + if (type == TagType::Start && name == "plist") + { + try + { + root = ParseObject(); + } + catch (PLException &ex) + { + delete root; + root = nullptr; + + fprintf(stderr, "XML PList parse error: %s\n", ex.what()); + } + + break; + } + } + + return root; +} + +PLArray * PListXmlParser::ParseArray() +{ + PLArray *arr = new PLArray(); + PLObject *obj; + + for (;;) + { + obj = ParseObject(); + + if (obj) + arr->m_array.push_back(obj); + else + break; + } + + return arr; +} + +PLDict * PListXmlParser::ParseDict() +{ + PLDict *dict = new PLDict(); + PLObject *obj; + + TagType tagtype; + std::string tagname; + std::string key; + + for (;;) + { + FindTag(tagname, tagtype); + + if (tagname == "dict" && tagtype == TagType::End) + break; + + if (tagname != "key" || tagtype != TagType::Start) + throw PLException("Invalid tag in dict"); + + GetContent(key); + + if (key.empty()) + throw PLException("Empty key in dict"); + + FindTag(tagname, tagtype); + + if (tagname != "key" || tagtype != TagType::End) + throw PLException("Invalid tag type, expected "); + + obj = ParseObject(); + + if (obj) + dict->m_dict[key] = obj; + } + + return dict; +} + +PLObject * PListXmlParser::ParseObject() +{ + std::string name; + TagType type; + std::string content_str; + const char *content_start; + size_t content_size; + PLObject *robj = nullptr; + + FindTag(name, type); + + if (type == TagType::Start) + { + if (name == "integer") + { + GetContent(content_str); + FindTag(name, type); + if (name != "integer" || type != TagType::End) + throw PLException("Invalid end tag, expected ."); + + PLInteger *obj = new PLInteger(); + obj->m_value = strtoll(content_str.c_str(), nullptr, 0); + robj = obj; + } + else if (name == "string") + { + GetContent(content_str); + FindTag(name, type); + if (name != "string" || type != TagType::End) + throw PLException("Invalid end tag, expected ."); + + PLString *obj = new PLString(); + obj->m_string = content_str; + robj = obj; + } + else if (name == "data") + { + content_start = m_data + m_idx; + content_size = GetContentSize(); + + FindTag(name, type); + if (name != "data" || type != TagType::End) + throw PLException("Invalid end tag, expected ."); + + PLData *obj = new PLData(); + Base64Decode(obj->m_data, content_start, content_size); + robj = obj; + } + else if (name == "array") + { + PLArray *obj = ParseArray(); + if (!obj) + return nullptr; + robj = obj; + } + else if (name == "dict") + { + PLDict *obj = ParseDict(); + if (!obj) + return nullptr; + robj = obj; + } + else + { + throw PLException("Unexpected start tag."); + } + } + else if (type == TagType::Empty) + { + if (name == "true") + { + PLInteger *obj = new PLInteger(); + obj->m_value = 1; + robj = obj; + } + else if (name == "false") + { + PLInteger *obj = new PLInteger(); + obj->m_value = 0; + robj = obj; + } + else + { + throw PLException("Unexpected empty tag."); + } + } + else if (type == TagType::End) + { + return nullptr; + } + + return robj; +} + +void PListXmlParser::Base64Decode(std::vector& bin, const char * str, size_t size) +{ + int chcnt; + uint32_t buf; + size_t ip; + char ch; + uint32_t dec; + + bin.clear(); + bin.reserve(size * 4 / 3); + + chcnt = 0; + buf = 0; + + for (ip = 0; ip < size; ip++) + { + ch = str[ip]; + + if (ch >= 'A' && ch <= 'Z') + dec = ch - 'A'; + else if (ch >= 'a' && ch <= 'z') + dec = ch - 'a' + 0x1A; + else if (ch >= '0' && ch <= '9') + dec = ch - '0' + 0x34; + else if (ch == '+') + dec = 0x3E; + else if (ch == '/') + dec = 0x3F; + else if (ch != '=') + continue; + else + break; + + buf = (buf << 6) | dec; + chcnt++; + + if (chcnt == 2) + bin.push_back((buf >> 4) & 0xFF); + else if (chcnt == 3) + bin.push_back((buf >> 2) & 0xFF); + else if (chcnt == 4) + { + bin.push_back((buf >> 0) & 0xFF); + chcnt = 0; + } + } +} + +bool PListXmlParser::FindTag(std::string & name, TagType & type) +{ + char ch; + bool in_name = false; + + name.clear(); + type = TagType::None; + + do { + ch = GetChar(); + } while (ch != '<' && ch != 0); + + if (ch == 0) + return false; + + ch = GetChar(); + + switch (ch) + { + case '?': + type = TagType::ProcInstr; + break; + case '!': + type = TagType::Doctype; + break; + case '/': + type = TagType::End; + in_name = true; + break; + default: + type = TagType::Start; + name.push_back(ch); + in_name = true; + break; + } + + do + { + ch = GetChar(); + + switch (ch) + { + case 0x09: + case 0x0A: + case 0x0D: + case ' ': + case '>': + in_name = false; + break; + case '/': + if (m_data[m_idx] == '>') + type = TagType::Empty; + in_name = false; + break; + } + + if (in_name) + name.push_back(ch); + } while (ch != '>' && ch != 0); + + return true; +} + +bool PListXmlParser::GetContent(std::string & content) +{ + size_t start = m_idx; + + content.clear(); + + while (m_idx < m_size && m_data[m_idx] != '<') + m_idx++; + + if (m_idx == m_size) + return false; + + content.assign(m_data + start, m_idx - start); + + return true; +} + +size_t PListXmlParser::GetContentSize() +{ + size_t start = m_idx; + + while (m_idx < m_size && m_data[m_idx] != '<') + m_idx++; + + if (m_idx == m_size) + return m_idx - start; + + return m_idx - start; +} + +PList::PList() +{ + m_root = nullptr; +} + +PList::~PList() +{ +} + +bool PList::parseXML(const char * data, size_t size) +{ + (void)data; + (void)size; + + return false; +} diff --git a/vendor/apfs-fuse/ApfsLib/PList.h b/vendor/apfs-fuse/ApfsLib/PList.h new file mode 100644 index 0000000..645df2d --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/PList.h @@ -0,0 +1,182 @@ +#pragma once + +#include +#include +#include + +enum PLType +{ + PLType_Integer, + PLType_String, + PLType_Data, + PLType_Array, + PLType_Dict +}; + +class PLInteger; +class PLString; +class PLData; +class PLArray; +class PLDict; + +class PLException : public std::exception +{ +public: + PLException(const char *reason) { m_reason = reason; } + + const char *what() const noexcept override { return m_reason; } + +private: + const char *m_reason; +}; + +class PLObject +{ +protected: + PLObject(); +public: + virtual ~PLObject(); + + virtual PLType type() const = 0; + + const PLInteger *toInt() const; + const PLString *toString() const; + const PLData *toData() const; + const PLArray *toArray() const; + const PLDict *toDict() const; +}; + +class PLInteger : public PLObject +{ + friend class PListXmlParser; +public: + PLInteger(); + virtual ~PLInteger(); + + PLType type() const override; + + int64_t value() const { return m_value; } + +private: + int64_t m_value; +}; + +class PLString : public PLObject +{ + friend class PListXmlParser; +public: + PLString(); + virtual ~PLString(); + + PLType type() const override; + + const std::string &string() const { return m_string; } + +private: + std::string m_string; +}; + +class PLData : public PLObject +{ + friend class PListXmlParser; +public: + PLData(); + virtual ~PLData(); + + PLType type() const override; + + const uint8_t *data() const { return m_data.data(); } + size_t size() const { return m_data.size(); } + +private: + std::vector m_data; +}; + +class PLArray : public PLObject +{ + friend class PListXmlParser; +public: + PLArray(); + virtual ~PLArray(); + + PLType type() const override; + + PLObject *get(size_t idx) const; + size_t size() const { return m_array.size(); } + + const std::vector &array() const { return m_array; } + +private: + std::vector m_array; +}; + +class PLDict : public PLObject +{ + friend class PListXmlParser; +public: + PLDict(); + virtual ~PLDict(); + + PLType type() const override; + + PLObject *get(const char *name) const; + + const std::map &dict() const { return m_dict; } + +private: + std::map m_dict; +}; + +class PListXmlParser +{ + enum class TagType + { + None, + Empty, + Start, + End, + ProcInstr, + Doctype + }; + +public: + PListXmlParser(const char *data, size_t size); + ~PListXmlParser(); + + PLObject *Parse(); + +private: + PLArray * ParseArray(); + PLDict * ParseDict(); + PLObject * ParseObject(); + void Base64Decode(std::vector &bin, const char *str, size_t size); + + bool FindTag(std::string &name, TagType &type); + bool GetContent(std::string &content); + size_t GetContentSize(); + + char GetChar() + { + if (m_idx < m_size) + return m_data[m_idx++]; + else + return 0; + } + + const char * const m_data; + const size_t m_size; + size_t m_idx; +}; + +// Not used ... maybe later, if we need bplists ... +class PList +{ +public: + PList(); + ~PList(); + + bool parseXML(const char *data, size_t size); + +private: + PLObject * m_root; +}; diff --git a/vendor/apfs-fuse/ApfsLib/Unicode.cpp b/vendor/apfs-fuse/ApfsLib/Unicode.cpp new file mode 100644 index 0000000..d2fbab1 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/Unicode.cpp @@ -0,0 +1,328 @@ +#include "Unicode.h" + +#include +#include + +#include "UnicodeTables_v10.h" + +int normalizeJimdo(char32_t ch, char32_t *nfd, uint8_t *ccc) +{ + constexpr int SBase = 0xAC00; + constexpr int LBase = 0x1100; + constexpr int VBase = 0x1161; + constexpr int TBase = 0x11A7; + // constexpr int LCount = 19; + constexpr int VCount = 21; + constexpr int TCount = 28; + constexpr int NCount = VCount * TCount; + // constexpr int SCount = LCount * NCount; + + int SIndex = ch - SBase; + int LIndex = SIndex / NCount; + int VIndex = (SIndex % NCount) / TCount; + int TIndex = SIndex % TCount; + + nfd[0] = LBase + LIndex; + ccc[0] = 0; + nfd[1] = VBase + VIndex; + ccc[1] = 0; + if (TIndex > 0) + { + nfd[2] = TBase + TIndex; + ccc[2] = 0; + return 3; + } + + return 2; +} + +int normalizeOptFoldU32Char(char32_t ch, bool case_fold, char32_t *nfd, uint8_t *ccc) +{ + char32_t ch_idx; + uint16_t hi_res; + uint16_t mi_res; + uint16_t lo_res; + const uint16_t *seq_u16 = 0; + const uint32_t *seq_u32 = 0; + uint32_t seq_len = 0; + uint32_t cnt; + char32_t c; + + ccc[0] = 0; + if (ch >= 0xF0000) + { + if ((ch & 0xFFFE) == 0xFFFE) + return -1; + else + { + nfd[0] = ch; + return 1; + } + } + + if (ch < 0x2FB00) + ch_idx = ch; + else if ((ch & 0xFFFFFE00) == 0xE0000) + ch_idx = ch - 0xB0500; + else + return -1; + + hi_res = nf_trie_hi[ch_idx >> 8]; + + if (hi_res == 0xFFFF) + return -1; + if (hi_res == 0 || ((hi_res & 0xFF00) == 0xAD00)) + { + nfd[0] = ch; + ccc[0] = hi_res & 0xFF; + return 1; + } + + if (hi_res == 0xAC00) // Naja, fast ... sollte funktionieren + return normalizeJimdo(ch, nfd, ccc); + + mi_res = nf_trie_mid[((hi_res & 0xFFF) << 4) | ((ch_idx >> 4) & 0xF)]; + + if (mi_res == 0xFFFF) + return -1; + + if (mi_res == 0xAC00) + return normalizeJimdo(ch, nfd, ccc); + + if (mi_res == 0 || (mi_res & 0xFF00) == 0xAD00) + { + ccc[0] = mi_res & 0xFF; + if (case_fold && (ch < 0x500)) + nfd[0] = nf_basic_cf[ch]; + else + nfd[0] = ch; + return 1; + } + + if ((mi_res & 0xFF00) == 0xAE00) + { + uint16_t mask = nf_u16_inv_masks[mi_res & 0xFF]; + if ((mask >> (ch_idx & 0xF)) & 1) + return -1; + if (case_fold && (ch < 0x500)) + nfd[0] = nf_basic_cf[ch]; + else + nfd[0] = ch; + return 1; + } + + lo_res = nf_trie_lo[((mi_res & 0xFFF) << 4) | (ch_idx & 0xF)]; + + if (lo_res == 0xFFFF) + return -1; + + if (lo_res == 0xAC00) + return normalizeJimdo(ch, nfd, ccc); + + if (lo_res < 0xB000 || lo_res >= 0xF900) + { + if (lo_res == 0 || ((lo_res & 0xFF00) == 0xAD00)) + ccc[0] = lo_res & 0xFF; + else + ch = lo_res; + + if (case_fold && (ch < 0x500)) + nfd[0] = nf_basic_cf[ch]; + else + nfd[0] = ch; + return 1; + } + + switch ((lo_res >> 12) & 0xF) + { + case 0xB: + if ((lo_res & 0x800) && !case_fold) + { + nfd[0] = ch; + return 1; + } + + seq_u16 = nf_u16_seq_2 + 2 * (lo_res & 0x7FF); + seq_len = 2; + break; + + case 0xC: + if ((lo_res & 0x800) && !case_fold) + { + nfd[0] = ch; + return 1; + } + + seq_u16 = nf_u16_seq_3 + 3 * (lo_res & 0x7FF); + seq_len = 3; + break; + + case 0xD: + seq_u16 = nf_u16_seq_misc + (lo_res & 0x3FF) + 1; + seq_len = nf_u16_seq_misc[lo_res & 0x3FF]; // Rest >> 4 in eax + ccc[0] = seq_len >> 4; + seq_len &= 0xF; + if (seq_len > 4) + return 0; + break; + + case 0xE: + if ((lo_res & 0x800) && !case_fold) + { + nfd[0] = ch; + return 1; + } + + seq_u32 = nf_u32_char + (lo_res & 0x7FF); + seq_len = 1; + break; + + case 0xF: + seq_u32 = nf_u32_seq_misc + (lo_res & 0x3FF) + 1; + seq_len = nf_u32_seq_misc[lo_res & 0x3FF]; + ccc[0] = seq_len >> 4; + seq_len &= 0xF; + if (seq_len > 4) + return 0; + break; + } + + for (cnt = 0; cnt < seq_len; cnt++) + { + if (seq_u16) + c = seq_u16[cnt]; + else + c = seq_u32[cnt]; + nfd[cnt] = c; + + if (cnt > 0) + { + if (c >= 0xF0000) + { + ccc[cnt] = 0; + continue; + } + + if (c == 0x3B9) + { + ccc[cnt] = 0xF0; + continue; + } + + ch_idx = (c > 0x2FB00) ? (c - 0xB0500) : c; + + hi_res = nf_trie_hi[ch_idx >> 8]; + + if (hi_res == 0 || ((hi_res & 0xFF00) == 0xAD00)) + { + ccc[cnt] = hi_res & 0xFF; + continue; + } + + mi_res = nf_trie_mid[((hi_res & 0xFFF) << 4) | ((ch_idx >> 4) & 0xF)]; + + if (mi_res == 0 || ((mi_res & 0xFF00) == 0xAE00)) + { + ccc[cnt] = 0; + continue; + } + if ((mi_res & 0xFF00) == 0xAD00) + { + ccc[cnt] = mi_res & 0xFF; + continue; + } + + lo_res = nf_trie_lo[((mi_res & 0xFFF) << 4) | (ch_idx & 0xF)]; + + if ((lo_res & 0xFF00) == 0xAD00) + ccc[cnt] = lo_res & 0xFF; + else + ccc[cnt] = 0; + } + } + + if (case_fold) + { + if (nfd[0] < 0x500) + nfd[0] = nf_basic_cf[nfd[0]]; + + if (cnt >= 2) + { + if (nfd[cnt - 1] == 0x345) + nfd[cnt - 1] = 0x3B9; + } + } + + return cnt; +} + +void CanonicalReorder(char32_t *nfd, uint8_t *ccc, size_t len) +{ + size_t i; + size_t k; + bool sorted = false; + uint8_t sort_ccc; + char32_t sort_ch; + + for (k = 0; k < (len - 1); k++) + { + if (ccc[k] == 0) + continue; + + if (ccc[k + 1] == 0) + continue; + + do + { + sorted = true; + + for (i = k; i < (len - 1) && ccc[i + 1] != 0; i++) + { + if (ccc[i] > ccc[i + 1]) + { + sorted = false; + sort_ccc = ccc[i + 1]; + ccc[i + 1] = ccc[i]; + ccc[i] = sort_ccc; + sort_ch = nfd[i + 1]; + nfd[i + 1] = nfd[i]; + nfd[i] = sort_ch; + } + } + } while (!sorted); + + k = i + 1; + } +} + +bool NormalizeFoldString(std::vector &nfd, const std::vector &in, bool case_fold) +{ + char32_t ch; + size_t k; + size_t out_size; + int rc; + + std::vector ccc; + + nfd.clear(); + nfd.resize(in.size() * 4); + ccc.resize(nfd.size()); + + for (k = 0, out_size = 0; k < in.size(); k++) + { + ch = in[k]; + rc = normalizeOptFoldU32Char(ch, case_fold, nfd.data() + out_size, ccc.data() + out_size); + + if (rc == -1) + return false; + + out_size += rc; + } + + nfd.resize(out_size); + ccc.resize(out_size); + + CanonicalReorder(nfd.data(), ccc.data(), nfd.size()); + + return true; +} diff --git a/vendor/apfs-fuse/ApfsLib/Unicode.h b/vendor/apfs-fuse/ApfsLib/Unicode.h new file mode 100644 index 0000000..140d59c --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/Unicode.h @@ -0,0 +1,10 @@ +#pragma once + +#include +#include +#include + +int normalizeOptFoldU32Char(char32_t ch, bool case_insensitive, char32_t *sequence_out, unsigned char *unknown_out); +void CanonicalReorder(char32_t *nfd, uint8_t *ccc, size_t len); + +bool NormalizeFoldString(std::vector &out, const std::vector &in, bool case_fold); diff --git a/vendor/apfs-fuse/ApfsLib/UnicodeTables_v10.h b/vendor/apfs-fuse/ApfsLib/UnicodeTables_v10.h new file mode 100644 index 0000000..ba5f011 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/UnicodeTables_v10.h @@ -0,0 +1,1027 @@ +#include + +static const uint16_t nf_trie_hi[0x300] = +{ + // 0000 + 0xC000, 0xC001, 0xC002, 0xC003, 0xC004, 0xC005, 0xC006, 0xC007, 0xC008, 0xC009, 0xC00A, 0xC00B, 0xC00C, 0xC00D, 0xC00E, 0xC00F, + 0xC010, 0x0000, 0xC011, 0xC012, 0x0000, 0x0000, 0xC013, 0xC014, 0xC015, 0xC016, 0xC017, 0xC018, 0xC019, 0xC01A, 0xC01B, 0xC01C, + 0xC01D, 0xC01E, 0xC01F, 0xC020, 0xC021, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xC022, 0xC023, 0xC024, 0xC025, 0xC026, 0xC027, + 0xC028, 0xC029, 0xC02A, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xC02B, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + // 0080 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xC02C, + 0x0000, 0x0000, 0x0000, 0x0000, 0xC02D, 0x0000, 0xC02E, 0xC02F, 0xC030, 0xC031, 0xC032, 0xC033, 0xAC00, 0xAC00, 0xAC00, 0xAC00, + 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, + 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, + 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xC034, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xC035, 0xC036, 0xC037, 0x0000, 0xC038, 0xC039, 0xC03A, + // 0100 + 0xC03B, 0xC03C, 0xC03D, 0xC03E, 0xC03F, 0xC040, 0x0000, 0xC041, 0xC042, 0xC043, 0xC044, 0xC045, 0xC046, 0xFFFF, 0xC047, 0xFFFF, + 0xC048, 0xC049, 0xC04A, 0xC04B, 0xC04C, 0xC04D, 0xC04E, 0xC04F, 0xC050, 0xFFFF, 0xC051, 0xFFFF, 0xC052, 0xC053, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0xC054, 0xC055, 0xC056, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0xC057, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0xC058, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0xC059, 0xC05A, 0xFFFF, 0xFFFF, 0xFFFF, 0xC05B, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + // 0180 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xC05C, 0x0000, 0x0000, 0xC05D, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0xC05E, 0xC05F, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xC060, 0xFFFF, 0xFFFF, 0xFFFF, + 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xC061, 0xC062, 0xC063, 0xC064, 0xC065, 0xC066, 0xC067, 0xC068, 0x0000, 0x0000, 0xC069, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xC06A, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xC06B, 0xC06C, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xC06D, 0xFFFF, + 0xC06E, 0xC06F, 0xC070, 0x0000, 0x0000, 0x0000, 0xC071, 0xC072, 0xC073, 0xC074, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + // 0200 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + // 0280 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xC075, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xC076, 0xC077, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xC078, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xC079, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xC07A, 0xC07B, 0xC07C, 0xC07D, 0xC07E, 0x0000, 0x0000, 0x0000 +}; + +static const uint16_t nf_trie_mid[0x7F0] = +{ + // 0000 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xC000, 0xC001, 0xC002, 0xC003, + 0xC004, 0xC005, 0xC006, 0xC007, 0xC008, 0xC009, 0xC00A, 0xC00B, 0x0000, 0x0000, 0xC00C, 0xC00D, 0xC00E, 0xC00F, 0xC010, 0xC011, + 0xC012, 0xC013, 0xC014, 0xC015, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0xADE6, 0xC016, 0xC017, 0xC018, 0xC019, 0xC01A, 0xC01B, 0xC01C, 0xC01D, 0xC01E, 0xC01F, 0xC020, 0xC021, 0xC022, 0x0000, 0x0000, + 0xC023, 0xC024, 0x0000, 0xC025, 0x0000, 0xC026, 0x0000, 0xC027, 0xC028, 0x0000, 0x0000, 0x0000, 0xC029, 0xC02A, 0xC02B, 0xC02C, + 0xC02D, 0xC02E, 0xC02F, 0xC030, 0xC031, 0xC032, 0xAE00, 0x0000, 0xC033, 0xC034, 0xC035, 0xC036, 0xC037, 0x0000, 0xAE01, 0xAE02, + 0x0000, 0xC038, 0xC039, 0x0000, 0xC03A, 0xC03B, 0x0000, 0xC03C, 0x0000, 0x0000, 0x0000, 0x0000, 0xC03D, 0xC03E, 0xC03F, 0x0000, + 0xAE03, 0xC040, 0x0000, 0xC041, 0xC042, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE04, 0x0000, 0x0000, 0xC043, 0xC044, + // 0080 + 0x0000, 0xC045, 0xC046, 0xAE05, 0x0000, 0xC047, 0xAE01, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0xAE06, 0xFFFF, 0xC048, 0xC049, 0xC04A, + 0x0000, 0x0000, 0xC04B, 0xC04C, 0xC04D, 0xC04E, 0x0000, 0x0000, 0xAE07, 0xAE08, 0xAE09, 0xC04F, 0xC050, 0xC051, 0xAE0A, 0xAE0B, + 0xAE0C, 0xAE08, 0xAE09, 0xC052, 0xC053, 0xC054, 0xAE0D, 0xAE0E, 0xAE0F, 0xAE10, 0xAE09, 0xC055, 0xC056, 0xAE11, 0xAE0A, 0xAE12, + 0xAE13, 0xAE08, 0xAE09, 0xC057, 0xC058, 0xC059, 0xAE0A, 0xAE14, 0xAE15, 0xC05A, 0xAE16, 0xAE17, 0xC05B, 0xAE18, 0xAE0D, 0xAE01, + 0xAE19, 0xAE1A, 0xAE09, 0xAE1B, 0xC05C, 0xC05D, 0xAE0A, 0xAE1C, 0xAE19, 0xAE1A, 0xAE09, 0xC05E, 0xC05F, 0xAE1D, 0xAE0A, 0xAE1E, + 0xAE19, 0xAE1A, 0x0000, 0xC060, 0xC061, 0xAE1F, 0xAE0A, 0x0000, 0xAE20, 0xAE21, 0x0000, 0xAE22, 0xC062, 0xC063, 0xAE0D, 0xAE23, + 0xAE00, 0x0000, 0x0000, 0xC064, 0xC065, 0xAE24, 0xFFFF, 0xFFFF, 0xAE25, 0xAE26, 0xAE27, 0xC066, 0xC067, 0xAE28, 0xFFFF, 0xFFFF, + 0x0000, 0xC068, 0x0000, 0xC069, 0xC06A, 0xC06B, 0xC06C, 0xC06D, 0xC06E, 0xC06F, 0xC070, 0xC071, 0xC072, 0xAE01, 0xFFFF, 0xFFFF, + // 0100 + 0x0000, 0x0000, 0xC073, 0xC074, 0x0000, 0x0000, 0x0000, 0x0000, 0xC075, 0x0000, 0xC076, 0xC077, 0xC078, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0xAE29, 0xAE2A, 0x0000, 0x0000, 0xAE29, 0x0000, 0x0000, 0xAE2B, 0xAE2C, 0xAE2D, 0x0000, 0x0000, + 0x0000, 0xAE2C, 0x0000, 0x0000, 0x0000, 0xC079, 0x0000, 0xAE2E, 0x0000, 0xAE2F, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xC07A, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE2E, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE30, + 0xAE31, 0xC07B, 0x0000, 0xC07C, 0x0000, 0xAE32, 0xAE31, 0xAE33, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xC07D, 0xAE2F, 0xAE2F, + 0xAE05, 0xAE2F, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE14, 0x0000, 0x0000, 0xC07E, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE0E, + 0x0000, 0xAE05, 0xAE24, 0xC07F, 0xAE34, 0x0000, 0xAE0B, 0xAE02, 0x0000, 0x0000, 0xAE24, 0x0000, 0xAE2F, 0xAE35, 0x0000, 0x0000, + 0x0000, 0xC080, 0x0000, 0x0000, 0x0000, 0xAE05, 0xC081, 0xC082, 0xAE2F, 0xAE2F, 0xAE0B, 0xC083, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + // 0180 + 0xC084, 0xC085, 0x0000, 0xC086, 0xC087, 0x0000, 0xC088, 0xC089, 0x0000, 0x0000, 0xC08A, 0x0000, 0x0000, 0x0000, 0xC08B, 0xC08C, + 0x0000, 0x0000, 0x0000, 0xC08D, 0xAE1B, 0x0000, 0x0000, 0x0000, 0xC08E, 0xFFFF, 0xFFFF, 0xFFFF, 0xAE14, 0xC08F, 0xC090, 0xC091, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xC092, 0xC093, 0xADE6, 0xC094, + 0xC095, 0xC096, 0xC097, 0xC098, 0xC099, 0xC09A, 0xC09B, 0xC09C, 0xC09D, 0xC09E, 0xC09F, 0xC0A0, 0xC0A1, 0xC0A2, 0xC0A3, 0xC0A4, + 0xC0A5, 0xC0A6, 0xC0A7, 0xC0A8, 0xC0A9, 0xC0AA, 0xC0AB, 0xC0AC, 0xC0AD, 0xC0AE, 0xC0AF, 0xC0B0, 0xC0B1, 0xC0B2, 0xC0B3, 0xC0B4, + 0xC0B5, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE36, 0xAE37, 0xAE05, 0xAE2E, 0x0000, 0x0000, 0xFFFF, 0xC0B6, 0xC0B7, 0xC0B8, + 0x0000, 0x0000, 0xC0B9, 0xC0BA, 0x0000, 0x0000, 0xC0BB, 0x0000, 0xC0BC, 0xC0BD, 0xC0BE, 0x0000, 0xC0BF, 0x0000, 0x0000, 0x0000, + 0xC0C0, 0x0000, 0xC0C1, 0x0000, 0xC0C2, 0x0000, 0xC0C3, 0xC0C4, 0xC0C5, 0x0000, 0xC0C6, 0x0000, 0x0000, 0x0000, 0xC0C7, 0x0000, + // 0200 + 0x0000, 0x0000, 0xC0C8, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xAE38, 0xFFFF, 0xAE01, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xC0C9, 0xC0CA, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xC0CB, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE0A, 0x0000, 0xAE39, 0x0000, 0xAE1B, 0xAE09, 0xAE3A, 0xAE3B, 0xFFFF, + 0xC0CC, 0xC0CD, 0xC0CE, 0x0000, 0x0000, 0xAE05, 0xC0CF, 0xC0D0, 0xC0D1, 0xC0D2, 0xC0D3, 0xC0D4, 0xC0D5, 0xC0D6, 0xC0D7, 0xC0D8, + 0x0000, 0x0000, 0xAE3C, 0x0000, 0x0000, 0x0000, 0xAE3D, 0xC0D9, 0x0000, 0xAE38, 0xAE3E, 0xAE3E, 0xAE3E, 0xAE3E, 0xADE6, 0xADE6, + 0x0000, 0x0000, 0x0000, 0x0000, 0xAE2F, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0xAE3F, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE32, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE0E, 0xFFFF, 0xAE24, + // 0280 + 0x0000, 0x0000, 0xC0DA, 0x0000, 0xC0DB, 0xC0DC, 0xC0DD, 0xC0DE, 0x0000, 0xC0DF, 0xC0E0, 0xC0E1, 0xC0E2, 0xC0E3, 0x0000, 0xC0E4, + 0xAE40, 0x0000, 0xAE05, 0xAE00, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE05, 0x0000, 0x0000, 0xAE01, 0x0000, 0x0000, 0xAE32, 0x0000, + 0x0000, 0xAE05, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE05, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE0E, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE01, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE2E, 0x0000, 0x0000, 0x0000, 0xAE38, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xAE24, 0xFFFF, 0xC0E5, 0xC0E6, 0xC0E7, 0xC0E8, 0xC0E9, 0xC0EA, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xC0EB, + 0x0000, 0x0000, 0xC0EC, 0xC0ED, 0xC0EE, 0xC0EF, 0xC0F0, 0xC0F1, 0xC0F2, 0xC0F3, 0xC0F4, 0xC0F5, 0xFFFF, 0xFFFF, 0xFFFF, 0xAE41, + // 0300 + 0xC0F6, 0x0000, 0xAE24, 0xAE2F, 0x0000, 0x0000, 0x0000, 0xAE14, 0x0000, 0x0000, 0x0000, 0x0000, 0xC0F7, 0xAE2F, 0xADE6, 0xC0F8, + 0x0000, 0x0000, 0xC0F9, 0x0000, 0x0000, 0xC0FA, 0x0000, 0xAE2E, 0x0000, 0x0000, 0x0000, 0xC0FB, 0xC0FC, 0xAE17, 0x0000, 0xAE05, + 0x0000, 0x0000, 0x0000, 0xAE38, 0xAE0B, 0xAE28, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xC0FD, 0xC0FE, 0xAE42, 0x0000, 0xC0FF, + 0xAE43, 0xAE44, 0xAE3E, 0x0000, 0x0000, 0x0000, 0xAE0E, 0xC100, 0xC101, 0xC102, 0xC103, 0xC104, 0x0000, 0x0000, 0xC105, 0xAE2F, + 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xC106, 0x0000, 0xAE45, 0x0000, 0x0000, 0xAE24, + 0xC107, 0xC108, 0xC109, 0xC10A, 0xC10B, 0xC10C, 0xC10D, 0xC10E, 0xC10F, 0xC110, 0xC111, 0xC112, 0xC113, 0xC114, 0xC115, 0xC116, + 0xC117, 0xC118, 0xC119, 0xC11A, 0xC11B, 0xC11C, 0xC11D, 0xC11E, 0xC11F, 0xC120, 0xC121, 0xC122, 0xC123, 0xC124, 0xFFFF, 0xFFFF, + 0xC125, 0xC126, 0xC127, 0xC128, 0xC129, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE04, 0xAE46, 0x0000, 0x0000, + // 0380 + 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE47, 0x0000, 0x0000, 0xAE14, 0xFFFF, 0xFFFF, 0xAE0B, + 0x0000, 0xAE2F, 0xC12A, 0x0000, 0x0000, 0xAE48, 0xAE49, 0xAE36, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE4A, + 0xAE00, 0x0000, 0xC12B, 0xC12C, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE05, 0xAE4B, 0xAE4C, 0xAE3E, 0xAE4D, + 0xAE4E, 0x0000, 0xAE2D, 0xAE4F, 0xAE0B, 0xAE0B, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE01, + 0xAE50, 0x0000, 0x0000, 0xAE51, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE05, 0xAE24, 0xAE11, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0xC12D, + 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0xAE2E, 0x0000, 0x0000, 0x0000, 0xAE11, 0xC12E, 0xAE24, + 0x0000, 0x0000, 0xAE52, 0x0000, 0xAE01, 0x0000, 0x0000, 0xC12F, 0x0000, 0xAE03, 0x0000, 0x0000, 0xAE53, 0xAE0E, 0xFFFF, 0xFFFF, + 0xC130, 0xC131, 0xC132, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE0B, 0xAE2F, 0xC133, 0xC134, 0xC135, 0x0000, 0xAE24, + // 0400 + 0x0000, 0x0000, 0xAE14, 0x0000, 0x0000, 0x0000, 0xAE54, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0xAE38, 0x0000, 0xAE0E, 0xAE14, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xAE55, 0x0000, 0x0000, 0xAE56, 0x0000, 0xAE57, 0x0000, 0x0000, 0x0000, 0xAE05, 0xAE41, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0xAE58, + 0x0000, 0xAE59, 0x0000, 0xAE5A, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0x0000, 0xAE5B, 0x0000, 0xAE47, 0x0000, 0x0000, + 0xC136, 0xAE5C, 0x0000, 0xC137, 0xAE14, 0xAE30, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0xC138, 0xAE38, + 0x0000, 0x0000, 0x0000, 0xAE5D, 0x0000, 0xAE39, 0x0000, 0xAE5E, 0x0000, 0xAE5F, 0xAE60, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0xAE30, 0xFFFF, 0xFFFF, 0xFFFF, 0xC139, 0xC13A, 0xC13B, 0xC13C, 0x0000, 0x0000, 0x0000, 0xAE61, + 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0xAE05, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + // 0480 + 0x0000, 0x0000, 0x0000, 0x0000, 0xC13D, 0xAE47, 0x0000, 0xC13E, 0x0000, 0xC13F, 0xC140, 0xC141, 0xAE04, 0x0000, 0xAE30, 0xAE2F, + 0xC142, 0x0000, 0xC143, 0xC144, 0xAE32, 0x0000, 0x0000, 0xC145, 0x0000, 0x0000, 0x0000, 0x0000, 0xC146, 0x0000, 0xAE00, 0xAE02, + 0x0000, 0xAE10, 0x0000, 0xC147, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xAE62, 0xAE03, 0xAE2F, 0x0000, 0x0000, 0x0000, 0xC148, 0xAE2F, + 0xAE07, 0xAE08, 0xAE09, 0xC149, 0xC14A, 0xAE63, 0xC14B, 0xC14C, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0xC14D, 0xAE64, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0x0000, 0xC14E, 0xC14F, 0xAE2F, 0xFFFF, 0xFFFF, + 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0x0000, 0xC150, 0xC151, 0xAE0B, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0xC152, 0xAE02, 0xAE2F, 0xAE2E, 0xFFFF, 0x0000, 0x0000, 0x0000, 0xC153, 0xAE2F, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0xAE1B, 0xC154, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + // 0500 + 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xC155, 0xC156, 0x0000, 0x0000, 0x0000, 0xAE65, + 0x0000, 0x0000, 0x0000, 0xC157, 0xC158, 0x0000, 0x0000, 0x0000, 0xAE0A, 0xC159, 0xAE3A, 0xFFFF, 0x0000, 0x0000, 0x0000, 0xAE30, + 0xAE09, 0x0000, 0x0000, 0xC15A, 0xAE0E, 0x0000, 0xAE2E, 0x0000, 0x0000, 0xAE47, 0xAE66, 0xAE38, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xAE67, 0x0000, 0x0000, 0xAE68, 0xC15B, 0xAE2F, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE2F, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE05, 0xAE02, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0xAE32, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0xAE05, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + // 0580 + 0x0000, 0x0000, 0x0000, 0x0000, 0xAE38, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0xAE30, 0x0000, 0xAE05, 0xAE17, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0xAE0B, 0xC15C, + 0x0000, 0x0000, 0x0000, 0xC15D, 0xAE0E, 0xAE3F, 0xAE10, 0xAE69, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0xAE02, 0x0000, 0x0000, 0xAE05, 0xAE6A, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xAE04, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE2E, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE3A, + 0x0000, 0xAE05, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE24, + // 0600 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE01, 0xAE2E, 0xAE30, 0xC15E, 0xAE32, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE0E, + 0x0000, 0x0000, 0xAE6B, 0x0000, 0x0000, 0xC15F, 0xC160, 0xC161, 0xC162, 0x0000, 0xC163, 0xC164, 0xC165, 0x0000, 0xAE30, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0xC166, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE38, 0x0000, 0xAE04, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE36, 0x0000, 0x0000, 0x0000, 0xAE31, 0xAE6C, 0xAE6D, 0xAE6E, 0x0000, 0x0000, 0x0000, + 0xAE6F, 0xAE70, 0x0000, 0xAE71, 0xAE72, 0xAE1A, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE39, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + // 0680 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE73, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE24, 0xAE42, 0xAE00, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xC167, 0xC168, 0xC169, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE74, 0xC16A, 0xFFFF, 0xFFFF, + 0xC16B, 0xC16C, 0xC16D, 0x0000, 0xC16E, 0xAE17, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xAE6E, 0x0000, 0xAE75, 0xAE76, 0xAE77, 0xAE78, 0xAE79, 0xAE7A, 0xAE3F, 0xAE24, 0xAE7B, 0xAE24, 0xFFFF, 0xFFFF, 0xFFFF, 0xAE04, + 0x0000, 0x0000, 0xAE24, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE32, 0xAE05, 0xAE00, 0xAE00, 0xAE00, 0x0000, 0xAE0E, + 0xAE2E, 0x0000, 0xAE05, 0x0000, 0x0000, 0x0000, 0xAE24, 0x0000, 0x0000, 0x0000, 0xAE2E, 0xFFFF, 0xFFFF, 0xFFFF, 0xAE0D, 0x0000, + // 0700 + 0xAE3A, 0x0000, 0x0000, 0xAE24, 0xAE30, 0xAE04, 0xAE0E, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE02, 0xAE2E, 0xAE30, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE32, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE02, 0xFFFF, 0xFFFF, + 0xAE24, 0x0000, 0x0000, 0x0000, 0xAE14, 0xAE2F, 0x0000, 0x0000, 0xAE14, 0x0000, 0xAE0B, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xAE24, 0x0000, 0x0000, 0xAE05, 0xAE2E, 0x0000, 0xAE24, 0xFFFF, 0x0000, 0xAE14, 0xFFFF, 0xFFFF, 0xAE11, 0x0000, 0xAE38, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE38, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0xAE02, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0xAE0B, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + // 0780 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE04, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAE11, 0xFFFF, + 0xC16F, 0xC170, 0xC171, 0xC172, 0xC173, 0xC174, 0xC175, 0xC176, 0xC177, 0xC178, 0xC179, 0xC17A, 0xC17B, 0xC17C, 0xC17D, 0xC17E, + 0xC17F, 0xC180, 0xC181, 0xC182, 0xC183, 0xC184, 0xC185, 0xC186, 0xC187, 0xC188, 0xC189, 0xC18A, 0xC18B, 0xC18C, 0xC18D, 0xC18E, + 0xC18F, 0xC190, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xAE7C, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF +}; + +static const uint16_t nf_basic_cf[0x500] = +{ + // 0000 + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + // 0080 + 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F, + 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F, + 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, + 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x03BC, 0x00B6, 0x00B7, 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF, + 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7, 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF, + 0x00F0, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00D7, 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00DF, + 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7, 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF, + 0x00F0, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7, 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00FF, + // 0100 + 0x0101, 0x0101, 0x0103, 0x0103, 0x0105, 0x0105, 0x0107, 0x0107, 0x0109, 0x0109, 0x010B, 0x010B, 0x010D, 0x010D, 0x010F, 0x010F, + 0x0111, 0x0111, 0x0113, 0x0113, 0x0115, 0x0115, 0x0117, 0x0117, 0x0119, 0x0119, 0x011B, 0x011B, 0x011D, 0x011D, 0x011F, 0x011F, + 0x0121, 0x0121, 0x0123, 0x0123, 0x0125, 0x0125, 0x0127, 0x0127, 0x0129, 0x0129, 0x012B, 0x012B, 0x012D, 0x012D, 0x012F, 0x012F, + 0x0130, 0x0131, 0x0133, 0x0133, 0x0135, 0x0135, 0x0137, 0x0137, 0x0138, 0x013A, 0x013A, 0x013C, 0x013C, 0x013E, 0x013E, 0x0140, + 0x0140, 0x0142, 0x0142, 0x0144, 0x0144, 0x0146, 0x0146, 0x0148, 0x0148, 0x0149, 0x014B, 0x014B, 0x014D, 0x014D, 0x014F, 0x014F, + 0x0151, 0x0151, 0x0153, 0x0153, 0x0155, 0x0155, 0x0157, 0x0157, 0x0159, 0x0159, 0x015B, 0x015B, 0x015D, 0x015D, 0x015F, 0x015F, + 0x0161, 0x0161, 0x0163, 0x0163, 0x0165, 0x0165, 0x0167, 0x0167, 0x0169, 0x0169, 0x016B, 0x016B, 0x016D, 0x016D, 0x016F, 0x016F, + 0x0171, 0x0171, 0x0173, 0x0173, 0x0175, 0x0175, 0x0177, 0x0177, 0x00FF, 0x017A, 0x017A, 0x017C, 0x017C, 0x017E, 0x017E, 0x0073, + // 0180 + 0x0180, 0x0253, 0x0183, 0x0183, 0x0185, 0x0185, 0x0254, 0x0188, 0x0188, 0x0256, 0x0257, 0x018C, 0x018C, 0x018D, 0x01DD, 0x0259, + 0x025B, 0x0192, 0x0192, 0x0260, 0x0263, 0x0195, 0x0269, 0x0268, 0x0199, 0x0199, 0x019A, 0x019B, 0x026F, 0x0272, 0x019E, 0x0275, + 0x01A1, 0x01A1, 0x01A3, 0x01A3, 0x01A5, 0x01A5, 0x0280, 0x01A8, 0x01A8, 0x0283, 0x01AA, 0x01AB, 0x01AD, 0x01AD, 0x0288, 0x01B0, + 0x01B0, 0x028A, 0x028B, 0x01B4, 0x01B4, 0x01B6, 0x01B6, 0x0292, 0x01B9, 0x01B9, 0x01BA, 0x01BB, 0x01BD, 0x01BD, 0x01BE, 0x01BF, + 0x01C0, 0x01C1, 0x01C2, 0x01C3, 0x01C6, 0x01C6, 0x01C6, 0x01C9, 0x01C9, 0x01C9, 0x01CC, 0x01CC, 0x01CC, 0x01CE, 0x01CE, 0x01D0, + 0x01D0, 0x01D2, 0x01D2, 0x01D4, 0x01D4, 0x01D6, 0x01D6, 0x01D8, 0x01D8, 0x01DA, 0x01DA, 0x01DC, 0x01DC, 0x01DD, 0x01DF, 0x01DF, + 0x01E1, 0x01E1, 0x01E3, 0x01E3, 0x01E5, 0x01E5, 0x01E7, 0x01E7, 0x01E9, 0x01E9, 0x01EB, 0x01EB, 0x01ED, 0x01ED, 0x01EF, 0x01EF, + 0x01F0, 0x01F3, 0x01F3, 0x01F3, 0x01F5, 0x01F5, 0x0195, 0x01BF, 0x01F9, 0x01F9, 0x01FB, 0x01FB, 0x01FD, 0x01FD, 0x01FF, 0x01FF, + // 0200 + 0x0201, 0x0201, 0x0203, 0x0203, 0x0205, 0x0205, 0x0207, 0x0207, 0x0209, 0x0209, 0x020B, 0x020B, 0x020D, 0x020D, 0x020F, 0x020F, + 0x0211, 0x0211, 0x0213, 0x0213, 0x0215, 0x0215, 0x0217, 0x0217, 0x0219, 0x0219, 0x021B, 0x021B, 0x021D, 0x021D, 0x021F, 0x021F, + 0x019E, 0x0221, 0x0223, 0x0223, 0x0225, 0x0225, 0x0227, 0x0227, 0x0229, 0x0229, 0x022B, 0x022B, 0x022D, 0x022D, 0x022F, 0x022F, + 0x0231, 0x0231, 0x0233, 0x0233, 0x0234, 0x0235, 0x0236, 0x0237, 0x0238, 0x0239, 0x2C65, 0x023C, 0x023C, 0x019A, 0x2C66, 0x023F, + 0x0240, 0x0242, 0x0242, 0x0180, 0x0289, 0x028C, 0x0247, 0x0247, 0x0249, 0x0249, 0x024B, 0x024B, 0x024D, 0x024D, 0x024F, 0x024F, + 0x0250, 0x0251, 0x0252, 0x0253, 0x0254, 0x0255, 0x0256, 0x0257, 0x0258, 0x0259, 0x025A, 0x025B, 0x025C, 0x025D, 0x025E, 0x025F, + 0x0260, 0x0261, 0x0262, 0x0263, 0x0264, 0x0265, 0x0266, 0x0267, 0x0268, 0x0269, 0x026A, 0x026B, 0x026C, 0x026D, 0x026E, 0x026F, + 0x0270, 0x0271, 0x0272, 0x0273, 0x0274, 0x0275, 0x0276, 0x0277, 0x0278, 0x0279, 0x027A, 0x027B, 0x027C, 0x027D, 0x027E, 0x027F, + // 0280 + 0x0280, 0x0281, 0x0282, 0x0283, 0x0284, 0x0285, 0x0286, 0x0287, 0x0288, 0x0289, 0x028A, 0x028B, 0x028C, 0x028D, 0x028E, 0x028F, + 0x0290, 0x0291, 0x0292, 0x0293, 0x0294, 0x0295, 0x0296, 0x0297, 0x0298, 0x0299, 0x029A, 0x029B, 0x029C, 0x029D, 0x029E, 0x029F, + 0x02A0, 0x02A1, 0x02A2, 0x02A3, 0x02A4, 0x02A5, 0x02A6, 0x02A7, 0x02A8, 0x02A9, 0x02AA, 0x02AB, 0x02AC, 0x02AD, 0x02AE, 0x02AF, + 0x02B0, 0x02B1, 0x02B2, 0x02B3, 0x02B4, 0x02B5, 0x02B6, 0x02B7, 0x02B8, 0x02B9, 0x02BA, 0x02BB, 0x02BC, 0x02BD, 0x02BE, 0x02BF, + 0x02C0, 0x02C1, 0x02C2, 0x02C3, 0x02C4, 0x02C5, 0x02C6, 0x02C7, 0x02C8, 0x02C9, 0x02CA, 0x02CB, 0x02CC, 0x02CD, 0x02CE, 0x02CF, + 0x02D0, 0x02D1, 0x02D2, 0x02D3, 0x02D4, 0x02D5, 0x02D6, 0x02D7, 0x02D8, 0x02D9, 0x02DA, 0x02DB, 0x02DC, 0x02DD, 0x02DE, 0x02DF, + 0x02E0, 0x02E1, 0x02E2, 0x02E3, 0x02E4, 0x02E5, 0x02E6, 0x02E7, 0x02E8, 0x02E9, 0x02EA, 0x02EB, 0x02EC, 0x02ED, 0x02EE, 0x02EF, + 0x02F0, 0x02F1, 0x02F2, 0x02F3, 0x02F4, 0x02F5, 0x02F6, 0x02F7, 0x02F8, 0x02F9, 0x02FA, 0x02FB, 0x02FC, 0x02FD, 0x02FE, 0x02FF, + // 0300 + 0x0300, 0x0301, 0x0302, 0x0303, 0x0304, 0x0305, 0x0306, 0x0307, 0x0308, 0x0309, 0x030A, 0x030B, 0x030C, 0x030D, 0x030E, 0x030F, + 0x0310, 0x0311, 0x0312, 0x0313, 0x0314, 0x0315, 0x0316, 0x0317, 0x0318, 0x0319, 0x031A, 0x031B, 0x031C, 0x031D, 0x031E, 0x031F, + 0x0320, 0x0321, 0x0322, 0x0323, 0x0324, 0x0325, 0x0326, 0x0327, 0x0328, 0x0329, 0x032A, 0x032B, 0x032C, 0x032D, 0x032E, 0x032F, + 0x0330, 0x0331, 0x0332, 0x0333, 0x0334, 0x0335, 0x0336, 0x0337, 0x0338, 0x0339, 0x033A, 0x033B, 0x033C, 0x033D, 0x033E, 0x033F, + 0x0340, 0x0341, 0x0342, 0x0343, 0x0344, 0x03B9, 0x0346, 0x0347, 0x0348, 0x0349, 0x034A, 0x034B, 0x034C, 0x034D, 0x034E, 0x034F, + 0x0350, 0x0351, 0x0352, 0x0353, 0x0354, 0x0355, 0x0356, 0x0357, 0x0358, 0x0359, 0x035A, 0x035B, 0x035C, 0x035D, 0x035E, 0x035F, + 0x0360, 0x0361, 0x0362, 0x0363, 0x0364, 0x0365, 0x0366, 0x0367, 0x0368, 0x0369, 0x036A, 0x036B, 0x036C, 0x036D, 0x036E, 0x036F, + 0x0371, 0x0371, 0x0373, 0x0373, 0x0374, 0x0375, 0x0377, 0x0377, 0x0378, 0x0379, 0x037A, 0x037B, 0x037C, 0x037D, 0x037E, 0x03F3, + // 0380 + 0x0380, 0x0381, 0x0382, 0x0383, 0x0384, 0x0385, 0x03AC, 0x0387, 0x03AD, 0x03AE, 0x03AF, 0x038B, 0x03CC, 0x038D, 0x03CD, 0x03CE, + 0x0390, 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7, 0x03B8, 0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF, + 0x03C0, 0x03C1, 0x03A2, 0x03C3, 0x03C4, 0x03C5, 0x03C6, 0x03C7, 0x03C8, 0x03C9, 0x03CA, 0x03CB, 0x03AC, 0x03AD, 0x03AE, 0x03AF, + 0x03B0, 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7, 0x03B8, 0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF, + 0x03C0, 0x03C1, 0x03C3, 0x03C3, 0x03C4, 0x03C5, 0x03C6, 0x03C7, 0x03C8, 0x03C9, 0x03CA, 0x03CB, 0x03CC, 0x03CD, 0x03CE, 0x03D7, + 0x03B2, 0x03B8, 0x03D2, 0x03D3, 0x03D4, 0x03C6, 0x03C0, 0x03D7, 0x03D9, 0x03D9, 0x03DB, 0x03DB, 0x03DD, 0x03DD, 0x03DF, 0x03DF, + 0x03E1, 0x03E1, 0x03E3, 0x03E3, 0x03E5, 0x03E5, 0x03E7, 0x03E7, 0x03E9, 0x03E9, 0x03EB, 0x03EB, 0x03ED, 0x03ED, 0x03EF, 0x03EF, + 0x03BA, 0x03C1, 0x03F2, 0x03F3, 0x03B8, 0x03B5, 0x03F6, 0x03F8, 0x03F8, 0x03F2, 0x03FB, 0x03FB, 0x03FC, 0x037B, 0x037C, 0x037D, + // 0400 + 0x0450, 0x0451, 0x0452, 0x0453, 0x0454, 0x0455, 0x0456, 0x0457, 0x0458, 0x0459, 0x045A, 0x045B, 0x045C, 0x045D, 0x045E, 0x045F, + 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F, + 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F, + 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F, + 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F, + 0x0450, 0x0451, 0x0452, 0x0453, 0x0454, 0x0455, 0x0456, 0x0457, 0x0458, 0x0459, 0x045A, 0x045B, 0x045C, 0x045D, 0x045E, 0x045F, + 0x0461, 0x0461, 0x0463, 0x0463, 0x0465, 0x0465, 0x0467, 0x0467, 0x0469, 0x0469, 0x046B, 0x046B, 0x046D, 0x046D, 0x046F, 0x046F, + 0x0471, 0x0471, 0x0473, 0x0473, 0x0475, 0x0475, 0x0477, 0x0477, 0x0479, 0x0479, 0x047B, 0x047B, 0x047D, 0x047D, 0x047F, 0x047F, + // 0480 + 0x0481, 0x0481, 0x0482, 0x0483, 0x0484, 0x0485, 0x0486, 0x0487, 0x0488, 0x0489, 0x048B, 0x048B, 0x048D, 0x048D, 0x048F, 0x048F, + 0x0491, 0x0491, 0x0493, 0x0493, 0x0495, 0x0495, 0x0497, 0x0497, 0x0499, 0x0499, 0x049B, 0x049B, 0x049D, 0x049D, 0x049F, 0x049F, + 0x04A1, 0x04A1, 0x04A3, 0x04A3, 0x04A5, 0x04A5, 0x04A7, 0x04A7, 0x04A9, 0x04A9, 0x04AB, 0x04AB, 0x04AD, 0x04AD, 0x04AF, 0x04AF, + 0x04B1, 0x04B1, 0x04B3, 0x04B3, 0x04B5, 0x04B5, 0x04B7, 0x04B7, 0x04B9, 0x04B9, 0x04BB, 0x04BB, 0x04BD, 0x04BD, 0x04BF, 0x04BF, + 0x04CF, 0x04C2, 0x04C2, 0x04C4, 0x04C4, 0x04C6, 0x04C6, 0x04C8, 0x04C8, 0x04CA, 0x04CA, 0x04CC, 0x04CC, 0x04CE, 0x04CE, 0x04CF, + 0x04D1, 0x04D1, 0x04D3, 0x04D3, 0x04D5, 0x04D5, 0x04D7, 0x04D7, 0x04D9, 0x04D9, 0x04DB, 0x04DB, 0x04DD, 0x04DD, 0x04DF, 0x04DF, + 0x04E1, 0x04E1, 0x04E3, 0x04E3, 0x04E5, 0x04E5, 0x04E7, 0x04E7, 0x04E9, 0x04E9, 0x04EB, 0x04EB, 0x04ED, 0x04ED, 0x04EF, 0x04EF, + 0x04F1, 0x04F1, 0x04F3, 0x04F3, 0x04F5, 0x04F5, 0x04F7, 0x04F7, 0x04F9, 0x04F9, 0x04FB, 0x04FB, 0x04FD, 0x04FD, 0x04FF, 0x04FF +}; + +static const uint16_t nf_u16_inv_masks[0x80] = +{ + // 0000 + 0x0001, 0xF800, 0xFFE0, 0x4000, 0xFFFC, 0x8000, 0xC020, 0x6010, 0x0006, 0x0200, 0x0030, 0xC000, 0x7811, 0x003F, 0xFFC0, 0x4011, + 0x0004, 0xFFFE, 0x01FC, 0x6011, 0xFF00, 0x3813, 0x38E7, 0x3C00, 0xFF7E, 0x2010, 0x0002, 0x1C00, 0x00FF, 0xBF9F, 0xFFF9, 0x000F, + 0x0013, 0x0380, 0xD004, 0xFFE3, 0xF000, 0xDA69, 0x010F, 0x1351, 0x0C00, 0xC200, 0xC280, 0x80C2, 0x00C2, 0x0080, 0xE000, 0xFC00, + 0xFE00, 0x2000, 0xFFF0, 0xFFF2, 0x000E, 0x3800, 0x0020, 0x000C, 0xFF80, 0x00C0, 0xFFF8, 0x0FFF, 0xDF40, 0x7F00, 0x8080, 0x0400, + 0x001F, 0x007F, 0x07FF, 0x8181, 0xFF81, 0x0780, 0x0007, 0x0003, 0x0008, 0xF080, 0x6000, 0x0303, 0xE303, 0xC1FF, 0x1000, 0x4800, + 0x0078, 0x0070, 0x1FF0, 0x00F0, 0x7FF0, 0x02C0, 0x6E40, 0x0040, 0x07C8, 0x7000, 0x7C00, 0x0F00, 0x0110, 0x01C0, 0x00F8, 0xE1FC, + 0x01FF, 0x03F8, 0x4280, 0x1F7E, 0xD400, 0x7FF8, 0x0100, 0x0480, 0x4B80, 0x1F00, 0x7FFF, 0x0180, 0x219B, 0x1400, 0x0010, 0x1840, + 0x2020, 0x8400, 0x03A0, 0x3000, 0x0060, 0x0169, 0xF508, 0x157B, 0x5569, 0x0869, 0xA108, 0x0411, 0xFFFD, 0x0000, 0x0000, 0x0000 +}; + +static const uint16_t nf_trie_lo[0x1910] = +{ + // 0000 + 0xB000, 0xB001, 0xB002, 0xB003, 0xB004, 0xB005, 0x0000, 0xB006, 0xB007, 0xB008, 0xB009, 0xB00A, 0xB00B, 0xB00C, 0xB00D, 0xB00E, + 0x0000, 0xB00F, 0xB010, 0xB011, 0xB012, 0xB013, 0xB014, 0x0000, 0x0000, 0xB015, 0xB016, 0xB017, 0xB018, 0xB019, 0x0000, 0xB81A, + 0xB01B, 0xB01C, 0xB01D, 0xB01E, 0xB01F, 0xB020, 0x0000, 0xB021, 0xB022, 0xB023, 0xB024, 0xB025, 0xB026, 0xB027, 0xB028, 0xB029, + 0x0000, 0xB02A, 0xB02B, 0xB02C, 0xB02D, 0xB02E, 0xB02F, 0x0000, 0x0000, 0xB030, 0xB031, 0xB032, 0xB033, 0xB034, 0x0000, 0xB035, + 0xB036, 0xB037, 0xB038, 0xB039, 0xB03A, 0xB03B, 0xB03C, 0xB03D, 0xB03E, 0xB03F, 0xB040, 0xB041, 0xB042, 0xB043, 0xB044, 0xB045, + 0x0000, 0x0000, 0xB046, 0xB047, 0xB048, 0xB049, 0xB04A, 0xB04B, 0xB04C, 0xB04D, 0xB04E, 0xB04F, 0xB050, 0xB051, 0xB052, 0xB053, + 0xB054, 0xB055, 0xB056, 0xB057, 0xB058, 0xB059, 0x0000, 0x0000, 0xB05A, 0xB05B, 0xB05C, 0xB05D, 0xB05E, 0xB05F, 0xB060, 0xB061, + 0xB062, 0x0000, 0x0000, 0x0000, 0xB063, 0xB064, 0xB065, 0xB066, 0x0000, 0xB067, 0xB068, 0xB069, 0xB06A, 0xB06B, 0xB06C, 0x0000, + // 0080 + 0x0000, 0x0000, 0x0000, 0xB06D, 0xB06E, 0xB06F, 0xB070, 0xB071, 0xB072, 0xB873, 0x0000, 0x0000, 0xB074, 0xB075, 0xB076, 0xB077, + 0xB078, 0xB079, 0x0000, 0x0000, 0xB07A, 0xB07B, 0xB07C, 0xB07D, 0xB07E, 0xB07F, 0xB080, 0xB081, 0xB082, 0xB083, 0xB084, 0xB085, + 0xB086, 0xB087, 0xB088, 0xB089, 0xB08A, 0xB08B, 0x0000, 0x0000, 0xB08C, 0xB08D, 0xB08E, 0xB08F, 0xB090, 0xB091, 0xB092, 0xB093, + 0xB094, 0xB095, 0xB096, 0xB097, 0xB098, 0xB099, 0xB09A, 0xB09B, 0xB09C, 0xB09D, 0xB09E, 0xB09F, 0xB0A0, 0xB0A1, 0xB0A2, 0x0000, + 0xB0A3, 0xB0A4, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB0A5, + 0xB0A6, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB0A7, 0xB0A8, 0xB0A9, + 0xB0AA, 0xB0AB, 0xB0AC, 0xB0AD, 0xB0AE, 0xC000, 0xC001, 0xC002, 0xC003, 0xC004, 0xC005, 0xC006, 0xC007, 0x0000, 0xC008, 0xC009, + // 0100 + 0xC00A, 0xC00B, 0xB0AF, 0xB0B0, 0x0000, 0x0000, 0xB0B1, 0xB0B2, 0xB0B3, 0xB0B4, 0xB0B5, 0xB0B6, 0xC00C, 0xC00D, 0xB0B7, 0xB0B8, + 0xB0B9, 0x0000, 0x0000, 0x0000, 0xB0BA, 0xB0BB, 0x0000, 0x0000, 0xB0BC, 0xB0BD, 0xC00E, 0xC00F, 0xB0BE, 0xB0BF, 0xB0C0, 0xB0C1, + 0xB0C2, 0xB0C3, 0xB0C4, 0xB0C5, 0xB0C6, 0xB0C7, 0xB0C8, 0xB0C9, 0xB0CA, 0xB0CB, 0xB0CC, 0xB0CD, 0xB0CE, 0xB0CF, 0xB0D0, 0xB0D1, + 0xB0D2, 0xB0D3, 0xB0D4, 0xB0D5, 0xB0D6, 0xB0D7, 0xB0D8, 0xB0D9, 0xB0DA, 0xB0DB, 0xB0DC, 0xB0DD, 0x0000, 0x0000, 0xB0DE, 0xB0DF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB0E0, 0xB0E1, 0xB0E2, 0xB0E3, 0xC010, 0xC011, 0xC012, 0xC013, 0xB0E4, 0xB0E5, + 0xC014, 0xC015, 0xB0E6, 0xB0E7, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE8, 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xADE8, 0xADD8, 0xADDC, 0xADDC, 0xADDC, 0xADDC, + 0xADDC, 0xADCA, 0xADCA, 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xADCA, 0xADCA, 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xADDC, + // 0180 + 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xAD01, 0xAD01, 0xAD01, 0xAD01, 0xAD01, 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xADE6, 0xADE6, 0xADE6, + 0xD000, 0xD002, 0xADE6, 0xD004, 0xD006, 0xADF0, 0xADE6, 0xADDC, 0xADDC, 0xADDC, 0xADE6, 0xADE6, 0xADE6, 0xADDC, 0xADDC, 0x0000, + 0xADE6, 0xADE6, 0xADE6, 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xADE6, 0xADE8, 0xADDC, 0xADDC, 0xADE6, 0xADE9, 0xADEA, 0xADEA, 0xADE9, + 0xADEA, 0xADEA, 0xADE9, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, + 0x0000, 0x0000, 0x0000, 0x0000, 0x02B9, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x003B, 0x0000, + 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0xB0E8, 0xB0E9, 0x00B7, 0xB0EA, 0xB0EB, 0xB0EC, 0xFFFF, 0xB0ED, 0xFFFF, 0xB0EE, 0xB0EF, + 0xC016, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB0F0, 0xB0F1, 0xB0F2, 0xB0F3, 0xB0F4, 0xB0F5, + // 0200 + 0xC017, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB0F6, 0xB0F7, 0xB0F8, 0xB0F9, 0xB0FA, 0x0000, + 0x0000, 0x0000, 0x0000, 0xB0FB, 0xB0FC, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0xB0FD, 0xB0FE, 0x0000, 0xB0FF, 0x0000, 0x0000, 0x0000, 0xB100, 0x0000, 0x0000, 0x0000, 0x0000, 0xB101, 0xB102, 0xB103, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB104, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB105, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0xB106, 0xB107, 0x0000, 0xB108, 0x0000, 0x0000, 0x0000, 0xB109, 0x0000, 0x0000, 0x0000, 0x0000, 0xB10A, 0xB10B, 0xB10C, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB10D, 0xB10E, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + // 0280 + 0x0000, 0x0000, 0x0000, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0xB10F, 0xB110, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0xB111, 0xB112, 0xB113, 0xB114, 0x0000, 0x0000, 0xB115, 0xB116, 0x0000, 0x0000, 0xB117, 0xB118, 0xB119, 0xB11A, 0xB11B, 0xB11C, + 0x0000, 0x0000, 0xB11D, 0xB11E, 0xB11F, 0xB120, 0xB121, 0xB122, 0x0000, 0x0000, 0xB123, 0xB124, 0xB125, 0xB126, 0xB127, 0xB128, + 0xB129, 0xB12A, 0xB12B, 0xB12C, 0xB12D, 0xB12E, 0x0000, 0x0000, 0xB12F, 0xB130, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0xE800, 0x0000, 0xE801, 0x0000, 0xE802, 0x0000, 0xE803, 0x0000, 0xE804, 0x0000, 0xE805, 0x0000, 0xE806, 0x0000, 0xE807, 0x0000, + 0xE808, 0x0000, 0xE809, 0x0000, 0xE80A, 0x0000, 0xE80B, 0x0000, 0xE80C, 0x0000, 0xE80D, 0x0000, 0xE80E, 0x0000, 0xE80F, 0x0000, + 0xE810, 0x0000, 0xE811, 0x0000, 0xE812, 0x0000, 0xE813, 0x0000, 0xE814, 0x0000, 0xE815, 0x0000, 0xE816, 0x0000, 0xE817, 0x0000, + // 0300 + 0xFFFF, 0xE818, 0xE819, 0xE81A, 0xE81B, 0xE81C, 0xE81D, 0xE81E, 0xE81F, 0xE820, 0xE821, 0xE822, 0xE823, 0xE824, 0xE825, 0xE826, + 0xE827, 0xE828, 0xE829, 0xE82A, 0xE82B, 0xE82C, 0xE82D, 0xE82E, 0xE82F, 0xE830, 0xE831, 0xE832, 0xE833, 0xE834, 0xE835, 0xE836, + 0xE837, 0xE838, 0xE839, 0xE83A, 0xE83B, 0xE83C, 0xE83D, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB931, 0xFFFF, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0x0000, + 0xFFFF, 0xADDC, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADDC, 0xADE6, 0xADE6, 0xADE6, 0xADDE, 0xADDC, 0xADE6, 0xADE6, 0xADE6, 0xADE6, + 0xADE6, 0xADE6, 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xADE6, 0xADE6, 0xADDC, 0xADE6, 0xADE6, 0xADDE, 0xADE4, 0xADE6, + 0xAD0A, 0xAD0B, 0xAD0C, 0xAD0D, 0xAD0E, 0xAD0F, 0xAD10, 0xAD11, 0xAD12, 0xAD13, 0xAD13, 0xAD14, 0xAD15, 0xAD16, 0x0000, 0xAD17, + 0x0000, 0xAD18, 0xAD19, 0x0000, 0xADE6, 0xADDC, 0x0000, 0xAD12, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + // 0380 + 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xAD1E, 0xAD1F, 0xAD20, 0x0000, 0x0000, 0xFFFF, 0x0000, 0x0000, + 0x0000, 0x0000, 0xB132, 0xB133, 0xB134, 0xB135, 0xB136, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD1B, 0xAD1C, 0xAD1D, 0xAD1E, 0xAD1F, + 0xAD20, 0xAD21, 0xAD22, 0xADE6, 0xADE6, 0xADDC, 0xADDC, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADDC, 0xADE6, 0xADE6, 0xADDC, + 0xAD23, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0xB137, 0x0000, 0xB138, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0xB139, 0x0000, 0x0000, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0x0000, 0x0000, 0xADE6, + 0xADE6, 0xADE6, 0xADE6, 0xADDC, 0xADE6, 0x0000, 0x0000, 0xADE6, 0xADE6, 0x0000, 0xADDC, 0xADE6, 0xADE6, 0xADDC, 0x0000, 0x0000, + // 0400 + 0x0000, 0xAD24, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0xADE6, 0xADDC, 0xADE6, 0xADE6, 0xADDC, 0xADE6, 0xADE6, 0xADDC, 0xADDC, 0xADDC, 0xADE6, 0xADDC, 0xADDC, 0xADE6, 0xADDC, 0xADE6, + 0xADE6, 0xADE6, 0xADDC, 0xADE6, 0xADDC, 0xADE6, 0xADDC, 0xADE6, 0xADDC, 0xADE6, 0xADE6, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, + 0xADE6, 0xADE6, 0xADDC, 0xADE6, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0x0000, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, + 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0x0000, 0xADE6, 0xADE6, 0xADE6, 0x0000, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xADDC, 0xADDC, 0xADDC, 0xFFFF, 0xFFFF, 0x0000, 0xFFFF, + // 0480 + 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, + 0xADE6, 0xADE6, 0x0000, 0xADDC, 0xADE6, 0xADE6, 0xADDC, 0xADE6, 0xADE6, 0xADDC, 0xADE6, 0xADE6, 0xADE6, 0xADDC, 0xADDC, 0xADDC, + 0xAD1B, 0xAD1C, 0xAD1D, 0xADE6, 0xADE6, 0xADE6, 0xADDC, 0xADE6, 0xADE6, 0xADDC, 0xADDC, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB13A, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0xB13B, 0x0000, 0x0000, 0xB13C, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD07, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD09, 0x0000, 0x0000, + 0x0000, 0xADE6, 0xADDC, 0xADE6, 0xADE6, 0x0000, 0x0000, 0x0000, 0xB13D, 0xB13E, 0xB13F, 0xB140, 0xB141, 0xB142, 0xB143, 0xB144, + 0x0000, 0xFFFF, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xAD07, 0x0000, 0x0000, 0x0000, + // 0500 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xB145, 0xB146, 0xAD09, 0x0000, 0xFFFF, + 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xB147, 0xB148, 0xFFFF, 0xB149, + 0x0000, 0xFFFF, 0x0000, 0xB14A, 0xFFFF, 0x0000, 0xB14B, 0xFFFF, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xAD07, 0xFFFF, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0xAD09, 0xFFFF, 0xFFFF, + 0xFFFF, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xB14C, 0xB14D, 0xB14E, 0x0000, 0xFFFF, 0xB14F, 0xFFFF, + 0x0000, 0xFFFF, 0x0000, 0x0000, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xAD07, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0x0000, 0x0000, 0x0000, 0xFFFF, 0x0000, 0x0000, 0xAD09, 0xFFFF, 0xFFFF, + 0x0000, 0xFFFF, 0x0000, 0x0000, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xAD07, 0x0000, 0x0000, 0x0000, + // 0580 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0x0000, 0xB150, 0xFFFF, 0xFFFF, 0xB151, 0xB152, 0xAD09, 0xFFFF, 0xFFFF, + 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xB153, 0xB154, 0xFFFF, 0x0000, + 0x0000, 0xFFFF, 0x0000, 0x0000, 0xB155, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0xFFFF, 0x0000, 0xFFFF, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xB156, 0xB157, 0xB158, 0xAD09, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0x0000, 0x0000, 0xB159, 0xFFFF, 0x0000, 0x0000, 0x0000, 0xAD09, 0xFFFF, 0xFFFF, + 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xAD54, 0xAD5B, 0xFFFF, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xAD07, 0x0000, 0x0000, 0x0000, + 0xB15A, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0x0000, 0xB15B, 0xB15C, 0xFFFF, 0xB15D, 0xC018, 0x0000, 0xAD09, 0xFFFF, 0xFFFF, + // 0600 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD09, 0xAD09, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xB15E, 0xB15F, 0xB160, 0xAD09, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xAD09, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0x0000, 0xFFFF, 0x0000, 0x0000, 0xB161, 0x0000, 0xB162, 0xC019, 0xB163, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD67, 0xAD67, 0xAD09, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD6B, 0xAD6B, 0xAD6B, 0xAD6B, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD76, 0xAD76, 0xFFFF, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0x0000, 0xFFFF, 0xAD7A, 0xAD7A, 0xAD7A, 0xAD7A, 0x0000, 0x0000, 0xFFFF, 0xFFFF, + // 0680 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xADDC, 0xADDC, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xADDC, 0x0000, 0xADDC, 0x0000, 0xADD8, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0xB164, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0xB165, 0x0000, 0x0000, + 0x0000, 0x0000, 0xB166, 0x0000, 0x0000, 0x0000, 0x0000, 0xB167, 0x0000, 0x0000, 0x0000, 0x0000, 0xB168, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB169, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, + 0xFFFF, 0xAD81, 0xAD82, 0xD009, 0xAD84, 0xD00C, 0xB16A, 0x0000, 0xB16B, 0x0000, 0xAD82, 0xAD82, 0xAD82, 0xAD82, 0x0000, 0x0000, + 0xAD82, 0xD00F, 0xADE6, 0xADE6, 0xAD09, 0x0000, 0xADE6, 0xADE6, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0xB16C, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0xB16D, 0x0000, 0x0000, + // 0700 + 0x0000, 0x0000, 0xB16E, 0x0000, 0x0000, 0x0000, 0x0000, 0xB16F, 0x0000, 0x0000, 0x0000, 0x0000, 0xB170, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB171, 0x0000, 0x0000, 0x0000, 0xFFFF, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xADDC, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB172, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD07, 0x0000, 0xAD09, 0xAD09, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xADDC, 0x0000, 0x0000, + 0xE83E, 0xE83F, 0xE840, 0xE841, 0xE842, 0xE843, 0xE844, 0xE845, 0xE846, 0xE847, 0xE848, 0xE849, 0xE84A, 0xE84B, 0xE84C, 0xE84D, + 0xE84E, 0xE84F, 0xE850, 0xE851, 0xE852, 0xE853, 0xE854, 0xE855, 0xE856, 0xE857, 0xE858, 0xE859, 0xE85A, 0xE85B, 0xE85C, 0xE85D, + // 0780 + 0xE85E, 0xE85F, 0xE860, 0xE861, 0xE862, 0xE863, 0xFFFF, 0xE864, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xE865, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xADE6, 0xADE6, 0xADE6, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xE866, 0xE867, 0xE868, 0xE869, 0xE86A, 0xE86B, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0xAD09, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0xAD09, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0xAD09, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xADE6, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xADE4, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xADDE, 0xADE6, 0xADDC, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + // 0800 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xADE6, 0xADDC, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0x0000, 0x0000, + 0xAD09, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xFFFF, 0xFFFF, 0xADDC, + 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xADE6, 0xADE6, 0xADDC, 0x0000, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB173, 0x0000, 0xB174, 0x0000, 0xB175, 0x0000, 0xB176, 0x0000, 0xB177, 0x0000, + 0x0000, 0x0000, 0xB178, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0xAD07, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB179, 0x0000, 0xB17A, 0x0000, 0x0000, + 0xB17B, 0xB17C, 0x0000, 0xB17D, 0xAD09, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + // 0880 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xADE6, 0xADDC, 0xADE6, 0xADE6, 0xADE6, + 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD09, 0xAD09, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD07, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xAD09, 0xAD09, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD07, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0xE86C, 0xE86D, 0xE86E, 0xE86F, 0xE870, 0xE871, 0xE872, 0xE873, 0xE874, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xADE6, 0xADE6, 0xADE6, 0x0000, 0xAD01, 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xADE6, 0xADE6, 0xADDC, 0xADDC, 0xADDC, 0xADDC, + // 0900 + 0xADE6, 0x0000, 0xAD01, 0xAD01, 0xAD01, 0xAD01, 0xAD01, 0xAD01, 0xAD01, 0x0000, 0x0000, 0x0000, 0x0000, 0xADDC, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0xADE6, 0x0000, 0x0000, 0x0000, 0xADE6, 0xADE6, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xADE6, 0xADE6, 0xADDC, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADDC, 0xADE6, 0xADE6, 0xADEA, 0xADD6, 0xADDC, + 0xADCA, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, + 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE8, 0xADE4, 0xADE4, 0xADDC, 0xFFFF, 0xADE6, 0xADE9, 0xADDC, 0xADE6, 0xADDC, + 0xB17E, 0xB17F, 0xB180, 0xB181, 0xB182, 0xB183, 0xB184, 0xB185, 0xC01A, 0xC01B, 0xB186, 0xB187, 0xB188, 0xB189, 0xB18A, 0xB18B, + 0xB18C, 0xB18D, 0xB18E, 0xB18F, 0xC01C, 0xC01D, 0xC01E, 0xC01F, 0xB190, 0xB191, 0xB192, 0xB193, 0xC020, 0xC021, 0xB194, 0xB195, + 0xB196, 0xB197, 0xB198, 0xB199, 0xB19A, 0xB19B, 0xB19C, 0xB19D, 0xB19E, 0xB19F, 0xB1A0, 0xB1A1, 0xB1A2, 0xB1A3, 0xC022, 0xC023, + // 0980 + 0xB1A4, 0xB1A5, 0xB1A6, 0xB1A7, 0xB1A8, 0xB1A9, 0xB1AA, 0xB1AB, 0xC024, 0xC025, 0xB1AC, 0xB1AD, 0xB1AE, 0xB1AF, 0xB1B0, 0xB1B1, + 0xB1B2, 0xB1B3, 0xB1B4, 0xB1B5, 0xB1B6, 0xB1B7, 0xB1B8, 0xB1B9, 0xB1BA, 0xB1BB, 0xB1BC, 0xB1BD, 0xC026, 0xC027, 0xC028, 0xC029, + 0xC02A, 0xC02B, 0xC02C, 0xC02D, 0xB1BE, 0xB1BF, 0xB1C0, 0xB1C1, 0xB1C2, 0xB1C3, 0xB1C4, 0xB1C5, 0xC02E, 0xC02F, 0xB1C6, 0xB1C7, + 0xB1C8, 0xB1C9, 0xB1CA, 0xB1CB, 0xC030, 0xC031, 0xC032, 0xC033, 0xC034, 0xC035, 0xB1CC, 0xB1CD, 0xB1CE, 0xB1CF, 0xB1D0, 0xB1D1, + 0xB1D2, 0xB1D3, 0xB1D4, 0xB1D5, 0xB1D6, 0xB1D7, 0xB1D8, 0xB1D9, 0xC036, 0xC037, 0xC038, 0xC039, 0xB1DA, 0xB1DB, 0xB1DC, 0xB1DD, + 0xB1DE, 0xB1DF, 0xB1E0, 0xB1E1, 0xB1E2, 0xB1E3, 0xB1E4, 0xB1E5, 0xB1E6, 0xB1E7, 0xB1E8, 0xB1E9, 0xB1EA, 0xB1EB, 0xB1EC, 0xB1ED, + 0xB1EE, 0xB1EF, 0xB1F0, 0xB1F1, 0xB1F2, 0xB1F3, 0xB1F4, 0xB1F5, 0xB1F6, 0xB1F7, 0xB9F8, 0xB1F9, 0x0000, 0x0000, 0xB9FA, 0x0000, + 0xB1FB, 0xB1FC, 0xB1FD, 0xB1FE, 0xC03A, 0xC03B, 0xC03C, 0xC03D, 0xC03E, 0xC03F, 0xC040, 0xC041, 0xC042, 0xC043, 0xC044, 0xC045, + // 0A00 + 0xC046, 0xC047, 0xC048, 0xC049, 0xC04A, 0xC04B, 0xC04C, 0xC04D, 0xB1FF, 0xB200, 0xB201, 0xB202, 0xB203, 0xB204, 0xC04E, 0xC04F, + 0xC050, 0xC051, 0xC052, 0xC053, 0xC054, 0xC055, 0xC056, 0xC057, 0xB205, 0xB206, 0xB207, 0xB208, 0xB209, 0xB20A, 0xB20B, 0xB20C, + 0xC058, 0xC059, 0xC05A, 0xC05B, 0xC05C, 0xC05D, 0xC05E, 0xC05F, 0xC060, 0xC061, 0xC062, 0xC063, 0xC064, 0xC065, 0xC066, 0xC067, + 0xC068, 0xC069, 0xC06A, 0xC06B, 0xB20D, 0xB20E, 0xB20F, 0xB210, 0xC06C, 0xC06D, 0xC06E, 0xC06F, 0xC070, 0xC071, 0xC072, 0xC073, + 0xC074, 0xC075, 0xB211, 0xB212, 0xB213, 0xB214, 0xB215, 0xB216, 0xB217, 0xB218, 0xE875, 0x0000, 0xE876, 0x0000, 0xE877, 0x0000, + 0xB219, 0xB21A, 0xC076, 0xC077, 0xC078, 0xC079, 0xC07A, 0xC07B, 0xB21B, 0xB21C, 0xC07C, 0xC07D, 0xC07E, 0xC07F, 0xC080, 0xC081, + 0xB21D, 0xB21E, 0xC082, 0xC083, 0xC084, 0xC085, 0xFFFF, 0xFFFF, 0xB21F, 0xB220, 0xC086, 0xC087, 0xC088, 0xC089, 0xFFFF, 0xFFFF, + 0xB221, 0xB222, 0xC08A, 0xC08B, 0xC08C, 0xC08D, 0xC08E, 0xC08F, 0xB223, 0xB224, 0xC090, 0xC091, 0xC092, 0xC093, 0xC094, 0xC095, + // 0A80 + 0xB225, 0xB226, 0xC096, 0xC097, 0xC098, 0xC099, 0xC09A, 0xC09B, 0xB227, 0xB228, 0xC09C, 0xC09D, 0xC09E, 0xC09F, 0xC0A0, 0xC0A1, + 0xB229, 0xB22A, 0xC0A2, 0xC0A3, 0xC0A4, 0xC0A5, 0xFFFF, 0xFFFF, 0xB22B, 0xB22C, 0xC0A6, 0xC0A7, 0xC0A8, 0xC0A9, 0xFFFF, 0xFFFF, + 0xB22D, 0xB22E, 0xC0AA, 0xC0AB, 0xC0AC, 0xC0AD, 0xC0AE, 0xC0AF, 0xFFFF, 0xB22F, 0xFFFF, 0xC0B0, 0xFFFF, 0xC0B1, 0xFFFF, 0xC0B2, + 0xB230, 0xB231, 0xC0B3, 0xC0B4, 0xC0B5, 0xC0B6, 0xC0B7, 0xC0B8, 0xB232, 0xB233, 0xC0B9, 0xC0BA, 0xC0BB, 0xC0BC, 0xC0BD, 0xC0BE, + 0xB234, 0xB235, 0xB236, 0xB237, 0xB238, 0xB239, 0xB23A, 0xB23B, 0xB23C, 0xB23D, 0xB23E, 0xB23F, 0xB240, 0xB241, 0xFFFF, 0xFFFF, + 0xC0BF, 0xC0C0, 0xD012, 0xD017, 0xD01C, 0xD021, 0xD026, 0xD02B, 0xC0C1, 0xC0C2, 0xD030, 0xD035, 0xD03A, 0xD03F, 0xD044, 0xD049, + 0xC0C3, 0xC0C4, 0xD04E, 0xD053, 0xD058, 0xD05D, 0xD062, 0xD067, 0xC0C5, 0xC0C6, 0xD06C, 0xD071, 0xD076, 0xD07B, 0xD080, 0xD085, + 0xC0C7, 0xC0C8, 0xD08A, 0xD08F, 0xD094, 0xD099, 0xD09E, 0xD0A3, 0xC0C9, 0xC0CA, 0xD0A8, 0xD0AD, 0xD0B2, 0xD0B7, 0xD0BC, 0xD0C1, + // 0B00 + 0xB242, 0xB243, 0xC0CB, 0xB244, 0xC0CC, 0xFFFF, 0xB245, 0xC0CD, 0xB246, 0xB247, 0xB248, 0xB249, 0xB24A, 0x0000, 0x03B9, 0x0000, + 0x0000, 0xB24B, 0xC0CE, 0xB24C, 0xC0CF, 0xFFFF, 0xB24D, 0xC0D0, 0xB24E, 0xB24F, 0xB250, 0xB251, 0xB252, 0xB253, 0xB254, 0xB255, + 0xB256, 0xB257, 0xC0D1, 0xC0D2, 0xFFFF, 0xFFFF, 0xB258, 0xC0D3, 0xB259, 0xB25A, 0xB25B, 0xB25C, 0xFFFF, 0xB25D, 0xB25E, 0xB25F, + 0xB260, 0xB261, 0xC0D4, 0xC0D5, 0xB262, 0xB263, 0xB264, 0xC0D6, 0xB265, 0xB266, 0xB267, 0xB268, 0xB269, 0xB26A, 0xB26B, 0x0060, + 0xFFFF, 0xFFFF, 0xC0D7, 0xB26C, 0xC0D8, 0xFFFF, 0xB26D, 0xC0D9, 0xB26E, 0xB26F, 0xB270, 0xB271, 0xB272, 0x00B4, 0x0000, 0xFFFF, + 0x2002, 0x2003, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0xADE6, 0xADE6, 0xAD01, 0xAD01, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xAD01, 0xAD01, 0xAD01, 0xADE6, 0xADE6, 0x0000, 0x0000, 0x0000, + 0x0000, 0xADE6, 0x0000, 0x0000, 0x0000, 0xAD01, 0xAD01, 0xADE6, 0xADDC, 0xADE6, 0xAD01, 0xAD01, 0xADDC, 0xADDC, 0xADDC, 0xADDC, + // 0B80 + 0xADE6, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x03A9, 0x0000, 0x0000, 0x0000, 0x004B, 0xB273, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xE878, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0xE879, 0xE87A, 0xE87B, 0xE87C, 0xE87D, 0xE87E, 0xE87F, 0xE880, 0xE881, 0xE882, 0xE883, 0xE884, 0xE885, 0xE886, 0xE887, 0xE888, + 0x0000, 0x0000, 0x0000, 0xE889, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB274, 0xB275, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB276, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB277, 0xB278, 0xB279, + // 0C00 + 0x0000, 0x0000, 0x0000, 0x0000, 0xB27A, 0x0000, 0x0000, 0x0000, 0x0000, 0xB27B, 0x0000, 0x0000, 0xB27C, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0xB27D, 0x0000, 0xB27E, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0xB27F, 0x0000, 0x0000, 0xB280, 0x0000, 0x0000, 0xB281, 0x0000, 0xB282, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0xB283, 0x0000, 0xB284, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB285, 0xB286, 0xB287, + 0xB288, 0xB289, 0x0000, 0x0000, 0xB28A, 0xB28B, 0x0000, 0x0000, 0xB28C, 0xB28D, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0xB28E, 0xB28F, 0x0000, 0x0000, 0xB290, 0xB291, 0x0000, 0x0000, 0xB292, 0xB293, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB294, 0xB295, 0xB296, 0xB297, + 0xB298, 0xB299, 0xB29A, 0xB29B, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB29C, 0xB29D, 0xB29E, 0xB29F, 0x0000, 0x0000, + // 0C80 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x3008, 0x3009, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xE88A, 0xE88B, 0xE88C, 0xE88D, 0xE88E, 0xE88F, 0xE890, 0xE891, 0xE892, 0xE893, + 0xE894, 0xE895, 0xE896, 0xE897, 0xE898, 0xE899, 0xE89A, 0xE89B, 0xE89C, 0xE89D, 0xE89E, 0xE89F, 0xE8A0, 0xE8A1, 0xE8A2, 0xE8A3, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB2A0, 0x0000, 0x0000, 0x0000, + 0xE8A4, 0xE8A5, 0xE8A6, 0xE8A7, 0xE8A8, 0xE8A9, 0xE8AA, 0xE8AB, 0xE8AC, 0xE8AD, 0xE8AE, 0xE8AF, 0xE8B0, 0xE8B1, 0xE8B2, 0xE8B3, + 0xE8B4, 0xE8B5, 0xE8B6, 0xE8B7, 0xE8B8, 0xE8B9, 0xE8BA, 0xE8BB, 0xE8BC, 0xE8BD, 0xE8BE, 0xE8BF, 0xE8C0, 0xE8C1, 0xE8C2, 0xE8C3, + 0xE8C4, 0xE8C5, 0xE8C6, 0xE8C7, 0xE8C8, 0xE8C9, 0xE8CA, 0xE8CB, 0xE8CC, 0xE8CD, 0xE8CE, 0xE8CF, 0xE8D0, 0xE8D1, 0xE8D2, 0xFFFF, + 0xE8D3, 0x0000, 0xE8D4, 0xE8D5, 0xE8D6, 0x0000, 0x0000, 0xE8D7, 0x0000, 0xE8D8, 0x0000, 0xE8D9, 0x0000, 0xE8DA, 0xE8DB, 0xE8DC, + // 0D00 + 0xE8DD, 0x0000, 0xE8DE, 0x0000, 0x0000, 0xE8DF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xE8E0, 0xE8E1, + 0xE8E2, 0x0000, 0xE8E3, 0x0000, 0xE8E4, 0x0000, 0xE8E5, 0x0000, 0xE8E6, 0x0000, 0xE8E7, 0x0000, 0xE8E8, 0x0000, 0xE8E9, 0x0000, + 0xE8EA, 0x0000, 0xE8EB, 0x0000, 0xE8EC, 0x0000, 0xE8ED, 0x0000, 0xE8EE, 0x0000, 0xE8EF, 0x0000, 0xE8F0, 0x0000, 0xE8F1, 0x0000, + 0xE8F2, 0x0000, 0xE8F3, 0x0000, 0xE8F4, 0x0000, 0xE8F5, 0x0000, 0xE8F6, 0x0000, 0xE8F7, 0x0000, 0xE8F8, 0x0000, 0xE8F9, 0x0000, + 0xE8FA, 0x0000, 0xE8FB, 0x0000, 0xE8FC, 0x0000, 0xE8FD, 0x0000, 0xE8FE, 0x0000, 0xE8FF, 0x0000, 0xE900, 0x0000, 0xE901, 0x0000, + 0xE902, 0x0000, 0xE903, 0x0000, 0xE904, 0x0000, 0xE905, 0x0000, 0xE906, 0x0000, 0xE907, 0x0000, 0xE908, 0x0000, 0xE909, 0x0000, + 0xE90A, 0x0000, 0xE90B, 0x0000, 0xE90C, 0x0000, 0xE90D, 0x0000, 0xE90E, 0x0000, 0xE90F, 0x0000, 0xE910, 0x0000, 0xE911, 0x0000, + 0xE912, 0x0000, 0xE913, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xE914, 0x0000, 0xE915, 0x0000, 0xADE6, + // 0D80 + 0xADE6, 0xADE6, 0xE916, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xAD09, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xADDA, 0xADE4, 0xADE8, 0xADDE, 0xADE0, 0xADE0, + 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB2A1, 0x0000, 0xB2A2, 0x0000, + 0xB2A3, 0x0000, 0xB2A4, 0x0000, 0xB2A5, 0x0000, 0xB2A6, 0x0000, 0xB2A7, 0x0000, 0xB2A8, 0x0000, 0xB2A9, 0x0000, 0xB2AA, 0x0000, + 0xB2AB, 0x0000, 0xB2AC, 0x0000, 0x0000, 0xB2AD, 0x0000, 0xB2AE, 0x0000, 0xB2AF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0xB2B0, 0xB2B1, 0x0000, 0xB2B2, 0xB2B3, 0x0000, 0xB2B4, 0xB2B5, 0x0000, 0xB2B6, 0xB2B7, 0x0000, 0xB2B8, 0xB2B9, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0xB2BA, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xAD08, 0xAD08, 0x0000, 0x0000, 0x0000, 0xB2BB, 0x0000, + // 0E00 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB2BC, 0x0000, 0xB2BD, 0x0000, + 0xB2BE, 0x0000, 0xB2BF, 0x0000, 0xB2C0, 0x0000, 0xB2C1, 0x0000, 0xB2C2, 0x0000, 0xB2C3, 0x0000, 0xB2C4, 0x0000, 0xB2C5, 0x0000, + 0xB2C6, 0x0000, 0xB2C7, 0x0000, 0x0000, 0xB2C8, 0x0000, 0xB2C9, 0x0000, 0xB2CA, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0xB2CB, 0xB2CC, 0x0000, 0xB2CD, 0xB2CE, 0x0000, 0xB2CF, 0xB2D0, 0x0000, 0xB2D1, 0xB2D2, 0x0000, 0xB2D3, 0xB2D4, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0xB2D5, 0x0000, 0x0000, 0xB2D6, 0xB2D7, 0xB2D8, 0xB2D9, 0x0000, 0x0000, 0x0000, 0xB2DA, 0x0000, + 0xE917, 0x0000, 0xE918, 0x0000, 0xE919, 0x0000, 0xE91A, 0x0000, 0xE91B, 0x0000, 0xE91C, 0x0000, 0xE91D, 0x0000, 0xE91E, 0x0000, + 0xE91F, 0x0000, 0xE920, 0x0000, 0xE921, 0x0000, 0xE922, 0x0000, 0xE923, 0x0000, 0xE924, 0x0000, 0xE925, 0x0000, 0xE926, 0x0000, + 0xE927, 0x0000, 0xE928, 0x0000, 0xE929, 0x0000, 0xE92A, 0x0000, 0xE92B, 0x0000, 0xE92C, 0x0000, 0xE92D, 0x0000, 0x0000, 0xADE6, + // 0E80 + 0x0000, 0x0000, 0x0000, 0x0000, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0x0000, 0x0000, + 0xE92E, 0x0000, 0xE92F, 0x0000, 0xE930, 0x0000, 0xE931, 0x0000, 0xE932, 0x0000, 0xE933, 0x0000, 0xE934, 0x0000, 0xE935, 0x0000, + 0xE936, 0x0000, 0xE937, 0x0000, 0xE938, 0x0000, 0xE939, 0x0000, 0xE93A, 0x0000, 0xE93B, 0x0000, 0x0000, 0x0000, 0xADE6, 0xADE6, + 0xADE6, 0xADE6, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0xE93C, 0x0000, 0xE93D, 0x0000, 0xE93E, 0x0000, 0xE93F, 0x0000, 0xE940, 0x0000, 0xE941, 0x0000, 0xE942, 0x0000, + 0x0000, 0x0000, 0xE943, 0x0000, 0xE944, 0x0000, 0xE945, 0x0000, 0xE946, 0x0000, 0xE947, 0x0000, 0xE948, 0x0000, 0xE949, 0x0000, + 0xE94A, 0x0000, 0xE94B, 0x0000, 0xE94C, 0x0000, 0xE94D, 0x0000, 0xE94E, 0x0000, 0xE94F, 0x0000, 0xE950, 0x0000, 0xE951, 0x0000, + 0xE952, 0x0000, 0xE953, 0x0000, 0xE954, 0x0000, 0xE955, 0x0000, 0xE956, 0x0000, 0xE957, 0x0000, 0xE958, 0x0000, 0xE959, 0x0000, + // 0F00 + 0xE95A, 0x0000, 0xE95B, 0x0000, 0xE95C, 0x0000, 0xE95D, 0x0000, 0xE95E, 0x0000, 0xE95F, 0x0000, 0xE960, 0x0000, 0xE961, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xE962, 0x0000, 0xE963, 0x0000, 0xE964, 0xE965, 0x0000, + 0xE966, 0x0000, 0xE967, 0x0000, 0xE968, 0x0000, 0xE969, 0x0000, 0x0000, 0x0000, 0x0000, 0xE96A, 0x0000, 0xE96B, 0x0000, 0x0000, + 0xE96C, 0x0000, 0xE96D, 0x0000, 0x0000, 0x0000, 0xE96E, 0x0000, 0xE96F, 0x0000, 0xE970, 0x0000, 0xE971, 0x0000, 0xE972, 0x0000, + 0xE973, 0x0000, 0xE974, 0x0000, 0xE975, 0x0000, 0xE976, 0x0000, 0xE977, 0x0000, 0xE978, 0xE979, 0xE97A, 0xE97B, 0xE97C, 0xFFFF, + 0xE97D, 0xE97E, 0xE97F, 0xE980, 0xE981, 0x0000, 0xE982, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD09, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0xAD09, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0x0000, + // 0F80 + 0xADE6, 0xADE6, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xADDC, 0xADDC, 0xADDC, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0xAD09, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, + 0x0000, 0x0000, 0x0000, 0xAD07, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0xAD09, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0x0000, + 0xADE6, 0x0000, 0xADE6, 0xADE6, 0xADDC, 0x0000, 0x0000, 0xADE6, 0xADE6, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xADE6, 0xADE6, + 0x0000, 0xADE6, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD09, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + // 1000 + 0xE983, 0xE984, 0xE985, 0xE986, 0xE987, 0xE988, 0xE989, 0xE98A, 0xE98B, 0xE98C, 0xE98D, 0xE98E, 0xE98F, 0xE990, 0xE991, 0xE992, + 0xE993, 0xE994, 0xE995, 0xE996, 0xE997, 0xE998, 0xE999, 0xE99A, 0xE99B, 0xE99C, 0xE99D, 0xE99E, 0xE99F, 0xE9A0, 0xE9A1, 0xE9A2, + 0xE9A3, 0xE9A4, 0xE9A5, 0xE9A6, 0xE9A7, 0xE9A8, 0xE9A9, 0xE9AA, 0xE9AB, 0xE9AC, 0xE9AD, 0xE9AE, 0xE9AF, 0xE9B0, 0xE9B1, 0xE9B2, + 0xE9B3, 0xE9B4, 0xE9B5, 0xE9B6, 0xE9B7, 0xE9B8, 0xE9B9, 0xE9BA, 0xE9BB, 0xE9BC, 0xE9BD, 0xE9BE, 0xE9BF, 0xE9C0, 0xE9C1, 0xE9C2, + 0xE9C3, 0xE9C4, 0xE9C5, 0xE9C6, 0xE9C7, 0xE9C8, 0xE9C9, 0xE9CA, 0xE9CB, 0xE9CC, 0xE9CD, 0xE9CE, 0xE9CF, 0xE9D0, 0xE9D1, 0xE9D2, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD09, 0xFFFF, 0xFFFF, + 0xAC00, 0xAC00, 0xAC00, 0xAC00, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x8C48, 0x66F4, 0x8ECA, 0x8CC8, 0x6ED1, 0x4E32, 0x53E5, 0x9F9C, 0x9F9C, 0x5951, 0x91D1, 0x5587, 0x5948, 0x61F6, 0x7669, 0x7F85, + // 1080 + 0x863F, 0x87BA, 0x88F8, 0x908F, 0x6A02, 0x6D1B, 0x70D9, 0x73DE, 0x843D, 0x916A, 0x99F1, 0x4E82, 0x5375, 0x6B04, 0x721B, 0x862D, + 0x9E1E, 0x5D50, 0x6FEB, 0x85CD, 0x8964, 0x62C9, 0x81D8, 0x881F, 0x5ECA, 0x6717, 0x6D6A, 0x72FC, 0x90CE, 0x4F86, 0x51B7, 0x52DE, + 0x64C4, 0x6AD3, 0x7210, 0x76E7, 0x8001, 0x8606, 0x865C, 0x8DEF, 0x9732, 0x9B6F, 0x9DFA, 0x788C, 0x797F, 0x7DA0, 0x83C9, 0x9304, + 0x9E7F, 0x8AD6, 0x58DF, 0x5F04, 0x7C60, 0x807E, 0x7262, 0x78CA, 0x8CC2, 0x96F7, 0x58D8, 0x5C62, 0x6A13, 0x6DDA, 0x6F0F, 0x7D2F, + 0x7E37, 0x964B, 0x52D2, 0x808B, 0x51DC, 0x51CC, 0x7A1C, 0x7DBE, 0x83F1, 0x9675, 0x8B80, 0x62CF, 0x6A02, 0x8AFE, 0x4E39, 0x5BE7, + 0x6012, 0x7387, 0x7570, 0x5317, 0x78FB, 0x4FBF, 0x5FA9, 0x4E0D, 0x6CCC, 0x6578, 0x7D22, 0x53C3, 0x585E, 0x7701, 0x8449, 0x8AAA, + 0x6BBA, 0x8FB0, 0x6C88, 0x62FE, 0x82E5, 0x63A0, 0x7565, 0x4EAE, 0x5169, 0x51C9, 0x6881, 0x7CE7, 0x826F, 0x8AD2, 0x91CF, 0x52F5, + 0x5442, 0x5973, 0x5EEC, 0x65C5, 0x6FFE, 0x792A, 0x95AD, 0x9A6A, 0x9E97, 0x9ECE, 0x529B, 0x66C6, 0x6B77, 0x8F62, 0x5E74, 0x6190, + // 1100 + 0x6200, 0x649A, 0x6F23, 0x7149, 0x7489, 0x79CA, 0x7DF4, 0x806F, 0x8F26, 0x84EE, 0x9023, 0x934A, 0x5217, 0x52A3, 0x54BD, 0x70C8, + 0x88C2, 0x8AAA, 0x5EC9, 0x5FF5, 0x637B, 0x6BAE, 0x7C3E, 0x7375, 0x4EE4, 0x56F9, 0x5BE7, 0x5DBA, 0x601C, 0x73B2, 0x7469, 0x7F9A, + 0x8046, 0x9234, 0x96F6, 0x9748, 0x9818, 0x4F8B, 0x79AE, 0x91B4, 0x96B8, 0x60E1, 0x4E86, 0x50DA, 0x5BEE, 0x5C3F, 0x6599, 0x6A02, + 0x71CE, 0x7642, 0x84FC, 0x907C, 0x9F8D, 0x6688, 0x962E, 0x5289, 0x677B, 0x67F3, 0x6D41, 0x6E9C, 0x7409, 0x7559, 0x786B, 0x7D10, + 0x985E, 0x516D, 0x622E, 0x9678, 0x502B, 0x5D19, 0x6DEA, 0x8F2A, 0x5F8B, 0x6144, 0x6817, 0x7387, 0x9686, 0x5229, 0x540F, 0x5C65, + 0x6613, 0x674E, 0x68A8, 0x6CE5, 0x7406, 0x75E2, 0x7F79, 0x88CF, 0x88E1, 0x91CC, 0x96E2, 0x533F, 0x6EBA, 0x541D, 0x71D0, 0x7498, + 0x85FA, 0x96A3, 0x9C57, 0x9E9F, 0x6797, 0x6DCB, 0x81E8, 0x7ACB, 0x7B20, 0x7C92, 0x72C0, 0x7099, 0x8B58, 0x4EC0, 0x8336, 0x523A, + 0x5207, 0x5EA6, 0x62D3, 0x7CD6, 0x5B85, 0x6D1E, 0x66B4, 0x8F3B, 0x884C, 0x964D, 0x898B, 0x5ED3, 0x5140, 0x55C0, 0x0000, 0x0000, + // 1180 + 0x585A, 0x0000, 0x6674, 0x0000, 0x0000, 0x51DE, 0x732A, 0x76CA, 0x793C, 0x795E, 0x7965, 0x798F, 0x9756, 0x7CBE, 0x7FBD, 0x0000, + 0x8612, 0x0000, 0x8AF8, 0x0000, 0x0000, 0x9038, 0x90FD, 0x0000, 0x0000, 0x0000, 0x98EF, 0x98FC, 0x9928, 0x9DB4, 0x90DE, 0x96B7, + 0x4FAE, 0x50E7, 0x514D, 0x52C9, 0x52E4, 0x5351, 0x559D, 0x5606, 0x5668, 0x5840, 0x58A8, 0x5C64, 0x5C6E, 0x6094, 0x6168, 0x618E, + 0x61F2, 0x654F, 0x65E2, 0x6691, 0x6885, 0x6D77, 0x6E1A, 0x6F22, 0x716E, 0x722B, 0x7422, 0x7891, 0x793E, 0x7949, 0x7948, 0x7950, + 0x7956, 0x795D, 0x798D, 0x798E, 0x7A40, 0x7A81, 0x7BC0, 0x7DF4, 0x7E09, 0x7E41, 0x7F72, 0x8005, 0x81ED, 0x8279, 0x8279, 0x8457, + 0x8910, 0x8996, 0x8B01, 0x8B39, 0x8CD3, 0x8D08, 0x8FB6, 0x9038, 0x96E3, 0x97FF, 0x983B, 0x6075, 0xE1D3, 0x8218, 0xFFFF, 0xFFFF, + 0x4E26, 0x51B5, 0x5168, 0x4F80, 0x5145, 0x5180, 0x52C7, 0x52FA, 0x559D, 0x5555, 0x5599, 0x55E2, 0x585A, 0x58B3, 0x5944, 0x5954, + 0x5A62, 0x5B28, 0x5ED2, 0x5ED9, 0x5F69, 0x5FAD, 0x60D8, 0x614E, 0x6108, 0x618E, 0x6160, 0x61F2, 0x6234, 0x63C4, 0x641C, 0x6452, + // 1200 + 0x6556, 0x6674, 0x6717, 0x671B, 0x6756, 0x6B79, 0x6BBA, 0x6D41, 0x6EDB, 0x6ECB, 0x6F22, 0x701E, 0x716E, 0x77A7, 0x7235, 0x72AF, + 0x732A, 0x7471, 0x7506, 0x753B, 0x761D, 0x761F, 0x76CA, 0x76DB, 0x76F4, 0x774A, 0x7740, 0x78CC, 0x7AB1, 0x7BC0, 0x7C7B, 0x7D5B, + 0x7DF4, 0x7F3E, 0x8005, 0x8352, 0x83EF, 0x8779, 0x8941, 0x8986, 0x8996, 0x8ABF, 0x8AF8, 0x8ACB, 0x8B01, 0x8AFE, 0x8AED, 0x8B39, + 0x8B8A, 0x8D08, 0x8F38, 0x9072, 0x9199, 0x9276, 0x967C, 0x96E3, 0x9756, 0x97DB, 0x97FF, 0x980B, 0x983B, 0x9B12, 0x9F9C, 0xE1D4, + 0xE1D5, 0xE1D6, 0x3B9D, 0x4018, 0x4039, 0xE1D7, 0xE1D8, 0xE1D9, 0x9F43, 0x9F8E, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xBADB, 0xBADC, 0xBADD, 0xC8DA, 0xC8DB, 0xBADE, 0xBADF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xFFFF, 0xFFFF, 0xFFFF, 0xBAE0, 0xBAE1, 0xBAE2, 0xBAE3, 0xBAE4, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xB2E5, 0xAD1A, 0xB2E6, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xB2E7, 0xB2E8, 0xC0DC, 0xC0DD, 0xB2E9, 0xB2EA, + // 1280 + 0xB2EB, 0xB2EC, 0xB2ED, 0xB2EE, 0xB2EF, 0xB2F0, 0xB2F1, 0xFFFF, 0xB2F2, 0xB2F3, 0xB2F4, 0xB2F5, 0xB2F6, 0xFFFF, 0xB2F7, 0xFFFF, + 0xB2F8, 0xB2F9, 0xFFFF, 0xB2FA, 0xB2FB, 0xFFFF, 0xB2FC, 0xB2FD, 0xB2FE, 0xB2FF, 0xB300, 0xB301, 0xB302, 0xB303, 0xB304, 0x0000, + 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xADE6, 0xADE6, + 0x0000, 0xE9DA, 0xE9DB, 0xE9DC, 0xE9DD, 0xE9DE, 0xE9DF, 0xE9E0, 0xE9E1, 0xE9E2, 0xE9E3, 0xE9E4, 0xE9E5, 0xE9E6, 0xE9E7, 0xE9E8, + 0xE9E9, 0xE9EA, 0xE9EB, 0xE9EC, 0xE9ED, 0xE9EE, 0xE9EF, 0xE9F0, 0xE9F1, 0xE9F2, 0xE9F3, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xADDC, 0xFFFF, 0xFFFF, + 0xADDC, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + // 1300 + 0xE9F4, 0xE9F5, 0xE9F6, 0xE9F7, 0xE9F8, 0xE9F9, 0xE9FA, 0xE9FB, 0xE9FC, 0xE9FD, 0xE9FE, 0xE9FF, 0xEA00, 0xEA01, 0xEA02, 0xEA03, + 0xEA04, 0xEA05, 0xEA06, 0xEA07, 0xEA08, 0xEA09, 0xEA0A, 0xEA0B, 0xEA0C, 0xEA0D, 0xEA0E, 0xEA0F, 0xEA10, 0xEA11, 0xEA12, 0xEA13, + 0xEA14, 0xEA15, 0xEA16, 0xEA17, 0xEA18, 0xEA19, 0xEA1A, 0xEA1B, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0xEA1C, 0xEA1D, 0xEA1E, 0xEA1F, 0xEA20, 0xEA21, 0xEA22, 0xEA23, 0xEA24, 0xEA25, 0xEA26, 0xEA27, 0xEA28, 0xEA29, 0xEA2A, 0xEA2B, + 0xEA2C, 0xEA2D, 0xEA2E, 0xEA2F, 0xEA30, 0xEA31, 0xEA32, 0xEA33, 0xEA34, 0xEA35, 0xEA36, 0xEA37, 0xEA38, 0xEA39, 0xEA3A, 0xEA3B, + 0xEA3C, 0xEA3D, 0xEA3E, 0xEA3F, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0xADDC, 0x0000, 0xADE6, + 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xADE6, 0xAD01, 0xADDC, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xAD09, + // 1380 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xADE6, 0xADDC, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0xEA40, 0xEA41, 0xEA42, 0xEA43, 0xEA44, 0xEA45, 0xEA46, 0xEA47, 0xEA48, 0xEA49, 0xEA4A, 0xEA4B, 0xEA4C, 0xEA4D, 0xEA4E, 0xEA4F, + 0xEA50, 0xEA51, 0xEA52, 0xEA53, 0xEA54, 0xEA55, 0xEA56, 0xEA57, 0xEA58, 0xEA59, 0xEA5A, 0xEA5B, 0xEA5C, 0xEA5D, 0xEA5E, 0xEA5F, + 0xEA60, 0xEA61, 0xEA62, 0xEA63, 0xEA64, 0xEA65, 0xEA66, 0xEA67, 0xEA68, 0xEA69, 0xEA6A, 0xEA6B, 0xEA6C, 0xEA6D, 0xEA6E, 0xEA6F, + 0xEA70, 0xEA71, 0xEA72, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD09, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, + 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xAD09, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xF000, 0x0000, 0xF003, 0x0000, 0x0000, 0x0000, + // 1400 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xF006, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD09, 0xAD07, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0xADE6, 0xADE6, 0xADE6, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xF009, 0xF00C, + 0x0000, 0x0000, 0x0000, 0xAD09, 0xAD09, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0xAD07, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xAD09, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD07, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD09, 0xAD07, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, + // 1480 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD07, 0xAD09, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0xFFFF, 0x0000, 0x0000, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xAD07, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xF00F, 0xF012, 0xAD09, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xFFFF, 0xFFFF, 0xFFFF, + 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0xAD09, 0x0000, 0x0000, 0x0000, 0xAD07, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xF015, 0xF018, 0x0000, 0xF01B, 0x0000, + 0x0000, 0x0000, 0xAD09, 0xAD07, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + // 1500 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0xF01E, 0xF021, 0x0000, 0x0000, 0x0000, 0xAD09, + 0xAD07, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD09, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD09, 0xAD07, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD09, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xEA73, 0xEA74, 0xEA75, 0xEA76, 0xEA77, 0xEA78, 0xEA79, 0xEA7A, 0xEA7B, 0xEA7C, 0xEA7D, 0xEA7E, 0xEA7F, 0xEA80, 0xEA81, 0xEA82, + 0xEA83, 0xEA84, 0xEA85, 0xEA86, 0xEA87, 0xEA88, 0xEA89, 0xEA8A, 0xEA8B, 0xEA8C, 0xEA8D, 0xEA8E, 0xEA8F, 0xEA90, 0xEA91, 0xEA92, + 0x0000, 0x0000, 0x0000, 0x0000, 0xAD09, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + // 1580 + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD09, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD09, 0x0000, 0x0000, 0x0000, 0xFFFF, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xAD09, + 0x0000, 0x0000, 0xAD07, 0x0000, 0xAD09, 0xAD09, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xAD01, 0xAD01, 0xAD01, 0xAD01, 0xAD01, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0xAD01, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xF024, 0xF027, + // 1600 + 0xF02A, 0xF02E, 0xF032, 0xF036, 0xF03A, 0xADD8, 0xADD8, 0xAD01, 0xAD01, 0xAD01, 0x0000, 0x0000, 0x0000, 0xADE2, 0xADD8, 0xADD8, + 0xADD8, 0xADD8, 0xADD8, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xADDC, + 0xADDC, 0xADDC, 0xADDC, 0x0000, 0x0000, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADDC, 0xADDC, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xF03E, 0xF041, 0xF044, 0xF048, 0xF04C, + 0xF050, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0xADE6, 0xADE6, 0xADE6, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xFFFF, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, + // 1680 + 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xFFFF, 0xFFFF, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, + 0xADE6, 0xADE6, 0xFFFF, 0xADE6, 0xADE6, 0xFFFF, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xADDC, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0xEA93, 0xEA94, 0xEA95, 0xEA96, 0xEA97, 0xEA98, 0xEA99, 0xEA9A, 0xEA9B, 0xEA9C, 0xEA9D, 0xEA9E, 0xEA9F, 0xEAA0, 0xEAA1, 0xEAA2, + 0xEAA3, 0xEAA4, 0xEAA5, 0xEAA6, 0xEAA7, 0xEAA8, 0xEAA9, 0xEAAA, 0xEAAB, 0xEAAC, 0xEAAD, 0xEAAE, 0xEAAF, 0xEAB0, 0xEAB1, 0xEAB2, + 0xEAB3, 0xEAB4, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xADE6, 0xAD07, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, + 0x4E3D, 0x4E38, 0x4E41, 0xE2B5, 0x4F60, 0x4FAE, 0x4FBB, 0x5002, 0x507A, 0x5099, 0x50E7, 0x50CF, 0x349E, 0xE2B6, 0x514D, 0x5154, + // 1700 + 0x5164, 0x5177, 0xE2B7, 0x34B9, 0x5167, 0x518D, 0xE2B8, 0x5197, 0x51A4, 0x4ECC, 0x51AC, 0x51B5, 0xE2B9, 0x51F5, 0x5203, 0x34DF, + 0x523B, 0x5246, 0x5272, 0x5277, 0x3515, 0x52C7, 0x52C9, 0x52E4, 0x52FA, 0x5305, 0x5306, 0x5317, 0x5349, 0x5351, 0x535A, 0x5373, + 0x537D, 0x537F, 0x537F, 0x537F, 0xE2BA, 0x7070, 0x53CA, 0x53DF, 0xE2BB, 0x53EB, 0x53F1, 0x5406, 0x549E, 0x5438, 0x5448, 0x5468, + 0x54A2, 0x54F6, 0x5510, 0x5553, 0x5563, 0x5584, 0x5584, 0x5599, 0x55AB, 0x55B3, 0x55C2, 0x5716, 0x5606, 0x5717, 0x5651, 0x5674, + 0x5207, 0x58EE, 0x57CE, 0x57F4, 0x580D, 0x578B, 0x5832, 0x5831, 0x58AC, 0xE2BC, 0x58F2, 0x58F7, 0x5906, 0x591A, 0x5922, 0x5962, + 0xE2BD, 0xE2BE, 0x59EC, 0x5A1B, 0x5A27, 0x59D8, 0x5A66, 0x36EE, 0x36FC, 0x5B08, 0x5B3E, 0x5B3E, 0xE2BF, 0x5BC3, 0x5BD8, 0x5BE7, + 0x5BF3, 0xE2C0, 0x5BFF, 0x5C06, 0x5F53, 0x5C22, 0x3781, 0x5C60, 0x5C6E, 0x5CC0, 0x5C8D, 0xE2C1, 0x5D43, 0xE2C2, 0x5D6E, 0x5D6B, + 0x5D7C, 0x5DE1, 0x5DE2, 0x382F, 0x5DFD, 0x5E28, 0x5E3D, 0x5E69, 0x3862, 0xE2C3, 0x387C, 0x5EB0, 0x5EB3, 0x5EB6, 0x5ECA, 0xE2C4, + // 1780 + 0x5EFE, 0xE2C5, 0xE2C6, 0x8201, 0x5F22, 0x5F22, 0x38C7, 0xE2C7, 0xE2C8, 0x5F62, 0x5F6B, 0x38E3, 0x5F9A, 0x5FCD, 0x5FD7, 0x5FF9, + 0x6081, 0x393A, 0x391C, 0x6094, 0xE2C9, 0x60C7, 0x6148, 0x614C, 0x614E, 0x614C, 0x617A, 0x618E, 0x61B2, 0x61A4, 0x61AF, 0x61DE, + 0x61F2, 0x61F6, 0x6210, 0x621B, 0x625D, 0x62B1, 0x62D4, 0x6350, 0xE2CA, 0x633D, 0x62FC, 0x6368, 0x6383, 0x63E4, 0xE2CB, 0x6422, + 0x63C5, 0x63A9, 0x3A2E, 0x6469, 0x647E, 0x649D, 0x6477, 0x3A6C, 0x654F, 0x656C, 0xE2CC, 0x65E3, 0x66F8, 0x6649, 0x3B19, 0x6691, + 0x3B08, 0x3AE4, 0x5192, 0x5195, 0x6700, 0x669C, 0x80AD, 0x43D9, 0x6717, 0x671B, 0x6721, 0x675E, 0x6753, 0xE2CD, 0x3B49, 0x67FA, + 0x6785, 0x6852, 0x6885, 0xE2CE, 0x688E, 0x681F, 0x6914, 0x3B9D, 0x6942, 0x69A3, 0x69EA, 0x6AA8, 0xE2CF, 0x6ADB, 0x3C18, 0x6B21, + 0xE2D0, 0x6B54, 0x3C4E, 0x6B72, 0x6B9F, 0x6BBA, 0x6BBB, 0xE2D1, 0xE2D2, 0xE2D3, 0x6C4E, 0xE2D4, 0x6CBF, 0x6CCD, 0x6C67, 0x6D16, + 0x6D3E, 0x6D77, 0x6D41, 0x6D69, 0x6D78, 0x6D85, 0xE2D5, 0x6D34, 0x6E2F, 0x6E6E, 0x3D33, 0x6ECB, 0x6EC7, 0xE2D6, 0x6DF9, 0x6F6E, + // 1800 + 0xE2D7, 0xE2D8, 0x6FC6, 0x7039, 0x701E, 0x701B, 0x3D96, 0x704A, 0x707D, 0x7077, 0x70AD, 0xE2D9, 0x7145, 0xE2DA, 0x719C, 0xE2DB, + 0x7228, 0x7235, 0x7250, 0xE2DC, 0x7280, 0x7295, 0xE2DD, 0xE2DE, 0x737A, 0x738B, 0x3EAC, 0x73A5, 0x3EB8, 0x3EB8, 0x7447, 0x745C, + 0x7471, 0x7485, 0x74CA, 0x3F1B, 0x7524, 0xE2DF, 0x753E, 0xE2E0, 0x7570, 0xE2E1, 0x7610, 0xE2E2, 0xE2E3, 0xE2E4, 0x3FFC, 0x4008, + 0x76F4, 0xE2E5, 0xE2E6, 0xE2E7, 0xE2E8, 0x771E, 0x771F, 0x771F, 0x774A, 0x4039, 0x778B, 0x4046, 0x4096, 0xE2E9, 0x784E, 0x788C, + 0x78CC, 0x40E3, 0xE2EA, 0x7956, 0xE2EB, 0xE2EC, 0x798F, 0x79EB, 0x412F, 0x7A40, 0x7A4A, 0x7A4F, 0xE2ED, 0xE2EE, 0xE2EF, 0x7AEE, + 0x4202, 0xE2F0, 0x7BC6, 0x7BC9, 0x4227, 0xE2F1, 0x7CD2, 0x42A0, 0x7CE8, 0x7CE3, 0x7D00, 0xE2F2, 0x7D63, 0x4301, 0x7DC7, 0x7E02, + 0x7E45, 0x4334, 0xE2F3, 0xE2F4, 0x4359, 0xE2F5, 0x7F7A, 0xE2F6, 0x7F95, 0x7FFA, 0x8005, 0xE2F7, 0xE2F8, 0x8060, 0xE2F9, 0x8070, + 0xE2FA, 0x43D5, 0x80B2, 0x8103, 0x440B, 0x813E, 0x5AB5, 0xE2FB, 0xE2FC, 0xE2FD, 0xE2FE, 0x8201, 0x8204, 0x8F9E, 0x446B, 0x8291, + // 1880 + 0x828B, 0x829D, 0x52B3, 0x82B1, 0x82B3, 0x82BD, 0x82E6, 0xE2FF, 0x82E5, 0x831D, 0x8363, 0x83AD, 0x8323, 0x83BD, 0x83E7, 0x8457, + 0x8353, 0x83CA, 0x83CC, 0x83DC, 0xE300, 0xE301, 0xE302, 0x452B, 0x84F1, 0x84F3, 0x8516, 0xE303, 0x8564, 0xE304, 0x455D, 0x4561, + 0xE305, 0xE306, 0x456B, 0x8650, 0x865C, 0x8667, 0x8669, 0x86A9, 0x8688, 0x870E, 0x86E2, 0x8779, 0x8728, 0x876B, 0x8786, 0x45D7, + 0x87E1, 0x8801, 0x45F9, 0x8860, 0x8863, 0xE307, 0x88D7, 0x88DE, 0x4635, 0x88FA, 0x34BB, 0xE308, 0xE309, 0x46BE, 0x46C7, 0x8AA0, + 0x8AED, 0x8B8A, 0x8C55, 0xE30A, 0x8CAB, 0x8CC1, 0x8D1B, 0x8D77, 0xE30B, 0xE30C, 0x8DCB, 0x8DBC, 0x8DF0, 0xE30D, 0x8ED4, 0x8F38, + 0xE30E, 0xE30F, 0x9094, 0x90F1, 0x9111, 0xE310, 0x911B, 0x9238, 0x92D7, 0x92D8, 0x927C, 0x93F9, 0x9415, 0xE311, 0x958B, 0x4995, + 0x95B7, 0xE312, 0x49E6, 0x96C3, 0x5DB2, 0x9723, 0xE313, 0xE314, 0x4A6E, 0x4A76, 0x97E0, 0xE315, 0x4AB2, 0xE316, 0x980B, 0x980B, + 0x9829, 0xE317, 0x98E2, 0x4B33, 0x9929, 0x99A7, 0x99C2, 0x99FE, 0x4BCE, 0xE318, 0x9B12, 0x9C40, 0x9CFD, 0x4CCE, 0x4CED, 0x9D67, + // 1900 + 0xE319, 0x4CF8, 0xE31A, 0xE31B, 0xE31C, 0x9EBB, 0x4D56, 0x9EF9, 0x9EFE, 0x9F05, 0x9F0F, 0x9F16, 0x9F3B, 0xE31D, 0xFFFF, 0xFFFF +}; + +static const uint16_t nf_u16_seq_2[0x610] = +{ + // 0000 + 0x0041, 0x0300, 0x0041, 0x0301, 0x0041, 0x0302, 0x0041, 0x0303, 0x0041, 0x0308, 0x0041, 0x030A, 0x0043, 0x0327, 0x0045, 0x0300, + 0x0045, 0x0301, 0x0045, 0x0302, 0x0045, 0x0308, 0x0049, 0x0300, 0x0049, 0x0301, 0x0049, 0x0302, 0x0049, 0x0308, 0x004E, 0x0303, + 0x004F, 0x0300, 0x004F, 0x0301, 0x004F, 0x0302, 0x004F, 0x0303, 0x004F, 0x0308, 0x0055, 0x0300, 0x0055, 0x0301, 0x0055, 0x0302, + 0x0055, 0x0308, 0x0059, 0x0301, 0x0073, 0x0073, 0x0061, 0x0300, 0x0061, 0x0301, 0x0061, 0x0302, 0x0061, 0x0303, 0x0061, 0x0308, + 0x0061, 0x030A, 0x0063, 0x0327, 0x0065, 0x0300, 0x0065, 0x0301, 0x0065, 0x0302, 0x0065, 0x0308, 0x0069, 0x0300, 0x0069, 0x0301, + 0x0069, 0x0302, 0x0069, 0x0308, 0x006E, 0x0303, 0x006F, 0x0300, 0x006F, 0x0301, 0x006F, 0x0302, 0x006F, 0x0303, 0x006F, 0x0308, + 0x0075, 0x0300, 0x0075, 0x0301, 0x0075, 0x0302, 0x0075, 0x0308, 0x0079, 0x0301, 0x0079, 0x0308, 0x0041, 0x0304, 0x0061, 0x0304, + 0x0041, 0x0306, 0x0061, 0x0306, 0x0041, 0x0328, 0x0061, 0x0328, 0x0043, 0x0301, 0x0063, 0x0301, 0x0043, 0x0302, 0x0063, 0x0302, + // 0080 + 0x0043, 0x0307, 0x0063, 0x0307, 0x0043, 0x030C, 0x0063, 0x030C, 0x0044, 0x030C, 0x0064, 0x030C, 0x0045, 0x0304, 0x0065, 0x0304, + 0x0045, 0x0306, 0x0065, 0x0306, 0x0045, 0x0307, 0x0065, 0x0307, 0x0045, 0x0328, 0x0065, 0x0328, 0x0045, 0x030C, 0x0065, 0x030C, + 0x0047, 0x0302, 0x0067, 0x0302, 0x0047, 0x0306, 0x0067, 0x0306, 0x0047, 0x0307, 0x0067, 0x0307, 0x0047, 0x0327, 0x0067, 0x0327, + 0x0048, 0x0302, 0x0068, 0x0302, 0x0049, 0x0303, 0x0069, 0x0303, 0x0049, 0x0304, 0x0069, 0x0304, 0x0049, 0x0306, 0x0069, 0x0306, + 0x0049, 0x0328, 0x0069, 0x0328, 0x0049, 0x0307, 0x004A, 0x0302, 0x006A, 0x0302, 0x004B, 0x0327, 0x006B, 0x0327, 0x004C, 0x0301, + 0x006C, 0x0301, 0x004C, 0x0327, 0x006C, 0x0327, 0x004C, 0x030C, 0x006C, 0x030C, 0x004E, 0x0301, 0x006E, 0x0301, 0x004E, 0x0327, + 0x006E, 0x0327, 0x004E, 0x030C, 0x006E, 0x030C, 0x02BC, 0x006E, 0x004F, 0x0304, 0x006F, 0x0304, 0x004F, 0x0306, 0x006F, 0x0306, + 0x004F, 0x030B, 0x006F, 0x030B, 0x0052, 0x0301, 0x0072, 0x0301, 0x0052, 0x0327, 0x0072, 0x0327, 0x0052, 0x030C, 0x0072, 0x030C, + // 0100 + 0x0053, 0x0301, 0x0073, 0x0301, 0x0053, 0x0302, 0x0073, 0x0302, 0x0053, 0x0327, 0x0073, 0x0327, 0x0053, 0x030C, 0x0073, 0x030C, + 0x0054, 0x0327, 0x0074, 0x0327, 0x0054, 0x030C, 0x0074, 0x030C, 0x0055, 0x0303, 0x0075, 0x0303, 0x0055, 0x0304, 0x0075, 0x0304, + 0x0055, 0x0306, 0x0075, 0x0306, 0x0055, 0x030A, 0x0075, 0x030A, 0x0055, 0x030B, 0x0075, 0x030B, 0x0055, 0x0328, 0x0075, 0x0328, + 0x0057, 0x0302, 0x0077, 0x0302, 0x0059, 0x0302, 0x0079, 0x0302, 0x0059, 0x0308, 0x005A, 0x0301, 0x007A, 0x0301, 0x005A, 0x0307, + 0x007A, 0x0307, 0x005A, 0x030C, 0x007A, 0x030C, 0x004F, 0x031B, 0x006F, 0x031B, 0x0055, 0x031B, 0x0075, 0x031B, 0x0041, 0x030C, + 0x0061, 0x030C, 0x0049, 0x030C, 0x0069, 0x030C, 0x004F, 0x030C, 0x006F, 0x030C, 0x0055, 0x030C, 0x0075, 0x030C, 0x00C6, 0x0304, + 0x00E6, 0x0304, 0x0047, 0x030C, 0x0067, 0x030C, 0x004B, 0x030C, 0x006B, 0x030C, 0x004F, 0x0328, 0x006F, 0x0328, 0x01B7, 0x030C, + 0x0292, 0x030C, 0x006A, 0x030C, 0x0047, 0x0301, 0x0067, 0x0301, 0x004E, 0x0300, 0x006E, 0x0300, 0x00C6, 0x0301, 0x00E6, 0x0301, + // 0180 + 0x00D8, 0x0301, 0x00F8, 0x0301, 0x0041, 0x030F, 0x0061, 0x030F, 0x0041, 0x0311, 0x0061, 0x0311, 0x0045, 0x030F, 0x0065, 0x030F, + 0x0045, 0x0311, 0x0065, 0x0311, 0x0049, 0x030F, 0x0069, 0x030F, 0x0049, 0x0311, 0x0069, 0x0311, 0x004F, 0x030F, 0x006F, 0x030F, + 0x004F, 0x0311, 0x006F, 0x0311, 0x0052, 0x030F, 0x0072, 0x030F, 0x0052, 0x0311, 0x0072, 0x0311, 0x0055, 0x030F, 0x0075, 0x030F, + 0x0055, 0x0311, 0x0075, 0x0311, 0x0053, 0x0326, 0x0073, 0x0326, 0x0054, 0x0326, 0x0074, 0x0326, 0x0048, 0x030C, 0x0068, 0x030C, + 0x0041, 0x0307, 0x0061, 0x0307, 0x0045, 0x0327, 0x0065, 0x0327, 0x004F, 0x0307, 0x006F, 0x0307, 0x0059, 0x0304, 0x0079, 0x0304, + 0x00A8, 0x0301, 0x0391, 0x0301, 0x0395, 0x0301, 0x0397, 0x0301, 0x0399, 0x0301, 0x039F, 0x0301, 0x03A5, 0x0301, 0x03A9, 0x0301, + 0x0399, 0x0308, 0x03A5, 0x0308, 0x03B1, 0x0301, 0x03B5, 0x0301, 0x03B7, 0x0301, 0x03B9, 0x0301, 0x03B9, 0x0308, 0x03C5, 0x0308, + 0x03BF, 0x0301, 0x03C5, 0x0301, 0x03C9, 0x0301, 0x03D2, 0x0301, 0x03D2, 0x0308, 0x0415, 0x0300, 0x0415, 0x0308, 0x0413, 0x0301, + // 0200 + 0x0406, 0x0308, 0x041A, 0x0301, 0x0418, 0x0300, 0x0423, 0x0306, 0x0418, 0x0306, 0x0438, 0x0306, 0x0435, 0x0300, 0x0435, 0x0308, + 0x0433, 0x0301, 0x0456, 0x0308, 0x043A, 0x0301, 0x0438, 0x0300, 0x0443, 0x0306, 0x0474, 0x030F, 0x0475, 0x030F, 0x0416, 0x0306, + 0x0436, 0x0306, 0x0410, 0x0306, 0x0430, 0x0306, 0x0410, 0x0308, 0x0430, 0x0308, 0x0415, 0x0306, 0x0435, 0x0306, 0x04D8, 0x0308, + 0x04D9, 0x0308, 0x0416, 0x0308, 0x0436, 0x0308, 0x0417, 0x0308, 0x0437, 0x0308, 0x0418, 0x0304, 0x0438, 0x0304, 0x0418, 0x0308, + 0x0438, 0x0308, 0x041E, 0x0308, 0x043E, 0x0308, 0x04E8, 0x0308, 0x04E9, 0x0308, 0x042D, 0x0308, 0x044D, 0x0308, 0x0423, 0x0304, + 0x0443, 0x0304, 0x0423, 0x0308, 0x0443, 0x0308, 0x0423, 0x030B, 0x0443, 0x030B, 0x0427, 0x0308, 0x0447, 0x0308, 0x042B, 0x0308, + 0x044B, 0x0308, 0x0565, 0x0582, 0x0627, 0x0653, 0x0627, 0x0654, 0x0648, 0x0654, 0x0627, 0x0655, 0x064A, 0x0654, 0x06D5, 0x0654, + 0x06C1, 0x0654, 0x06D2, 0x0654, 0x0928, 0x093C, 0x0930, 0x093C, 0x0933, 0x093C, 0x0915, 0x093C, 0x0916, 0x093C, 0x0917, 0x093C, + // 0280 + 0x091C, 0x093C, 0x0921, 0x093C, 0x0922, 0x093C, 0x092B, 0x093C, 0x092F, 0x093C, 0x09C7, 0x09BE, 0x09C7, 0x09D7, 0x09A1, 0x09BC, + 0x09A2, 0x09BC, 0x09AF, 0x09BC, 0x0A32, 0x0A3C, 0x0A38, 0x0A3C, 0x0A16, 0x0A3C, 0x0A17, 0x0A3C, 0x0A1C, 0x0A3C, 0x0A2B, 0x0A3C, + 0x0B47, 0x0B56, 0x0B47, 0x0B3E, 0x0B47, 0x0B57, 0x0B21, 0x0B3C, 0x0B22, 0x0B3C, 0x0B92, 0x0BD7, 0x0BC6, 0x0BBE, 0x0BC7, 0x0BBE, + 0x0BC6, 0x0BD7, 0x0C46, 0x0C56, 0x0CBF, 0x0CD5, 0x0CC6, 0x0CD5, 0x0CC6, 0x0CD6, 0x0CC6, 0x0CC2, 0x0D46, 0x0D3E, 0x0D47, 0x0D3E, + 0x0D46, 0x0D57, 0x0DD9, 0x0DCA, 0x0DD9, 0x0DCF, 0x0DD9, 0x0DDF, 0x0F42, 0x0FB7, 0x0F4C, 0x0FB7, 0x0F51, 0x0FB7, 0x0F56, 0x0FB7, + 0x0F5B, 0x0FB7, 0x0F40, 0x0FB5, 0x0FB2, 0x0F80, 0x0FB3, 0x0F80, 0x0F92, 0x0FB7, 0x0F9C, 0x0FB7, 0x0FA1, 0x0FB7, 0x0FA6, 0x0FB7, + 0x0FAB, 0x0FB7, 0x0F90, 0x0FB5, 0x1025, 0x102E, 0x1B05, 0x1B35, 0x1B07, 0x1B35, 0x1B09, 0x1B35, 0x1B0B, 0x1B35, 0x1B0D, 0x1B35, + 0x1B11, 0x1B35, 0x1B3A, 0x1B35, 0x1B3C, 0x1B35, 0x1B3E, 0x1B35, 0x1B3F, 0x1B35, 0x1B42, 0x1B35, 0x0041, 0x0325, 0x0061, 0x0325, + // 0300 + 0x0042, 0x0307, 0x0062, 0x0307, 0x0042, 0x0323, 0x0062, 0x0323, 0x0042, 0x0331, 0x0062, 0x0331, 0x0044, 0x0307, 0x0064, 0x0307, + 0x0044, 0x0323, 0x0064, 0x0323, 0x0044, 0x0331, 0x0064, 0x0331, 0x0044, 0x0327, 0x0064, 0x0327, 0x0044, 0x032D, 0x0064, 0x032D, + 0x0045, 0x032D, 0x0065, 0x032D, 0x0045, 0x0330, 0x0065, 0x0330, 0x0046, 0x0307, 0x0066, 0x0307, 0x0047, 0x0304, 0x0067, 0x0304, + 0x0048, 0x0307, 0x0068, 0x0307, 0x0048, 0x0323, 0x0068, 0x0323, 0x0048, 0x0308, 0x0068, 0x0308, 0x0048, 0x0327, 0x0068, 0x0327, + 0x0048, 0x032E, 0x0068, 0x032E, 0x0049, 0x0330, 0x0069, 0x0330, 0x004B, 0x0301, 0x006B, 0x0301, 0x004B, 0x0323, 0x006B, 0x0323, + 0x004B, 0x0331, 0x006B, 0x0331, 0x004C, 0x0323, 0x006C, 0x0323, 0x004C, 0x0331, 0x006C, 0x0331, 0x004C, 0x032D, 0x006C, 0x032D, + 0x004D, 0x0301, 0x006D, 0x0301, 0x004D, 0x0307, 0x006D, 0x0307, 0x004D, 0x0323, 0x006D, 0x0323, 0x004E, 0x0307, 0x006E, 0x0307, + 0x004E, 0x0323, 0x006E, 0x0323, 0x004E, 0x0331, 0x006E, 0x0331, 0x004E, 0x032D, 0x006E, 0x032D, 0x0050, 0x0301, 0x0070, 0x0301, + // 0380 + 0x0050, 0x0307, 0x0070, 0x0307, 0x0052, 0x0307, 0x0072, 0x0307, 0x0052, 0x0323, 0x0072, 0x0323, 0x0052, 0x0331, 0x0072, 0x0331, + 0x0053, 0x0307, 0x0073, 0x0307, 0x0053, 0x0323, 0x0073, 0x0323, 0x0054, 0x0307, 0x0074, 0x0307, 0x0054, 0x0323, 0x0074, 0x0323, + 0x0054, 0x0331, 0x0074, 0x0331, 0x0054, 0x032D, 0x0074, 0x032D, 0x0055, 0x0324, 0x0075, 0x0324, 0x0055, 0x0330, 0x0075, 0x0330, + 0x0055, 0x032D, 0x0075, 0x032D, 0x0056, 0x0303, 0x0076, 0x0303, 0x0056, 0x0323, 0x0076, 0x0323, 0x0057, 0x0300, 0x0077, 0x0300, + 0x0057, 0x0301, 0x0077, 0x0301, 0x0057, 0x0308, 0x0077, 0x0308, 0x0057, 0x0307, 0x0077, 0x0307, 0x0057, 0x0323, 0x0077, 0x0323, + 0x0058, 0x0307, 0x0078, 0x0307, 0x0058, 0x0308, 0x0078, 0x0308, 0x0059, 0x0307, 0x0079, 0x0307, 0x005A, 0x0302, 0x007A, 0x0302, + 0x005A, 0x0323, 0x007A, 0x0323, 0x005A, 0x0331, 0x007A, 0x0331, 0x0068, 0x0331, 0x0074, 0x0308, 0x0077, 0x030A, 0x0079, 0x030A, + 0x0061, 0x02BE, 0x017F, 0x0307, 0x0073, 0x0073, 0x0041, 0x0323, 0x0061, 0x0323, 0x0041, 0x0309, 0x0061, 0x0309, 0x0045, 0x0323, + // 0400 + 0x0065, 0x0323, 0x0045, 0x0309, 0x0065, 0x0309, 0x0045, 0x0303, 0x0065, 0x0303, 0x0049, 0x0309, 0x0069, 0x0309, 0x0049, 0x0323, + 0x0069, 0x0323, 0x004F, 0x0323, 0x006F, 0x0323, 0x004F, 0x0309, 0x006F, 0x0309, 0x0055, 0x0323, 0x0075, 0x0323, 0x0055, 0x0309, + 0x0075, 0x0309, 0x0059, 0x0300, 0x0079, 0x0300, 0x0059, 0x0323, 0x0079, 0x0323, 0x0059, 0x0309, 0x0079, 0x0309, 0x0059, 0x0303, + 0x0079, 0x0303, 0x03B1, 0x0313, 0x03B1, 0x0314, 0x0391, 0x0313, 0x0391, 0x0314, 0x03B5, 0x0313, 0x03B5, 0x0314, 0x0395, 0x0313, + 0x0395, 0x0314, 0x03B7, 0x0313, 0x03B7, 0x0314, 0x0397, 0x0313, 0x0397, 0x0314, 0x03B9, 0x0313, 0x03B9, 0x0314, 0x0399, 0x0313, + 0x0399, 0x0314, 0x03BF, 0x0313, 0x03BF, 0x0314, 0x039F, 0x0313, 0x039F, 0x0314, 0x03C5, 0x0313, 0x03C5, 0x0314, 0x03A5, 0x0314, + 0x03C9, 0x0313, 0x03C9, 0x0314, 0x03A9, 0x0313, 0x03A9, 0x0314, 0x03B1, 0x0300, 0x03B1, 0x0301, 0x03B5, 0x0300, 0x03B5, 0x0301, + 0x03B7, 0x0300, 0x03B7, 0x0301, 0x03B9, 0x0300, 0x03B9, 0x0301, 0x03BF, 0x0300, 0x03BF, 0x0301, 0x03C5, 0x0300, 0x03C5, 0x0301, + // 0480 + 0x03C9, 0x0300, 0x03C9, 0x0301, 0x03B1, 0x0306, 0x03B1, 0x0304, 0x03B1, 0x0345, 0x03B1, 0x0342, 0x0391, 0x0306, 0x0391, 0x0304, + 0x0391, 0x0300, 0x0391, 0x0301, 0x0391, 0x0345, 0x00A8, 0x0342, 0x03B7, 0x0345, 0x03B7, 0x0342, 0x0395, 0x0300, 0x0395, 0x0301, + 0x0397, 0x0300, 0x0397, 0x0301, 0x0397, 0x0345, 0x1FBF, 0x0300, 0x1FBF, 0x0301, 0x1FBF, 0x0342, 0x03B9, 0x0306, 0x03B9, 0x0304, + 0x03B9, 0x0342, 0x0399, 0x0306, 0x0399, 0x0304, 0x0399, 0x0300, 0x0399, 0x0301, 0x1FFE, 0x0300, 0x1FFE, 0x0301, 0x1FFE, 0x0342, + 0x03C5, 0x0306, 0x03C5, 0x0304, 0x03C1, 0x0313, 0x03C1, 0x0314, 0x03C5, 0x0342, 0x03A5, 0x0306, 0x03A5, 0x0304, 0x03A5, 0x0300, + 0x03A5, 0x0301, 0x03A1, 0x0314, 0x00A8, 0x0300, 0x00A8, 0x0301, 0x03C9, 0x0345, 0x03C9, 0x0342, 0x039F, 0x0300, 0x039F, 0x0301, + 0x03A9, 0x0300, 0x03A9, 0x0301, 0x03A9, 0x0345, 0x0041, 0x030A, 0x2190, 0x0338, 0x2192, 0x0338, 0x2194, 0x0338, 0x21D0, 0x0338, + 0x21D4, 0x0338, 0x21D2, 0x0338, 0x2203, 0x0338, 0x2208, 0x0338, 0x220B, 0x0338, 0x2223, 0x0338, 0x2225, 0x0338, 0x223C, 0x0338, + // 0500 + 0x2243, 0x0338, 0x2245, 0x0338, 0x2248, 0x0338, 0x003D, 0x0338, 0x2261, 0x0338, 0x224D, 0x0338, 0x003C, 0x0338, 0x003E, 0x0338, + 0x2264, 0x0338, 0x2265, 0x0338, 0x2272, 0x0338, 0x2273, 0x0338, 0x2276, 0x0338, 0x2277, 0x0338, 0x227A, 0x0338, 0x227B, 0x0338, + 0x2282, 0x0338, 0x2283, 0x0338, 0x2286, 0x0338, 0x2287, 0x0338, 0x22A2, 0x0338, 0x22A8, 0x0338, 0x22A9, 0x0338, 0x22AB, 0x0338, + 0x227C, 0x0338, 0x227D, 0x0338, 0x2291, 0x0338, 0x2292, 0x0338, 0x22B2, 0x0338, 0x22B3, 0x0338, 0x22B4, 0x0338, 0x22B5, 0x0338, + 0x2ADD, 0x0338, 0x304B, 0x3099, 0x304D, 0x3099, 0x304F, 0x3099, 0x3051, 0x3099, 0x3053, 0x3099, 0x3055, 0x3099, 0x3057, 0x3099, + 0x3059, 0x3099, 0x305B, 0x3099, 0x305D, 0x3099, 0x305F, 0x3099, 0x3061, 0x3099, 0x3064, 0x3099, 0x3066, 0x3099, 0x3068, 0x3099, + 0x306F, 0x3099, 0x306F, 0x309A, 0x3072, 0x3099, 0x3072, 0x309A, 0x3075, 0x3099, 0x3075, 0x309A, 0x3078, 0x3099, 0x3078, 0x309A, + 0x307B, 0x3099, 0x307B, 0x309A, 0x3046, 0x3099, 0x309D, 0x3099, 0x30AB, 0x3099, 0x30AD, 0x3099, 0x30AF, 0x3099, 0x30B1, 0x3099, + // 0580 + 0x30B3, 0x3099, 0x30B5, 0x3099, 0x30B7, 0x3099, 0x30B9, 0x3099, 0x30BB, 0x3099, 0x30BD, 0x3099, 0x30BF, 0x3099, 0x30C1, 0x3099, + 0x30C4, 0x3099, 0x30C6, 0x3099, 0x30C8, 0x3099, 0x30CF, 0x3099, 0x30CF, 0x309A, 0x30D2, 0x3099, 0x30D2, 0x309A, 0x30D5, 0x3099, + 0x30D5, 0x309A, 0x30D8, 0x3099, 0x30D8, 0x309A, 0x30DB, 0x3099, 0x30DB, 0x309A, 0x30A6, 0x3099, 0x30EF, 0x3099, 0x30F0, 0x3099, + 0x30F1, 0x3099, 0x30F2, 0x3099, 0x30FD, 0x3099, 0x0066, 0x0066, 0x0066, 0x0069, 0x0066, 0x006C, 0x0073, 0x0074, 0x0073, 0x0074, + 0x0574, 0x0576, 0x0574, 0x0565, 0x0574, 0x056B, 0x057E, 0x0576, 0x0574, 0x056D, 0x05D9, 0x05B4, 0x05F2, 0x05B7, 0x05E9, 0x05C1, + 0x05E9, 0x05C2, 0x05D0, 0x05B7, 0x05D0, 0x05B8, 0x05D0, 0x05BC, 0x05D1, 0x05BC, 0x05D2, 0x05BC, 0x05D3, 0x05BC, 0x05D4, 0x05BC, + 0x05D5, 0x05BC, 0x05D6, 0x05BC, 0x05D8, 0x05BC, 0x05D9, 0x05BC, 0x05DA, 0x05BC, 0x05DB, 0x05BC, 0x05DC, 0x05BC, 0x05DE, 0x05BC, + 0x05E0, 0x05BC, 0x05E1, 0x05BC, 0x05E3, 0x05BC, 0x05E4, 0x05BC, 0x05E6, 0x05BC, 0x05E7, 0x05BC, 0x05E8, 0x05BC, 0x05E9, 0x05BC, + // 0600 + 0x05EA, 0x05BC, 0x05D5, 0x05B9, 0x05D1, 0x05BF, 0x05DB, 0x05BF, 0x05E4, 0x05BF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 +}; + +static const uint16_t nf_u16_seq_3[0x2A0] = +{ + // 0000 + 0x0055, 0x0308, 0x0304, 0x0075, 0x0308, 0x0304, 0x0055, 0x0308, 0x0301, 0x0075, 0x0308, 0x0301, 0x0055, 0x0308, 0x030C, 0x0075, + 0x0308, 0x030C, 0x0055, 0x0308, 0x0300, 0x0075, 0x0308, 0x0300, 0x0041, 0x0308, 0x0304, 0x0061, 0x0308, 0x0304, 0x0041, 0x0307, + 0x0304, 0x0061, 0x0307, 0x0304, 0x004F, 0x0328, 0x0304, 0x006F, 0x0328, 0x0304, 0x0041, 0x030A, 0x0301, 0x0061, 0x030A, 0x0301, + 0x004F, 0x0308, 0x0304, 0x006F, 0x0308, 0x0304, 0x004F, 0x0303, 0x0304, 0x006F, 0x0303, 0x0304, 0x004F, 0x0307, 0x0304, 0x006F, + 0x0307, 0x0304, 0x03B9, 0x0308, 0x0301, 0x03C5, 0x0308, 0x0301, 0x0CC6, 0x0CC2, 0x0CD5, 0x0DD9, 0x0DCF, 0x0DCA, 0x0043, 0x0327, + 0x0301, 0x0063, 0x0327, 0x0301, 0x0045, 0x0304, 0x0300, 0x0065, 0x0304, 0x0300, 0x0045, 0x0304, 0x0301, 0x0065, 0x0304, 0x0301, + 0x0045, 0x0327, 0x0306, 0x0065, 0x0327, 0x0306, 0x0049, 0x0308, 0x0301, 0x0069, 0x0308, 0x0301, 0x004C, 0x0323, 0x0304, 0x006C, + 0x0323, 0x0304, 0x004F, 0x0303, 0x0301, 0x006F, 0x0303, 0x0301, 0x004F, 0x0303, 0x0308, 0x006F, 0x0303, 0x0308, 0x004F, 0x0304, + // 0080 + 0x0300, 0x006F, 0x0304, 0x0300, 0x004F, 0x0304, 0x0301, 0x006F, 0x0304, 0x0301, 0x0052, 0x0323, 0x0304, 0x0072, 0x0323, 0x0304, + 0x0053, 0x0301, 0x0307, 0x0073, 0x0301, 0x0307, 0x0053, 0x030C, 0x0307, 0x0073, 0x030C, 0x0307, 0x0053, 0x0323, 0x0307, 0x0073, + 0x0323, 0x0307, 0x0055, 0x0303, 0x0301, 0x0075, 0x0303, 0x0301, 0x0055, 0x0304, 0x0308, 0x0075, 0x0304, 0x0308, 0x0041, 0x0302, + 0x0301, 0x0061, 0x0302, 0x0301, 0x0041, 0x0302, 0x0300, 0x0061, 0x0302, 0x0300, 0x0041, 0x0302, 0x0309, 0x0061, 0x0302, 0x0309, + 0x0041, 0x0302, 0x0303, 0x0061, 0x0302, 0x0303, 0x0041, 0x0323, 0x0302, 0x0061, 0x0323, 0x0302, 0x0041, 0x0306, 0x0301, 0x0061, + 0x0306, 0x0301, 0x0041, 0x0306, 0x0300, 0x0061, 0x0306, 0x0300, 0x0041, 0x0306, 0x0309, 0x0061, 0x0306, 0x0309, 0x0041, 0x0306, + 0x0303, 0x0061, 0x0306, 0x0303, 0x0041, 0x0323, 0x0306, 0x0061, 0x0323, 0x0306, 0x0045, 0x0302, 0x0301, 0x0065, 0x0302, 0x0301, + 0x0045, 0x0302, 0x0300, 0x0065, 0x0302, 0x0300, 0x0045, 0x0302, 0x0309, 0x0065, 0x0302, 0x0309, 0x0045, 0x0302, 0x0303, 0x0065, + // 0100 + 0x0302, 0x0303, 0x0045, 0x0323, 0x0302, 0x0065, 0x0323, 0x0302, 0x004F, 0x0302, 0x0301, 0x006F, 0x0302, 0x0301, 0x004F, 0x0302, + 0x0300, 0x006F, 0x0302, 0x0300, 0x004F, 0x0302, 0x0309, 0x006F, 0x0302, 0x0309, 0x004F, 0x0302, 0x0303, 0x006F, 0x0302, 0x0303, + 0x004F, 0x0323, 0x0302, 0x006F, 0x0323, 0x0302, 0x004F, 0x031B, 0x0301, 0x006F, 0x031B, 0x0301, 0x004F, 0x031B, 0x0300, 0x006F, + 0x031B, 0x0300, 0x004F, 0x031B, 0x0309, 0x006F, 0x031B, 0x0309, 0x004F, 0x031B, 0x0303, 0x006F, 0x031B, 0x0303, 0x004F, 0x031B, + 0x0323, 0x006F, 0x031B, 0x0323, 0x0055, 0x031B, 0x0301, 0x0075, 0x031B, 0x0301, 0x0055, 0x031B, 0x0300, 0x0075, 0x031B, 0x0300, + 0x0055, 0x031B, 0x0309, 0x0075, 0x031B, 0x0309, 0x0055, 0x031B, 0x0303, 0x0075, 0x031B, 0x0303, 0x0055, 0x031B, 0x0323, 0x0075, + 0x031B, 0x0323, 0x03B1, 0x0313, 0x0300, 0x03B1, 0x0314, 0x0300, 0x03B1, 0x0313, 0x0301, 0x03B1, 0x0314, 0x0301, 0x03B1, 0x0313, + 0x0342, 0x03B1, 0x0314, 0x0342, 0x0391, 0x0313, 0x0300, 0x0391, 0x0314, 0x0300, 0x0391, 0x0313, 0x0301, 0x0391, 0x0314, 0x0301, + // 0180 + 0x0391, 0x0313, 0x0342, 0x0391, 0x0314, 0x0342, 0x03B5, 0x0313, 0x0300, 0x03B5, 0x0314, 0x0300, 0x03B5, 0x0313, 0x0301, 0x03B5, + 0x0314, 0x0301, 0x0395, 0x0313, 0x0300, 0x0395, 0x0314, 0x0300, 0x0395, 0x0313, 0x0301, 0x0395, 0x0314, 0x0301, 0x03B7, 0x0313, + 0x0300, 0x03B7, 0x0314, 0x0300, 0x03B7, 0x0313, 0x0301, 0x03B7, 0x0314, 0x0301, 0x03B7, 0x0313, 0x0342, 0x03B7, 0x0314, 0x0342, + 0x0397, 0x0313, 0x0300, 0x0397, 0x0314, 0x0300, 0x0397, 0x0313, 0x0301, 0x0397, 0x0314, 0x0301, 0x0397, 0x0313, 0x0342, 0x0397, + 0x0314, 0x0342, 0x03B9, 0x0313, 0x0300, 0x03B9, 0x0314, 0x0300, 0x03B9, 0x0313, 0x0301, 0x03B9, 0x0314, 0x0301, 0x03B9, 0x0313, + 0x0342, 0x03B9, 0x0314, 0x0342, 0x0399, 0x0313, 0x0300, 0x0399, 0x0314, 0x0300, 0x0399, 0x0313, 0x0301, 0x0399, 0x0314, 0x0301, + 0x0399, 0x0313, 0x0342, 0x0399, 0x0314, 0x0342, 0x03BF, 0x0313, 0x0300, 0x03BF, 0x0314, 0x0300, 0x03BF, 0x0313, 0x0301, 0x03BF, + 0x0314, 0x0301, 0x039F, 0x0313, 0x0300, 0x039F, 0x0314, 0x0300, 0x039F, 0x0313, 0x0301, 0x039F, 0x0314, 0x0301, 0x03C5, 0x0313, + // 0200 + 0x0300, 0x03C5, 0x0314, 0x0300, 0x03C5, 0x0313, 0x0301, 0x03C5, 0x0314, 0x0301, 0x03C5, 0x0313, 0x0342, 0x03C5, 0x0314, 0x0342, + 0x03A5, 0x0314, 0x0300, 0x03A5, 0x0314, 0x0301, 0x03A5, 0x0314, 0x0342, 0x03C9, 0x0313, 0x0300, 0x03C9, 0x0314, 0x0300, 0x03C9, + 0x0313, 0x0301, 0x03C9, 0x0314, 0x0301, 0x03C9, 0x0313, 0x0342, 0x03C9, 0x0314, 0x0342, 0x03A9, 0x0313, 0x0300, 0x03A9, 0x0314, + 0x0300, 0x03A9, 0x0313, 0x0301, 0x03A9, 0x0314, 0x0301, 0x03A9, 0x0313, 0x0342, 0x03A9, 0x0314, 0x0342, 0x03B1, 0x0313, 0x0345, + 0x03B1, 0x0314, 0x0345, 0x0391, 0x0313, 0x0345, 0x0391, 0x0314, 0x0345, 0x03B7, 0x0313, 0x0345, 0x03B7, 0x0314, 0x0345, 0x0397, + 0x0313, 0x0345, 0x0397, 0x0314, 0x0345, 0x03C9, 0x0313, 0x0345, 0x03C9, 0x0314, 0x0345, 0x03A9, 0x0313, 0x0345, 0x03A9, 0x0314, + 0x0345, 0x03B1, 0x0300, 0x0345, 0x03B1, 0x0301, 0x0345, 0x03B1, 0x0342, 0x0345, 0x03B7, 0x0300, 0x0345, 0x03B7, 0x0301, 0x0345, + 0x03B7, 0x0342, 0x0345, 0x03B9, 0x0308, 0x0300, 0x03B9, 0x0308, 0x0301, 0x03B9, 0x0308, 0x0342, 0x03C5, 0x0308, 0x0300, 0x03C5, + // 0280 + 0x0308, 0x0301, 0x03C5, 0x0308, 0x0342, 0x03C9, 0x0300, 0x0345, 0x03C9, 0x0301, 0x0345, 0x03C9, 0x0342, 0x0345, 0x0066, 0x0066, + 0x0069, 0x0066, 0x0066, 0x006C, 0x05E9, 0x05BC, 0x05C1, 0x05E9, 0x05BC, 0x05C2, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 +}; + +static const uint16_t nf_u16_seq_misc[0xC8] = +{ + // 0000 + 0x0E61, 0x0300, 0x0E61, 0x0301, 0x0E61, 0x0313, 0x0E62, 0x0308, 0x0301, 0x0812, 0x0F71, 0x0F72, 0x0812, 0x0F71, 0x0F74, 0x0812, + 0x0F71, 0x0F80, 0x0004, 0x03B1, 0x0313, 0x0300, 0x0345, 0x0004, 0x03B1, 0x0314, 0x0300, 0x0345, 0x0004, 0x03B1, 0x0313, 0x0301, + 0x0345, 0x0004, 0x03B1, 0x0314, 0x0301, 0x0345, 0x0004, 0x03B1, 0x0313, 0x0342, 0x0345, 0x0004, 0x03B1, 0x0314, 0x0342, 0x0345, + 0x0004, 0x0391, 0x0313, 0x0300, 0x0345, 0x0004, 0x0391, 0x0314, 0x0300, 0x0345, 0x0004, 0x0391, 0x0313, 0x0301, 0x0345, 0x0004, + 0x0391, 0x0314, 0x0301, 0x0345, 0x0004, 0x0391, 0x0313, 0x0342, 0x0345, 0x0004, 0x0391, 0x0314, 0x0342, 0x0345, 0x0004, 0x03B7, + 0x0313, 0x0300, 0x0345, 0x0004, 0x03B7, 0x0314, 0x0300, 0x0345, 0x0004, 0x03B7, 0x0313, 0x0301, 0x0345, 0x0004, 0x03B7, 0x0314, + 0x0301, 0x0345, 0x0004, 0x03B7, 0x0313, 0x0342, 0x0345, 0x0004, 0x03B7, 0x0314, 0x0342, 0x0345, 0x0004, 0x0397, 0x0313, 0x0300, + 0x0345, 0x0004, 0x0397, 0x0314, 0x0300, 0x0345, 0x0004, 0x0397, 0x0313, 0x0301, 0x0345, 0x0004, 0x0397, 0x0314, 0x0301, 0x0345, + // 0080 + 0x0004, 0x0397, 0x0313, 0x0342, 0x0345, 0x0004, 0x0397, 0x0314, 0x0342, 0x0345, 0x0004, 0x03C9, 0x0313, 0x0300, 0x0345, 0x0004, + 0x03C9, 0x0314, 0x0300, 0x0345, 0x0004, 0x03C9, 0x0313, 0x0301, 0x0345, 0x0004, 0x03C9, 0x0314, 0x0301, 0x0345, 0x0004, 0x03C9, + 0x0313, 0x0342, 0x0345, 0x0004, 0x03C9, 0x0314, 0x0342, 0x0345, 0x0004, 0x03A9, 0x0313, 0x0300, 0x0345, 0x0004, 0x03A9, 0x0314, + 0x0300, 0x0345, 0x0004, 0x03A9, 0x0313, 0x0301, 0x0345, 0x0004, 0x03A9, 0x0314, 0x0301, 0x0345, 0x0004, 0x03A9, 0x0313, 0x0342, + 0x0345, 0x0004, 0x03A9, 0x0314, 0x0342, 0x0345, 0x0000, 0x0000 +}; + +static const uint32_t nf_u32_char[0x320] = +{ + // 0000 + 0x00000501, 0x00000503, 0x00000505, 0x00000507, 0x00000509, 0x0000050B, 0x0000050D, 0x0000050F, 0x00000511, 0x00000513, 0x00000515, 0x00000517, 0x00000519, 0x0000051B, 0x0000051D, 0x0000051F, + 0x00000521, 0x00000523, 0x00000525, 0x00000527, 0x00000529, 0x0000052B, 0x0000052D, 0x0000052F, 0x00000561, 0x00000562, 0x00000563, 0x00000564, 0x00000565, 0x00000566, 0x00000567, 0x00000568, + 0x00000569, 0x0000056A, 0x0000056B, 0x0000056C, 0x0000056D, 0x0000056E, 0x0000056F, 0x00000570, 0x00000571, 0x00000572, 0x00000573, 0x00000574, 0x00000575, 0x00000576, 0x00000577, 0x00000578, + 0x00000579, 0x0000057A, 0x0000057B, 0x0000057C, 0x0000057D, 0x0000057E, 0x0000057F, 0x00000580, 0x00000581, 0x00000582, 0x00000583, 0x00000584, 0x00000585, 0x00000586, 0x00002D00, 0x00002D01, + 0x00002D02, 0x00002D03, 0x00002D04, 0x00002D05, 0x00002D06, 0x00002D07, 0x00002D08, 0x00002D09, 0x00002D0A, 0x00002D0B, 0x00002D0C, 0x00002D0D, 0x00002D0E, 0x00002D0F, 0x00002D10, 0x00002D11, + 0x00002D12, 0x00002D13, 0x00002D14, 0x00002D15, 0x00002D16, 0x00002D17, 0x00002D18, 0x00002D19, 0x00002D1A, 0x00002D1B, 0x00002D1C, 0x00002D1D, 0x00002D1E, 0x00002D1F, 0x00002D20, 0x00002D21, + 0x00002D22, 0x00002D23, 0x00002D24, 0x00002D25, 0x00002D27, 0x00002D2D, 0x000013F0, 0x000013F1, 0x000013F2, 0x000013F3, 0x000013F4, 0x000013F5, 0x00000432, 0x00000434, 0x0000043E, 0x00000441, + 0x00000442, 0x00000442, 0x0000044A, 0x00000463, 0x0000A64B, 0x00001EFB, 0x00001EFD, 0x00001EFF, 0x0000214E, 0x00002170, 0x00002171, 0x00002172, 0x00002173, 0x00002174, 0x00002175, 0x00002176, + // 0080 + 0x00002177, 0x00002178, 0x00002179, 0x0000217A, 0x0000217B, 0x0000217C, 0x0000217D, 0x0000217E, 0x0000217F, 0x00002184, 0x000024D0, 0x000024D1, 0x000024D2, 0x000024D3, 0x000024D4, 0x000024D5, + 0x000024D6, 0x000024D7, 0x000024D8, 0x000024D9, 0x000024DA, 0x000024DB, 0x000024DC, 0x000024DD, 0x000024DE, 0x000024DF, 0x000024E0, 0x000024E1, 0x000024E2, 0x000024E3, 0x000024E4, 0x000024E5, + 0x000024E6, 0x000024E7, 0x000024E8, 0x000024E9, 0x00002C30, 0x00002C31, 0x00002C32, 0x00002C33, 0x00002C34, 0x00002C35, 0x00002C36, 0x00002C37, 0x00002C38, 0x00002C39, 0x00002C3A, 0x00002C3B, + 0x00002C3C, 0x00002C3D, 0x00002C3E, 0x00002C3F, 0x00002C40, 0x00002C41, 0x00002C42, 0x00002C43, 0x00002C44, 0x00002C45, 0x00002C46, 0x00002C47, 0x00002C48, 0x00002C49, 0x00002C4A, 0x00002C4B, + 0x00002C4C, 0x00002C4D, 0x00002C4E, 0x00002C4F, 0x00002C50, 0x00002C51, 0x00002C52, 0x00002C53, 0x00002C54, 0x00002C55, 0x00002C56, 0x00002C57, 0x00002C58, 0x00002C59, 0x00002C5A, 0x00002C5B, + 0x00002C5C, 0x00002C5D, 0x00002C5E, 0x00002C61, 0x0000026B, 0x00001D7D, 0x0000027D, 0x00002C68, 0x00002C6A, 0x00002C6C, 0x00000251, 0x00000271, 0x00000250, 0x00000252, 0x00002C73, 0x00002C76, + 0x0000023F, 0x00000240, 0x00002C81, 0x00002C83, 0x00002C85, 0x00002C87, 0x00002C89, 0x00002C8B, 0x00002C8D, 0x00002C8F, 0x00002C91, 0x00002C93, 0x00002C95, 0x00002C97, 0x00002C99, 0x00002C9B, + 0x00002C9D, 0x00002C9F, 0x00002CA1, 0x00002CA3, 0x00002CA5, 0x00002CA7, 0x00002CA9, 0x00002CAB, 0x00002CAD, 0x00002CAF, 0x00002CB1, 0x00002CB3, 0x00002CB5, 0x00002CB7, 0x00002CB9, 0x00002CBB, + // 0100 + 0x00002CBD, 0x00002CBF, 0x00002CC1, 0x00002CC3, 0x00002CC5, 0x00002CC7, 0x00002CC9, 0x00002CCB, 0x00002CCD, 0x00002CCF, 0x00002CD1, 0x00002CD3, 0x00002CD5, 0x00002CD7, 0x00002CD9, 0x00002CDB, + 0x00002CDD, 0x00002CDF, 0x00002CE1, 0x00002CE3, 0x00002CEC, 0x00002CEE, 0x00002CF3, 0x0000A641, 0x0000A643, 0x0000A645, 0x0000A647, 0x0000A649, 0x0000A64B, 0x0000A64D, 0x0000A64F, 0x0000A651, + 0x0000A653, 0x0000A655, 0x0000A657, 0x0000A659, 0x0000A65B, 0x0000A65D, 0x0000A65F, 0x0000A661, 0x0000A663, 0x0000A665, 0x0000A667, 0x0000A669, 0x0000A66B, 0x0000A66D, 0x0000A681, 0x0000A683, + 0x0000A685, 0x0000A687, 0x0000A689, 0x0000A68B, 0x0000A68D, 0x0000A68F, 0x0000A691, 0x0000A693, 0x0000A695, 0x0000A697, 0x0000A699, 0x0000A69B, 0x0000A723, 0x0000A725, 0x0000A727, 0x0000A729, + 0x0000A72B, 0x0000A72D, 0x0000A72F, 0x0000A733, 0x0000A735, 0x0000A737, 0x0000A739, 0x0000A73B, 0x0000A73D, 0x0000A73F, 0x0000A741, 0x0000A743, 0x0000A745, 0x0000A747, 0x0000A749, 0x0000A74B, + 0x0000A74D, 0x0000A74F, 0x0000A751, 0x0000A753, 0x0000A755, 0x0000A757, 0x0000A759, 0x0000A75B, 0x0000A75D, 0x0000A75F, 0x0000A761, 0x0000A763, 0x0000A765, 0x0000A767, 0x0000A769, 0x0000A76B, + 0x0000A76D, 0x0000A76F, 0x0000A77A, 0x0000A77C, 0x00001D79, 0x0000A77F, 0x0000A781, 0x0000A783, 0x0000A785, 0x0000A787, 0x0000A78C, 0x00000265, 0x0000A791, 0x0000A793, 0x0000A797, 0x0000A799, + 0x0000A79B, 0x0000A79D, 0x0000A79F, 0x0000A7A1, 0x0000A7A3, 0x0000A7A5, 0x0000A7A7, 0x0000A7A9, 0x00000266, 0x0000025C, 0x00000261, 0x0000026C, 0x0000026A, 0x0000029E, 0x00000287, 0x0000029D, + // 0180 + 0x0000AB53, 0x0000A7B5, 0x0000A7B7, 0x000013A0, 0x000013A1, 0x000013A2, 0x000013A3, 0x000013A4, 0x000013A5, 0x000013A6, 0x000013A7, 0x000013A8, 0x000013A9, 0x000013AA, 0x000013AB, 0x000013AC, + 0x000013AD, 0x000013AE, 0x000013AF, 0x000013B0, 0x000013B1, 0x000013B2, 0x000013B3, 0x000013B4, 0x000013B5, 0x000013B6, 0x000013B7, 0x000013B8, 0x000013B9, 0x000013BA, 0x000013BB, 0x000013BC, + 0x000013BD, 0x000013BE, 0x000013BF, 0x000013C0, 0x000013C1, 0x000013C2, 0x000013C3, 0x000013C4, 0x000013C5, 0x000013C6, 0x000013C7, 0x000013C8, 0x000013C9, 0x000013CA, 0x000013CB, 0x000013CC, + 0x000013CD, 0x000013CE, 0x000013CF, 0x000013D0, 0x000013D1, 0x000013D2, 0x000013D3, 0x000013D4, 0x000013D5, 0x000013D6, 0x000013D7, 0x000013D8, 0x000013D9, 0x000013DA, 0x000013DB, 0x000013DC, + 0x000013DD, 0x000013DE, 0x000013DF, 0x000013E0, 0x000013E1, 0x000013E2, 0x000013E3, 0x000013E4, 0x000013E5, 0x000013E6, 0x000013E7, 0x000013E8, 0x000013E9, 0x000013EA, 0x000013EB, 0x000013EC, + 0x000013ED, 0x000013EE, 0x000013EF, 0x000242EE, 0x0002284A, 0x00022844, 0x000233D5, 0x00025249, 0x00025CD0, 0x00027ED3, 0x0000FF41, 0x0000FF42, 0x0000FF43, 0x0000FF44, 0x0000FF45, 0x0000FF46, + 0x0000FF47, 0x0000FF48, 0x0000FF49, 0x0000FF4A, 0x0000FF4B, 0x0000FF4C, 0x0000FF4D, 0x0000FF4E, 0x0000FF4F, 0x0000FF50, 0x0000FF51, 0x0000FF52, 0x0000FF53, 0x0000FF54, 0x0000FF55, 0x0000FF56, + 0x0000FF57, 0x0000FF58, 0x0000FF59, 0x0000FF5A, 0x00010428, 0x00010429, 0x0001042A, 0x0001042B, 0x0001042C, 0x0001042D, 0x0001042E, 0x0001042F, 0x00010430, 0x00010431, 0x00010432, 0x00010433, + // 0200 + 0x00010434, 0x00010435, 0x00010436, 0x00010437, 0x00010438, 0x00010439, 0x0001043A, 0x0001043B, 0x0001043C, 0x0001043D, 0x0001043E, 0x0001043F, 0x00010440, 0x00010441, 0x00010442, 0x00010443, + 0x00010444, 0x00010445, 0x00010446, 0x00010447, 0x00010448, 0x00010449, 0x0001044A, 0x0001044B, 0x0001044C, 0x0001044D, 0x0001044E, 0x0001044F, 0x000104D8, 0x000104D9, 0x000104DA, 0x000104DB, + 0x000104DC, 0x000104DD, 0x000104DE, 0x000104DF, 0x000104E0, 0x000104E1, 0x000104E2, 0x000104E3, 0x000104E4, 0x000104E5, 0x000104E6, 0x000104E7, 0x000104E8, 0x000104E9, 0x000104EA, 0x000104EB, + 0x000104EC, 0x000104ED, 0x000104EE, 0x000104EF, 0x000104F0, 0x000104F1, 0x000104F2, 0x000104F3, 0x000104F4, 0x000104F5, 0x000104F6, 0x000104F7, 0x000104F8, 0x000104F9, 0x000104FA, 0x000104FB, + 0x00010CC0, 0x00010CC1, 0x00010CC2, 0x00010CC3, 0x00010CC4, 0x00010CC5, 0x00010CC6, 0x00010CC7, 0x00010CC8, 0x00010CC9, 0x00010CCA, 0x00010CCB, 0x00010CCC, 0x00010CCD, 0x00010CCE, 0x00010CCF, + 0x00010CD0, 0x00010CD1, 0x00010CD2, 0x00010CD3, 0x00010CD4, 0x00010CD5, 0x00010CD6, 0x00010CD7, 0x00010CD8, 0x00010CD9, 0x00010CDA, 0x00010CDB, 0x00010CDC, 0x00010CDD, 0x00010CDE, 0x00010CDF, + 0x00010CE0, 0x00010CE1, 0x00010CE2, 0x00010CE3, 0x00010CE4, 0x00010CE5, 0x00010CE6, 0x00010CE7, 0x00010CE8, 0x00010CE9, 0x00010CEA, 0x00010CEB, 0x00010CEC, 0x00010CED, 0x00010CEE, 0x00010CEF, + 0x00010CF0, 0x00010CF1, 0x00010CF2, 0x000118C0, 0x000118C1, 0x000118C2, 0x000118C3, 0x000118C4, 0x000118C5, 0x000118C6, 0x000118C7, 0x000118C8, 0x000118C9, 0x000118CA, 0x000118CB, 0x000118CC, + // 0280 + 0x000118CD, 0x000118CE, 0x000118CF, 0x000118D0, 0x000118D1, 0x000118D2, 0x000118D3, 0x000118D4, 0x000118D5, 0x000118D6, 0x000118D7, 0x000118D8, 0x000118D9, 0x000118DA, 0x000118DB, 0x000118DC, + 0x000118DD, 0x000118DE, 0x000118DF, 0x0001E922, 0x0001E923, 0x0001E924, 0x0001E925, 0x0001E926, 0x0001E927, 0x0001E928, 0x0001E929, 0x0001E92A, 0x0001E92B, 0x0001E92C, 0x0001E92D, 0x0001E92E, + 0x0001E92F, 0x0001E930, 0x0001E931, 0x0001E932, 0x0001E933, 0x0001E934, 0x0001E935, 0x0001E936, 0x0001E937, 0x0001E938, 0x0001E939, 0x0001E93A, 0x0001E93B, 0x0001E93C, 0x0001E93D, 0x0001E93E, + 0x0001E93F, 0x0001E940, 0x0001E941, 0x0001E942, 0x0001E943, 0x00020122, 0x0002063A, 0x0002051C, 0x0002054B, 0x000291DF, 0x00020A2C, 0x00020B63, 0x000214E4, 0x000216A8, 0x000216EA, 0x000219C8, + 0x00021B18, 0x00021DE4, 0x00021DE6, 0x00022183, 0x0002A392, 0x00022331, 0x00022331, 0x000232B8, 0x000261DA, 0x000226D4, 0x00022B0C, 0x00022BF1, 0x0002300A, 0x000233C3, 0x0002346D, 0x000236A3, + 0x000238A7, 0x00023A8D, 0x00021D0B, 0x00023AFA, 0x00023CBC, 0x00023D1E, 0x00023ED1, 0x00023F5E, 0x00023F8E, 0x00020525, 0x00024263, 0x000243AB, 0x00024608, 0x00024735, 0x00024814, 0x00024C36, + 0x00024C92, 0x0002219F, 0x00024FA1, 0x00024FB8, 0x00025044, 0x000250F3, 0x000250F2, 0x00025119, 0x00025133, 0x0002541D, 0x00025626, 0x0002569A, 0x000256C5, 0x0002597C, 0x00025AA7, 0x00025AA7, + 0x00025BAB, 0x00025C80, 0x00025F86, 0x00026228, 0x00026247, 0x000262D9, 0x0002633E, 0x000264DA, 0x00026523, 0x000265A8, 0x0002335F, 0x000267A7, 0x000267B5, 0x00023393, 0x0002339C, 0x00026B3C, + // 0300 + 0x00026C36, 0x00026D6B, 0x00026CD5, 0x000273CA, 0x00026F2C, 0x00026FB1, 0x000270D2, 0x00027667, 0x000278AE, 0x00027966, 0x00027CA8, 0x00027F2F, 0x00020804, 0x000208DE, 0x000285D2, 0x000285ED, + 0x0002872E, 0x00028BFA, 0x00028D77, 0x00029145, 0x0002921A, 0x0002940A, 0x00029496, 0x000295B6, 0x00029B30, 0x0002A0CE, 0x0002A105, 0x0002A20E, 0x0002A291, 0x0002A600, 0x00000000, 0x00000000 +}; + +static const uint32_t nf_u32_seq_misc[0x54] = +{ + // 0000 + 0x00000002, 0x00011099, 0x000110BA, 0x00000002, 0x0001109B, 0x000110BA, 0x00000002, 0x000110A5, 0x000110BA, 0x00000002, 0x00011131, 0x00011127, 0x00000002, 0x00011132, 0x00011127, 0x00000002, + 0x00011347, 0x0001133E, 0x00000002, 0x00011347, 0x00011357, 0x00000002, 0x000114B9, 0x000114BA, 0x00000002, 0x000114B9, 0x000114B0, 0x00000002, 0x000114B9, 0x000114BD, 0x00000002, 0x000115B8, + 0x000115AF, 0x00000002, 0x000115B9, 0x000115AF, 0x00000002, 0x0001D157, 0x0001D165, 0x00000002, 0x0001D158, 0x0001D165, 0x00000003, 0x0001D158, 0x0001D165, 0x0001D16E, 0x00000003, 0x0001D158, + 0x0001D165, 0x0001D16F, 0x00000003, 0x0001D158, 0x0001D165, 0x0001D170, 0x00000003, 0x0001D158, 0x0001D165, 0x0001D171, 0x00000003, 0x0001D158, 0x0001D165, 0x0001D172, 0x00000002, 0x0001D1B9, + 0x0001D165, 0x00000002, 0x0001D1BA, 0x0001D165, 0x00000003, 0x0001D1B9, 0x0001D165, 0x0001D16E, 0x00000003, 0x0001D1BA, 0x0001D165, 0x0001D16E, 0x00000003, 0x0001D1B9, 0x0001D165, 0x0001D16F, + 0x00000003, 0x0001D1BA, 0x0001D165, 0x0001D16F +}; + diff --git a/vendor/apfs-fuse/ApfsLib/Util.cpp b/vendor/apfs-fuse/ApfsLib/Util.cpp new file mode 100644 index 0000000..f05de6a --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/Util.cpp @@ -0,0 +1,792 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#include "Unicode.h" + +#include + +#include +#include +#include +#include +#if defined(__linux__) || defined(__APPLE__) +#include +#endif +#include + +#include "Util.h" +#include "Crc32.h" + +#include +#include + +extern "C" { +#include +#include +} + +static Crc32 g_crc(true, 0x1EDC6F41); + +uint64_t Fletcher64(const uint32_t *data, size_t cnt, uint64_t init) +{ + size_t k; + + uint64_t sum1 = init & 0xFFFFFFFFU; + uint64_t sum2 = (init >> 32); + + for (k = 0; k < cnt; k++) + { + sum1 = (sum1 + data[k]); + sum2 = (sum2 + sum1); + } + + sum1 = sum1 % 0xFFFFFFFF; + sum2 = sum2 % 0xFFFFFFFF; + + return (static_cast(sum2) << 32) | static_cast(sum1); +} + +bool VerifyBlock(const void *block, size_t size) +{ + uint64_t cs; + const uint32_t * const data = reinterpret_cast(block); + + size /= sizeof(uint32_t); + + cs = *reinterpret_cast(block); + if (cs == 0) + return false; + if (cs == 0xFFFFFFFFFFFFFFFFULL) + return false; + + cs = Fletcher64(data + 2, size - 2, 0); + cs = Fletcher64(data, 2, cs); + + return cs == 0; +} + +bool IsZero(const uint8_t* data, size_t size) +{ + for (size_t k = 0; k < size; k++) + { + if (data[k] != 0) + return false; + } + return true; +} + +bool IsEmptyBlock(const void *data, size_t blksize) +{ + blksize /= sizeof(uint64_t); + const uint64_t *qdata = reinterpret_cast(data); + + for (size_t k = 0; k < blksize; k++) + { + if (qdata[k] != 0) + return false; + } + return true; +} + +void DumpHex(std::ostream &os, const uint8_t *data, size_t size, size_t lineSize) +{ + using namespace std; + + size_t i, j; + uint8_t b; + + if (size == 0) + return; + + os << hex << uppercase << setfill('0'); + + for (i = 0; i < size; i += lineSize) + { + os << setw(4) << i << ": "; + for (j = 0; (j < lineSize) && ((i + j) < size); j++) + os << setw(2) << static_cast(data[i + j]) << ' '; + for (; j < lineSize; j++) + os << " "; + + os << "- "; + + for (j = 0; (j < lineSize) && ((i + j) < size); j++) + { + b = data[i + j]; + if (b >= 0x20 && b < 0x7F) + os << b; + else + os << '.'; + } + + os << endl; + } + + // os << dec; +} + +// Like DumpHex, but prints a label. +void DumpBuffer(const uint8_t *data, size_t len, const char *label) +{ + std::cout << "dumping " << label << std::endl; + DumpHex(std::cout, data, len); +} + +std::string uuidstr(const apfs_uuid_t &uuid) +{ + using namespace std; + + stringstream st; + int k; + + st << hex << uppercase << setfill('0'); + for (k = 0; k < 4; k++) + st << setw(2) << static_cast(uuid[k]); + st << '-'; + for (k = 4; k < 6; k++) + st << setw(2) << static_cast(uuid[k]); + st << '-'; + for (k = 6; k < 8; k++) + st << setw(2) << static_cast(uuid[k]); + st << '-'; + for (k = 8; k < 10; k++) + st << setw(2) << static_cast(uuid[k]); + st << '-'; + for (k = 10; k < 16; k++) + st << setw(2) << static_cast(uuid[k]); + + return st.str(); +} + +std::string hexstr(const uint8_t *data, size_t size) +{ + using namespace std; + + std::ostringstream st; + + size_t k; + + st << hex << uppercase << setfill('0'); + + for (k = 0; k < size; k++) + st << setw(2) << static_cast(data[k]); + + return st.str(); +} + +void dump_utf8(std::ostream &st, const uint8_t* str) +{ + size_t ptr = 0; + uint8_t ch; + char32_t uch; + int cnt; + + st << std::hex << std::uppercase; + + while (str[ptr] != 0) + { + ch = str[ptr++]; + + if (ch < 0x80) { + uch = ch; + cnt = 0; + } else if (ch < 0xC0) { + break; + } else if (ch < 0xE0) { + uch = ch & 0x1F; + cnt = 1; + } else if (ch < 0xF0) { + uch = ch & 0x0F; + cnt = 2; + } else if (ch < 0xF8) { + uch = ch & 0x07; + cnt = 3; + } else + break; + + for (; cnt > 0; --cnt) + { + ch = str[ptr++]; + if ((ch & 0xC0) == 0x80) + uch = (uch << 6) | (ch & 0x3F); + else + break; + } + + st << uch << ' '; + } + + st << str << std::endl; +} + +void dump_utf32(std::ostream &st, const char32_t *str, size_t size) +{ + size_t k; + + st << std::hex << std::uppercase; + + for (k = 0; k < size; k++) + st << str[k] << ' '; + for (k = 0; k < size; k++) + if (str[k] >= 0x20 && str[k] < 0x7F) + st << static_cast(str[k]); + else + st << '.'; + st << std::endl; +} + +uint32_t HashFilename(const uint8_t* utf8str, uint16_t name_len, bool case_fold) +{ + std::vector utf32; + std::vector utf32_nfd; + uint32_t hash; + + Utf8toUtf32(utf32, utf8str); + + NormalizeFoldString(utf32_nfd, utf32, case_fold); + +#if 0 + if (g_debug & Dbg_Dir) + { + dump_utf32(utf32.data(), utf32.size()); + dump_utf32(utf32_nfd.data(), utf32_nfd.size()); + } +#endif + + g_crc.SetCRC(0xFFFFFFFF); + g_crc.Calc(reinterpret_cast(utf32_nfd.data()), utf32_nfd.size() * sizeof(char32_t)); + + hash = g_crc.GetCRC(); + + hash = ((hash & 0x3FFFFF) << 10) | (name_len & 0x3FF); + + return hash; +} + +int apfs_strncmp(const uint8_t *s1, size_t s1_len, const uint8_t *s2, size_t s2_len) +{ + while (s1_len > 0 && s2_len > 0) { + if (*s1 < *s2) return -1; + if (*s1 > *s2) return 1; + if (*s1 == 0) return 0; + ++s1; + ++s2; + --s1_len; + --s2_len; + } + if (s2_len) return -1; + if (s1_len) return 1; + return 0; +} + +int StrCmpUtf8NormalizedFolded(const uint8_t* s1, const uint8_t* s2, bool case_fold) +{ + std::vector s1_u32; + std::vector s2_u32; + std::vector s1_u32_nfd; + std::vector s2_u32_nfd; + size_t k; + + Utf8toUtf32(s1_u32, s1); + Utf8toUtf32(s2_u32, s2); + + NormalizeFoldString(s1_u32_nfd, s1_u32, case_fold); + NormalizeFoldString(s2_u32_nfd, s2_u32, case_fold); + + s1_u32_nfd.push_back(0); + s2_u32_nfd.push_back(0); + + k = 0; + while (true) + { + if (s1_u32_nfd[k] < s2_u32_nfd[k]) + return -1; + if (s1_u32_nfd[k] > s2_u32_nfd[k]) + return 1; + if (s1_u32_nfd[k] == 0 || s2_u32_nfd[k] == 0) + break; + ++k; + } + + return 0; +} + +bool Utf8toUtf32(std::vector &str32, const uint8_t* str) +{ + size_t ip = 0; + int cnt = 0; + + char32_t ch; + uint8_t c; + + bool ok = true; + + while (str[ip] != 0) + { + c = str[ip++]; + + if (c < 0x80) + { + ch = c; + cnt = 0; + } + else if (c < 0xC0) + { + ok = false; + break; + } + else if (c < 0xE0) + { + ch = c & 0x1F; + cnt = 1; + } + else if (c < 0xF0) + { + ch = c & 0x0F; + cnt = 2; + } + else if (c < 0xF8) + { + ch = c & 0x07; + cnt = 3; + } + else + { + ok = false; + break; + } + + for (; cnt > 0; --cnt) + { + c = str[ip++]; + if ((c & 0xC0) == 0x80) + ch = (ch << 6) | (c & 0x3F); + else + { + ok = false; + break; + } + } + + if (!ok) + break; + + str32.push_back(ch); + + if (ch == 0) + break; + } + + return ok; +} + +bool GetPassword(std::string &pw) +{ +#if defined(__linux__) || defined(__APPLE__) + struct termios told, tnew; + FILE *stream = stdin; + + /* Turn echoing off and fail if we can’t. */ + if (tcgetattr (fileno (stream), &told) != 0) + return false; + tnew = told; + tnew.c_lflag &= ~ECHO; + if (tcsetattr (fileno (stream), TCSAFLUSH, &tnew) != 0) + return false; + + /* Read the password. */ + std::getline(std::cin, pw); + + /* Restore terminal. */ + (void) tcsetattr (fileno (stream), TCSAFLUSH, &told); + + std::cout << std::endl; + + return true; +#else + std::getline(std::cin, pw); + std::cout << std::endl; + + return true; +#endif +} + +size_t DecompressZLib(uint8_t *dst, size_t dst_size, const uint8_t *src, size_t src_size) +{ + // size_t nwr = 0; + int ret; + + z_stream strm; + + memset(&strm, 0, sizeof(strm)); + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.avail_in = src_size; + strm.avail_out = dst_size; + strm.next_in = const_cast(src); + strm.next_out = dst; + + ret = inflateInit2(&strm, 15); + if (ret != Z_OK) + { + std::cerr << "DecompressZLib: inflateInit failed." << std::endl; + return 0; + } + + do { + ret = inflate(&strm, Z_NO_FLUSH); + if (ret == Z_NEED_DICT || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR) + { + strm.avail_out = 0; + break; + } + } while (ret != Z_STREAM_END); + + inflateEnd(&strm); + + return dst_size - strm.avail_out; +} + +size_t DecompressADC(uint8_t * dst, size_t dst_size, const uint8_t * src, size_t src_size) +{ + size_t in_idx = 0; + size_t out_idx = 0; + uint8_t ctl; + int len; + int dist; + int cnt; + + for (;;) + { + if (in_idx >= src_size) + break; + if (out_idx >= dst_size) + break; + + ctl = src[in_idx++]; + + if (ctl & 0x80) + { + len = (ctl & 0x7F) + 1; + + for (cnt = 0; cnt < len; cnt++) + dst[out_idx++] = src[in_idx++]; + } + else + { + if (ctl & 0x40) + { + len = ctl - 0x3C; + dist = src[in_idx++]; + dist = (dist << 8) | src[in_idx++]; + dist += 1; + } + else + { + len = ((ctl >> 2) & 0xF) + 3; + dist = (((ctl & 3) << 8) | src[in_idx++]) + 1; + } + + for (cnt = 0; cnt < len; cnt++) + { + dst[out_idx] = dst[out_idx - dist]; + out_idx++; + } + } + } + + assert(in_idx == src_size); + assert(out_idx == dst_size); + + return out_idx; +} + +size_t DecompressLZVN(uint8_t * dst, size_t dst_size, const uint8_t * src, size_t src_size) +{ + lzvn_decoder_state state; + + memset(&state, 0, sizeof(state)); + + state.src = src; + state.src_end = src + src_size; + state.dst = dst; + state.dst_begin = dst; + state.dst_end = dst + dst_size; + state.dst_current = dst; + + lzvn_decode(&state); + + return static_cast(state.dst - dst); +} + +size_t DecompressBZ2(uint8_t * dst, size_t dst_size, const uint8_t * src, size_t src_size) +{ + bz_stream strm; + + memset(&strm, 0, sizeof(strm)); + + BZ2_bzDecompressInit(&strm, 0, 0); + + strm.next_in = const_cast(reinterpret_cast(src)); + strm.avail_in = src_size; + strm.next_out = reinterpret_cast(dst); + strm.avail_out = dst_size; + + BZ2_bzDecompress(&strm); + + BZ2_bzDecompressEnd(&strm); + + return strm.total_out_lo32; +} + +size_t DecompressLZFSE(uint8_t * dst, size_t dst_size, const uint8_t * src, size_t src_size) +{ + return lzfse_decode_buffer(dst, dst_size, src, src_size, nullptr); +} + +size_t DecompressLZBITMAP(uint8_t* dst, size_t dst_size, const uint8_t* src, size_t src_size) +{ + uint8_t scratch[0x2000]; + uint8_t flags; + uint32_t compressed_size; + uint32_t uncompressed_size; + uint32_t distances_offset; + uint32_t bitmap_offset; + uint32_t token_offset; + uint8_t token_map_bmp[16] = {}; + uint8_t token_map_tkn[16] = {}; + const uint8_t* inp = src; + const uint8_t* in_end = src + src_size; + uint8_t* outp = dst; + const uint8_t* out_end = dst + dst_size; + const uint8_t* block_end; + const uint8_t* bitmaps; + const uint8_t* literals; + const uint8_t* distances; + unsigned n; + unsigned m; + unsigned o; + const uint8_t* p; + uint8_t tkn_split = 3; // TODO is dependent on flag ... + uint8_t t; + uint32_t rlecnt; + uint16_t bitbuf; + int nbits; + uint32_t ntoken; + uint32_t tmp_size; + uint8_t bmp; + uint8_t tkn; + uint32_t dist; + uint32_t bp; + uint32_t dp; + uint32_t lp; + + if (inp[0] != 'Z' || inp[1] != 'B' || inp[2] != 'M' || (inp[3] & 0xF0) != 0) + return 0; + + flags = inp[3]; + + assert(flags == 9); + if (flags != 9) { + log_error("LZBITMAP: Flags != 0x09\n"); + return 0; + } + + inp += 4; + + while (true) { + compressed_size = inp[0] | inp[1] << 8 | inp[2] << 16; + uncompressed_size = inp[3] | inp[4] << 8 | inp[5] << 16; + + if (compressed_size == 6) { + if (uncompressed_size != 0) + log_error("LZBITMAP: Compressed size = 6, uncompressed size != 0???\n"); + break; + } + + if (compressed_size == uncompressed_size + 6) { + if (outp + uncompressed_size > out_end || inp + compressed_size > in_end) { + log_error("LZBITMAP: Buffer overrun.\n"); + break; + } + memcpy(outp, inp + 6, uncompressed_size); + outp += uncompressed_size; + inp += compressed_size; + continue; + } + + distances_offset = inp[6] | inp[7] << 8 | inp[8] << 16; + bitmap_offset = inp[9] | inp[10] << 8 | inp[11] << 16; + token_offset = inp[12] | inp[13] << 8 | inp[14] << 16; + + literals = inp + 15; + distances = inp + distances_offset; + bitmaps = inp + bitmap_offset; + + p = inp + compressed_size - 0x11; + + for (n = 0; n < tkn_split; n++) { + token_map_bmp[n] = 0; + token_map_tkn[n] = n; + } + + nbits = 0; + bitbuf = 0; + for (; n < 15; n++) { + while (nbits < 10) { + bitbuf |= *p << nbits; + nbits += 8; + p++; + } + token_map_bmp[n] = bitbuf & 0xFF; + token_map_tkn[n] = bitbuf >> 8 & 3; + bitbuf >>= 10; + nbits -= 10; + } + + ntoken = (uncompressed_size + 7) >> 3; + + tmp_size = compressed_size - token_offset - 0x11; + + for (n = 0; n < tmp_size; n++) { + scratch[2 * n + 0x1000] = inp[token_offset + n] & 0xF; + scratch[2 * n + 0x1001] = inp[token_offset + n] >> 4; + } + + tmp_size *= 2; + p = scratch + 0x1000; + + o = 0; + for (n = 0; n < tmp_size; n++) { + t = p[n]; + if (t == 0xF) { + t = p[n - 1]; + rlecnt = 3; + do { + n++; + rlecnt += p[n]; + } while (p[n] == 0xF); + + for (m = 0; m < rlecnt; m++) + scratch[o++] = t; + } + else { + scratch[o++] = t; + } + } + + if (o != ntoken) + log_debug("RLE size = %X, ntoken = %X\n", o, ntoken); + + o = 0; + m = 0; + + dist = 8; + dp = 0; + bp = 0; + lp = 0; + block_end = outp + uncompressed_size; + assert(block_end <= out_end); + + if (block_end > out_end) { + log_error("LZBITMAP: Output buffer too small.\n"); + block_end = out_end; + } + + for (n = 0; n < ntoken; n++) { + t = scratch[n]; + if (t < tkn_split) + bmp = bitmaps[bp++]; + else + bmp = token_map_bmp[t]; + tkn = token_map_tkn[t]; + + if (tkn == 1) { + dist = distances[dp++]; + } + else if (tkn == 2) { + dist = distances[dp] | distances[dp + 1] << 8; + dp += 2; + } + + for (m = 0; m < 8 && outp < block_end; m++) { + if (bmp & 1) + *outp = literals[lp++]; + else + *outp = *(outp - dist); + bmp >>= 1; + outp++; + } + } + + if (lp != distances_offset - 15) + log_error("lp = %06X (%06X)\n", lp, distances_offset - 15); + if (dp != bitmap_offset - distances_offset) + log_error("dp = %06X (%06X)\n", dp, bitmap_offset - distances_offset); + if (bp != token_offset - bitmap_offset) + log_error("bp = %06X (%06X)\n", bp, token_offset - bitmap_offset); + + inp += compressed_size; + } + + return outp - dst; +} + +int log2(uint32_t val) +{ + int r = 0; + if (val & 0xFFFF0000) { val >>= 16; r += 16; } + if (val & 0xFF00) { val >>= 8; r += 8; } + if (val & 0xF0) { val >>= 4; r += 4; } + if (val & 0xC) { val >>= 2; r += 2; } + if (val & 0x2) { val >>= 1; r += 1; } + return r; +} + +FILE *g_log = stderr; + +void log_debug(const char *msg, ...) +{ + if (g_debug & Dbg_Cmpfs) { + va_list va; + va_start(va, msg); + vfprintf(g_log, msg, va); + va_end(va); + } +} + +void log_warn(const char *msg, ...) +{ + if (g_debug & Dbg_Info) { + va_list va; + va_start(va, msg); + vfprintf(g_log, msg, va); + va_end(va); + } +} + +void log_error(const char *msg, ...) +{ + if (g_debug & Dbg_Errors) { + va_list va; + va_start(va, msg); + vfprintf(g_log, msg, va); + va_end(va); + } +} diff --git a/vendor/apfs-fuse/ApfsLib/Util.h b/vendor/apfs-fuse/ApfsLib/Util.h new file mode 100644 index 0000000..626ca38 --- /dev/null +++ b/vendor/apfs-fuse/ApfsLib/Util.h @@ -0,0 +1,62 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#pragma once + +#include +#include +#include +#include +#include + +#include "ApfsTypes.h" + +uint64_t Fletcher64(const uint32_t *data, size_t cnt, uint64_t init); +bool VerifyBlock(const void *block, size_t size); +bool IsZero(const uint8_t *data, size_t size); +bool IsEmptyBlock(const void *data, size_t blksize); +void DumpHex(std::ostream &os, const uint8_t *data, size_t size, size_t line_size = 16); +void DumpBuffer(const uint8_t *data, size_t len, const char *label); + +std::string uuidstr(const apfs_uuid_t &uuid); +std::string hexstr(const uint8_t *data, size_t size); +void dump_utf8(std::ostream &st, const uint8_t *str); +void dump_utf32(std::ostream &st, const char32_t *str, size_t size); + +uint32_t HashFilename(const uint8_t *utf8str, uint16_t name_len, bool case_fold); + +int apfs_strncmp(const uint8_t *s1, size_t s1_len, const uint8_t *s2, size_t s2_len); +int StrCmpUtf8NormalizedFolded(const uint8_t *s1, const uint8_t *s2, bool case_fold); + +bool Utf8toUtf32(std::vector &str32, const uint8_t * str); + +size_t DecompressZLib(uint8_t *dst, size_t dst_size, const uint8_t *src, size_t src_size); +size_t DecompressADC(uint8_t *dst, size_t dst_size, const uint8_t *src, size_t src_size); +size_t DecompressLZVN(uint8_t *dst, size_t dst_size, const uint8_t *src, size_t src_size); +size_t DecompressBZ2(uint8_t *dst, size_t dst_size, const uint8_t *src, size_t src_size); +size_t DecompressLZFSE(uint8_t *dst, size_t dst_size, const uint8_t *src, size_t src_size); +size_t DecompressLZBITMAP(uint8_t *dst, size_t dst_size, const uint8_t *src, size_t src_size); + +bool GetPassword(std::string &pw); + +int log2(uint32_t val); + +void log_debug(const char *msg, ...) __attribute__((format(printf, 1, 2))); +void log_warn(const char *msg, ...) __attribute__((format(printf, 1, 2))); +void log_error(const char *msg, ...) __attribute__((format(printf, 1, 2))); diff --git a/vendor/apfs-fuse/ApfsUtil/ApfsUtil.cpp b/vendor/apfs-fuse/ApfsUtil/ApfsUtil.cpp new file mode 100644 index 0000000..779f265 --- /dev/null +++ b/vendor/apfs-fuse/ApfsUtil/ApfsUtil.cpp @@ -0,0 +1,156 @@ +#include +#include +#include + +#include +#include + +static void print_apfs_uuid(const apfs_uuid_t &uuid) +{ + printf("%02X%02X%02X%02X-", uuid[0], uuid[1], uuid[2], uuid[3]); + printf("%02X%02X-", uuid[4], uuid[5]); + printf("%02X%02X-", uuid[6], uuid[7]); + printf("%02X%02X-", uuid[8], uuid[9]); + printf("%02X%02X%02X%02X%02X%02X", uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]); +} + +static void print_role(uint16_t role) +{ + static const char *rolestr[] = { + "System", "User", "Recovery", "VM", "Preboot", "Installer" + }; + static const char *rolestr_enum[] = { + "", "Data", "Baseband", "Update", + "Xart", "Hardware", "Backup", "Reserved-7", + "Reserved-8", "Enterprise", "Reserved-10", "Prelogin" + }; + bool first = true; + int k; + + if (role == 0) + printf("No specific role"); + else if (role <= 0x20) { + for (k = 0; k < 6; k++) { + if (role & (1 << k)) { + if (!first) + printf(", "); + printf("%s", rolestr[k]); + first = false; + } + } + } else if (role <= APFS_VOL_ROLE_PRELOGIN) { + printf("%s", rolestr_enum[role >> APFS_VOLUME_ENUM_SHIFT]); + } + // printf(" (%d)", role); +} + +static void print_filevault(uint64_t flags) +{ + if (flags == 1) + printf("No"); + else + printf("Yes"); +} + +int main(int argc, char *argv[]) +{ + const char *devname = nullptr; + Device *device = nullptr; + ApfsContainer *container = nullptr; + uint64_t offset; + uint64_t size; + apfs_superblock_t apsb; + + g_debug = 0; + + if (argc < 2) + { + printf("Syntax: %s [device]\n", argv[0]); + return EINVAL; + } + + devname = argv[1]; + + device = Device::OpenDevice(devname); + + if (device) { + offset = 0; + size = device->GetSize(); + + GptPartitionMap gpt; + if (gpt.LoadAndVerify(*device)) { + printf("Found partitions:\n"); + gpt.ListEntries(); + + int partnum = gpt.FindFirstAPFSPartition(); + if (partnum >= 0) { + printf("First APFS partition is %d\n", partnum); + gpt.GetPartitionOffsetAndSize(partnum, offset, size); + } + printf("\n"); + } + + container = new ApfsContainer(device, offset, size); + + if (container->Init()) { + // printf("Listing volumes:\n"); + for (int k = 0; k < NX_MAX_FILE_SYSTEMS; k++) { + if (!container->GetVolumeInfo(k, apsb)) + continue; + printf("Volume %d ", k); + print_apfs_uuid(apsb.apfs_vol_uuid); + printf("\n"); + printf("---------------------------------------------\n"); + + printf("Role: "); + print_role(apsb.apfs_role); + printf("\n"); + printf("Name: %s", apsb.apfs_volname); + if (apsb.apfs_incompatible_features & APFS_INCOMPAT_CASE_INSENSITIVE) + printf(" (Case-insensitive)\n"); + else if (apsb.apfs_incompatible_features & APFS_INCOMPAT_NORMALIZATION_INSENSITIVE) + printf(" (Case-sensitive)\n"); + else + printf("\n"); + printf("Capacity Consumed: %" PRIu64 " Bytes\n", apsb.apfs_fs_alloc_count * container->GetBlocksize()); + printf("FileVault: "); + print_filevault(apsb.apfs_fs_flags & APFS_FS_CRYPTOFLAGS); + printf("\n"); + if (apsb.apfs_snap_meta_tree_oid) { + printf("Snapshots:\n"); + BTree snap_tree(*container); + BTreeIterator it; + BTreeEntry bte; + const j_snap_metadata_key_t *sme_k; + const j_snap_metadata_val_t *sme_v; + + snap_tree.Init(apsb.apfs_snap_meta_tree_oid, apsb.apfs_o.o_xid); + if (snap_tree.GetIteratorBegin(it)) { + for (;;) { + if (!it.GetEntry(bte)) break; + sme_k = reinterpret_cast(bte.key); + sme_v = reinterpret_cast(bte.val); + if ((sme_k->hdr.obj_id_and_type >> OBJ_TYPE_SHIFT) != APFS_TYPE_SNAP_METADATA) break; + printf(" %" PRIu64 " : '%s'\n", sme_k->hdr.obj_id_and_type & OBJ_ID_MASK, sme_v->name); + if (!it.next()) break; + } + } + } + printf("\n"); + } + } else { + printf("Unable to open APFS container\n"); + } + + delete container; + + device->Close(); + delete device; + } else { + printf("Error opening device.\n"); + return EIO; + } + + + return 0; +} diff --git a/vendor/apfs-fuse/CMakeLists.txt b/vendor/apfs-fuse/CMakeLists.txt new file mode 100644 index 0000000..03acf26 --- /dev/null +++ b/vendor/apfs-fuse/CMakeLists.txt @@ -0,0 +1,142 @@ +cmake_minimum_required(VERSION 3.0) +# set(CMAKE_C_COMPILER "clang") +# set(CMAKE_CXX_COMPILER "clang++") + +project(Apfs) + +option(USE_FUSE3 "Use the FUSE 3 library (required on 32-bit systems)" ON) + +set(CMAKE_C_STANDARD 99) +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_BUILD_TYPE Release) + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") + +# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -march=native -fsanitize=undefined") +# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -march=native -fsanitize=undefined") +# set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined") + +include_directories(. 3rdparty/lzfse/src) + +add_library(lzfse + 3rdparty/lzfse/src/lzfse.h + 3rdparty/lzfse/src/lzfse_decode.c + 3rdparty/lzfse/src/lzfse_decode_base.c + 3rdparty/lzfse/src/lzfse_encode.c + 3rdparty/lzfse/src/lzfse_encode_base.c + 3rdparty/lzfse/src/lzfse_encode_tables.h + 3rdparty/lzfse/src/lzfse_fse.c + 3rdparty/lzfse/src/lzfse_fse.h + 3rdparty/lzfse/src/lzfse_internal.h + 3rdparty/lzfse/src/lzfse_tunables.h + 3rdparty/lzfse/src/lzvn_decode_base.c + 3rdparty/lzfse/src/lzvn_decode_base.h + 3rdparty/lzfse/src/lzvn_encode_base.c + 3rdparty/lzfse/src/lzvn_encode_base.h + ) + +add_library(crypto + Crypto/Aes.cpp + Crypto/Aes.h + Crypto/AesXts.cpp + Crypto/AesXts.h + Crypto/Asn1Der.cpp + Crypto/Asn1Der.h + Crypto/Crypto.cpp + Crypto/Crypto.h + Crypto/Des.cpp + Crypto/Des.h + Crypto/Sha1.cpp + Crypto/Sha1.h + Crypto/Sha256.cpp + Crypto/Sha256.h + Crypto/TripleDes.cpp + Crypto/TripleDes.h +) + +add_library(apfs + ApfsLib/ApfsContainer.cpp + ApfsLib/ApfsContainer.h + ApfsLib/ApfsDir.cpp + ApfsLib/ApfsDir.h + ApfsLib/ApfsNodeMapper.cpp + ApfsLib/ApfsNodeMapper.h + ApfsLib/ApfsNodeMapperBTree.cpp + ApfsLib/ApfsNodeMapperBTree.h + ApfsLib/ApfsVolume.cpp + ApfsLib/ApfsVolume.h + ApfsLib/BlockDumper.cpp + ApfsLib/BlockDumper.h + ApfsLib/BTree.cpp + ApfsLib/BTree.h + ApfsLib/CheckPointMap.cpp + ApfsLib/CheckPointMap.h + ApfsLib/Crc32.cpp + ApfsLib/Crc32.h + ApfsLib/Decmpfs.cpp + ApfsLib/Decmpfs.h + ApfsLib/Device.cpp + ApfsLib/Device.h + ApfsLib/DeviceDMG.cpp + ApfsLib/DeviceDMG.h + ApfsLib/DeviceLinux.cpp + ApfsLib/DeviceLinux.h + ApfsLib/DeviceMac.cpp + ApfsLib/DeviceMac.h + ApfsLib/DeviceSparseImage.cpp + ApfsLib/DeviceSparseImage.h + ApfsLib/DeviceWinFile.cpp + ApfsLib/DeviceWinFile.h + ApfsLib/DeviceWinPhys.cpp + ApfsLib/DeviceWinPhys.h + ApfsLib/DiskImageFile.cpp + ApfsLib/DiskImageFile.h + ApfsLib/DiskStruct.h + ApfsLib/Endian.h + ApfsLib/Global.h + ApfsLib/GptPartitionMap.cpp + ApfsLib/GptPartitionMap.h + ApfsLib/KeyMgmt.cpp + ApfsLib/KeyMgmt.h + ApfsLib/PList.cpp + ApfsLib/PList.h + ApfsLib/Util.cpp + ApfsLib/Util.h + ApfsLib/Unicode.cpp + ApfsLib/Unicode.h) +target_link_libraries(apfs z bz2 lzfse crypto) +target_compile_definitions(apfs PUBLIC _FILE_OFFSET_BITS=64 _DARWIN_USE_64_BIT_INODE) + +add_executable(apfs-dump + ApfsDump/Dumper.cpp + ApfsDump/Dumper.h + ApfsDump/Apfs.cpp) +target_link_libraries(apfs-dump apfs) + +add_executable(apfs-dump-quick + ApfsDumpQuick/ApfsDumpQuick.cpp) +target_link_libraries(apfs-dump-quick apfs) + +add_executable(apfs-fuse + apfsfuse/ApfsFuse.cpp) +target_compile_definitions(apfs-fuse PRIVATE _FILE_OFFSET_BITS=64 _DARWIN_USE_64_BIT_INODE) +if (APPLE) +target_include_directories(apfs-fuse PRIVATE /usr/local/include/osxfuse/) +# link_directories(/usr/local/lib/) +target_link_libraries(apfs-fuse apfs /usr/local/lib/libosxfuse.dylib) +else() +if (USE_FUSE3) +target_link_libraries(apfs-fuse apfs fuse3) +else() +target_link_libraries(apfs-fuse apfs fuse) +target_compile_definitions(apfs-fuse PRIVATE USE_FUSE2) +endif() +endif() + +add_executable(apfsutil ApfsUtil/ApfsUtil.cpp) +target_link_libraries(apfsutil apfs) + +include(GNUInstallDirs) +install(TARGETS apfs-fuse RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") +install(TARGETS apfsutil RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") diff --git a/vendor/apfs-fuse/Crypto/Aes.cpp b/vendor/apfs-fuse/Crypto/Aes.cpp new file mode 100644 index 0000000..5c76b49 --- /dev/null +++ b/vendor/apfs-fuse/Crypto/Aes.cpp @@ -0,0 +1,584 @@ +#include +#include +#include + +// #include "Endian.h" +#include "Aes.h" + +const uint32_t AES::Te0[256] = { + 0xC66363A5U, 0xF87C7C84U, 0xEE777799U, 0xF67B7B8DU, 0xFFF2F20DU, 0xD66B6BBDU, 0xDE6F6FB1U, 0x91C5C554U, + 0x60303050U, 0x02010103U, 0xCE6767A9U, 0x562B2B7DU, 0xE7FEFE19U, 0xB5D7D762U, 0x4DABABE6U, 0xEC76769AU, + 0x8FCACA45U, 0x1F82829DU, 0x89C9C940U, 0xFA7D7D87U, 0xEFFAFA15U, 0xB25959EBU, 0x8E4747C9U, 0xFBF0F00BU, + 0x41ADADECU, 0xB3D4D467U, 0x5FA2A2FDU, 0x45AFAFEAU, 0x239C9CBFU, 0x53A4A4F7U, 0xE4727296U, 0x9BC0C05BU, + 0x75B7B7C2U, 0xE1FDFD1CU, 0x3D9393AEU, 0x4C26266AU, 0x6C36365AU, 0x7E3F3F41U, 0xF5F7F702U, 0x83CCCC4FU, + 0x6834345CU, 0x51A5A5F4U, 0xD1E5E534U, 0xF9F1F108U, 0xE2717193U, 0xABD8D873U, 0x62313153U, 0x2A15153FU, + 0x0804040CU, 0x95C7C752U, 0x46232365U, 0x9DC3C35EU, 0x30181828U, 0x379696A1U, 0x0A05050FU, 0x2F9A9AB5U, + 0x0E070709U, 0x24121236U, 0x1B80809BU, 0xDFE2E23DU, 0xCDEBEB26U, 0x4E272769U, 0x7FB2B2CDU, 0xEA75759FU, + 0x1209091BU, 0x1D83839EU, 0x582C2C74U, 0x341A1A2EU, 0x361B1B2DU, 0xDC6E6EB2U, 0xB45A5AEEU, 0x5BA0A0FBU, + 0xA45252F6U, 0x763B3B4DU, 0xB7D6D661U, 0x7DB3B3CEU, 0x5229297BU, 0xDDE3E33EU, 0x5E2F2F71U, 0x13848497U, + 0xA65353F5U, 0xB9D1D168U, 0x00000000U, 0xC1EDED2CU, 0x40202060U, 0xE3FCFC1FU, 0x79B1B1C8U, 0xB65B5BEDU, + 0xD46A6ABEU, 0x8DCBCB46U, 0x67BEBED9U, 0x7239394BU, 0x944A4ADEU, 0x984C4CD4U, 0xB05858E8U, 0x85CFCF4AU, + 0xBBD0D06BU, 0xC5EFEF2AU, 0x4FAAAAE5U, 0xEDFBFB16U, 0x864343C5U, 0x9A4D4DD7U, 0x66333355U, 0x11858594U, + 0x8A4545CFU, 0xE9F9F910U, 0x04020206U, 0xFE7F7F81U, 0xA05050F0U, 0x783C3C44U, 0x259F9FBAU, 0x4BA8A8E3U, + 0xA25151F3U, 0x5DA3A3FEU, 0x804040C0U, 0x058F8F8AU, 0x3F9292ADU, 0x219D9DBCU, 0x70383848U, 0xF1F5F504U, + 0x63BCBCDFU, 0x77B6B6C1U, 0xAFDADA75U, 0x42212163U, 0x20101030U, 0xE5FFFF1AU, 0xFDF3F30EU, 0xBFD2D26DU, + 0x81CDCD4CU, 0x180C0C14U, 0x26131335U, 0xC3ECEC2FU, 0xBE5F5FE1U, 0x359797A2U, 0x884444CCU, 0x2E171739U, + 0x93C4C457U, 0x55A7A7F2U, 0xFC7E7E82U, 0x7A3D3D47U, 0xC86464ACU, 0xBA5D5DE7U, 0x3219192BU, 0xE6737395U, + 0xC06060A0U, 0x19818198U, 0x9E4F4FD1U, 0xA3DCDC7FU, 0x44222266U, 0x542A2A7EU, 0x3B9090ABU, 0x0B888883U, + 0x8C4646CAU, 0xC7EEEE29U, 0x6BB8B8D3U, 0x2814143CU, 0xA7DEDE79U, 0xBC5E5EE2U, 0x160B0B1DU, 0xADDBDB76U, + 0xDBE0E03BU, 0x64323256U, 0x743A3A4EU, 0x140A0A1EU, 0x924949DBU, 0x0C06060AU, 0x4824246CU, 0xB85C5CE4U, + 0x9FC2C25DU, 0xBDD3D36EU, 0x43ACACEFU, 0xC46262A6U, 0x399191A8U, 0x319595A4U, 0xD3E4E437U, 0xF279798BU, + 0xD5E7E732U, 0x8BC8C843U, 0x6E373759U, 0xDA6D6DB7U, 0x018D8D8CU, 0xB1D5D564U, 0x9C4E4ED2U, 0x49A9A9E0U, + 0xD86C6CB4U, 0xAC5656FAU, 0xF3F4F407U, 0xCFEAEA25U, 0xCA6565AFU, 0xF47A7A8EU, 0x47AEAEE9U, 0x10080818U, + 0x6FBABAD5U, 0xF0787888U, 0x4A25256FU, 0x5C2E2E72U, 0x381C1C24U, 0x57A6A6F1U, 0x73B4B4C7U, 0x97C6C651U, + 0xCBE8E823U, 0xA1DDDD7CU, 0xE874749CU, 0x3E1F1F21U, 0x964B4BDDU, 0x61BDBDDCU, 0x0D8B8B86U, 0x0F8A8A85U, + 0xE0707090U, 0x7C3E3E42U, 0x71B5B5C4U, 0xCC6666AAU, 0x904848D8U, 0x06030305U, 0xF7F6F601U, 0x1C0E0E12U, + 0xC26161A3U, 0x6A35355FU, 0xAE5757F9U, 0x69B9B9D0U, 0x17868691U, 0x99C1C158U, 0x3A1D1D27U, 0x279E9EB9U, + 0xD9E1E138U, 0xEBF8F813U, 0x2B9898B3U, 0x22111133U, 0xD26969BBU, 0xA9D9D970U, 0x078E8E89U, 0x339494A7U, + 0x2D9B9BB6U, 0x3C1E1E22U, 0x15878792U, 0xC9E9E920U, 0x87CECE49U, 0xAA5555FFU, 0x50282878U, 0xA5DFDF7AU, + 0x038C8C8FU, 0x59A1A1F8U, 0x09898980U, 0x1A0D0D17U, 0x65BFBFDAU, 0xD7E6E631U, 0x844242C6U, 0xD06868B8U, + 0x824141C3U, 0x299999B0U, 0x5A2D2D77U, 0x1E0F0F11U, 0x7BB0B0CBU, 0xA85454FCU, 0x6DBBBBD6U, 0x2C16163AU +}; + +const uint32_t AES::Te1[256] = { + 0xA5C66363U, 0x84F87C7CU, 0x99EE7777U, 0x8DF67B7BU, 0x0DFFF2F2U, 0xBDD66B6BU, 0xB1DE6F6FU, 0x5491C5C5U, + 0x50603030U, 0x03020101U, 0xA9CE6767U, 0x7D562B2BU, 0x19E7FEFEU, 0x62B5D7D7U, 0xE64DABABU, 0x9AEC7676U, + 0x458FCACAU, 0x9D1F8282U, 0x4089C9C9U, 0x87FA7D7DU, 0x15EFFAFAU, 0xEBB25959U, 0xC98E4747U, 0x0BFBF0F0U, + 0xEC41ADADU, 0x67B3D4D4U, 0xFD5FA2A2U, 0xEA45AFAFU, 0xBF239C9CU, 0xF753A4A4U, 0x96E47272U, 0x5B9BC0C0U, + 0xC275B7B7U, 0x1CE1FDFDU, 0xAE3D9393U, 0x6A4C2626U, 0x5A6C3636U, 0x417E3F3FU, 0x02F5F7F7U, 0x4F83CCCCU, + 0x5C683434U, 0xF451A5A5U, 0x34D1E5E5U, 0x08F9F1F1U, 0x93E27171U, 0x73ABD8D8U, 0x53623131U, 0x3F2A1515U, + 0x0C080404U, 0x5295C7C7U, 0x65462323U, 0x5E9DC3C3U, 0x28301818U, 0xA1379696U, 0x0F0A0505U, 0xB52F9A9AU, + 0x090E0707U, 0x36241212U, 0x9B1B8080U, 0x3DDFE2E2U, 0x26CDEBEBU, 0x694E2727U, 0xCD7FB2B2U, 0x9FEA7575U, + 0x1B120909U, 0x9E1D8383U, 0x74582C2CU, 0x2E341A1AU, 0x2D361B1BU, 0xB2DC6E6EU, 0xEEB45A5AU, 0xFB5BA0A0U, + 0xF6A45252U, 0x4D763B3BU, 0x61B7D6D6U, 0xCE7DB3B3U, 0x7B522929U, 0x3EDDE3E3U, 0x715E2F2FU, 0x97138484U, + 0xF5A65353U, 0x68B9D1D1U, 0x00000000U, 0x2CC1EDEDU, 0x60402020U, 0x1FE3FCFCU, 0xC879B1B1U, 0xEDB65B5BU, + 0xBED46A6AU, 0x468DCBCBU, 0xD967BEBEU, 0x4B723939U, 0xDE944A4AU, 0xD4984C4CU, 0xE8B05858U, 0x4A85CFCFU, + 0x6BBBD0D0U, 0x2AC5EFEFU, 0xE54FAAAAU, 0x16EDFBFBU, 0xC5864343U, 0xD79A4D4DU, 0x55663333U, 0x94118585U, + 0xCF8A4545U, 0x10E9F9F9U, 0x06040202U, 0x81FE7F7FU, 0xF0A05050U, 0x44783C3CU, 0xBA259F9FU, 0xE34BA8A8U, + 0xF3A25151U, 0xFE5DA3A3U, 0xC0804040U, 0x8A058F8FU, 0xAD3F9292U, 0xBC219D9DU, 0x48703838U, 0x04F1F5F5U, + 0xDF63BCBCU, 0xC177B6B6U, 0x75AFDADAU, 0x63422121U, 0x30201010U, 0x1AE5FFFFU, 0x0EFDF3F3U, 0x6DBFD2D2U, + 0x4C81CDCDU, 0x14180C0CU, 0x35261313U, 0x2FC3ECECU, 0xE1BE5F5FU, 0xA2359797U, 0xCC884444U, 0x392E1717U, + 0x5793C4C4U, 0xF255A7A7U, 0x82FC7E7EU, 0x477A3D3DU, 0xACC86464U, 0xE7BA5D5DU, 0x2B321919U, 0x95E67373U, + 0xA0C06060U, 0x98198181U, 0xD19E4F4FU, 0x7FA3DCDCU, 0x66442222U, 0x7E542A2AU, 0xAB3B9090U, 0x830B8888U, + 0xCA8C4646U, 0x29C7EEEEU, 0xD36BB8B8U, 0x3C281414U, 0x79A7DEDEU, 0xE2BC5E5EU, 0x1D160B0BU, 0x76ADDBDBU, + 0x3BDBE0E0U, 0x56643232U, 0x4E743A3AU, 0x1E140A0AU, 0xDB924949U, 0x0A0C0606U, 0x6C482424U, 0xE4B85C5CU, + 0x5D9FC2C2U, 0x6EBDD3D3U, 0xEF43ACACU, 0xA6C46262U, 0xA8399191U, 0xA4319595U, 0x37D3E4E4U, 0x8BF27979U, + 0x32D5E7E7U, 0x438BC8C8U, 0x596E3737U, 0xB7DA6D6DU, 0x8C018D8DU, 0x64B1D5D5U, 0xD29C4E4EU, 0xE049A9A9U, + 0xB4D86C6CU, 0xFAAC5656U, 0x07F3F4F4U, 0x25CFEAEAU, 0xAFCA6565U, 0x8EF47A7AU, 0xE947AEAEU, 0x18100808U, + 0xD56FBABAU, 0x88F07878U, 0x6F4A2525U, 0x725C2E2EU, 0x24381C1CU, 0xF157A6A6U, 0xC773B4B4U, 0x5197C6C6U, + 0x23CBE8E8U, 0x7CA1DDDDU, 0x9CE87474U, 0x213E1F1FU, 0xDD964B4BU, 0xDC61BDBDU, 0x860D8B8BU, 0x850F8A8AU, + 0x90E07070U, 0x427C3E3EU, 0xC471B5B5U, 0xAACC6666U, 0xD8904848U, 0x05060303U, 0x01F7F6F6U, 0x121C0E0EU, + 0xA3C26161U, 0x5F6A3535U, 0xF9AE5757U, 0xD069B9B9U, 0x91178686U, 0x5899C1C1U, 0x273A1D1DU, 0xB9279E9EU, + 0x38D9E1E1U, 0x13EBF8F8U, 0xB32B9898U, 0x33221111U, 0xBBD26969U, 0x70A9D9D9U, 0x89078E8EU, 0xA7339494U, + 0xB62D9B9BU, 0x223C1E1EU, 0x92158787U, 0x20C9E9E9U, 0x4987CECEU, 0xFFAA5555U, 0x78502828U, 0x7AA5DFDFU, + 0x8F038C8CU, 0xF859A1A1U, 0x80098989U, 0x171A0D0DU, 0xDA65BFBFU, 0x31D7E6E6U, 0xC6844242U, 0xB8D06868U, + 0xC3824141U, 0xB0299999U, 0x775A2D2DU, 0x111E0F0FU, 0xCB7BB0B0U, 0xFCA85454U, 0xD66DBBBBU, 0x3A2C1616U +}; + +const uint32_t AES::Te2[256] = { + 0x63A5C663U, 0x7C84F87CU, 0x7799EE77U, 0x7B8DF67BU, 0xF20DFFF2U, 0x6BBDD66BU, 0x6FB1DE6FU, 0xC55491C5U, + 0x30506030U, 0x01030201U, 0x67A9CE67U, 0x2B7D562BU, 0xFE19E7FEU, 0xD762B5D7U, 0xABE64DABU, 0x769AEC76U, + 0xCA458FCAU, 0x829D1F82U, 0xC94089C9U, 0x7D87FA7DU, 0xFA15EFFAU, 0x59EBB259U, 0x47C98E47U, 0xF00BFBF0U, + 0xADEC41ADU, 0xD467B3D4U, 0xA2FD5FA2U, 0xAFEA45AFU, 0x9CBF239CU, 0xA4F753A4U, 0x7296E472U, 0xC05B9BC0U, + 0xB7C275B7U, 0xFD1CE1FDU, 0x93AE3D93U, 0x266A4C26U, 0x365A6C36U, 0x3F417E3FU, 0xF702F5F7U, 0xCC4F83CCU, + 0x345C6834U, 0xA5F451A5U, 0xE534D1E5U, 0xF108F9F1U, 0x7193E271U, 0xD873ABD8U, 0x31536231U, 0x153F2A15U, + 0x040C0804U, 0xC75295C7U, 0x23654623U, 0xC35E9DC3U, 0x18283018U, 0x96A13796U, 0x050F0A05U, 0x9AB52F9AU, + 0x07090E07U, 0x12362412U, 0x809B1B80U, 0xE23DDFE2U, 0xEB26CDEBU, 0x27694E27U, 0xB2CD7FB2U, 0x759FEA75U, + 0x091B1209U, 0x839E1D83U, 0x2C74582CU, 0x1A2E341AU, 0x1B2D361BU, 0x6EB2DC6EU, 0x5AEEB45AU, 0xA0FB5BA0U, + 0x52F6A452U, 0x3B4D763BU, 0xD661B7D6U, 0xB3CE7DB3U, 0x297B5229U, 0xE33EDDE3U, 0x2F715E2FU, 0x84971384U, + 0x53F5A653U, 0xD168B9D1U, 0x00000000U, 0xED2CC1EDU, 0x20604020U, 0xFC1FE3FCU, 0xB1C879B1U, 0x5BEDB65BU, + 0x6ABED46AU, 0xCB468DCBU, 0xBED967BEU, 0x394B7239U, 0x4ADE944AU, 0x4CD4984CU, 0x58E8B058U, 0xCF4A85CFU, + 0xD06BBBD0U, 0xEF2AC5EFU, 0xAAE54FAAU, 0xFB16EDFBU, 0x43C58643U, 0x4DD79A4DU, 0x33556633U, 0x85941185U, + 0x45CF8A45U, 0xF910E9F9U, 0x02060402U, 0x7F81FE7FU, 0x50F0A050U, 0x3C44783CU, 0x9FBA259FU, 0xA8E34BA8U, + 0x51F3A251U, 0xA3FE5DA3U, 0x40C08040U, 0x8F8A058FU, 0x92AD3F92U, 0x9DBC219DU, 0x38487038U, 0xF504F1F5U, + 0xBCDF63BCU, 0xB6C177B6U, 0xDA75AFDAU, 0x21634221U, 0x10302010U, 0xFF1AE5FFU, 0xF30EFDF3U, 0xD26DBFD2U, + 0xCD4C81CDU, 0x0C14180CU, 0x13352613U, 0xEC2FC3ECU, 0x5FE1BE5FU, 0x97A23597U, 0x44CC8844U, 0x17392E17U, + 0xC45793C4U, 0xA7F255A7U, 0x7E82FC7EU, 0x3D477A3DU, 0x64ACC864U, 0x5DE7BA5DU, 0x192B3219U, 0x7395E673U, + 0x60A0C060U, 0x81981981U, 0x4FD19E4FU, 0xDC7FA3DCU, 0x22664422U, 0x2A7E542AU, 0x90AB3B90U, 0x88830B88U, + 0x46CA8C46U, 0xEE29C7EEU, 0xB8D36BB8U, 0x143C2814U, 0xDE79A7DEU, 0x5EE2BC5EU, 0x0B1D160BU, 0xDB76ADDBU, + 0xE03BDBE0U, 0x32566432U, 0x3A4E743AU, 0x0A1E140AU, 0x49DB9249U, 0x060A0C06U, 0x246C4824U, 0x5CE4B85CU, + 0xC25D9FC2U, 0xD36EBDD3U, 0xACEF43ACU, 0x62A6C462U, 0x91A83991U, 0x95A43195U, 0xE437D3E4U, 0x798BF279U, + 0xE732D5E7U, 0xC8438BC8U, 0x37596E37U, 0x6DB7DA6DU, 0x8D8C018DU, 0xD564B1D5U, 0x4ED29C4EU, 0xA9E049A9U, + 0x6CB4D86CU, 0x56FAAC56U, 0xF407F3F4U, 0xEA25CFEAU, 0x65AFCA65U, 0x7A8EF47AU, 0xAEE947AEU, 0x08181008U, + 0xBAD56FBAU, 0x7888F078U, 0x256F4A25U, 0x2E725C2EU, 0x1C24381CU, 0xA6F157A6U, 0xB4C773B4U, 0xC65197C6U, + 0xE823CBE8U, 0xDD7CA1DDU, 0x749CE874U, 0x1F213E1FU, 0x4BDD964BU, 0xBDDC61BDU, 0x8B860D8BU, 0x8A850F8AU, + 0x7090E070U, 0x3E427C3EU, 0xB5C471B5U, 0x66AACC66U, 0x48D89048U, 0x03050603U, 0xF601F7F6U, 0x0E121C0EU, + 0x61A3C261U, 0x355F6A35U, 0x57F9AE57U, 0xB9D069B9U, 0x86911786U, 0xC15899C1U, 0x1D273A1DU, 0x9EB9279EU, + 0xE138D9E1U, 0xF813EBF8U, 0x98B32B98U, 0x11332211U, 0x69BBD269U, 0xD970A9D9U, 0x8E89078EU, 0x94A73394U, + 0x9BB62D9BU, 0x1E223C1EU, 0x87921587U, 0xE920C9E9U, 0xCE4987CEU, 0x55FFAA55U, 0x28785028U, 0xDF7AA5DFU, + 0x8C8F038CU, 0xA1F859A1U, 0x89800989U, 0x0D171A0DU, 0xBFDA65BFU, 0xE631D7E6U, 0x42C68442U, 0x68B8D068U, + 0x41C38241U, 0x99B02999U, 0x2D775A2DU, 0x0F111E0FU, 0xB0CB7BB0U, 0x54FCA854U, 0xBBD66DBBU, 0x163A2C16U +}; + +const uint32_t AES::Te3[256] = { + 0x6363A5C6U, 0x7C7C84F8U, 0x777799EEU, 0x7B7B8DF6U, 0xF2F20DFFU, 0x6B6BBDD6U, 0x6F6FB1DEU, 0xC5C55491U, + 0x30305060U, 0x01010302U, 0x6767A9CEU, 0x2B2B7D56U, 0xFEFE19E7U, 0xD7D762B5U, 0xABABE64DU, 0x76769AECU, + 0xCACA458FU, 0x82829D1FU, 0xC9C94089U, 0x7D7D87FAU, 0xFAFA15EFU, 0x5959EBB2U, 0x4747C98EU, 0xF0F00BFBU, + 0xADADEC41U, 0xD4D467B3U, 0xA2A2FD5FU, 0xAFAFEA45U, 0x9C9CBF23U, 0xA4A4F753U, 0x727296E4U, 0xC0C05B9BU, + 0xB7B7C275U, 0xFDFD1CE1U, 0x9393AE3DU, 0x26266A4CU, 0x36365A6CU, 0x3F3F417EU, 0xF7F702F5U, 0xCCCC4F83U, + 0x34345C68U, 0xA5A5F451U, 0xE5E534D1U, 0xF1F108F9U, 0x717193E2U, 0xD8D873ABU, 0x31315362U, 0x15153F2AU, + 0x04040C08U, 0xC7C75295U, 0x23236546U, 0xC3C35E9DU, 0x18182830U, 0x9696A137U, 0x05050F0AU, 0x9A9AB52FU, + 0x0707090EU, 0x12123624U, 0x80809B1BU, 0xE2E23DDFU, 0xEBEB26CDU, 0x2727694EU, 0xB2B2CD7FU, 0x75759FEAU, + 0x09091B12U, 0x83839E1DU, 0x2C2C7458U, 0x1A1A2E34U, 0x1B1B2D36U, 0x6E6EB2DCU, 0x5A5AEEB4U, 0xA0A0FB5BU, + 0x5252F6A4U, 0x3B3B4D76U, 0xD6D661B7U, 0xB3B3CE7DU, 0x29297B52U, 0xE3E33EDDU, 0x2F2F715EU, 0x84849713U, + 0x5353F5A6U, 0xD1D168B9U, 0x00000000U, 0xEDED2CC1U, 0x20206040U, 0xFCFC1FE3U, 0xB1B1C879U, 0x5B5BEDB6U, + 0x6A6ABED4U, 0xCBCB468DU, 0xBEBED967U, 0x39394B72U, 0x4A4ADE94U, 0x4C4CD498U, 0x5858E8B0U, 0xCFCF4A85U, + 0xD0D06BBBU, 0xEFEF2AC5U, 0xAAAAE54FU, 0xFBFB16EDU, 0x4343C586U, 0x4D4DD79AU, 0x33335566U, 0x85859411U, + 0x4545CF8AU, 0xF9F910E9U, 0x02020604U, 0x7F7F81FEU, 0x5050F0A0U, 0x3C3C4478U, 0x9F9FBA25U, 0xA8A8E34BU, + 0x5151F3A2U, 0xA3A3FE5DU, 0x4040C080U, 0x8F8F8A05U, 0x9292AD3FU, 0x9D9DBC21U, 0x38384870U, 0xF5F504F1U, + 0xBCBCDF63U, 0xB6B6C177U, 0xDADA75AFU, 0x21216342U, 0x10103020U, 0xFFFF1AE5U, 0xF3F30EFDU, 0xD2D26DBFU, + 0xCDCD4C81U, 0x0C0C1418U, 0x13133526U, 0xECEC2FC3U, 0x5F5FE1BEU, 0x9797A235U, 0x4444CC88U, 0x1717392EU, + 0xC4C45793U, 0xA7A7F255U, 0x7E7E82FCU, 0x3D3D477AU, 0x6464ACC8U, 0x5D5DE7BAU, 0x19192B32U, 0x737395E6U, + 0x6060A0C0U, 0x81819819U, 0x4F4FD19EU, 0xDCDC7FA3U, 0x22226644U, 0x2A2A7E54U, 0x9090AB3BU, 0x8888830BU, + 0x4646CA8CU, 0xEEEE29C7U, 0xB8B8D36BU, 0x14143C28U, 0xDEDE79A7U, 0x5E5EE2BCU, 0x0B0B1D16U, 0xDBDB76ADU, + 0xE0E03BDBU, 0x32325664U, 0x3A3A4E74U, 0x0A0A1E14U, 0x4949DB92U, 0x06060A0CU, 0x24246C48U, 0x5C5CE4B8U, + 0xC2C25D9FU, 0xD3D36EBDU, 0xACACEF43U, 0x6262A6C4U, 0x9191A839U, 0x9595A431U, 0xE4E437D3U, 0x79798BF2U, + 0xE7E732D5U, 0xC8C8438BU, 0x3737596EU, 0x6D6DB7DAU, 0x8D8D8C01U, 0xD5D564B1U, 0x4E4ED29CU, 0xA9A9E049U, + 0x6C6CB4D8U, 0x5656FAACU, 0xF4F407F3U, 0xEAEA25CFU, 0x6565AFCAU, 0x7A7A8EF4U, 0xAEAEE947U, 0x08081810U, + 0xBABAD56FU, 0x787888F0U, 0x25256F4AU, 0x2E2E725CU, 0x1C1C2438U, 0xA6A6F157U, 0xB4B4C773U, 0xC6C65197U, + 0xE8E823CBU, 0xDDDD7CA1U, 0x74749CE8U, 0x1F1F213EU, 0x4B4BDD96U, 0xBDBDDC61U, 0x8B8B860DU, 0x8A8A850FU, + 0x707090E0U, 0x3E3E427CU, 0xB5B5C471U, 0x6666AACCU, 0x4848D890U, 0x03030506U, 0xF6F601F7U, 0x0E0E121CU, + 0x6161A3C2U, 0x35355F6AU, 0x5757F9AEU, 0xB9B9D069U, 0x86869117U, 0xC1C15899U, 0x1D1D273AU, 0x9E9EB927U, + 0xE1E138D9U, 0xF8F813EBU, 0x9898B32BU, 0x11113322U, 0x6969BBD2U, 0xD9D970A9U, 0x8E8E8907U, 0x9494A733U, + 0x9B9BB62DU, 0x1E1E223CU, 0x87879215U, 0xE9E920C9U, 0xCECE4987U, 0x5555FFAAU, 0x28287850U, 0xDFDF7AA5U, + 0x8C8C8F03U, 0xA1A1F859U, 0x89898009U, 0x0D0D171AU, 0xBFBFDA65U, 0xE6E631D7U, 0x4242C684U, 0x6868B8D0U, + 0x4141C382U, 0x9999B029U, 0x2D2D775AU, 0x0F0F111EU, 0xB0B0CB7BU, 0x5454FCA8U, 0xBBBBD66DU, 0x16163A2CU +}; + +const uint32_t AES::Te4[256] = { + 0x63636363U, 0x7C7C7C7CU, 0x77777777U, 0x7B7B7B7BU, 0xF2F2F2F2U, 0x6B6B6B6BU, 0x6F6F6F6FU, 0xC5C5C5C5U, + 0x30303030U, 0x01010101U, 0x67676767U, 0x2B2B2B2BU, 0xFEFEFEFEU, 0xD7D7D7D7U, 0xABABABABU, 0x76767676U, + 0xCACACACAU, 0x82828282U, 0xC9C9C9C9U, 0x7D7D7D7DU, 0xFAFAFAFAU, 0x59595959U, 0x47474747U, 0xF0F0F0F0U, + 0xADADADADU, 0xD4D4D4D4U, 0xA2A2A2A2U, 0xAFAFAFAFU, 0x9C9C9C9CU, 0xA4A4A4A4U, 0x72727272U, 0xC0C0C0C0U, + 0xB7B7B7B7U, 0xFDFDFDFDU, 0x93939393U, 0x26262626U, 0x36363636U, 0x3F3F3F3FU, 0xF7F7F7F7U, 0xCCCCCCCCU, + 0x34343434U, 0xA5A5A5A5U, 0xE5E5E5E5U, 0xF1F1F1F1U, 0x71717171U, 0xD8D8D8D8U, 0x31313131U, 0x15151515U, + 0x04040404U, 0xC7C7C7C7U, 0x23232323U, 0xC3C3C3C3U, 0x18181818U, 0x96969696U, 0x05050505U, 0x9A9A9A9AU, + 0x07070707U, 0x12121212U, 0x80808080U, 0xE2E2E2E2U, 0xEBEBEBEBU, 0x27272727U, 0xB2B2B2B2U, 0x75757575U, + 0x09090909U, 0x83838383U, 0x2C2C2C2CU, 0x1A1A1A1AU, 0x1B1B1B1BU, 0x6E6E6E6EU, 0x5A5A5A5AU, 0xA0A0A0A0U, + 0x52525252U, 0x3B3B3B3BU, 0xD6D6D6D6U, 0xB3B3B3B3U, 0x29292929U, 0xE3E3E3E3U, 0x2F2F2F2FU, 0x84848484U, + 0x53535353U, 0xD1D1D1D1U, 0x00000000U, 0xEDEDEDEDU, 0x20202020U, 0xFCFCFCFCU, 0xB1B1B1B1U, 0x5B5B5B5BU, + 0x6A6A6A6AU, 0xCBCBCBCBU, 0xBEBEBEBEU, 0x39393939U, 0x4A4A4A4AU, 0x4C4C4C4CU, 0x58585858U, 0xCFCFCFCFU, + 0xD0D0D0D0U, 0xEFEFEFEFU, 0xAAAAAAAAU, 0xFBFBFBFBU, 0x43434343U, 0x4D4D4D4DU, 0x33333333U, 0x85858585U, + 0x45454545U, 0xF9F9F9F9U, 0x02020202U, 0x7F7F7F7FU, 0x50505050U, 0x3C3C3C3CU, 0x9F9F9F9FU, 0xA8A8A8A8U, + 0x51515151U, 0xA3A3A3A3U, 0x40404040U, 0x8F8F8F8FU, 0x92929292U, 0x9D9D9D9DU, 0x38383838U, 0xF5F5F5F5U, + 0xBCBCBCBCU, 0xB6B6B6B6U, 0xDADADADAU, 0x21212121U, 0x10101010U, 0xFFFFFFFFU, 0xF3F3F3F3U, 0xD2D2D2D2U, + 0xCDCDCDCDU, 0x0C0C0C0CU, 0x13131313U, 0xECECECECU, 0x5F5F5F5FU, 0x97979797U, 0x44444444U, 0x17171717U, + 0xC4C4C4C4U, 0xA7A7A7A7U, 0x7E7E7E7EU, 0x3D3D3D3DU, 0x64646464U, 0x5D5D5D5DU, 0x19191919U, 0x73737373U, + 0x60606060U, 0x81818181U, 0x4F4F4F4FU, 0xDCDCDCDCU, 0x22222222U, 0x2A2A2A2AU, 0x90909090U, 0x88888888U, + 0x46464646U, 0xEEEEEEEEU, 0xB8B8B8B8U, 0x14141414U, 0xDEDEDEDEU, 0x5E5E5E5EU, 0x0B0B0B0BU, 0xDBDBDBDBU, + 0xE0E0E0E0U, 0x32323232U, 0x3A3A3A3AU, 0x0A0A0A0AU, 0x49494949U, 0x06060606U, 0x24242424U, 0x5C5C5C5CU, + 0xC2C2C2C2U, 0xD3D3D3D3U, 0xACACACACU, 0x62626262U, 0x91919191U, 0x95959595U, 0xE4E4E4E4U, 0x79797979U, + 0xE7E7E7E7U, 0xC8C8C8C8U, 0x37373737U, 0x6D6D6D6DU, 0x8D8D8D8DU, 0xD5D5D5D5U, 0x4E4E4E4EU, 0xA9A9A9A9U, + 0x6C6C6C6CU, 0x56565656U, 0xF4F4F4F4U, 0xEAEAEAEAU, 0x65656565U, 0x7A7A7A7AU, 0xAEAEAEAEU, 0x08080808U, + 0xBABABABAU, 0x78787878U, 0x25252525U, 0x2E2E2E2EU, 0x1C1C1C1CU, 0xA6A6A6A6U, 0xB4B4B4B4U, 0xC6C6C6C6U, + 0xE8E8E8E8U, 0xDDDDDDDDU, 0x74747474U, 0x1F1F1F1FU, 0x4B4B4B4BU, 0xBDBDBDBDU, 0x8B8B8B8BU, 0x8A8A8A8AU, + 0x70707070U, 0x3E3E3E3EU, 0xB5B5B5B5U, 0x66666666U, 0x48484848U, 0x03030303U, 0xF6F6F6F6U, 0x0E0E0E0EU, + 0x61616161U, 0x35353535U, 0x57575757U, 0xB9B9B9B9U, 0x86868686U, 0xC1C1C1C1U, 0x1D1D1D1DU, 0x9E9E9E9EU, + 0xE1E1E1E1U, 0xF8F8F8F8U, 0x98989898U, 0x11111111U, 0x69696969U, 0xD9D9D9D9U, 0x8E8E8E8EU, 0x94949494U, + 0x9B9B9B9BU, 0x1E1E1E1EU, 0x87878787U, 0xE9E9E9E9U, 0xCECECECEU, 0x55555555U, 0x28282828U, 0xDFDFDFDFU, + 0x8C8C8C8CU, 0xA1A1A1A1U, 0x89898989U, 0x0D0D0D0DU, 0xBFBFBFBFU, 0xE6E6E6E6U, 0x42424242U, 0x68686868U, + 0x41414141U, 0x99999999U, 0x2D2D2D2DU, 0x0F0F0F0FU, 0xB0B0B0B0U, 0x54545454U, 0xBBBBBBBBU, 0x16161616U +}; + +const uint32_t AES::Td0[256] = { + 0x51F4A750U, 0x7E416553U, 0x1A17A4C3U, 0x3A275E96U, 0x3BAB6BCBU, 0x1F9D45F1U, 0xACFA58ABU, 0x4BE30393U, + 0x2030FA55U, 0xAD766DF6U, 0x88CC7691U, 0xF5024C25U, 0x4FE5D7FCU, 0xC52ACBD7U, 0x26354480U, 0xB562A38FU, + 0xDEB15A49U, 0x25BA1B67U, 0x45EA0E98U, 0x5DFEC0E1U, 0xC32F7502U, 0x814CF012U, 0x8D4697A3U, 0x6BD3F9C6U, + 0x038F5FE7U, 0x15929C95U, 0xBF6D7AEBU, 0x955259DAU, 0xD4BE832DU, 0x587421D3U, 0x49E06929U, 0x8EC9C844U, + 0x75C2896AU, 0xF48E7978U, 0x99583E6BU, 0x27B971DDU, 0xBEE14FB6U, 0xF088AD17U, 0xC920AC66U, 0x7DCE3AB4U, + 0x63DF4A18U, 0xE51A3182U, 0x97513360U, 0x62537F45U, 0xB16477E0U, 0xBB6BAE84U, 0xFE81A01CU, 0xF9082B94U, + 0x70486858U, 0x8F45FD19U, 0x94DE6C87U, 0x527BF8B7U, 0xAB73D323U, 0x724B02E2U, 0xE31F8F57U, 0x6655AB2AU, + 0xB2EB2807U, 0x2FB5C203U, 0x86C57B9AU, 0xD33708A5U, 0x302887F2U, 0x23BFA5B2U, 0x02036ABAU, 0xED16825CU, + 0x8ACF1C2BU, 0xA779B492U, 0xF307F2F0U, 0x4E69E2A1U, 0x65DAF4CDU, 0x0605BED5U, 0xD134621FU, 0xC4A6FE8AU, + 0x342E539DU, 0xA2F355A0U, 0x058AE132U, 0xA4F6EB75U, 0x0B83EC39U, 0x4060EFAAU, 0x5E719F06U, 0xBD6E1051U, + 0x3E218AF9U, 0x96DD063DU, 0xDD3E05AEU, 0x4DE6BD46U, 0x91548DB5U, 0x71C45D05U, 0x0406D46FU, 0x605015FFU, + 0x1998FB24U, 0xD6BDE997U, 0x894043CCU, 0x67D99E77U, 0xB0E842BDU, 0x07898B88U, 0xE7195B38U, 0x79C8EEDBU, + 0xA17C0A47U, 0x7C420FE9U, 0xF8841EC9U, 0x00000000U, 0x09808683U, 0x322BED48U, 0x1E1170ACU, 0x6C5A724EU, + 0xFD0EFFFBU, 0x0F853856U, 0x3DAED51EU, 0x362D3927U, 0x0A0FD964U, 0x685CA621U, 0x9B5B54D1U, 0x24362E3AU, + 0x0C0A67B1U, 0x9357E70FU, 0xB4EE96D2U, 0x1B9B919EU, 0x80C0C54FU, 0x61DC20A2U, 0x5A774B69U, 0x1C121A16U, + 0xE293BA0AU, 0xC0A02AE5U, 0x3C22E043U, 0x121B171DU, 0x0E090D0BU, 0xF28BC7ADU, 0x2DB6A8B9U, 0x141EA9C8U, + 0x57F11985U, 0xAF75074CU, 0xEE99DDBBU, 0xA37F60FDU, 0xF701269FU, 0x5C72F5BCU, 0x44663BC5U, 0x5BFB7E34U, + 0x8B432976U, 0xCB23C6DCU, 0xB6EDFC68U, 0xB8E4F163U, 0xD731DCCAU, 0x42638510U, 0x13972240U, 0x84C61120U, + 0x854A247DU, 0xD2BB3DF8U, 0xAEF93211U, 0xC729A16DU, 0x1D9E2F4BU, 0xDCB230F3U, 0x0D8652ECU, 0x77C1E3D0U, + 0x2BB3166CU, 0xA970B999U, 0x119448FAU, 0x47E96422U, 0xA8FC8CC4U, 0xA0F03F1AU, 0x567D2CD8U, 0x223390EFU, + 0x87494EC7U, 0xD938D1C1U, 0x8CCAA2FEU, 0x98D40B36U, 0xA6F581CFU, 0xA57ADE28U, 0xDAB78E26U, 0x3FADBFA4U, + 0x2C3A9DE4U, 0x5078920DU, 0x6A5FCC9BU, 0x547E4662U, 0xF68D13C2U, 0x90D8B8E8U, 0x2E39F75EU, 0x82C3AFF5U, + 0x9F5D80BEU, 0x69D0937CU, 0x6FD52DA9U, 0xCF2512B3U, 0xC8AC993BU, 0x10187DA7U, 0xE89C636EU, 0xDB3BBB7BU, + 0xCD267809U, 0x6E5918F4U, 0xEC9AB701U, 0x834F9AA8U, 0xE6956E65U, 0xAAFFE67EU, 0x21BCCF08U, 0xEF15E8E6U, + 0xBAE79BD9U, 0x4A6F36CEU, 0xEA9F09D4U, 0x29B07CD6U, 0x31A4B2AFU, 0x2A3F2331U, 0xC6A59430U, 0x35A266C0U, + 0x744EBC37U, 0xFC82CAA6U, 0xE090D0B0U, 0x33A7D815U, 0xF104984AU, 0x41ECDAF7U, 0x7FCD500EU, 0x1791F62FU, + 0x764DD68DU, 0x43EFB04DU, 0xCCAA4D54U, 0xE49604DFU, 0x9ED1B5E3U, 0x4C6A881BU, 0xC12C1FB8U, 0x4665517FU, + 0x9D5EEA04U, 0x018C355DU, 0xFA877473U, 0xFB0B412EU, 0xB3671D5AU, 0x92DBD252U, 0xE9105633U, 0x6DD64713U, + 0x9AD7618CU, 0x37A10C7AU, 0x59F8148EU, 0xEB133C89U, 0xCEA927EEU, 0xB761C935U, 0xE11CE5EDU, 0x7A47B13CU, + 0x9CD2DF59U, 0x55F2733FU, 0x1814CE79U, 0x73C737BFU, 0x53F7CDEAU, 0x5FFDAA5BU, 0xDF3D6F14U, 0x7844DB86U, + 0xCAAFF381U, 0xB968C43EU, 0x3824342CU, 0xC2A3405FU, 0x161DC372U, 0xBCE2250CU, 0x283C498BU, 0xFF0D9541U, + 0x39A80171U, 0x080CB3DEU, 0xD8B4E49CU, 0x6456C190U, 0x7BCB8461U, 0xD532B670U, 0x486C5C74U, 0xD0B85742U +}; + +const uint32_t AES::Td1[256] = { + 0x5051F4A7U, 0x537E4165U, 0xC31A17A4U, 0x963A275EU, 0xCB3BAB6BU, 0xF11F9D45U, 0xABACFA58U, 0x934BE303U, + 0x552030FAU, 0xF6AD766DU, 0x9188CC76U, 0x25F5024CU, 0xFC4FE5D7U, 0xD7C52ACBU, 0x80263544U, 0x8FB562A3U, + 0x49DEB15AU, 0x6725BA1BU, 0x9845EA0EU, 0xE15DFEC0U, 0x02C32F75U, 0x12814CF0U, 0xA38D4697U, 0xC66BD3F9U, + 0xE7038F5FU, 0x9515929CU, 0xEBBF6D7AU, 0xDA955259U, 0x2DD4BE83U, 0xD3587421U, 0x2949E069U, 0x448EC9C8U, + 0x6A75C289U, 0x78F48E79U, 0x6B99583EU, 0xDD27B971U, 0xB6BEE14FU, 0x17F088ADU, 0x66C920ACU, 0xB47DCE3AU, + 0x1863DF4AU, 0x82E51A31U, 0x60975133U, 0x4562537FU, 0xE0B16477U, 0x84BB6BAEU, 0x1CFE81A0U, 0x94F9082BU, + 0x58704868U, 0x198F45FDU, 0x8794DE6CU, 0xB7527BF8U, 0x23AB73D3U, 0xE2724B02U, 0x57E31F8FU, 0x2A6655ABU, + 0x07B2EB28U, 0x032FB5C2U, 0x9A86C57BU, 0xA5D33708U, 0xF2302887U, 0xB223BFA5U, 0xBA02036AU, 0x5CED1682U, + 0x2B8ACF1CU, 0x92A779B4U, 0xF0F307F2U, 0xA14E69E2U, 0xCD65DAF4U, 0xD50605BEU, 0x1FD13462U, 0x8AC4A6FEU, + 0x9D342E53U, 0xA0A2F355U, 0x32058AE1U, 0x75A4F6EBU, 0x390B83ECU, 0xAA4060EFU, 0x065E719FU, 0x51BD6E10U, + 0xF93E218AU, 0x3D96DD06U, 0xAEDD3E05U, 0x464DE6BDU, 0xB591548DU, 0x0571C45DU, 0x6F0406D4U, 0xFF605015U, + 0x241998FBU, 0x97D6BDE9U, 0xCC894043U, 0x7767D99EU, 0xBDB0E842U, 0x8807898BU, 0x38E7195BU, 0xDB79C8EEU, + 0x47A17C0AU, 0xE97C420FU, 0xC9F8841EU, 0x00000000U, 0x83098086U, 0x48322BEDU, 0xAC1E1170U, 0x4E6C5A72U, + 0xFBFD0EFFU, 0x560F8538U, 0x1E3DAED5U, 0x27362D39U, 0x640A0FD9U, 0x21685CA6U, 0xD19B5B54U, 0x3A24362EU, + 0xB10C0A67U, 0x0F9357E7U, 0xD2B4EE96U, 0x9E1B9B91U, 0x4F80C0C5U, 0xA261DC20U, 0x695A774BU, 0x161C121AU, + 0x0AE293BAU, 0xE5C0A02AU, 0x433C22E0U, 0x1D121B17U, 0x0B0E090DU, 0xADF28BC7U, 0xB92DB6A8U, 0xC8141EA9U, + 0x8557F119U, 0x4CAF7507U, 0xBBEE99DDU, 0xFDA37F60U, 0x9FF70126U, 0xBC5C72F5U, 0xC544663BU, 0x345BFB7EU, + 0x768B4329U, 0xDCCB23C6U, 0x68B6EDFCU, 0x63B8E4F1U, 0xCAD731DCU, 0x10426385U, 0x40139722U, 0x2084C611U, + 0x7D854A24U, 0xF8D2BB3DU, 0x11AEF932U, 0x6DC729A1U, 0x4B1D9E2FU, 0xF3DCB230U, 0xEC0D8652U, 0xD077C1E3U, + 0x6C2BB316U, 0x99A970B9U, 0xFA119448U, 0x2247E964U, 0xC4A8FC8CU, 0x1AA0F03FU, 0xD8567D2CU, 0xEF223390U, + 0xC787494EU, 0xC1D938D1U, 0xFE8CCAA2U, 0x3698D40BU, 0xCFA6F581U, 0x28A57ADEU, 0x26DAB78EU, 0xA43FADBFU, + 0xE42C3A9DU, 0x0D507892U, 0x9B6A5FCCU, 0x62547E46U, 0xC2F68D13U, 0xE890D8B8U, 0x5E2E39F7U, 0xF582C3AFU, + 0xBE9F5D80U, 0x7C69D093U, 0xA96FD52DU, 0xB3CF2512U, 0x3BC8AC99U, 0xA710187DU, 0x6EE89C63U, 0x7BDB3BBBU, + 0x09CD2678U, 0xF46E5918U, 0x01EC9AB7U, 0xA8834F9AU, 0x65E6956EU, 0x7EAAFFE6U, 0x0821BCCFU, 0xE6EF15E8U, + 0xD9BAE79BU, 0xCE4A6F36U, 0xD4EA9F09U, 0xD629B07CU, 0xAF31A4B2U, 0x312A3F23U, 0x30C6A594U, 0xC035A266U, + 0x37744EBCU, 0xA6FC82CAU, 0xB0E090D0U, 0x1533A7D8U, 0x4AF10498U, 0xF741ECDAU, 0x0E7FCD50U, 0x2F1791F6U, + 0x8D764DD6U, 0x4D43EFB0U, 0x54CCAA4DU, 0xDFE49604U, 0xE39ED1B5U, 0x1B4C6A88U, 0xB8C12C1FU, 0x7F466551U, + 0x049D5EEAU, 0x5D018C35U, 0x73FA8774U, 0x2EFB0B41U, 0x5AB3671DU, 0x5292DBD2U, 0x33E91056U, 0x136DD647U, + 0x8C9AD761U, 0x7A37A10CU, 0x8E59F814U, 0x89EB133CU, 0xEECEA927U, 0x35B761C9U, 0xEDE11CE5U, 0x3C7A47B1U, + 0x599CD2DFU, 0x3F55F273U, 0x791814CEU, 0xBF73C737U, 0xEA53F7CDU, 0x5B5FFDAAU, 0x14DF3D6FU, 0x867844DBU, + 0x81CAAFF3U, 0x3EB968C4U, 0x2C382434U, 0x5FC2A340U, 0x72161DC3U, 0x0CBCE225U, 0x8B283C49U, 0x41FF0D95U, + 0x7139A801U, 0xDE080CB3U, 0x9CD8B4E4U, 0x906456C1U, 0x617BCB84U, 0x70D532B6U, 0x74486C5CU, 0x42D0B857U +}; + +const uint32_t AES::Td2[256] = { + 0xA75051F4U, 0x65537E41U, 0xA4C31A17U, 0x5E963A27U, 0x6BCB3BABU, 0x45F11F9DU, 0x58ABACFAU, 0x03934BE3U, + 0xFA552030U, 0x6DF6AD76U, 0x769188CCU, 0x4C25F502U, 0xD7FC4FE5U, 0xCBD7C52AU, 0x44802635U, 0xA38FB562U, + 0x5A49DEB1U, 0x1B6725BAU, 0x0E9845EAU, 0xC0E15DFEU, 0x7502C32FU, 0xF012814CU, 0x97A38D46U, 0xF9C66BD3U, + 0x5FE7038FU, 0x9C951592U, 0x7AEBBF6DU, 0x59DA9552U, 0x832DD4BEU, 0x21D35874U, 0x692949E0U, 0xC8448EC9U, + 0x896A75C2U, 0x7978F48EU, 0x3E6B9958U, 0x71DD27B9U, 0x4FB6BEE1U, 0xAD17F088U, 0xAC66C920U, 0x3AB47DCEU, + 0x4A1863DFU, 0x3182E51AU, 0x33609751U, 0x7F456253U, 0x77E0B164U, 0xAE84BB6BU, 0xA01CFE81U, 0x2B94F908U, + 0x68587048U, 0xFD198F45U, 0x6C8794DEU, 0xF8B7527BU, 0xD323AB73U, 0x02E2724BU, 0x8F57E31FU, 0xAB2A6655U, + 0x2807B2EBU, 0xC2032FB5U, 0x7B9A86C5U, 0x08A5D337U, 0x87F23028U, 0xA5B223BFU, 0x6ABA0203U, 0x825CED16U, + 0x1C2B8ACFU, 0xB492A779U, 0xF2F0F307U, 0xE2A14E69U, 0xF4CD65DAU, 0xBED50605U, 0x621FD134U, 0xFE8AC4A6U, + 0x539D342EU, 0x55A0A2F3U, 0xE132058AU, 0xEB75A4F6U, 0xEC390B83U, 0xEFAA4060U, 0x9F065E71U, 0x1051BD6EU, + 0x8AF93E21U, 0x063D96DDU, 0x05AEDD3EU, 0xBD464DE6U, 0x8DB59154U, 0x5D0571C4U, 0xD46F0406U, 0x15FF6050U, + 0xFB241998U, 0xE997D6BDU, 0x43CC8940U, 0x9E7767D9U, 0x42BDB0E8U, 0x8B880789U, 0x5B38E719U, 0xEEDB79C8U, + 0x0A47A17CU, 0x0FE97C42U, 0x1EC9F884U, 0x00000000U, 0x86830980U, 0xED48322BU, 0x70AC1E11U, 0x724E6C5AU, + 0xFFFBFD0EU, 0x38560F85U, 0xD51E3DAEU, 0x3927362DU, 0xD9640A0FU, 0xA621685CU, 0x54D19B5BU, 0x2E3A2436U, + 0x67B10C0AU, 0xE70F9357U, 0x96D2B4EEU, 0x919E1B9BU, 0xC54F80C0U, 0x20A261DCU, 0x4B695A77U, 0x1A161C12U, + 0xBA0AE293U, 0x2AE5C0A0U, 0xE0433C22U, 0x171D121BU, 0x0D0B0E09U, 0xC7ADF28BU, 0xA8B92DB6U, 0xA9C8141EU, + 0x198557F1U, 0x074CAF75U, 0xDDBBEE99U, 0x60FDA37FU, 0x269FF701U, 0xF5BC5C72U, 0x3BC54466U, 0x7E345BFBU, + 0x29768B43U, 0xC6DCCB23U, 0xFC68B6EDU, 0xF163B8E4U, 0xDCCAD731U, 0x85104263U, 0x22401397U, 0x112084C6U, + 0x247D854AU, 0x3DF8D2BBU, 0x3211AEF9U, 0xA16DC729U, 0x2F4B1D9EU, 0x30F3DCB2U, 0x52EC0D86U, 0xE3D077C1U, + 0x166C2BB3U, 0xB999A970U, 0x48FA1194U, 0x642247E9U, 0x8CC4A8FCU, 0x3F1AA0F0U, 0x2CD8567DU, 0x90EF2233U, + 0x4EC78749U, 0xD1C1D938U, 0xA2FE8CCAU, 0x0B3698D4U, 0x81CFA6F5U, 0xDE28A57AU, 0x8E26DAB7U, 0xBFA43FADU, + 0x9DE42C3AU, 0x920D5078U, 0xCC9B6A5FU, 0x4662547EU, 0x13C2F68DU, 0xB8E890D8U, 0xF75E2E39U, 0xAFF582C3U, + 0x80BE9F5DU, 0x937C69D0U, 0x2DA96FD5U, 0x12B3CF25U, 0x993BC8ACU, 0x7DA71018U, 0x636EE89CU, 0xBB7BDB3BU, + 0x7809CD26U, 0x18F46E59U, 0xB701EC9AU, 0x9AA8834FU, 0x6E65E695U, 0xE67EAAFFU, 0xCF0821BCU, 0xE8E6EF15U, + 0x9BD9BAE7U, 0x36CE4A6FU, 0x09D4EA9FU, 0x7CD629B0U, 0xB2AF31A4U, 0x23312A3FU, 0x9430C6A5U, 0x66C035A2U, + 0xBC37744EU, 0xCAA6FC82U, 0xD0B0E090U, 0xD81533A7U, 0x984AF104U, 0xDAF741ECU, 0x500E7FCDU, 0xF62F1791U, + 0xD68D764DU, 0xB04D43EFU, 0x4D54CCAAU, 0x04DFE496U, 0xB5E39ED1U, 0x881B4C6AU, 0x1FB8C12CU, 0x517F4665U, + 0xEA049D5EU, 0x355D018CU, 0x7473FA87U, 0x412EFB0BU, 0x1D5AB367U, 0xD25292DBU, 0x5633E910U, 0x47136DD6U, + 0x618C9AD7U, 0x0C7A37A1U, 0x148E59F8U, 0x3C89EB13U, 0x27EECEA9U, 0xC935B761U, 0xE5EDE11CU, 0xB13C7A47U, + 0xDF599CD2U, 0x733F55F2U, 0xCE791814U, 0x37BF73C7U, 0xCDEA53F7U, 0xAA5B5FFDU, 0x6F14DF3DU, 0xDB867844U, + 0xF381CAAFU, 0xC43EB968U, 0x342C3824U, 0x405FC2A3U, 0xC372161DU, 0x250CBCE2U, 0x498B283CU, 0x9541FF0DU, + 0x017139A8U, 0xB3DE080CU, 0xE49CD8B4U, 0xC1906456U, 0x84617BCBU, 0xB670D532U, 0x5C74486CU, 0x5742D0B8U +}; + +const uint32_t AES::Td3[256] = { + 0xF4A75051U, 0x4165537EU, 0x17A4C31AU, 0x275E963AU, 0xAB6BCB3BU, 0x9D45F11FU, 0xFA58ABACU, 0xE303934BU, + 0x30FA5520U, 0x766DF6ADU, 0xCC769188U, 0x024C25F5U, 0xE5D7FC4FU, 0x2ACBD7C5U, 0x35448026U, 0x62A38FB5U, + 0xB15A49DEU, 0xBA1B6725U, 0xEA0E9845U, 0xFEC0E15DU, 0x2F7502C3U, 0x4CF01281U, 0x4697A38DU, 0xD3F9C66BU, + 0x8F5FE703U, 0x929C9515U, 0x6D7AEBBFU, 0x5259DA95U, 0xBE832DD4U, 0x7421D358U, 0xE0692949U, 0xC9C8448EU, + 0xC2896A75U, 0x8E7978F4U, 0x583E6B99U, 0xB971DD27U, 0xE14FB6BEU, 0x88AD17F0U, 0x20AC66C9U, 0xCE3AB47DU, + 0xDF4A1863U, 0x1A3182E5U, 0x51336097U, 0x537F4562U, 0x6477E0B1U, 0x6BAE84BBU, 0x81A01CFEU, 0x082B94F9U, + 0x48685870U, 0x45FD198FU, 0xDE6C8794U, 0x7BF8B752U, 0x73D323ABU, 0x4B02E272U, 0x1F8F57E3U, 0x55AB2A66U, + 0xEB2807B2U, 0xB5C2032FU, 0xC57B9A86U, 0x3708A5D3U, 0x2887F230U, 0xBFA5B223U, 0x036ABA02U, 0x16825CEDU, + 0xCF1C2B8AU, 0x79B492A7U, 0x07F2F0F3U, 0x69E2A14EU, 0xDAF4CD65U, 0x05BED506U, 0x34621FD1U, 0xA6FE8AC4U, + 0x2E539D34U, 0xF355A0A2U, 0x8AE13205U, 0xF6EB75A4U, 0x83EC390BU, 0x60EFAA40U, 0x719F065EU, 0x6E1051BDU, + 0x218AF93EU, 0xDD063D96U, 0x3E05AEDDU, 0xE6BD464DU, 0x548DB591U, 0xC45D0571U, 0x06D46F04U, 0x5015FF60U, + 0x98FB2419U, 0xBDE997D6U, 0x4043CC89U, 0xD99E7767U, 0xE842BDB0U, 0x898B8807U, 0x195B38E7U, 0xC8EEDB79U, + 0x7C0A47A1U, 0x420FE97CU, 0x841EC9F8U, 0x00000000U, 0x80868309U, 0x2BED4832U, 0x1170AC1EU, 0x5A724E6CU, + 0x0EFFFBFDU, 0x8538560FU, 0xAED51E3DU, 0x2D392736U, 0x0FD9640AU, 0x5CA62168U, 0x5B54D19BU, 0x362E3A24U, + 0x0A67B10CU, 0x57E70F93U, 0xEE96D2B4U, 0x9B919E1BU, 0xC0C54F80U, 0xDC20A261U, 0x774B695AU, 0x121A161CU, + 0x93BA0AE2U, 0xA02AE5C0U, 0x22E0433CU, 0x1B171D12U, 0x090D0B0EU, 0x8BC7ADF2U, 0xB6A8B92DU, 0x1EA9C814U, + 0xF1198557U, 0x75074CAFU, 0x99DDBBEEU, 0x7F60FDA3U, 0x01269FF7U, 0x72F5BC5CU, 0x663BC544U, 0xFB7E345BU, + 0x4329768BU, 0x23C6DCCBU, 0xEDFC68B6U, 0xE4F163B8U, 0x31DCCAD7U, 0x63851042U, 0x97224013U, 0xC6112084U, + 0x4A247D85U, 0xBB3DF8D2U, 0xF93211AEU, 0x29A16DC7U, 0x9E2F4B1DU, 0xB230F3DCU, 0x8652EC0DU, 0xC1E3D077U, + 0xB3166C2BU, 0x70B999A9U, 0x9448FA11U, 0xE9642247U, 0xFC8CC4A8U, 0xF03F1AA0U, 0x7D2CD856U, 0x3390EF22U, + 0x494EC787U, 0x38D1C1D9U, 0xCAA2FE8CU, 0xD40B3698U, 0xF581CFA6U, 0x7ADE28A5U, 0xB78E26DAU, 0xADBFA43FU, + 0x3A9DE42CU, 0x78920D50U, 0x5FCC9B6AU, 0x7E466254U, 0x8D13C2F6U, 0xD8B8E890U, 0x39F75E2EU, 0xC3AFF582U, + 0x5D80BE9FU, 0xD0937C69U, 0xD52DA96FU, 0x2512B3CFU, 0xAC993BC8U, 0x187DA710U, 0x9C636EE8U, 0x3BBB7BDBU, + 0x267809CDU, 0x5918F46EU, 0x9AB701ECU, 0x4F9AA883U, 0x956E65E6U, 0xFFE67EAAU, 0xBCCF0821U, 0x15E8E6EFU, + 0xE79BD9BAU, 0x6F36CE4AU, 0x9F09D4EAU, 0xB07CD629U, 0xA4B2AF31U, 0x3F23312AU, 0xA59430C6U, 0xA266C035U, + 0x4EBC3774U, 0x82CAA6FCU, 0x90D0B0E0U, 0xA7D81533U, 0x04984AF1U, 0xECDAF741U, 0xCD500E7FU, 0x91F62F17U, + 0x4DD68D76U, 0xEFB04D43U, 0xAA4D54CCU, 0x9604DFE4U, 0xD1B5E39EU, 0x6A881B4CU, 0x2C1FB8C1U, 0x65517F46U, + 0x5EEA049DU, 0x8C355D01U, 0x877473FAU, 0x0B412EFBU, 0x671D5AB3U, 0xDBD25292U, 0x105633E9U, 0xD647136DU, + 0xD7618C9AU, 0xA10C7A37U, 0xF8148E59U, 0x133C89EBU, 0xA927EECEU, 0x61C935B7U, 0x1CE5EDE1U, 0x47B13C7AU, + 0xD2DF599CU, 0xF2733F55U, 0x14CE7918U, 0xC737BF73U, 0xF7CDEA53U, 0xFDAA5B5FU, 0x3D6F14DFU, 0x44DB8678U, + 0xAFF381CAU, 0x68C43EB9U, 0x24342C38U, 0xA3405FC2U, 0x1DC37216U, 0xE2250CBCU, 0x3C498B28U, 0x0D9541FFU, + 0xA8017139U, 0x0CB3DE08U, 0xB4E49CD8U, 0x56C19064U, 0xCB84617BU, 0x32B670D5U, 0x6C5C7448U, 0xB85742D0U +}; + +const uint32_t AES::Td4[256] = { + 0x52525252U, 0x09090909U, 0x6A6A6A6AU, 0xD5D5D5D5U, 0x30303030U, 0x36363636U, 0xA5A5A5A5U, 0x38383838U, + 0xBFBFBFBFU, 0x40404040U, 0xA3A3A3A3U, 0x9E9E9E9EU, 0x81818181U, 0xF3F3F3F3U, 0xD7D7D7D7U, 0xFBFBFBFBU, + 0x7C7C7C7CU, 0xE3E3E3E3U, 0x39393939U, 0x82828282U, 0x9B9B9B9BU, 0x2F2F2F2FU, 0xFFFFFFFFU, 0x87878787U, + 0x34343434U, 0x8E8E8E8EU, 0x43434343U, 0x44444444U, 0xC4C4C4C4U, 0xDEDEDEDEU, 0xE9E9E9E9U, 0xCBCBCBCBU, + 0x54545454U, 0x7B7B7B7BU, 0x94949494U, 0x32323232U, 0xA6A6A6A6U, 0xC2C2C2C2U, 0x23232323U, 0x3D3D3D3DU, + 0xEEEEEEEEU, 0x4C4C4C4CU, 0x95959595U, 0x0B0B0B0BU, 0x42424242U, 0xFAFAFAFAU, 0xC3C3C3C3U, 0x4E4E4E4EU, + 0x08080808U, 0x2E2E2E2EU, 0xA1A1A1A1U, 0x66666666U, 0x28282828U, 0xD9D9D9D9U, 0x24242424U, 0xB2B2B2B2U, + 0x76767676U, 0x5B5B5B5BU, 0xA2A2A2A2U, 0x49494949U, 0x6D6D6D6DU, 0x8B8B8B8BU, 0xD1D1D1D1U, 0x25252525U, + 0x72727272U, 0xF8F8F8F8U, 0xF6F6F6F6U, 0x64646464U, 0x86868686U, 0x68686868U, 0x98989898U, 0x16161616U, + 0xD4D4D4D4U, 0xA4A4A4A4U, 0x5C5C5C5CU, 0xCCCCCCCCU, 0x5D5D5D5DU, 0x65656565U, 0xB6B6B6B6U, 0x92929292U, + 0x6C6C6C6CU, 0x70707070U, 0x48484848U, 0x50505050U, 0xFDFDFDFDU, 0xEDEDEDEDU, 0xB9B9B9B9U, 0xDADADADAU, + 0x5E5E5E5EU, 0x15151515U, 0x46464646U, 0x57575757U, 0xA7A7A7A7U, 0x8D8D8D8DU, 0x9D9D9D9DU, 0x84848484U, + 0x90909090U, 0xD8D8D8D8U, 0xABABABABU, 0x00000000U, 0x8C8C8C8CU, 0xBCBCBCBCU, 0xD3D3D3D3U, 0x0A0A0A0AU, + 0xF7F7F7F7U, 0xE4E4E4E4U, 0x58585858U, 0x05050505U, 0xB8B8B8B8U, 0xB3B3B3B3U, 0x45454545U, 0x06060606U, + 0xD0D0D0D0U, 0x2C2C2C2CU, 0x1E1E1E1EU, 0x8F8F8F8FU, 0xCACACACAU, 0x3F3F3F3FU, 0x0F0F0F0FU, 0x02020202U, + 0xC1C1C1C1U, 0xAFAFAFAFU, 0xBDBDBDBDU, 0x03030303U, 0x01010101U, 0x13131313U, 0x8A8A8A8AU, 0x6B6B6B6BU, + 0x3A3A3A3AU, 0x91919191U, 0x11111111U, 0x41414141U, 0x4F4F4F4FU, 0x67676767U, 0xDCDCDCDCU, 0xEAEAEAEAU, + 0x97979797U, 0xF2F2F2F2U, 0xCFCFCFCFU, 0xCECECECEU, 0xF0F0F0F0U, 0xB4B4B4B4U, 0xE6E6E6E6U, 0x73737373U, + 0x96969696U, 0xACACACACU, 0x74747474U, 0x22222222U, 0xE7E7E7E7U, 0xADADADADU, 0x35353535U, 0x85858585U, + 0xE2E2E2E2U, 0xF9F9F9F9U, 0x37373737U, 0xE8E8E8E8U, 0x1C1C1C1CU, 0x75757575U, 0xDFDFDFDFU, 0x6E6E6E6EU, + 0x47474747U, 0xF1F1F1F1U, 0x1A1A1A1AU, 0x71717171U, 0x1D1D1D1DU, 0x29292929U, 0xC5C5C5C5U, 0x89898989U, + 0x6F6F6F6FU, 0xB7B7B7B7U, 0x62626262U, 0x0E0E0E0EU, 0xAAAAAAAAU, 0x18181818U, 0xBEBEBEBEU, 0x1B1B1B1BU, + 0xFCFCFCFCU, 0x56565656U, 0x3E3E3E3EU, 0x4B4B4B4BU, 0xC6C6C6C6U, 0xD2D2D2D2U, 0x79797979U, 0x20202020U, + 0x9A9A9A9AU, 0xDBDBDBDBU, 0xC0C0C0C0U, 0xFEFEFEFEU, 0x78787878U, 0xCDCDCDCDU, 0x5A5A5A5AU, 0xF4F4F4F4U, + 0x1F1F1F1FU, 0xDDDDDDDDU, 0xA8A8A8A8U, 0x33333333U, 0x88888888U, 0x07070707U, 0xC7C7C7C7U, 0x31313131U, + 0xB1B1B1B1U, 0x12121212U, 0x10101010U, 0x59595959U, 0x27272727U, 0x80808080U, 0xECECECECU, 0x5F5F5F5FU, + 0x60606060U, 0x51515151U, 0x7F7F7F7FU, 0xA9A9A9A9U, 0x19191919U, 0xB5B5B5B5U, 0x4A4A4A4AU, 0x0D0D0D0DU, + 0x2D2D2D2DU, 0xE5E5E5E5U, 0x7A7A7A7AU, 0x9F9F9F9FU, 0x93939393U, 0xC9C9C9C9U, 0x9C9C9C9CU, 0xEFEFEFEFU, + 0xA0A0A0A0U, 0xE0E0E0E0U, 0x3B3B3B3BU, 0x4D4D4D4DU, 0xAEAEAEAEU, 0x2A2A2A2AU, 0xF5F5F5F5U, 0xB0B0B0B0U, + 0xC8C8C8C8U, 0xEBEBEBEBU, 0xBBBBBBBBU, 0x3C3C3C3CU, 0x83838383U, 0x53535353U, 0x99999999U, 0x61616161U, + 0x17171717U, 0x2B2B2B2BU, 0x04040404U, 0x7E7E7E7EU, 0xBABABABAU, 0x77777777U, 0xD6D6D6D6U, 0x26262626U, + 0xE1E1E1E1U, 0x69696969U, 0x14141414U, 0x63636363U, 0x55555555U, 0x21212121U, 0x0C0C0C0CU, 0x7D7D7D7DU +}; + +const uint32_t AES::rcon[11] = { + 0x00000000U, 0x01000000U, 0x02000000U, 0x04000000U, 0x08000000U, 0x10000000U, 0x20000000U, 0x40000000U, + 0x80000000U, 0x1B000000U, 0x36000000U +}; + +AES::AES() +{ + Nb = 4; + Nk = 4; + Nr = 10; + + std::fill_n(_iv, 16, 0); + std::fill_n(_erk, 60, 0); + std::fill_n(_drk, 60, 0); + + _tp = 0; +} + +AES::~AES() +{ + CleanUp(); +} + +void AES::CleanUp() +{ + std::fill_n(_iv, 16, 0); + std::fill_n(_erk, 60, 0); + std::fill_n(_drk, 60, 0); + _tp = 0; +} + +void AES::SetKey(const uint8_t *key, Mode mode) +{ + int i; + uint32_t tmp; + int ks_sz; + + switch (mode) { + case AES_128: Nk = 4; Nr = 10; break; + case AES_192: Nk = 6; Nr = 12; break; + case AES_256: Nk = 8; Nr = 14; break; + } + + ks_sz = Nb * (Nr + 1); + + for (i = 0; i < Nk; i++) { + _erk[i] = (key[4*i] << 24) | (key[4*i+1] << 16) | (key[4*i+2] << 8) | key[4*i+3]; + } + + for (; i < ks_sz; i++) { + tmp = _erk[i-1]; + if (i % Nk == 0) { + tmp = (Te4[(tmp >> 16) & 0xFF] & 0xFF000000) ^ (Te4[(tmp >> 8) & 0xFF] & 0xFF0000) ^ (Te4[tmp & 0xFF] & 0xFF00) ^ (Te4[(tmp >> 24) & 0xFF] & 0xFF) ^ rcon[i / Nk]; + } else if ((Nk > 6) && ((i % Nk) == 4)) { + tmp = (Te4[(tmp >> 24) & 0xFF] & 0xFF000000) ^ (Te4[(tmp >> 16) & 0xFF] & 0xFF0000) ^ (Te4[(tmp >> 8) & 0xFF] & 0xFF00) ^ (Te4[tmp & 0xFF] & 0xFF); + } + _erk[i] = _erk[i-Nk] ^ tmp; + } + + for (i = 0; i < ks_sz; i += 4) { + _drk[i ] = _erk[ks_sz - i - 4]; + _drk[i + 1] = _erk[ks_sz - i - 3]; + _drk[i + 2] = _erk[ks_sz - i - 2]; + _drk[i + 3] = _erk[ks_sz - i - 1]; + } + + for (i = 4; i < (ks_sz - 4); i++) { + _drk[i] = Td0[Te4[(_drk[i] >> 24)] & 0xFF] ^ Td1[Te4[(_drk[i] >> 16) & 0xFF] & 0xFF] ^ Td2[Te4[(_drk[i] >> 8) & 0xFF] & 0xFF] ^ Td3[Te4[_drk[i] & 0xFF] & 0xFF]; + } + + SetIV(0); +} + +void AES::Encrypt(const uint8_t *src, uint8_t *dst) +{ + uint32_t s0, s1, s2, s3, t0, t1, t2, t3; + int r, ki; + + s0 = (src[0] << 24 | src[1] << 16 | src[2] << 8 | src[3]) ^ _erk[0]; + s1 = (src[4] << 24 | src[5] << 16 | src[6] << 8 | src[7]) ^ _erk[1]; + s2 = (src[8] << 24 | src[9] << 16 | src[10] << 8 | src[11]) ^ _erk[2]; + s3 = (src[12] << 24 | src[13] << 16 | src[14] << 8 | src[15]) ^ _erk[3]; + + ki = 4; + r = Nr >> 1; + + for (;;) { + t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xFF] ^ Te2[(s2 >> 8) & 0xFF] ^ Te3[s3 & 0xFF] ^ _erk[ki++]; + t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xFF] ^ Te2[(s3 >> 8) & 0xFF] ^ Te3[s0 & 0xFF] ^ _erk[ki++]; + t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xFF] ^ Te2[(s0 >> 8) & 0xFF] ^ Te3[s1 & 0xFF] ^ _erk[ki++]; + t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xFF] ^ Te2[(s1 >> 8) & 0xFF] ^ Te3[s2 & 0xFF] ^ _erk[ki++]; + if (--r == 0) break; + s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xFF] ^ Te2[(t2 >> 8) & 0xFF] ^ Te3[t3 & 0xFF] ^ _erk[ki++]; + s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xFF] ^ Te2[(t3 >> 8) & 0xFF] ^ Te3[t0 & 0xFF] ^ _erk[ki++]; + s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xFF] ^ Te2[(t0 >> 8) & 0xFF] ^ Te3[t1 & 0xFF] ^ _erk[ki++]; + s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xFF] ^ Te2[(t1 >> 8) & 0xFF] ^ Te3[t2 & 0xFF] ^ _erk[ki++]; + } + + s0 = (Te4[t0 >> 24] & 0xFF000000) ^ (Te4[(t1 >> 16) & 0xFF] & 0xFF0000) ^ (Te4[(t2 >> 8) & 0xFF] & 0xFF00) ^ (Te4[t3 & 0xFF] & 0xFF) ^ _erk[ki++]; + s1 = (Te4[t1 >> 24] & 0xFF000000) ^ (Te4[(t2 >> 16) & 0xFF] & 0xFF0000) ^ (Te4[(t3 >> 8) & 0xFF] & 0xFF00) ^ (Te4[t0 & 0xFF] & 0xFF) ^ _erk[ki++]; + s2 = (Te4[t2 >> 24] & 0xFF000000) ^ (Te4[(t3 >> 16) & 0xFF] & 0xFF0000) ^ (Te4[(t0 >> 8) & 0xFF] & 0xFF00) ^ (Te4[t1 & 0xFF] & 0xFF) ^ _erk[ki++]; + s3 = (Te4[t3 >> 24] & 0xFF000000) ^ (Te4[(t0 >> 16) & 0xFF] & 0xFF0000) ^ (Te4[(t1 >> 8) & 0xFF] & 0xFF00) ^ (Te4[t2 & 0xFF] & 0xFF) ^ _erk[ki++]; + + dst[0] = s0 >> 24; dst[1] = s0 >> 16; dst[2] = s0 >> 8; dst[3] = s0; + dst[4] = s1 >> 24; dst[5] = s1 >> 16; dst[6] = s1 >> 8; dst[7] = s1; + dst[8] = s2 >> 24; dst[9] = s2 >> 16; dst[10] = s2 >> 8; dst[11] = s2; + dst[12] = s3 >> 24; dst[13] = s3 >> 16; dst[14] = s3 >> 8; dst[15] = s3; +} + +void AES::Decrypt(const uint8_t *src, uint8_t *dst) +{ + uint32_t s0, s1, s2, s3, t0, t1, t2, t3; + int r, ki; + + s0 = (src[0] << 24 | src[1] << 16 | src[2] << 8 | src[3]) ^ _drk[0]; + s1 = (src[4] << 24 | src[5] << 16 | src[6] << 8 | src[7]) ^ _drk[1]; + s2 = (src[8] << 24 | src[9] << 16 | src[10] << 8 | src[11]) ^ _drk[2]; + s3 = (src[12] << 24 | src[13] << 16 | src[14] << 8 | src[15]) ^ _drk[3]; + + ki = 4; + r = Nr >> 1; + + for (;;) { + t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xFF] ^ Td2[(s2 >> 8) & 0xFF] ^ Td3[s1 & 0xFF] ^ _drk[ki++]; + t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xFF] ^ Td2[(s3 >> 8) & 0xFF] ^ Td3[s2 & 0xFF] ^ _drk[ki++]; + t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xFF] ^ Td2[(s0 >> 8) & 0xFF] ^ Td3[s3 & 0xFF] ^ _drk[ki++]; + t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xFF] ^ Td2[(s1 >> 8) & 0xFF] ^ Td3[s0 & 0xFF] ^ _drk[ki++]; + if (--r == 0) break; + s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xFF] ^ Td2[(t2 >> 8) & 0xFF] ^ Td3[t1 & 0xFF] ^ _drk[ki++]; + s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xFF] ^ Td2[(t3 >> 8) & 0xFF] ^ Td3[t2 & 0xFF] ^ _drk[ki++]; + s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xFF] ^ Td2[(t0 >> 8) & 0xFF] ^ Td3[t3 & 0xFF] ^ _drk[ki++]; + s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xFF] ^ Td2[(t1 >> 8) & 0xFF] ^ Td3[t0 & 0xFF] ^ _drk[ki++]; + } + + s0 = (Td4[t0 >> 24] & 0xFF000000) ^ (Td4[(t3 >> 16) & 0xFF] & 0xFF0000) ^ (Td4[(t2 >> 8) & 0xFF] & 0xFF00) ^ (Td4[t1 & 0xFF] & 0xFF) ^ _drk[ki++]; + s1 = (Td4[t1 >> 24] & 0xFF000000) ^ (Td4[(t0 >> 16) & 0xFF] & 0xFF0000) ^ (Td4[(t3 >> 8) & 0xFF] & 0xFF00) ^ (Td4[t2 & 0xFF] & 0xFF) ^ _drk[ki++]; + s2 = (Td4[t2 >> 24] & 0xFF000000) ^ (Td4[(t1 >> 16) & 0xFF] & 0xFF0000) ^ (Td4[(t0 >> 8) & 0xFF] & 0xFF00) ^ (Td4[t3 & 0xFF] & 0xFF) ^ _drk[ki++]; + s3 = (Td4[t3 >> 24] & 0xFF000000) ^ (Td4[(t2 >> 16) & 0xFF] & 0xFF0000) ^ (Td4[(t1 >> 8) & 0xFF] & 0xFF00) ^ (Td4[t0 & 0xFF] & 0xFF) ^ _drk[ki++]; + + dst[0] = s0 >> 24; dst[1] = s0 >> 16; dst[2] = s0 >> 8; dst[3] = s0; + dst[4] = s1 >> 24; dst[5] = s1 >> 16; dst[6] = s1 >> 8; dst[7] = s1; + dst[8] = s2 >> 24; dst[9] = s2 >> 16; dst[10] = s2 >> 8; dst[11] = s2; + dst[12] = s3 >> 24; dst[13] = s3 >> 16; dst[14] = s3 >> 8; dst[15] = s3; +} + +void AES::SetIV(const uint8_t *iv) +{ + size_t j; + + _tp = 0; + + if (iv == 0) + iv = zeros; + + for (j = 0; j < 16; j++) _iv[j] = iv[j]; +} + +void AES::EncryptCBC(const uint8_t *src, uint8_t *dst, size_t size) +{ + size_t i, j; + uint8_t tmp[16]; + + for (i = 0; i < size; i += 16) { + for (j = 0; j < 16; j++) tmp[j] = src[i+j] ^ _iv[j]; + Encrypt(tmp, &dst[i]); + for (j = 0; j < 16; j++) _iv[j] = dst[i+j]; + } +} + +void AES::DecryptCBC(const uint8_t *src, uint8_t *dst, size_t size) +{ + size_t i, j; + uint8_t tmp[16]; + + for (i = 0; i < size; i += 16) { + for (j = 0; j < 16; j++) tmp[j] = src[i+j]; + Decrypt(&src[i], &dst[i]); + for (j = 0; j < 16; j++) { + dst[i+j] ^= _iv[j]; + _iv[j] = tmp[j]; + } + } +} + +void AES::EncryptCFB(const uint8_t *src, uint8_t *dst, size_t size) +{ + size_t i; + + for (i = 0; i < size; i++, _tp = (_tp + 1) & 15) { + if (_tp == 0) + Encrypt(_iv, _iv); + dst[i] = src[i] ^ _iv[_tp]; + _iv[_tp] = dst[i]; + } +} + +void AES::DecryptCFB(const uint8_t *src, uint8_t *dst, size_t size) +{ + size_t i; + uint8_t t; + + for (i = 0; i < size; i++, _tp = (_tp + 1) & 15) { + if (_tp == 0) + Encrypt(_iv, _iv); + t = src[i]; + dst[i] = src[i] ^ _iv[_tp]; + _iv[_tp] = t; + } +} + +void AES::CryptOFB(const uint8_t *src, uint8_t *dst, size_t size) +{ + size_t i; + + for (i = 0; i < size; i++, _tp = (_tp + 1) & 15) { + if (_tp == 0) + Encrypt(_iv, _iv); + dst[i] = src[i] ^ _iv[_tp]; + } +} + +const uint8_t AES::zeros[32] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; diff --git a/vendor/apfs-fuse/Crypto/Aes.h b/vendor/apfs-fuse/Crypto/Aes.h new file mode 100644 index 0000000..7db71a7 --- /dev/null +++ b/vendor/apfs-fuse/Crypto/Aes.h @@ -0,0 +1,172 @@ +#pragma once + +#include +#include + +/** + * @brief The AES class + * + * Class for AES en- and decryption. Supports all AES modes specified + * in NIST FIPS-197. + * + * Several chaining modes are supported, namely ECB, CBC, CFB and OFB. + * + * Usage: + * + * @li Set a key with SetKey. + * @li If necessary, set an IV with SetIV. + * @li Encrypt / Decrypt your data with the desired functions. + * @li When you are done, CleanUp behind you to make life harder for hackers. + */ +class AES +{ +public: + AES(); + ~AES(); + + /** + * @brief Encryption key size in bits. + */ + enum Mode { + AES_128, + AES_192, + AES_256 + }; + + /** + * @brief Clean Up + * + * Deletes all data which may contain key / plaintext material. + * + * @note After using this, a new key must be set before additional + * encryption is done. + */ + void CleanUp(); + + /** + * @brief Set AES Key + * + * Sets the AES encryption key for this instance. Also sets the IV + * to 0. + * + * @param key Key data. Length depends on the mode parameter. + * @param mode Encryption mode (128, 192 or 256 bit key). + */ + void SetKey(const uint8_t *key, Mode mode); + + /** + * @brief Set Initialization Vector + * + * Set the initialization vector. This also resets the counter for the + * stream cipher modes (CFB, OFB). + * + * @param iv Initialization Vector. If NULL, a 0-vector is used. + */ + void SetIV(const uint8_t *iv); + + /** + * @brief Encrypt Block + * + * Encrypts a block of 16 bytes with the previously selected key (ECB mode). + * + * @param src Plaintext block (128 bits, 16 bytes) + * @param dst Encrypted block (128 bits, 16 bytes) + */ + void Encrypt(const uint8_t *src, uint8_t *dst); + + /** + * @brief Decrypt Block + * + * Decrypts a block of 16 bytes with the previously selected key (ECB mode). + * + * @param src Encrypted block (128 bits, 16 bytes) + * @param dst Decrypted block (128 bits, 16 bytes) + */ + void Decrypt(const uint8_t *src, uint8_t *dst); + + /** + * @brief Encrypt CBC + * + * Encrypt data in CBC mode. + * + * @param src Plaintext data. + * @param dst Encrypted data. + * @param size Number of bytes. Must be a multiple of 16. + */ + void EncryptCBC(const uint8_t *src, uint8_t *dst, size_t size); + + /** + * @brief Decrypt CBC + * + * Decrypt data in CBC mode. + * + * @param src Encrypted data. + * @param dst Decrypted data. + * @param size Number of bytes. Must be a multiple of 16. + */ + void DecryptCBC(const uint8_t *src, uint8_t *dst, size_t size); + + /** + * @brief Encrypt CFB + * + * Encrypt data in CFB mode. + * + * @param src Plaintext data. + * @param dst Encrypted data. + * @param size Number of bytes. + */ + void EncryptCFB(const uint8_t *src, uint8_t *dst, size_t size); + + /** + * @brief Decrypt CFB + * + * Decrypt data in CFB mode. + * + * @param src Encrypted data. + * @param dst Decrypted data. + * @param size Number of bytes. + */ + void DecryptCFB(const uint8_t *src, uint8_t *dst, size_t size); + + /** + * @brief Crypt OFB + * + * En- / Decrypt data in OFB mode. + * + * @param src Input data. + * @param dst Output data. + * @param size Number of bytes. + */ + void CryptOFB(const uint8_t *src, uint8_t *dst, size_t size); + +private: + /// Encryption round key. + uint32_t _erk[60]; + /// Decryption round key. + uint32_t _drk[60]; + /// Initialization vector. + uint8_t _iv[16]; + /// Byte counter for CFB / OFB modes. + int _tp; + + // AES constants (depending on mode) + int Nk; + int Nr; + int Nb; + + // AES tables + static const uint32_t Td0[256]; + static const uint32_t Td1[256]; + static const uint32_t Td2[256]; + static const uint32_t Td3[256]; + static const uint32_t Td4[256]; + static const uint32_t Te0[256]; + static const uint32_t Te1[256]; + static const uint32_t Te2[256]; + static const uint32_t Te3[256]; + static const uint32_t Te4[256]; + static const uint32_t rcon[11]; + + /// Zero data + static const uint8_t zeros[32]; +}; diff --git a/vendor/apfs-fuse/Crypto/AesXts.cpp b/vendor/apfs-fuse/Crypto/AesXts.cpp new file mode 100644 index 0000000..6c0fc2b --- /dev/null +++ b/vendor/apfs-fuse/Crypto/AesXts.cpp @@ -0,0 +1,102 @@ +#include +#include "AesXts.h" + +#include +#include + +AesXts::AesXts() +{ + CleanUp(); +} + +AesXts::~AesXts() +{ +} + +void AesXts::CleanUp() +{ + m_aes_1.CleanUp(); + m_aes_2.CleanUp(); +} + +void AesXts::SetKey(const uint8_t* key1, const uint8_t* key2) +{ + m_aes_1.SetKey(key1, AES::AES_128); + m_aes_2.SetKey(key2, AES::AES_128); +} + +void AesXts::Encrypt(uint8_t* cipher, const uint8_t* plain, std::size_t size, uint64_t unit_no) +{ + uint8_t pp[0x10]; + uint8_t cc[0x10]; + uint64_t tweak[2]; + size_t k = 0; + + tweak[0] = htole64(unit_no); + tweak[1] = 0; + + m_aes_2.Encrypt(reinterpret_cast(tweak), reinterpret_cast(tweak)); + + for (k = 0; k < size; k += 0x10) + { + Xor128(pp, plain + k, tweak); + m_aes_1.Encrypt(pp, cc); + Xor128(cipher + k, cc, tweak); + MultiplyTweak(tweak); + } +} + +void AesXts::Decrypt(uint8_t* plain, const uint8_t* cipher, std::size_t size, uint64_t unit_no) +{ + uint8_t pp[0x10]; + uint8_t cc[0x10]; + uint64_t tweak[2]; + size_t k = 0; + + tweak[0] = htole64(unit_no); + tweak[1] = 0; + + m_aes_2.Encrypt(reinterpret_cast(tweak), reinterpret_cast(tweak)); + + for (k = 0; k < size; k += 0x10) + { + Xor128(cc, cipher + k, tweak); + m_aes_1.Decrypt(cc, pp); + Xor128(plain + k, pp, tweak); + MultiplyTweak(tweak); + } +} + +void AesXts::Xor128(void *out, const void *op1, const void *op2) +{ + reinterpret_cast(out)[0] = reinterpret_cast(op1)[0] ^ reinterpret_cast(op2)[0]; + reinterpret_cast(out)[1] = reinterpret_cast(op1)[1] ^ reinterpret_cast(op2)[1]; +} + +void AesXts::MultiplyTweak(uint64_t* tweak) +{ + uint8_t c1; + uint8_t c2; + +#if (__BYTE_ORDER == __LITTLE_ENDIAN) + c1 = (tweak[0] & 0x8000000000000000ULL) ? 1 : 0; + c2 = (tweak[1] & 0x8000000000000000ULL) ? 0x87 : 0; + + tweak[0] = (tweak[0] << 1) ^ c2; + tweak[1] = (tweak[1] << 1) | c1; +#else + uint64_t t0; + uint64_t t1; + + t0 = le64toh(tweak[0]); + t1 = le64toh(tweak[1]); + + c1 = (t0 & 0x8000000000000000ULL) ? 1 : 0; + c2 = (t1 & 0x8000000000000000ULL) ? 0x87 : 0; + t0 = (t0 << 1) ^ c2; + t1 = (t1 << 1) | c1; + + tweak[0] = htole64(t0); + tweak[1] = htole64(t1); +#endif +} diff --git a/vendor/apfs-fuse/Crypto/AesXts.h b/vendor/apfs-fuse/Crypto/AesXts.h new file mode 100644 index 0000000..5e09748 --- /dev/null +++ b/vendor/apfs-fuse/Crypto/AesXts.h @@ -0,0 +1,26 @@ +#pragma once + +#include + +#include "Aes.h" + +class AesXts +{ +public: + AesXts(); + ~AesXts(); + + void CleanUp(); + + void SetKey(const uint8_t *key1, const uint8_t *key2); + + void Encrypt(uint8_t *cipher, const uint8_t *plain, size_t size, uint64_t unit_no); + void Decrypt(uint8_t *plain, const uint8_t *cipher, size_t size, uint64_t unit_no); + +private: + void Xor128(void *out, const void *op1, const void *op2); + void MultiplyTweak(uint64_t *tweak); + + AES m_aes_1; + AES m_aes_2; +}; diff --git a/vendor/apfs-fuse/Crypto/Asn1Der.cpp b/vendor/apfs-fuse/Crypto/Asn1Der.cpp new file mode 100644 index 0000000..a66a55e --- /dev/null +++ b/vendor/apfs-fuse/Crypto/Asn1Der.cpp @@ -0,0 +1,184 @@ +#include +#include +#include + +#include "Asn1Der.h" + +const uint8_t* der_decode_tag(der_tag_t& tag, const uint8_t* der, const uint8_t* der_end) +{ + uint64_t t; + uint8_t b; + uint8_t flg; + + tag = 0; + + if (der == nullptr || der >= der_end) + return nullptr; + flg = *der++; + + if ((flg & 0x1F) == 0x1F) { + t = 0; + do { + if (der >= der_end) return nullptr; + b = *der++; + t = t << 7 | (b & 0x7F); + } while (b & 0x80); + tag = static_cast(flg & 0xE0) << 56 | (t & 0x1FFFFFFFFFFFFFFFU); + } + else { + tag = static_cast(flg & 0xE0) << 56 | (flg & 0x1F); + } + return der; +} + +const uint8_t* der_decode_len(size_t& len, const uint8_t* der, const uint8_t* der_end) +{ + uint64_t s; + uint8_t nb; + unsigned k; + + len = 0; + + if (der == nullptr || der >= der_end) + return nullptr; + nb = *der++; + + if (nb & 0x80) { + s = 0; + nb &= 0x7F; + if ((der + nb) >= der_end) return nullptr; + for (k = 0; k < nb; k++) + s = s << 8 | *der++; + len = s; + } + else { + len = nb; + } + return der; +} + +const uint8_t* der_decode_tl(der_tag_t expected_tag, size_t& len, const uint8_t* der, const uint8_t* der_end) +{ + der_tag_t tag; + der = der_decode_tag(tag, der, der_end); + if (tag != expected_tag) + return nullptr; + der = der_decode_len(len, der, der_end); + if (der + len > der_end) + return nullptr; + return der; +} + +const uint8_t* der_decode_constructed_tl(der_tag_t expected_tag, const uint8_t*& body_end, const uint8_t* der, const uint8_t* der_end) +{ + size_t len; + der = der_decode_tl(expected_tag, len, der, der_end); + if (der == nullptr) + return nullptr; + body_end = der + len; + return der; +} + +const uint8_t* der_decode_sequence_tl(const uint8_t*& body_end, const uint8_t* der, const uint8_t* der_end) +{ + return der_decode_constructed_tl(DER_CONSTRUCTED | DER_SEQUENCE, body_end, der, der_end); +} + +const uint8_t* der_decode_uint(size_t n, uint64_t& val, const uint8_t* der, const uint8_t* der_end) +{ + uint64_t v = 0; + size_t k; + + val = 0; + if (der == nullptr || (der + n) > der_end) + return nullptr; + for (k = 0; k < n; k++) + v = v << 8 | *der++; + val = v; + return der; +} + +const uint8_t * der_decode_uint64(der_tag_t expected_tag, uint64_t& val, const uint8_t* der, const uint8_t* der_end) +{ + size_t len; + + val = 0; + der = der_decode_tl(expected_tag, len, der, der_end); + if (der == nullptr || len > 8) return nullptr; + der = der_decode_uint(len, val, der, der_end); + return der; +} + +const uint8_t * der_decode_octet_string_copy(der_tag_t expected_tag, uint8_t* buf, size_t len, const uint8_t* der, const uint8_t* der_end) +{ + size_t slen; + + der = der_decode_tl(expected_tag, slen, der, der_end); + if (der == nullptr || slen != len || (der + slen) > der_end) return nullptr; + memcpy(buf, der, slen); + return der + slen; +} + +/* +static void der_dump_data(const uint8_t* data, size_t len) +{ + size_t y; + size_t x; + constexpr size_t ls = 32; + size_t ll; + + for (y = 0; y < len; y += ls) { + ll = ls; + if ((len - y) < ls) ll = len - y; + for (x = 0; x < (ll - 1); x++) + printf("%02X ", data[y + x]); + printf("%02X\n", data[y + x]); + } +} +*/ + +static void der_dump_hex(const uint8_t* data, size_t len) +{ + size_t n; + + for (n = 0; n < len; n++) + printf("%02X", data[n]); + printf("\n"); +} + +static void der_dump_internal(const uint8_t* der, const uint8_t* der_end, int indent) +{ + der_tag_t tag; + size_t len; + int n; + // const uint8_t* der_start = der; + // printf("DER %zX\n", der_end - der); + + while (der < der_end && der != nullptr) { + // printf("[%04zX", der - der_start); + der = der_decode_tag(tag, der, der_end); + der = der_decode_len(len, der, der_end); + // printf("/%04zX] ", der - der_start); + if (der == nullptr || (der + len) > der_end) break; + for (n = 0; n < indent; n++) + putchar(' '); + printf("%016" PRIX64 " %04zX", tag, len); + if (tag & DER_CONSTRUCTED) { + printf("\n"); + der_dump_internal(der, der + len, indent + 2); + } else { + printf(" : "); + der_dump_hex(der, len); + } + der += len; + } + + if (der == nullptr) + printf("Malformed ASN.1 DER\n"); +} + +void der_dump(const uint8_t* data, size_t size) +{ + // der_dump_data(data, size); + der_dump_internal(data, data + size, 0); +} diff --git a/vendor/apfs-fuse/Crypto/Asn1Der.h b/vendor/apfs-fuse/Crypto/Asn1Der.h new file mode 100644 index 0000000..951bb6a --- /dev/null +++ b/vendor/apfs-fuse/Crypto/Asn1Der.h @@ -0,0 +1,53 @@ +#pragma once + +#include +#include + +typedef uint64_t der_tag_t; + +static constexpr der_tag_t DER_CONSTRUCTED = 0x2000000000000000U; +static constexpr der_tag_t DER_CONTEXT_SPECIFIC = 0x8000000000000000U; + +enum der_tag_type_t +{ + DER_BOOLEAN = 1, + DER_INTEGER = 2, + DER_BIT_STRING = 3, + DER_OCTET_STRING = 4, + DER_NULL = 5, + DER_OBJECT_IDENTIFIER = 6, + DER_OBJECT_DESCRIPTOR = 7, + DER_EXTERNAL = 8, + DER_REAL = 9, + DER_ENUMERATED = 10, + DER_EMBEDDED_PDV = 11, + DER_UTF8String = 12, + DER_RELATIVE_OID = 13, + DER_SEQUENCE = 16, + DER_SET = 17, + DER_NumericString = 18, + DER_PrintableString = 19, + DER_TeletexString = 20, + DER_VideotexString = 21, + DER_IA5String = 22, + DER_UTCTime = 23, + DER_GeneralizedTime = 24, + DER_GraphicString = 25, + DER_VisibleString = 26, + DER_GeneralString = 27, + DER_UniversalString = 28, + DER_CHARACTER_STRING = 29, + DER_BMPString = 30 +}; + +const uint8_t* der_decode_tag(der_tag_t& tag, const uint8_t* der, const uint8_t* der_end); +const uint8_t* der_decode_len(size_t& len, const uint8_t* der, const uint8_t* der_end); +const uint8_t* der_decode_tl(der_tag_t expected_tag, size_t& len, const uint8_t* der, const uint8_t* der_end); +const uint8_t* der_decode_constructed_tl(der_tag_t expected_tag, const uint8_t*& body_end, const uint8_t* der, const uint8_t* der_end); +const uint8_t* der_decode_sequence_tl(const uint8_t*& body_end, const uint8_t* der, const uint8_t* der_end); +const uint8_t* der_decode_uint(size_t n, uint64_t& val, const uint8_t* der, const uint8_t* der_end); + +const uint8_t* der_decode_uint64(der_tag_t expected_tag, uint64_t& val, const uint8_t* der, const uint8_t* der_end); +const uint8_t* der_decode_octet_string_copy(der_tag_t expected_tag, uint8_t* buf, size_t len, const uint8_t* der, const uint8_t* der_end); + +void der_dump(const uint8_t* data, size_t size); diff --git a/vendor/apfs-fuse/Crypto/Crypto.cpp b/vendor/apfs-fuse/Crypto/Crypto.cpp new file mode 100644 index 0000000..c429779 --- /dev/null +++ b/vendor/apfs-fuse/Crypto/Crypto.cpp @@ -0,0 +1,287 @@ +#include "Sha1.h" +#include "Sha256.h" +#include "Crypto.h" +// #include "Util.h" + +#include +#include + +#include +#include + +#include + +union Rfc3394_Unit { + uint64_t u64[2]; + uint8_t u8[16]; +}; + +constexpr uint64_t rfc_3394_default_iv = 0xA6A6A6A6A6A6A6A6ULL; + +// TODO: Not tested on big-endian machines ... +void Rfc3394_KeyWrap(uint8_t *crypto, const uint8_t *plain, size_t size, const uint8_t *key, AES::Mode aes_mode, uint64_t iv) +{ + Rfc3394_Unit u; + uint64_t a; + uint64_t r[6]; + int i; + int j; + int n = size / sizeof(uint64_t); + uint64_t t; + const uint64_t *p = reinterpret_cast(plain); + uint64_t *c = reinterpret_cast(crypto); + AES aes; + + aes.SetKey(key, aes_mode); + a = iv; + t = 1; + + for (i = 0; i < n; i++) + r[i] = p[i]; + + for (j = 0; j < 6; j++) + { + for (i = 0; i < n; i++) + { + u.u64[1] = r[i]; + u.u64[0] = a; + aes.Encrypt(u.u8, u.u8); + a = u.u64[0] ^ bswap_be(t); + r[i] = u.u64[1]; + t++; + } + } + + c[0] = a; + for (i = 0; i < n; i++) + c[i + 1] = r[i]; +} + +// TODO: Not tested on big-endian machines ... +bool Rfc3394_KeyUnwrap(uint8_t *plain, const uint8_t *crypto, size_t size, const uint8_t *key, AES::Mode aes_mode, uint64_t *iv) +{ + Rfc3394_Unit u; + uint64_t a; + uint64_t r[6]; + int i; + int j; + int n = size / sizeof(uint64_t); + uint64_t t; + const uint64_t *c = reinterpret_cast(crypto); + uint64_t *p = reinterpret_cast(plain); + AES aes; + + aes.SetKey(key, aes_mode); + t = 6 * n; + + a = c[0]; + for (i = 0; i < n; i++) + r[i] = c[i + 1]; + + for (j = 5; j >= 0; j--) + { + for (i = n - 1; i >= 0; i--) + { + u.u64[0] = a ^ bswap_be(t); + u.u64[1] = r[i]; + aes.Decrypt(u.u8, u.u8); + a = u.u64[0]; + r[i] = u.u64[1]; + t--; + } + } + + for (i = 0; i < n; i++) + p[i] = r[i]; + if (iv) + *iv = a; + + return a == rfc_3394_default_iv; +} + +void HMAC_SHA1(const uint8_t *key, size_t key_len, const uint8_t *data, size_t data_len, uint8_t *mac) +{ + uint8_t kdata[0x40]; + uint8_t digest[0x14]; + constexpr uint8_t ipad = 0x36; + constexpr uint8_t opad = 0x5C; + Sha1 sha1; + + if (key_len > sizeof(kdata)) + { + sha1.Init(); + sha1.Update(key, key_len); + sha1.Final(digest); + + memcpy(kdata, digest, sizeof(digest)); + key_len = sizeof(digest); + } + else + { + memcpy(kdata, key, key_len); + } + if (key_len < sizeof(kdata)) + memset(kdata + key_len, 0, sizeof(kdata) - key_len); + + for (size_t k = 0; k < sizeof(kdata); k++) + kdata[k] ^= ipad; + + sha1.Init(); + sha1.Update(kdata, sizeof(kdata)); + sha1.Update(data, data_len); + sha1.Final(digest); + + for (size_t k = 0; k < sizeof(kdata); k++) + kdata[k] ^= (ipad ^ opad); + + sha1.Init(); + sha1.Update(kdata, sizeof(kdata)); + sha1.Update(digest, sizeof(digest)); + sha1.Final(mac); + + memset(digest, 0, sizeof(digest)); +} + +void HMAC_SHA256(const uint8_t *key, size_t key_len, const uint8_t *data, size_t data_len, uint8_t *mac) +{ + uint8_t kdata[0x40]; + uint8_t digest[0x20]; + constexpr uint8_t ipad = 0x36; + constexpr uint8_t opad = 0x5C; + SHA256 sha256; + + if (key_len > sizeof(kdata)) + { + sha256.Init(); + sha256.Update(key, key_len); + sha256.Final(digest); + + memcpy(kdata, digest, sizeof(digest)); + key_len = sizeof(digest); + } + else + { + memcpy(kdata, key, key_len); + } + if (key_len < sizeof(kdata)) + memset(kdata + key_len, 0, sizeof(kdata) - key_len); + + for (size_t k = 0; k < sizeof(kdata); k++) + kdata[k] ^= ipad; + + sha256.Init(); + sha256.Update(kdata, sizeof(kdata)); + sha256.Update(data, data_len); + sha256.Final(digest); + + for (size_t k = 0; k < sizeof(kdata); k++) + kdata[k] ^= (ipad ^ opad); + + sha256.Init(); + sha256.Update(kdata, sizeof(kdata)); + sha256.Update(digest, sizeof(digest)); + sha256.Final(mac); + + memset(digest, 0, sizeof(digest)); +} + +void PBKDF2_HMAC_SHA1(const uint8_t* pw, size_t pw_len, const uint8_t* salt, size_t salt_len, int iterations, uint8_t* derived_key, size_t dk_len) +{ + assert(salt_len <= 0x20); + assert(dk_len <= 0x20); + + constexpr size_t h_len = 0x14; + size_t r; + size_t l; + uint8_t t[h_len]; + uint8_t u[h_len]; + uint8_t s[0x24]; + size_t k; + int j; + uint32_t i; + size_t n; + + r = dk_len % h_len; + l = dk_len / h_len; + if (r > 0) l++; + + for (i = 1, k = 0; k < dk_len; i++, k += h_len) + { + // F(P,S,c,i) + + memcpy(s, salt, salt_len); + s[salt_len + 0] = (i >> 24) & 0xFF; + s[salt_len + 1] = (i >> 16) & 0xFF; + s[salt_len + 2] = (i >> 8) & 0xFF; + s[salt_len + 3] = i & 0xFF; + + HMAC_SHA1(pw, pw_len, s, salt_len + 4, u); + memcpy(t, u, sizeof(t)); + + for (j = 1; j < iterations; j++) + { + HMAC_SHA1(pw, pw_len, u, sizeof(u), u); + for (n = 0; n < h_len; n++) + t[n] ^= u[n]; + } + + for (n = 0; n < h_len && (n + k) < dk_len; n++) + derived_key[n + k] = t[n]; + } +} + +void PBKDF2_HMAC_SHA256(const uint8_t* pw, size_t pw_len, const uint8_t* salt, size_t salt_len, int iterations, uint8_t* derived_key, size_t dk_len) +{ + // HMAC_SHA256(pw, key_len, salt, salt_len, mac) + + // l = ceil(dkLen / hLen); // No of blocks + // r = dkLen - (l - 1) * hLen; // Octets in last block + // T[k] = F(P, S, c, k + 1); // k = 1 .. iterations + + // F(P, S, c, k) = U_1 ^ U_2 ^ ... U_c + // U_1 = PRF(P, S || INT(i)) + // U_2 = PRF(P, U_1) + // U_c = PRF(P, U_{c-1}) + + assert(salt_len <= 0x10); + assert(dk_len <= 0x20); + + constexpr size_t h_len = 0x20; + size_t r; + size_t l; + uint8_t t[h_len]; + uint8_t u[h_len]; + uint8_t s[0x14]; + size_t k; + int j; + uint32_t i; + size_t n; + + r = dk_len % h_len; + l = dk_len / h_len; + if (r > 0) l++; + + for (i = 1, k = 0; k < dk_len; i++, k += h_len) + { + // F(P,S,c,i) + + memcpy(s, salt, salt_len); + s[salt_len + 0] = (i >> 24) & 0xFF; + s[salt_len + 1] = (i >> 16) & 0xFF; + s[salt_len + 2] = (i >> 8) & 0xFF; + s[salt_len + 3] = i & 0xFF; + + HMAC_SHA256(pw, pw_len, s, salt_len + 4, u); + memcpy(t, u, sizeof(t)); + + for (j = 1; j < iterations; j++) + { + HMAC_SHA256(pw, pw_len, u, sizeof(u), u); + for (n = 0; n < h_len; n++) + t[n] ^= u[n]; + } + + for (n = 0; n < h_len && (n + k) < dk_len; n++) + derived_key[n + k] = t[n]; + } +} diff --git a/vendor/apfs-fuse/Crypto/Crypto.h b/vendor/apfs-fuse/Crypto/Crypto.h new file mode 100644 index 0000000..ed166b4 --- /dev/null +++ b/vendor/apfs-fuse/Crypto/Crypto.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +#include "Aes.h" + +void Rfc3394_KeyWrap(uint8_t *crypto, const uint8_t *plain, size_t size, const uint8_t *key, AES::Mode aes_mode, uint64_t iv); +bool Rfc3394_KeyUnwrap(uint8_t *plain, const uint8_t *crypto, size_t size, const uint8_t *key, AES::Mode aes_mode, uint64_t *iv); +void HMAC_SHA1(const uint8_t *key, size_t key_len, const uint8_t *data, size_t data_len, uint8_t *mac); +void HMAC_SHA256(const uint8_t *key, size_t key_len, const uint8_t *data, size_t data_len, uint8_t *mac); +void PBKDF2_HMAC_SHA1(const uint8_t* pw, size_t pw_len, const uint8_t* salt, size_t salt_len, int iterations, uint8_t* derived_key, size_t dk_len); +void PBKDF2_HMAC_SHA256(const uint8_t* pw, size_t pw_len, const uint8_t* salt, size_t salt_len, int iterations, uint8_t* derived_key, size_t dk_len); diff --git a/vendor/apfs-fuse/Crypto/Des.cpp b/vendor/apfs-fuse/Crypto/Des.cpp new file mode 100644 index 0000000..ed08b65 --- /dev/null +++ b/vendor/apfs-fuse/Crypto/Des.cpp @@ -0,0 +1,357 @@ +#include +#include +#include + +#include "Des.h" + +#define rol28(v,n) ((v << n) & 0xFFFFFFF) | ((v >> (28-n)) & 0xFFFFFFF) +#define ror28(v,n) ((v >> n) & 0xFFFFFFF) | ((v << (28-n)) & 0xFFFFFFF) + +DES::DES() +{ + memset(m_keySchedule, 0, sizeof(m_keySchedule)); + m_initVector = 0; +} + +DES::~DES() +{ + memset(m_keySchedule, 0, sizeof(m_keySchedule)); + m_initVector = 0; +} + +void DES::Encrypt(uint8_t *cipher, const uint8_t *plain, size_t size) +{ + size_t off; + uint64_t r; + + for (off = 0; off < size; off += 8) + { + r = BytesToU64(plain + off); + r = InitialPermutation(r); + r = EncryptInternal(r, m_keySchedule); + r = FinalPermutation(r); + U64ToBytes(cipher + off, r); + } +} + +void DES::Decrypt(uint8_t *plain, const uint8_t *cipher, size_t size) +{ + size_t off; + uint64_t r; + + for (off = 0; off < size; off += 8) + { + r = BytesToU64(cipher + off); + r = InitialPermutation(r); + r = DecryptInternal(r, m_keySchedule); + r = FinalPermutation(r); + U64ToBytes(plain + off, r); + } +} + +void DES::EncryptCBC(uint8_t *cipher, const uint8_t *plain, size_t size) +{ + size_t off; + uint64_t r; + + for (off = 0; off < size; off += 8) + { + r = BytesToU64(plain + off); + r = r ^ m_initVector; + r = InitialPermutation(r); + r = EncryptInternal(r, m_keySchedule); + r = FinalPermutation(r); + m_initVector = r; + U64ToBytes(cipher + off, r); + } +} + +void DES::DecryptCBC(uint8_t *plain, const uint8_t *cipher, size_t size) +{ + size_t off; + uint64_t r; + uint64_t iv; + + for (off = 0; off < size; off += 8) + { + r = BytesToU64(cipher + off); + iv = r; + r = InitialPermutation(r); + r = DecryptInternal(r, m_keySchedule); + r = FinalPermutation(r); + r = r ^ m_initVector; + m_initVector = iv; + U64ToBytes(plain + off, r); + } +} + +void DES::SetKey(const uint8_t *key) +{ + memset(m_keySchedule, 0, sizeof(m_keySchedule)); + m_initVector = 0; + uint64_t k = BytesToU64(key); + KeySchedule(k, m_keySchedule); +} + +void DES::SetIV(const uint8_t *iv) +{ + if (iv) + m_initVector = BytesToU64(iv); + else + m_initVector = 0; +} + +void DES::PrintBits(uint64_t v, int bits) +{ + int i; + + for (i = bits - 1; i >= 0; i--) + { + if ((v >> i) & 1) + printf("1"); + else + printf("0"); + if ((i & 7) == 0) + printf(" "); + } + printf("\n"); +} + +uint64_t DES::Permute(const uint8_t *box, uint64_t v) +{ + int i; + uint64_t r; + + r = 0; + for (i = 0; i < 64; i++) { + r = (r << 1) | ((v >> (64 - box[i])) & 1); + } + + return r; +} + +uint64_t DES::InitialPermutation(uint64_t v) +{ + return Permute(m_ip_box, v); +} + +uint64_t DES::FinalPermutation(uint64_t v) +{ + return Permute(m_fp_box, v); +} + +uint32_t DES::Function(uint32_t r, uint64_t ks) +{ + int i; + uint64_t e; + uint32_t v; + uint32_t f; + uint8_t s[8]; + + e = 0; + for (i = 0; i < 48; i++) + e = (e << 1) | ((r >> (32 - m_e_box[i])) & 1); + + e = e ^ ks; + + for (i = 0; i < 8; i++) + s[i] = (e >> (42 - 6 * i)) & 0x3F; + + v = (m_s1[s[0]] << 28) | (m_s2[s[1]] << 24) | (m_s3[s[2]] << 20) | (m_s4[s[3]] << 16) | (m_s5[s[4]] << 12) | (m_s6[s[5]] << 8) | (m_s7[s[6]] << 4) | m_s8[s[7]]; + + f = 0; + for (i = 0; i < 32; i++) + f = (f << 1) | ((v >> (32 - m_p_box[i])) & 1); + + return f; +} + +void DES::KeySchedule(uint64_t key, uint64_t *ks) +{ + uint32_t c, d; + uint64_t t; + uint64_t k; + int i; + int j; + + t = 0; + + for (i = 0; i < 56; i++) + t = (t << 1) | ((key >> (64 - m_pc1_box[i])) & 1); + + c = (t >> 28) & 0xFFFFFFF; + d = t & 0xFFFFFFF; + + for (i = 0; i < 16; i++) { + c = rol28(c, m_shifts[i]); + d = rol28(d, m_shifts[i]); + + t = ((uint64_t)c << 28) | d; + + k = 0; + for (j = 0; j < 48; j++) + k = (k << 1) | ((t >> (56 - m_pc2_box[j])) & 1); + + ks[i] = k; + } +} + +uint64_t DES::EncryptInternal(uint64_t v, const uint64_t *ks) +{ + uint32_t l, r; + uint32_t t; + int i; + + l = v >> 32; + r = v & 0xFFFFFFFF; + + for (i = 0; i < 16; i++) { + t = r; + r = l ^ Function(r, ks[i]); + l = t; + } + + return ((uint64_t)r << 32) | l; +} + +uint64_t DES::DecryptInternal(uint64_t v, const uint64_t *ks) +{ + uint32_t l, r; + uint32_t t; + int i; + + r = v >> 32; + l = v & 0xFFFFFFFF; + + for (i = 15; i >= 0; i--) { + t = l; + l = r ^ Function(l, ks[i]); + r = t; + } + + return ((uint64_t)l << 32) | r; +} + +uint64_t DES::BytesToU64(const uint8_t *data) +{ + uint64_t d = 0; + + for (int n = 0; n < 8; n++) + d = (d << 8) | data[n]; + + return d; +} + +void DES::U64ToBytes(uint8_t *data, uint64_t val) +{ + for (int n = 7; n >= 0; n--) + { + data[n] = val & 0xFF; + val >>= 8; + } +} + + +const uint8_t DES::m_ip_box[64] = { + 58, 50, 42, 34, 26, 18, 10, 2, + 60, 52, 44, 36, 28, 20, 12, 4, + 62, 54, 46, 38, 30, 22, 14, 6, + 64, 56, 48, 40, 32, 24, 16, 8, + 57, 49, 41, 33, 25, 17, 9, 1, + 59, 51, 43, 35, 27, 19, 11, 3, + 61, 53, 45, 37, 29, 21, 13, 5, + 63, 55, 47, 39, 31, 23, 15, 7 +}; + +const uint8_t DES::m_fp_box[64] = { + 40, 8, 48, 16, 56, 24, 64, 32, + 39, 7, 47, 15, 55, 23, 63, 31, + 38, 6, 46, 14, 54, 22, 62, 30, + 37, 5, 45, 13, 53, 21, 61, 29, + 36, 4, 44, 12, 52, 20, 60, 28, + 35, 3, 43, 11, 51, 19, 59, 27, + 34, 2, 42, 10, 50, 18, 58, 26, + 33, 1, 41, 9, 49, 17, 57, 25 +}; + +const uint8_t DES::m_e_box[48] = { + 32, 1, 2, 3, 4, 5, + 4, 5, 6, 7, 8, 9, + 8, 9, 10, 11, 12, 13, + 12, 13, 14, 15, 16, 17, + 16, 17, 18, 19, 20, 21, + 20, 21, 22, 23, 24, 25, + 24, 25, 26, 27, 28, 29, + 28, 29, 30, 31, 32, 1 +}; + +const uint8_t DES::m_s1[64] = { + 14, 0, 4, 15, 13, 7, 1, 4, 2, 14, 15, 2, 11, 13, 8, 1, 3, 10, 10, 6, 6, 12, 12, 11, 5, 9, 9, 5, 0, 3, 7, 8, + 4, 15, 1, 12, 14, 8, 8, 2, 13, 4, 6, 9, 2, 1, 11, 7, 15, 5, 12, 11, 9, 3, 7, 14, 3, 10, 10, 0, 5, 6, 0, 13 +}; +const uint8_t DES::m_s2[64] = { + 15, 3, 1, 13, 8, 4, 14, 7, 6, 15, 11, 2, 3, 8, 4, 14, 9, 12, 7, 0, 2, 1, 13, 10, 12, 6, 0, 9, 5, 11, 10, 5, + 0, 13, 14, 8, 7, 10, 11, 1, 10, 3, 4, 15, 13, 4, 1, 2, 5, 11, 8, 6, 12, 7, 6, 12, 9, 0, 3, 5, 2, 14, 15, 9 +}; +const uint8_t DES::m_s3[64] = { + 10, 13, 0, 7, 9, 0, 14, 9, 6, 3, 3, 4, 15, 6, 5, 10, 1, 2, 13, 8, 12, 5, 7, 14, 11, 12, 4, 11, 2, 15, 8, 1, + 13, 1, 6, 10, 4, 13, 9, 0, 8, 6, 15, 9, 3, 8, 0, 7, 11, 4, 1, 15, 2, 14, 12, 3, 5, 11, 10, 5, 14, 2, 7, 12 +}; +const uint8_t DES::m_s4[64] = { + 7, 13, 13, 8, 14, 11, 3, 5, 0, 6, 6, 15, 9, 0, 10, 3, 1, 4, 2, 7, 8, 2, 5, 12, 11, 1, 12, 10, 4, 14, 15, 9, + 10, 3, 6, 15, 9, 0, 0, 6, 12, 10, 11, 1, 7, 13, 13, 8, 15, 9, 1, 4, 3, 5, 14, 11, 5, 12, 2, 7, 8, 2, 4, 14 +}; +const uint8_t DES::m_s5[64] = { + 2, 14, 12, 11, 4, 2, 1, 12, 7, 4, 10, 7, 11, 13, 6, 1, 8, 5, 5, 0, 3, 15, 15, 10, 13, 3, 0, 9, 14, 8, 9, 6, + 4, 11, 2, 8, 1, 12, 11, 7, 10, 1, 13, 14, 7, 2, 8, 13, 15, 6, 9, 15, 12, 0, 5, 9, 6, 10, 3, 4, 0, 5, 14, 3 +}; +const uint8_t DES::m_s6[64] = { + 12, 10, 1, 15, 10, 4, 15, 2, 9, 7, 2, 12, 6, 9, 8, 5, 0, 6, 13, 1, 3, 13, 4, 14, 14, 0, 7, 11, 5, 3, 11, 8, + 9, 4, 14, 3, 15, 2, 5, 12, 2, 9, 8, 5, 12, 15, 3, 10, 7, 11, 0, 14, 4, 1, 10, 7, 1, 6, 13, 0, 11, 8, 6, 13 +}; +const uint8_t DES::m_s7[64] = { + 4, 13, 11, 0, 2, 11, 14, 7, 15, 4, 0, 9, 8, 1, 13, 10, 3, 14, 12, 3, 9, 5, 7, 12, 5, 2, 10, 15, 6, 8, 1, 6, + 1, 6, 4, 11, 11, 13, 13, 8, 12, 1, 3, 4, 7, 10, 14, 7, 10, 9, 15, 5, 6, 0, 8, 15, 0, 14, 5, 2, 9, 3, 2, 12 +}; +const uint8_t DES::m_s8[64] = { + 13, 1, 2, 15, 8, 13, 4, 8, 6, 10, 15, 3, 11, 7, 1, 4, 10, 12, 9, 5, 3, 6, 14, 11, 5, 0, 0, 14, 12, 9, 7, 2, + 7, 2, 11, 1, 4, 14, 1, 7, 9, 4, 12, 10, 14, 8, 2, 13, 0, 15, 6, 12, 10, 9, 13, 0, 15, 3, 3, 5, 5, 6, 8, 11 +}; + +const uint8_t DES::m_p_box[32] = { + 16, 7, 20, 21, + 29, 12, 28, 17, + 1, 15, 23, 26, + 5, 18, 31, 10, + 2, 8, 24, 14, + 32, 27, 3, 9, + 19, 13, 30, 6, + 22, 11, 4, 25 +}; + +const uint8_t DES::m_pc1_box[56] = { + 57, 49, 41, 33, 25, 17, 9, + 1, 58, 50, 42, 34, 26, 18, + 10, 2, 59, 51, 43, 35, 27, + 19, 11, 3, 60, 52, 44, 36, + + 63, 55, 47, 39, 31, 23, 15, + 7, 62, 54, 46, 38, 30, 22, + 14, 6, 61, 53, 45, 37, 29, + 21, 13, 5, 28, 20, 12, 4 +}; + +const uint8_t DES::m_pc2_box[48] = { + 14, 17, 11, 24, 1, 5, + 3, 28, 15, 6, 21, 10, + 23, 19, 12, 4, 26, 8, + 16, 7, 27, 20, 13, 2, + 41, 52, 31, 37, 47, 55, + 30, 40, 51, 45, 33, 48, + 44, 49, 39, 56, 34, 53, + 46, 42, 50, 36, 29, 32 +}; + +const unsigned int DES::m_shifts[16] = { + 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 +}; diff --git a/vendor/apfs-fuse/Crypto/Des.h b/vendor/apfs-fuse/Crypto/Des.h new file mode 100644 index 0000000..dbebcf3 --- /dev/null +++ b/vendor/apfs-fuse/Crypto/Des.h @@ -0,0 +1,53 @@ +#pragma once + +#include +#include + +class DES +{ + friend class TripleDES; + +public: + DES(); + ~DES(); + + void Encrypt(uint8_t *cipher, const uint8_t *plain, size_t size); + void Decrypt(uint8_t *plain, const uint8_t *cipher, size_t size); + void EncryptCBC(uint8_t *cipher, const uint8_t *plain, size_t size); + void DecryptCBC(uint8_t *plain, const uint8_t *cipher, size_t size); + void SetKey(const uint8_t *key); + void SetIV(const uint8_t *iv); + +private: + static uint64_t InitialPermutation(uint64_t v); + static uint64_t FinalPermutation(uint64_t v); + static void KeySchedule(uint64_t key, uint64_t *ks); + static uint64_t EncryptInternal(uint64_t v, const uint64_t *ks); + static uint64_t DecryptInternal(uint64_t v, const uint64_t *ks); + + static void PrintBits(uint64_t v, int bits); + static uint64_t Permute(const uint8_t *box, uint64_t v); + static uint32_t Function(uint32_t r, uint64_t ks); + + static uint64_t BytesToU64(const uint8_t *data); + static void U64ToBytes(uint8_t *data, uint64_t val); + + uint64_t m_keySchedule[16]; + uint64_t m_initVector; + + static const uint8_t m_ip_box[64]; + static const uint8_t m_fp_box[64]; + static const uint8_t m_e_box[48]; + static const uint8_t m_s1[64]; + static const uint8_t m_s2[64]; + static const uint8_t m_s3[64]; + static const uint8_t m_s4[64]; + static const uint8_t m_s5[64]; + static const uint8_t m_s6[64]; + static const uint8_t m_s7[64]; + static const uint8_t m_s8[64]; + static const uint8_t m_p_box[32]; + static const uint8_t m_pc1_box[56]; + static const uint8_t m_pc2_box[48]; + static const unsigned int m_shifts[16]; +}; diff --git a/vendor/apfs-fuse/Crypto/Sha1.cpp b/vendor/apfs-fuse/Crypto/Sha1.cpp new file mode 100644 index 0000000..5260c2e --- /dev/null +++ b/vendor/apfs-fuse/Crypto/Sha1.cpp @@ -0,0 +1,174 @@ +#include "Sha1.h" + +inline uint32_t Ch(uint32_t x, uint32_t y, uint32_t z) +{ + return (x & y) ^ ((~x) & z); +} + +inline uint32_t Parity(uint32_t x, uint32_t y, uint32_t z) +{ + return x ^ y ^ z; +} + +inline uint32_t Maj(uint32_t x, uint32_t y, uint32_t z) +{ + return (x & y) ^ (x & z) ^ (y & z); +} + +inline uint32_t Rotl(int sh, uint32_t v) +{ + return (v << sh) | (v >> (32 - sh)); +} + +Sha1::Sha1() +{ + Init(); +} + +Sha1::~Sha1() +{ +} + +void Sha1::Init() +{ + m_hash[0] = 0x67452301; + m_hash[1] = 0xEFCDAB89; + m_hash[2] = 0x98BADCFE; + m_hash[3] = 0x10325476; + m_hash[4] = 0xC3D2E1F0; + + m_bit_cnt = 0; + m_buf_idx = 0; + + for (size_t n = 0; n < 64; n++) + m_buffer[n] = 0; +} + +void Sha1::Update(const void * ptr, size_t size) +{ + size_t n; + const uint8_t *data = reinterpret_cast(ptr); + + for (n = 0; n < size; n++) + { + m_buffer[m_buf_idx] = data[n]; + m_buf_idx++; + if (m_buf_idx == 64) + { + Round(); + m_buf_idx = 0; + } + } + + m_bit_cnt += (8 * size); +} + +void Sha1::Final(uint8_t * hash) +{ + size_t n; + + m_buffer[m_buf_idx++] = 0x80; + + if (m_buf_idx > 56) + { + for (; m_buf_idx < 64; m_buf_idx++) + m_buffer[m_buf_idx] = 0; + Round(); + m_buf_idx = 0; + } + + for (; m_buf_idx < 56; m_buf_idx++) + m_buffer[m_buf_idx] = 0; + m_buffer[56] = (m_bit_cnt >> 56) & 0xFF; + m_buffer[57] = (m_bit_cnt >> 48) & 0xFF; + m_buffer[58] = (m_bit_cnt >> 40) & 0xFF; + m_buffer[59] = (m_bit_cnt >> 32) & 0xFF; + m_buffer[60] = (m_bit_cnt >> 24) & 0xFF; + m_buffer[61] = (m_bit_cnt >> 16) & 0xFF; + m_buffer[62] = (m_bit_cnt >> 8) & 0xFF; + m_buffer[63] = (m_bit_cnt >> 0) & 0xFF; + Round(); + + for (n = 0; n < 5; n++) + { + hash[4 * n + 0] = (m_hash[n] >> 24) & 0xFF; + hash[4 * n + 1] = (m_hash[n] >> 16) & 0xFF; + hash[4 * n + 2] = (m_hash[n] >> 8) & 0xFF; + hash[4 * n + 3] = (m_hash[n] >> 0) & 0xFF; + } +} + +void Sha1::Round() +{ + uint32_t w[80]; + uint32_t a; + uint32_t b; + uint32_t c; + uint32_t d; + uint32_t e; + uint32_t T; + int k; + + for (k = 0; k < 16; k++) + w[k] = (m_buffer[4 * k] << 24) | (m_buffer[4 * k + 1] << 16) | (m_buffer[4 * k + 2] << 8) | m_buffer[4 * k + 3]; + for (k = 16; k < 80; k++) + w[k] = Rotl(1, w[k - 3] ^ w[k - 8] ^ w[k - 14] ^ w[k - 16]); + + a = m_hash[0]; + b = m_hash[1]; + c = m_hash[2]; + d = m_hash[3]; + e = m_hash[4]; + + for (k = 0; k < 20; k++) + { + T = Rotl(5, a) + Ch(b, c, d) + e + m_K[0] + w[k]; + e = d; + d = c; + c = Rotl(30, b); + b = a; + a = T; + } + + for (k = 20; k < 40; k++) + { + T = Rotl(5, a) + Parity(b, c, d) + e + m_K[1] + w[k]; + e = d; + d = c; + c = Rotl(30, b); + b = a; + a = T; + } + + for (k = 40; k < 60; k++) + { + T = Rotl(5, a) + Maj(b, c, d) + e + m_K[2] + w[k]; + e = d; + d = c; + c = Rotl(30, b); + b = a; + a = T; + } + + for (k = 60; k < 80; k++) + { + T = Rotl(5, a) + Parity(b, c, d) + e + m_K[3] + w[k]; + e = d; + d = c; + c = Rotl(30, b); + b = a; + a = T; + } + + m_hash[0] = a + m_hash[0]; + m_hash[1] = b + m_hash[1]; + m_hash[2] = c + m_hash[2]; + m_hash[3] = d + m_hash[3]; + m_hash[4] = e + m_hash[4]; +} + + +const uint32_t Sha1::m_K[4] = +{ + 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6 +}; diff --git a/vendor/apfs-fuse/Crypto/Sha1.h b/vendor/apfs-fuse/Crypto/Sha1.h new file mode 100644 index 0000000..ff8bccf --- /dev/null +++ b/vendor/apfs-fuse/Crypto/Sha1.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#include + +class Sha1 +{ +public: + Sha1(); + ~Sha1(); + + void Init(); + void Update(const void *data, size_t size); + void Final(uint8_t *hash); + +private: + void Round(); + + uint8_t m_buffer[64]; + uint32_t m_hash[5]; + uint64_t m_bit_cnt; + size_t m_buf_idx; + + static const uint32_t m_K[4]; +}; + diff --git a/vendor/apfs-fuse/Crypto/Sha256.cpp b/vendor/apfs-fuse/Crypto/Sha256.cpp new file mode 100644 index 0000000..bdc6d22 --- /dev/null +++ b/vendor/apfs-fuse/Crypto/Sha256.cpp @@ -0,0 +1,179 @@ +#include +#include + +#include "Sha256.h" + +#ifdef _MSC_VER +#include +#else +static inline uint32_t _rotr(uint32_t v, int sh) +{ + return (v >> sh) | (v << (32 - sh)); +} + +#endif + +static inline uint32_t Ch(uint32_t x, uint32_t y, uint32_t z) +{ + return (x & y) ^ (~x & z); +} + +static inline uint32_t Maj(uint32_t x, uint32_t y, uint32_t z) +{ + return (x & y) ^ (x & z) ^ (y & z); +} + +static inline uint32_t S0(uint32_t x) +{ + return _rotr(x, 2) ^ _rotr(x, 13) ^ _rotr(x, 22); +} + +static inline uint32_t S1(uint32_t x) +{ + return _rotr(x, 6) ^ _rotr(x, 11) ^ _rotr(x, 25); +} + +static inline uint32_t s0(uint32_t x) +{ + return _rotr(x, 7) ^ _rotr(x, 18) ^ (x >> 3); +} + +static inline uint32_t s1(uint32_t x) +{ + return _rotr(x, 17) ^ _rotr(x, 19) ^ (x >> 10); +} + +const uint32_t SHA256::m_k[64] = { + 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, + 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, + 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA, + 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967, + 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, + 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070, + 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3, + 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2 +}; + +SHA256::SHA256() +{ + Init(); +} + +SHA256::~SHA256() +{ +} + +void SHA256::Init() +{ + int i; + + m_hash[0] = 0x6A09E667; + m_hash[1] = 0xBB67AE85; + m_hash[2] = 0x3C6EF372; + m_hash[3] = 0xA54FF53A; + m_hash[4] = 0x510E527F; + m_hash[5] = 0x9B05688C; + m_hash[6] = 0x1F83D9AB; + m_hash[7] = 0x5BE0CD19; + m_bufferPtr = 0; + m_byteCnt = 0; + + for (i = 0; i < 64; i++) + m_buffer[i] = 0; +} + +void SHA256::Round() +{ + uint32_t a, b, c, d, e, f, g, h; + uint32_t w[64]; + int t; + uint32_t t1, t2; + + for (t = 0; t < 16; t++) + w[t] = (m_buffer[4*t] << 24) | (m_buffer[4*t+1] << 16) | (m_buffer[4*t+2] << 8) | m_buffer[4*t+3]; + for (t = 16; t < 64; t++) + w[t] = s1(w[t-2]) + w[t-7] + s0(w[t-15]) + w[t-16]; + + a = m_hash[0]; + b = m_hash[1]; + c = m_hash[2]; + d = m_hash[3]; + e = m_hash[4]; + f = m_hash[5]; + g = m_hash[6]; + h = m_hash[7]; + + for (t = 0; t < 64; t++) { + t1 = h + S1(e) + Ch(e, f, g) + m_k[t] + w[t]; + t2 = S0(a) + Maj(a, b, c); + h = g; + g = f; + f = e; + e = d + t1; + d = c; + c = b; + b = a; + a = t1 + t2; + } + + m_hash[0] += a; + m_hash[1] += b; + m_hash[2] += c; + m_hash[3] += d; + m_hash[4] += e; + m_hash[5] += f; + m_hash[6] += g; + m_hash[7] += h; + + for (t = 0; t < 64; t++) + m_buffer[t] = 0; + + m_bufferPtr = 0; +} + +void SHA256::Update(const void *data, size_t cnt) +{ + size_t i; + const uint8_t *bdata = reinterpret_cast(data); + + for (i = 0; i < cnt; i++) { + m_buffer[m_bufferPtr++] = bdata[i]; + if (m_bufferPtr == 64) + Round(); + } + + m_byteCnt += cnt; +} + +void SHA256::Final(uint8_t *hash) +{ + uint32_t len_h, len_l; + int i; + + m_buffer[m_bufferPtr] = 0x80; + if (m_bufferPtr >= 55) + Round(); + + len_h = static_cast(m_byteCnt >> 29); + len_l = static_cast(m_byteCnt << 3); + + m_buffer[56] = (len_h >> 24) & 0xFF; + m_buffer[57] = (len_h >> 16) & 0xFF; + m_buffer[58] = (len_h >> 8) & 0xFF; + m_buffer[59] = len_h & 0xFF; + m_buffer[60] = (len_l >> 24) & 0xFF; + m_buffer[61] = (len_l >> 16) & 0xFF; + m_buffer[62] = (len_l >> 8) & 0xFF; + m_buffer[63] = len_l & 0xFF; + + Round(); + + for (i = 0; i < 8; i++) { + hash[4*i] = (m_hash[i] >> 24) & 0xFF; + hash[4*i+1] = (m_hash[i] >> 16) & 0xFF; + hash[4*i+2] = (m_hash[i] >> 8) & 0xFF; + hash[4*i+3] = m_hash[i] & 0xFF; + } + + Init(); +} diff --git a/vendor/apfs-fuse/Crypto/Sha256.h b/vendor/apfs-fuse/Crypto/Sha256.h new file mode 100644 index 0000000..f118e4c --- /dev/null +++ b/vendor/apfs-fuse/Crypto/Sha256.h @@ -0,0 +1,25 @@ +#pragma once + +#include +#include + +class SHA256 +{ +public: + SHA256(); + ~SHA256(); + + void Init(); + void Update(const void *data, size_t size); + void Final(uint8_t *hash); + +private: + void Round(); + + uint8_t m_buffer[64]; + uint32_t m_hash[8]; + uint32_t m_bufferPtr; + size_t m_byteCnt; + + static const uint32_t m_k[64]; +}; diff --git a/vendor/apfs-fuse/Crypto/TripleDes.cpp b/vendor/apfs-fuse/Crypto/TripleDes.cpp new file mode 100644 index 0000000..2d374fd --- /dev/null +++ b/vendor/apfs-fuse/Crypto/TripleDes.cpp @@ -0,0 +1,111 @@ +#include "Des.h" +#include "TripleDes.h" + +#include + +TripleDES::TripleDES() +{ + memset(m_keySchedule, 0, sizeof(m_keySchedule)); + m_iv = 0; +} + +TripleDES::~TripleDES() +{ + memset(m_keySchedule, 0, sizeof(m_keySchedule)); + m_iv = 0; +} + +void TripleDES::Encrypt(uint8_t * cipher, const uint8_t * plain, size_t size) +{ + size_t off; + uint64_t r; + + for (off = 0; off < size; off += 8) + { + r = DES::BytesToU64(plain + off); + r = DES::InitialPermutation(r); + r = DES::EncryptInternal(r, m_keySchedule[0]); + r = DES::DecryptInternal(r, m_keySchedule[1]); + r = DES::EncryptInternal(r, m_keySchedule[2]); + r = DES::FinalPermutation(r); + DES::U64ToBytes(cipher + off, r); + } +} + +void TripleDES::Decrypt(uint8_t * plain, const uint8_t * cipher, size_t size) +{ + size_t off; + uint64_t r; + + for (off = 0; off < size; off += 8) + { + r = DES::BytesToU64(cipher + off); + r = DES::InitialPermutation(r); + r = DES::DecryptInternal(r, m_keySchedule[2]); + r = DES::EncryptInternal(r, m_keySchedule[1]); + r = DES::DecryptInternal(r, m_keySchedule[0]); + r = DES::FinalPermutation(r); + DES::U64ToBytes(plain + off, r); + } +} + +void TripleDES::EncryptCBC(uint8_t * cipher, const uint8_t * plain, size_t size) +{ + size_t off; + uint64_t r; + + for (off = 0; off < size; off += 8) + { + r = DES::BytesToU64(plain + off); + r = r ^ m_iv; + r = DES::InitialPermutation(r); + r = DES::EncryptInternal(r, m_keySchedule[0]); + r = DES::DecryptInternal(r, m_keySchedule[1]); + r = DES::EncryptInternal(r, m_keySchedule[2]); + r = DES::FinalPermutation(r); + m_iv = r; + DES::U64ToBytes(cipher + off, r); + } +} + +void TripleDES::DecryptCBC(uint8_t * plain, const uint8_t * cipher, size_t size) +{ + size_t off; + uint64_t r; + uint64_t iv; + + for (off = 0; off < size; off += 8) + { + r = DES::BytesToU64(cipher + off); + iv = r; + r = DES::InitialPermutation(r); + r = DES::DecryptInternal(r, m_keySchedule[2]); + r = DES::EncryptInternal(r, m_keySchedule[1]); + r = DES::DecryptInternal(r, m_keySchedule[0]); + r = DES::FinalPermutation(r); + r = r ^ m_iv; + m_iv = iv; + DES::U64ToBytes(plain + off, r); + } +} + +void TripleDES::SetKey(const uint8_t * key) +{ + uint64_t k[3]; + memset(m_keySchedule, 0, sizeof(m_keySchedule)); + m_iv = 0; + k[0] = DES::BytesToU64(key + 0); + k[1] = DES::BytesToU64(key + 8); + k[2] = DES::BytesToU64(key + 16); + DES::KeySchedule(k[0], m_keySchedule[0]); + DES::KeySchedule(k[1], m_keySchedule[1]); + DES::KeySchedule(k[2], m_keySchedule[2]); +} + +void TripleDES::SetIV(const uint8_t * iv) +{ + if (iv) + m_iv = DES::BytesToU64(iv); + else + m_iv = 0; +} diff --git a/vendor/apfs-fuse/Crypto/TripleDes.h b/vendor/apfs-fuse/Crypto/TripleDes.h new file mode 100644 index 0000000..177c9a4 --- /dev/null +++ b/vendor/apfs-fuse/Crypto/TripleDes.h @@ -0,0 +1,23 @@ +#pragma once + +#include +#include + +class TripleDES +{ +public: + TripleDES(); + ~TripleDES(); + + void Encrypt(uint8_t *cipher, const uint8_t *plain, size_t size); + void Decrypt(uint8_t *plain, const uint8_t *cipher, size_t size); + void EncryptCBC(uint8_t *cipher, const uint8_t *plain, size_t size); + void DecryptCBC(uint8_t *plain, const uint8_t *cipher, size_t size); + + void SetKey(const uint8_t *key); + void SetIV(const uint8_t *iv); + +private: + uint64_t m_keySchedule[3][16]; + uint64_t m_iv; +}; diff --git a/vendor/apfs-fuse/LICENSE b/vendor/apfs-fuse/LICENSE new file mode 100644 index 0000000..d159169 --- /dev/null +++ b/vendor/apfs-fuse/LICENSE @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/vendor/apfs-fuse/README.md b/vendor/apfs-fuse/README.md new file mode 100644 index 0000000..84bf994 --- /dev/null +++ b/vendor/apfs-fuse/README.md @@ -0,0 +1,214 @@ + +# APFS FUSE Driver for Linux + +This project is a read-only FUSE driver for the new Apple File System. It also supports software +encrypted volumes and fusion drives. Firmlinks are not supported yet. + +Be aware that not all compression methods are supported yet (only the ones I have encountered so far). +Thus, the driver may return compressed files instead of uncompressed ones. Although most of the time it +should just report an error. + +## Changelog + +| Date | Comment | +|------|---------| +| 2020-07-08 | Added support for mounting snapshots and sealed volumes | +| 2018-04-20 | Added support for mounting DMGs | +| 2018-04-14 | Added support for partition tables (GPT only) | +| 2018-04-10 | Fixed and extended FileVault encryption support | +| 2018-03-28 | Added support for FileVault encryption | +| 2017-10-25 | Added support for encryption | +| 2017-10-14 | Initial version | + +## Usage + +### Compile the source code +The following libraries are needed (including the -dev/-devel packages): + +* FUSE 2.6 or greater (on 32-bit systems, FUSE 3.0 or greater) +* zlib +* bzip2 +* libattr (on some Linux distributions) + +Development tools: +* cmake +* gcc-c++ (or clang++) +* git (for cloning) + +Example for Linux: +``` +sudo apt update +sudo apt install fuse libfuse3-dev bzip2 libbz2-dev cmake gcc-c++ git libattr1-dev zlib1g-dev +``` +Of course these commands depend on the Linux distribution. + +Clone the repository: +``` +git clone https://github.com/sgan81/apfs-fuse.git +cd apfs-fuse +git submodule init +git submodule update +``` +The driver uses Apple's lzfse library and includes it as a submodule. + +Compile the driver: +``` +mkdir build +cd build +cmake .. +ccmake . # Only if you want to change build options +make +``` + +Note that the driver uses FUSE 3.0 by default (required on 32-bit systems). If +you want do compile using FUSE 2.6, use `ccmake .` to change the option +`USE_FUSE3` to `OFF`. + +### Mount a drive +``` +apfs-fuse +``` +#### Supported options: +* `-d n`: If n > 0, enable debug output (see below for details). +* `-f device`: Specify secondary device for Fusion drive. +* `-o opts`: Comma-separated list of mount options. +* `-l`: Lax mode: when unexpected data is encountered, try to continue, even if this means + returning potentially incorrect data. +* `-v n`: Instead of mounting the first volume in a container, mount volume n (starting at 0). +* `-r recovery_key`: Mount an encrypted volume by supplying a password or Personal Recovery Key (PRK). +* `-s n`: Find the container at offset n inside the device. This is useful when using an image file + instead of a disk device, and therefore partitions are not exposed. +* `-p n`: Find the container at partition n inside the device. + +If you are using an image file containing partitions, the driver will now detect if there is a valid GPT +partition table. If there is, it will look for the first APFS partition and use that one for the container. +If your drive contains more than one APFS container, you can specify the partition/container id with the +`-p` option. + +The device has to be the one containing the APFS container. If a container contains more than one volume, +the volume can be specified by the `-v` option. + +If a volume is encrypted, the apfs-fuse command will prompt for a password, unless a password or PRK is +specified on the command line. The PRK can also be used as password. + +It is also possible to directly mount DMG files. The driver will automatically detect if a dmg +has to be mounted and take appropriate action. If a dmg is encrypted, you will be asked for the password. +Note that dmg support is currently a bit slow (especially when compressed), but it should work properly. + +The debug flags are now a combination of bits. So to enable specific output, you just add the numbers +mentioned below together, and use the result as parameter for -d. + +* 1 Display more information about errors +* 2 Display additional generic information +* 4 Display information about directory-related operations +* 8 Display information about on-the-fly compression +* 16 Display information about cryptographic operations (caution, displays keys as well) + +#### Mount options (-o ...) +In addition to the mount options supported by fuse, the following mount options are supported: +* uid=n: Pretend that all files have UID n. +* gid=n: Pretend that all files have GID n. +* vol=n: Same as -v, specify the volume number to mount if you don't want volume 0. +* blksize=n: Set the physical block size (default: 512 bytes). +* pass=...: Specify volume passphrase (same as -r). +* xid=...: Try to mount older XID. May be useful if the container is corrupt. +* snap=...: Mount snapshot with given XID. Use apfsutil to display snapshot ids. + +The blksize parameter is required for proper partition table parsing on some newer +macs. However the current driver should be able to detect the block size automatically. + +If you mount a volume as root and want some user to be able to access it, use: +``` +apfs-fuse -o uid=,gid=,allow_other /dev/ +``` + +If you want to mount a device as user, add yourself to the disk group. This might not be too safe though, +as it allows any application to read and write anywhere on a drive. + +### Unmount a drive +As root: +``` +umount +``` + +As user: +``` +fusermount -u +``` + +## Features +The following features are implemented: + +* Can read macOS 10.13 case sensitive and insensitive volumes, as well as iOS 11 / macOS 10.12 volumes +* Transparent decompression of ZLib and LZVN +* Symlinks +* Hardlinks (it seems ...) +* Extended attributes +* Software encryption (at least full-disk encryption) +* Automatic detection of GPT partition tables +* Direct mounting of DMG images (supports zlib/adc compression and encryption) + +## Limitations +These things are not supported (yet): + +* Transparent decompression of LZFSE +* Writing +* Hardware-encrypted volumes (internal drives of Macs with T2 chip) + +## Debugging + +Since the driver is still experimental and based on analysis of a limited set of drives / volumes, crashes +may unfortunately still happen. If a crash happens, providing me with useful information can be very helpful. + +One of the most important pieces of information is the location where the program crashed. You can find that +out by debugging the tool. In order to debug the program, do the following: + +Change the main CMakeLists.txt as follows: In the line `set(CMAKE_BUILD_TYPE Release)`, +replace `Release` with `Debug`. + +Rebuild everything. + +Run it under `gdb`. Like this: +``` +gdb apfs-fuse + +(In gdb): +set args +run + +(And if/when it crashes): +backtrace + +(When you're finished): +quit +``` +And then send the output of `backtrace` to me. Adding `-d 1` to options might help as well, but be aware that +it will generate a lot of output on the text console. + +### Some tools that might be useful +#### apfs-dump-quick + +If you encounter problems with some file, it may be that I overlooked something during reverse engineering. In that +case, you can use the `apfs-dump-quick` command to dump the management structures of the whole drive. It can be run +as follows: +``` +apfs-dump-quick +``` +The tool will dump the most current version of the disk structures into a logfile. This file can become quite big, like +a few 100 MB. So to limit the amount of information to report, look for the name of the file in the log. Try to find a +line starting with `File` and containing the filename. The number immediately after `File` is the ID. Find all lines +having this ID, and include them in your bug report. +#### apfs-dump +There is another command available: +``` +apfs-dump +``` +This tool was the one I originally used for reverse engineering. It will scan the whole volume for clusters having +correct checksums (and thus being part of some management structure), and then it will try to dump them. This will +take a very long time to run on big volumes, and create huge log files. So using the quick version will be much faster. +#### apfsutil +``` +apfsutil +``` +This is a new tool that just displays some information from a container. For now, it lists the volumes a container +contains, and snapshots if there are some. This tool might be extended in the future. diff --git a/vendor/apfs-fuse/apfsfuse/ApfsFuse.cpp b/vendor/apfs-fuse/apfsfuse/ApfsFuse.cpp new file mode 100644 index 0000000..bf01558 --- /dev/null +++ b/vendor/apfs-fuse/apfsfuse/ApfsFuse.cpp @@ -0,0 +1,963 @@ +/* + This file is part of apfs-fuse, a read-only implementation of APFS + (Apple File System) for FUSE. + Copyright (C) 2017 Simon Gander + + Apfs-fuse is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + Apfs-fuse is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with apfs-fuse. If not, see . +*/ + +#ifdef USE_FUSE2 +#define FUSE_USE_VERSION 26 +#else +#define FUSE_USE_VERSION 30 +#endif + +#ifdef __linux__ +#ifdef USE_FUSE2 +#include +#include +#else +#include +#include +#endif +#include +#endif +#ifdef __APPLE__ +#include +#include +#endif + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include + +static_assert(sizeof(fuse_ino_t) == 8, "Sorry, on 32-bit systems, you need to use FUSE-3."); + +constexpr double FUSE_TIMEOUT = 86400.0; + +static struct fuse_lowlevel_ops ops; +static Device *g_disk_main = nullptr; +static Device *g_disk_tier2 = nullptr; +static ApfsContainer *g_container = nullptr; +static ApfsVolume *g_volume = nullptr; +static unsigned int g_vol_id = 0; +static uid_t g_uid = 0; +static gid_t g_gid = 0; +static xid_t g_xid = 0; +static bool g_set_uid = false; +static bool g_set_gid = false; +static int g_physblksize = 512; +static std::string g_password; +static xid_t g_snap_xid = 0; + +struct Directory +{ + Directory() {} + ~Directory() {} + + std::vector dirbuf; +}; + +struct File +{ + File() {} + ~File() {} + + bool IsCompressed() const { return (ino.bsd_flags & APFS_UF_COMPRESSED) != 0; } + + ApfsDir::Inode ino; + std::vector decomp_data; +}; + +static bool apfs_stat_internal(fuse_ino_t ino, struct stat &st) +{ + ApfsDir dir(*g_volume); + ApfsDir::Inode rec; + bool rc = false; + + memset(&st, 0, sizeof(st)); + + if (ino == ROOT_DIR_PARENT) + { + st.st_ino = 1; + st.st_mode = S_IFDIR | 0755; + st.st_nlink = 2; + return true; + } + + rc = dir.GetInode(rec, ino); + + if (!rc) + { + std::cerr << "Unable to read inode " << ino << std::endl; + return false; + } + else + { + constexpr uint64_t div_nsec = 1000000000; + + // st_dev? + st.st_ino = ino; + st.st_mode = rec.mode; + // st.st_nlink = rec.ino.refcnt; + st.st_nlink = 1; + + st.st_uid = g_set_uid ? g_uid : rec.owner; + st.st_gid = g_set_gid ? g_gid : rec.group; + + if (rec.optional_present_flags & ApfsDir::Inode::INO_HAS_RDEV) + st.st_rdev = rec.rdev; + + // st_uid + // st_gid + // st_rdev? + + if (S_ISREG(st.st_mode)) + { + if (rec.bsd_flags & APFS_UF_COMPRESSED) // Compressed + { + if (rec.internal_flags & INODE_HAS_UNCOMPRESSED_SIZE) { + st.st_size = rec.uncompressed_size; + } else { + std::vector data; + rc = dir.GetAttribute(data, ino, "com.apple.decmpfs"); + if (rc) + { + const CompressionHeader *decmpfs = reinterpret_cast(data.data()); + + if (IsDecompAlgoSupported(decmpfs->algo)) + { + st.st_size = decmpfs->size; + // st.st_blocks = data.size() / 512; + if (g_debug & Dbg_Cmpfs) + std::cout << "Compressed size " << decmpfs->size << " bytes." << std::endl; + } + else if (IsDecompAlgoInRsrc(decmpfs->algo)) + { + rc = dir.GetAttribute(data, ino, "com.apple.ResourceFork"); + + if (!rc) + st.st_size = 0; + else + // Compressed size + st.st_size = data.size(); + } + else + { + st.st_size = data.size(); + std::cerr << "Unknown compression algorithm " << decmpfs->algo << std::endl; + if (!g_lax) + return false; + } + } + else + { + std::cerr << "Flag 0x20 set but no com.apple.decmpfs attribute!!!" << std::endl; + if (!g_lax) + return false; + } + } + } + else if (rec.optional_present_flags & ApfsDir::Inode::INO_HAS_DSTREAM) + { + st.st_size = rec.ds_size; + + // st.st_size = rec.sizes.size; + // st_blksize + // st.st_blocks = rec.sizes.size_on_disk / 512; + } + else + { + st.st_size = 0; + } + } + else if (S_ISDIR(st.st_mode)) + { + st.st_size = rec.nchildren_nlink; + } + +#ifdef __linux__ + // What about this? + // st.st_birthtime.tv_sec = rec.ino->create_time / div_nsec; + // st.st_birthtime.tv_nsec = rec.ino->create_time % div_nsec; + + st.st_mtim.tv_sec = rec.mod_time / div_nsec; + st.st_mtim.tv_nsec = rec.mod_time % div_nsec; + st.st_ctim.tv_sec = rec.change_time / div_nsec; + st.st_ctim.tv_nsec = rec.change_time % div_nsec; + st.st_atim.tv_sec = rec.access_time / div_nsec; + st.st_atim.tv_nsec = rec.access_time % div_nsec; +#endif +#ifdef __APPLE__ + st.st_birthtimespec.tv_sec = rec.create_time / div_nsec; + st.st_birthtimespec.tv_nsec = rec.create_time % div_nsec; + st.st_mtimespec.tv_sec = rec.mod_time / div_nsec; + st.st_mtimespec.tv_nsec = rec.mod_time % div_nsec; + st.st_ctimespec.tv_sec = rec.change_time / div_nsec; + st.st_ctimespec.tv_nsec = rec.change_time % div_nsec; + st.st_atimespec.tv_sec = rec.access_time / div_nsec; + st.st_atimespec.tv_nsec = rec.access_time % div_nsec; + + // st.st_gen = rec.ino.gen_count; +#endif + return true; + } +} + +/* +static void apfs_bmap(fuse_req_t req, fuse_ino_t ino, size_t blocksize, uint64_t idx) +{ + (void)req; + (void)ino; + (void)blocksize; + (void)idx; + + // fuse_reply_bmap(req, idx); +} +*/ + +/* +static void apfs_destroy(void *userdata) +{ + (void)userdata; +} +*/ + +static void apfs_getattr(fuse_req_t req, fuse_ino_t ino, fuse_file_info *fi) +{ + (void)fi; + + ApfsDir dir(*g_volume); + ApfsDir::Inode rec; + bool rc = false; + struct stat st; + + if (g_debug & Dbg_Info) + std::cout << "apfs_getattr: ino=" << ino << " => "; + + rc = apfs_stat_internal(ino, st); + + if (g_debug & Dbg_Info) + std::cout << (rc ? "OK" : "FAIL") << std::endl; + + if (rc) + fuse_reply_attr(req, &st, FUSE_TIMEOUT); + else + fuse_reply_err(req, ENOENT); +} + +#ifdef __APPLE__ +static void apfs_getxattr_mac(fuse_req_t req, fuse_ino_t ino, const char *name, size_t size, uint32_t position) +{ + ApfsDir dir(*g_volume); + bool rc = false; + std::vector data; + + if (g_debug & Dbg_Info) + std::cout << "apfs_getxattr: " << std::hex << ino << " " << name << " => "; + + rc = dir.GetAttribute(data, ino, name); + + if (g_debug & Dbg_Info) + std::cout << (rc ? "OK" : "FAIL") << std::endl; + + if (!rc) + fuse_reply_err(req, ENODATA); + else + { + if (size == 0) + fuse_reply_xattr(req, data.size()); // xattr size + else + fuse_reply_buf(req, reinterpret_cast(data.data() + position), std::min(data.size(), size)); + } +} +#endif +#ifdef __linux__ +static void apfs_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name, size_t size) +{ + ApfsDir dir(*g_volume); + bool rc = false; + std::vector data; + + if (g_debug & Dbg_Info) + std::cout << "apfs_getxattr: " << std::hex << ino << " " << name << " => "; + + rc = dir.GetAttribute(data, ino, name); + + if (g_debug & Dbg_Info) + std::cout << (rc ? "OK" : "FAIL") << std::endl; + + if (!rc) + fuse_reply_err(req, ENODATA); + else + { + if (size == 0) + fuse_reply_xattr(req, data.size()); // xattr size + else + fuse_reply_buf(req, reinterpret_cast(data.data()), std::min(data.size(), size)); + } +} +#endif + +static void apfs_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size) +{ + ApfsDir dir(*g_volume); + bool rc = false; + std::vector names; + std::string reply; + + rc = dir.ListAttributes(names, ino); + + if (g_debug & Dbg_Info) + std::cout << "apfs_listxattr:" << std::endl; + + if (rc) + { + for (size_t k = 0; k < names.size(); k++) + { + if (g_debug & Dbg_Info) + std::cout << names[k] << std::endl; + reply.append(names[k]); + reply.push_back(0); + } + } + + if (size == 0) + fuse_reply_xattr(req, reply.size()); + else if (size < reply.size()) + fuse_reply_err(req, ERANGE); + else + fuse_reply_buf(req, reply.c_str(), reply.size()); +} + +static void apfs_lookup(fuse_req_t req, fuse_ino_t ino, const char *name) +{ + if (g_debug & Dbg_Info) + std::cout << "apfs_lookup: ino=" << ino << " name=" << name << " => "; + + ApfsDir dir(*g_volume); + ApfsDir::DirRec res; + bool rc; + + rc = dir.LookupName(res, ino, name); + + if (g_debug & Dbg_Info) + std::cout << (rc ? "OK" : "FAIL") << std::endl; + + if (!rc) + { + fuse_reply_err(req, ENOENT); + } + else + { + fuse_entry_param e; + + e.ino = res.file_id; + e.attr_timeout = FUSE_TIMEOUT; + e.entry_timeout = FUSE_TIMEOUT; + + rc = apfs_stat_internal(res.file_id, e.attr); + + if (g_debug & Dbg_Info) + std::cout << " apfs_stat_internal " << res.file_id << " => " << (rc ? "OK" : "FAIL") << std::endl; + + fuse_reply_entry(req, &e); + } +} + +static void apfs_open(fuse_req_t req, fuse_ino_t ino, fuse_file_info *fi) +{ + if (g_debug & Dbg_Info) + std::cout << std::hex << "apfs_open: " << ino << std::endl; + + bool rc; + + if ((fi->flags & 3) != O_RDONLY) + fuse_reply_err(req, EACCES); + else + { + File *f = new File(); + ApfsDir dir(*g_volume); + + rc = dir.GetInode(f->ino, ino); + + if (!rc) + { + std::cerr << "Couldn't get inode " << ino << std::endl; + fuse_reply_err(req, ENOENT); + delete f; + return; + } + + if (f->IsCompressed()) + { + std::vector attr; + + rc = dir.GetAttribute(attr, ino, "com.apple.decmpfs"); + + if (!rc) + { + std::cerr << "Couldn't get attribute com.apple.decmpfs for " << ino << std::endl; + fuse_reply_err(req, ENOENT); + delete f; + return; + } + + if (g_debug & Dbg_Info) + { + // std::cout << "Inode info: size=" << f->ino.sizes.size << ", alloced_size=" << f->ino.sizes.alloced_size << std::endl; + } + rc = DecompressFile(dir, ino, f->decomp_data, attr); + // In strict mode, do not return uncompressed data. + if (!rc && !g_lax) + { + fuse_reply_err(req, EIO); + delete f; + return; + } + } + + fi->fh = reinterpret_cast(f); + + fuse_reply_open(req, fi); + } +} + +static void apfs_opendir(fuse_req_t req, fuse_ino_t ino, fuse_file_info *fi) +{ + if (g_debug & Dbg_Info) + std::cout << std::hex << "apfs_opendir: " << ino << std::endl; + + Directory *dir = new Directory(); + + fi->fh = reinterpret_cast(dir); + + fuse_reply_open(req, fi); +} + +static void apfs_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, struct fuse_file_info *fi) +{ + ApfsDir dir(*g_volume); + File *file = reinterpret_cast(fi->fh); + + if (g_debug & Dbg_Info) + std::cout << std::hex << "apfs_read: ino=" << ino << " size=" << size << " off=" << off << std::endl; + + if (!file->IsCompressed()) + { + // bool rc; + std::vector buf(size, 0); + + // rc = + dir.ReadFile(buf.data(), file->ino.private_id, off, size); + + // std::cerr << "apfs_read: fuse_reply_buf(req, " << reinterpret_cast(buf.data()) << ", " << size << ")" << std::endl; + + fuse_reply_buf(req, buf.data(), buf.size()); + } + else + { + if (static_cast(off) >= file->decomp_data.size()) + size = 0; + else if (off + size > file->decomp_data.size()) + size = file->decomp_data.size() - off; + + // std::cerr << "apfs_read: fuse_reply_buf(req, " << reinterpret_cast(file->decomp_data.data()) + off << ", " << size << ")" << std::endl; + + fuse_reply_buf(req, reinterpret_cast(file->decomp_data.data()) + off, size); + } +} + +static void dirbuf_add(fuse_req_t req, std::vector &dirbuf, const char *name, fuse_ino_t ino, mode_t mode) +{ + struct stat st; + size_t oldsize; + + memset(&st, 0, sizeof(st)); + oldsize = dirbuf.size(); + dirbuf.resize(oldsize + fuse_add_direntry(req, nullptr, 0, name, nullptr, 0)); + st.st_ino = ino; + st.st_mode = mode; + fuse_add_direntry(req, &dirbuf[oldsize], dirbuf.size() - oldsize, name, &st, dirbuf.size()); +} + +#define min(x, y) ((x) < (y) ? (x) : (y)) + +static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize, off_t off, size_t maxsize) +{ + if (static_cast(off) < bufsize) + return fuse_reply_buf(req, buf + off, min(bufsize - off, maxsize)); + else + return fuse_reply_buf(req, NULL, 0); +} + + +static void apfs_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, struct fuse_file_info *fi) +{ + ApfsDir dir(*g_volume); + std::vector dir_list; + Directory *dirptr = reinterpret_cast(fi->fh); + std::vector &dirbuf = dirptr->dirbuf; + bool rc; + + if (g_debug & Dbg_Info) + std::cout << "apfs_readdir: " << std::hex << ino << std::endl; + + if (dirbuf.size() == 0) + { + dirbuf.reserve(0x1000); + +#if 0 // Not needed + if (ino != 1) + { + ApfsDir::Inode dirrec; + + rc = dir.GetInode(dirrec, ino); + + if (!rc) + { + fuse_reply_err(req, ENOENT); + return; + } + + dirbuf_add(req, dirbuf, ".", ino); + dirbuf_add(req, dirbuf, "..", dirrec.ino.parent_id); + } +#endif + rc = dir.ListDirectory(dir_list, ino); + if (!rc) + { + fuse_reply_err(req, ENOENT); + return; + } + + for (size_t k = 0; k < dir_list.size(); k++) + dirbuf_add(req, dirbuf, dir_list[k].name.c_str(), dir_list[k].file_id, (dir_list[k].flags & DREC_TYPE_MASK) << 12); + } + + reply_buf_limited(req, dirbuf.data(), dirbuf.size(), off, size); +} + +static void apfs_readlink(fuse_req_t req, fuse_ino_t ino) +{ + ApfsDir dir(*g_volume); + bool rc = false; + std::vector data; + + rc = dir.GetAttribute(data, ino, "com.apple.fs.symlink"); + if (!rc) + fuse_reply_err(req, ENOENT); + else + fuse_reply_readlink(req, reinterpret_cast(data.data())); +} + +static void apfs_release(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi) +{ + if (g_debug & Dbg_Info) + std::cout << std::hex << "apfs_release " << ino << std::endl; + + File *file = reinterpret_cast(fi->fh); + delete file; + + fuse_reply_err(req, 0); +} + +static void apfs_releasedir(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi) +{ + if (g_debug & Dbg_Info) + std::cout << std::hex << "apfs_releasedir " << ino << std::endl; + + Directory *dir = reinterpret_cast(fi->fh); + delete dir; + + fuse_reply_err(req, 0); +} + +static void apfs_statfs(fuse_req_t req, fuse_ino_t ino) +{ + struct statvfs st; + + (void)ino; + + memset(&st, 0, sizeof(struct statvfs)); + + st.f_bsize = g_container->GetBlocksize(); + st.f_frsize = st.f_bsize; + st.f_blocks = g_container->GetBlockCount(); + st.f_bfree = g_container->GetFreeBlocks(); + st.f_bavail = st.f_bfree; + + st.f_files = 0; + st.f_ffree = 0; + st.f_favail = 0; + + st.f_fsid = 0; + st.f_flag = 0; + st.f_namemax = 255; + + fuse_reply_statfs(req, &st); +} + +struct apfs { + char *disk; + char *logfile; + int vol_id; +}; + +void usage(const char *name) +{ + std::cout << name << " [options] " << std::endl; + // [-d level] [-o options] [-v volume-id] [-r passphrase] [-s offset] + std::cout << std::endl; + std::cout << "Options:" << std::endl; + std::cout << "-d level : Enable debug output in the console." << std::endl; + std::cout << "-f device : Specify secondary device for fusion drives." << std::endl; + std::cout << "-o options : Additional mount options (see below)." << std::endl; + std::cout << "-v volume-id : Specify number of volume to be mounted." << std::endl; + std::cout << "-r passphrase : Specify volume passphrase. The driver will ask for it if it is" << std::endl; + std::cout << " needed and hasn't been specified here." << std::endl; + std::cout << "-s offset : Specify offset to the beginning of the container." << std::endl; + std::cout << "-p partition : Specify partition id containing the container." << std::endl; + std::cout << "-l : Allow driver to return potentially corrupt data instead of" << std::endl; + std::cout << " failing, if it can't handle something." << std::endl; + std::cout << std::endl; + std::cout << "Additional mount options (using -o):" << std::endl; + std::cout << "uid=N : Pretend that all files have UID N." << std::endl; + std::cout << "gid=N : Pretend that all files have GID N." << std::endl; + std::cout << "vol=N : Same as -v, select volume id to mount." << std::endl; + std::cout << "blksize=N : Set physical block size. Only needed if a partition table needs" << std::endl; + std::cout << " to be parsed and the sector size is not 512 bytes." << std::endl; + std::cout << "pass=... : Specify volume passphrase (same as -r)." << std::endl; + std::cout << "xid=N : Mount specific xid." << std::endl; + std::cout << "snap=N : Mount snapshot with given id. Use apfsutil for getting the ids." << std::endl; + std::cout << std::endl; +} + +bool add_option(std::string &optstr, const char *name, const char *value) +{ + if (!optstr.empty()) + optstr.push_back(','); + optstr.append(name); + if (value) + { + optstr.push_back('='); + optstr.append(value); + } + + return true; +} + +static int apfs_parse_fuse_opt(void *data, const char *arg, int key, struct fuse_args* outargs) +{ + if (key == FUSE_OPT_KEY_OPT) { + if (!strncmp(arg, "uid=", 4)) { + g_uid = strtol(strchr(arg, '=') + sizeof(char), nullptr, 10); + g_set_uid = true; + return 0; + } + else if (!strncmp(arg, "gid=", 4)) { + g_gid = strtol(strchr(arg, '=') + sizeof(char), nullptr, 10); + g_set_gid = true; + return 0; + } + else if (!strncmp(arg, "vol=", 4)) { + g_vol_id = strtoul(strchr(arg, '=') + sizeof(char), nullptr, 10); + return 0; + } + else if (!strncmp(arg, "blksize=", 8)) { + g_physblksize = strtoul(strchr(arg, '=') + sizeof(char), nullptr, 10); + return 0; + } + else if (!strncmp(arg, "pass=", 5)) { + g_password = strchr(arg, '=') + sizeof(char); + return 0; + } + else if (!strncmp(arg, "xid=", 4)) { + g_xid = strtoull(strchr(arg, '=') + sizeof(char), nullptr, 10); + return 0; + } + else if (!strncmp(arg, "snap=", 5)) { + g_snap_xid = strtoull(strchr(arg, '=') + sizeof(char), nullptr, 10); + return 0; + } + } + return 1; +} + +int main(int argc, char *argv[]) +{ + struct fuse_args args = FUSE_ARGS_INIT(0, nullptr); +#ifdef USE_FUSE2 + struct fuse_chan *ch; +#else + struct fuse_session *se; +#endif + const char *mountpoint = nullptr; + const char *main_dev_path = nullptr; + const char *tier2_dev_path = nullptr; + int err = -1; + int opt; + std::string mount_options; + uint64_t main_offset = 0; + uint64_t main_size = 0; + uint64_t tier2_offset = 0; + uint64_t tier2_size = 0; + int partition_id = -1; + + memset(&ops, 0, sizeof(ops)); + + // ops.bmap = apfs_bmap; + // ops.destroy = apfs_destroy; + ops.getattr = apfs_getattr; +#ifdef __linux__ + ops.getxattr = apfs_getxattr; +#endif +#ifdef __APPLE__ + ops.getxattr = apfs_getxattr_mac; +#endif + ops.listxattr = apfs_listxattr; + ops.lookup = apfs_lookup; + ops.open = apfs_open; + ops.opendir = apfs_opendir; + ops.read = apfs_read; + ops.readdir = apfs_readdir; + ops.readlink = apfs_readlink; + ops.release = apfs_release; + ops.releasedir = apfs_releasedir; + ops.statfs = apfs_statfs; + + g_uid = geteuid(); + g_gid = getegid(); + g_set_uid = false; + g_set_gid = false; + + while ((opt = getopt(argc, argv, "d:f:o:p:v:r:s:l")) != -1) + { + switch (opt) + { + case 'd': + g_debug = strtoul(optarg, nullptr, 10); + break; + case 'f': + tier2_dev_path = optarg; + break; + case 'o': + add_option(mount_options, optarg, nullptr); + break; + case 'p': + partition_id = strtoul(optarg, nullptr, 10); + break; + case 'v': + g_vol_id = strtoul(optarg, nullptr, 10); + break; + case 'r': + g_password = optarg; + break; + case 's': + main_offset = strtoul(optarg, nullptr, 10); + break; + case 'l': + g_lax = true; + break; + default: + usage(argv[0]); + return 1; + break; + } + } + + if ((argc - optind) != 2) + { + usage(argv[0]); + return 1; + } + + main_dev_path = argv[optind]; + mountpoint = argv[optind + 1]; + + add_option(mount_options, "ro", nullptr); + add_option(mount_options, "fsname", main_dev_path); + // add_option(mount_options, "allow_other", nullptr); + + fuse_opt_add_arg(&args, "apfs-fuse"); + fuse_opt_add_arg(&args, "-o"); + fuse_opt_add_arg(&args, mount_options.c_str()); + + if (fuse_opt_parse(&args, NULL, NULL, apfs_parse_fuse_opt) != 0) + { + std::cerr << "Unable to parse mount options!" << std::endl; + } + + + g_disk_main = Device::OpenDevice(main_dev_path); + if (tier2_dev_path) + g_disk_tier2 = Device::OpenDevice(tier2_dev_path); + + if (!g_disk_main) + { + std::cerr << "Error opening device!" << std::endl; + return 1; + } + + if (tier2_dev_path && !g_disk_tier2) + { + std::cerr << "Error opening secondary device!" << std::endl; + return 1; + } + + main_size = g_disk_main->GetSize(); + if (g_disk_tier2) + tier2_size = g_disk_tier2->GetSize(); + + if (main_offset >= main_size) + { + std::cerr << "Invalid container offset specified" << std::endl; + g_disk_main->Close(); + delete g_disk_main; + return 1; + } + + if (g_physblksize != 512) + { + if (g_disk_main) + g_disk_main->SetSectorSize(g_physblksize); + if (g_disk_tier2) + g_disk_tier2->SetSectorSize(g_physblksize); + } + + if (main_offset == 0) + { + GptPartitionMap gpt; + bool rc; + + rc = gpt.LoadAndVerify(*g_disk_main); + if (rc) + { + if (g_debug & Dbg_Info) + std::cout << "Found valid GPT partition table. Looking for APFS partition." << std::endl; + + if (partition_id == -1) + partition_id = gpt.FindFirstAPFSPartition(); + + if (partition_id != -1) + gpt.GetPartitionOffsetAndSize(partition_id, main_offset, main_size); + } + } + else + main_size -= main_offset; + + if (g_disk_tier2) + { + GptPartitionMap gpt; + + if (gpt.LoadAndVerify(*g_disk_tier2)) + { + if (g_debug & Dbg_Info) + std::cout << "Found valid GPT partition table on secondary device. Looking for APFS partition." << std::endl; + + partition_id = gpt.FindFirstAPFSPartition(); + + if (partition_id != -1) + gpt.GetPartitionOffsetAndSize(partition_id, tier2_offset, tier2_size); + } + } + + g_container = new ApfsContainer(g_disk_main, main_offset, main_size, g_disk_tier2, tier2_offset, tier2_size); + if (!g_container->Init(g_xid)) + { + std::cerr << "Unable to load container." << std::endl; + delete g_container; + g_disk_main->Close(); + delete g_disk_main; + return EINVAL; + } + g_volume = g_container->GetVolume(g_vol_id, g_password, g_snap_xid); + if (!g_volume) + { + std::cerr << "Unable to get volume!" << std::endl; + delete g_container; + g_disk_main->Close(); + delete g_disk_main; + return 1; + } + +#ifdef USE_FUSE2 + if ((ch = fuse_mount(mountpoint, &args)) != NULL) + { + struct fuse_session *se; + + se = fuse_lowlevel_new(&args, &ops, sizeof(ops), NULL); + if (se != NULL) + { + if (fuse_set_signal_handlers(se) != -1) + { + if (g_debug == 0) + fuse_daemonize(0); + fuse_session_add_chan(se, ch); + err = fuse_session_loop(se); + fuse_remove_signal_handlers(se); + fuse_session_remove_chan(ch); + } + fuse_session_destroy(se); + } + fuse_unmount(mountpoint, ch); + } +#else + se = fuse_session_new(&args, &ops, sizeof(ops), nullptr); + if (se) + { + if (fuse_set_signal_handlers(se) != -1) + { + if (fuse_session_mount(se, mountpoint) == 0) + { + if (g_debug == 0) + fuse_daemonize(0); + + err = fuse_session_loop(se); + + fuse_session_unmount(se); + } + fuse_remove_signal_handlers(se); + } + fuse_session_destroy(se); + } +#endif + fuse_opt_free_args(&args); + + delete g_volume; + delete g_container; + g_disk_main->Close(); + delete g_disk_main; + if (g_disk_tier2) { + g_disk_tier2->Close(); + delete g_disk_tier2; + } + + return err ? 1 : 0; +}