Add pinned APFS FUSE source and guarded read-only NAS probe

这个提交包含在:
mac 于 2026-11-11 22:07:35 +08:00 提交
1 父节点 08e7da7f23
当前提交 737773eb76
111 个文件已更改
+26555 -3

没有与搜索条件相匹配的文件

+482
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#include <cstring>
#include <iostream>
#include <fstream>
#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<uint8_t> 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<uint8_t> 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<const nx_superblock_t *>(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<const spaceman_phys_t *>(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<uint8_t> 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<uint8_t> 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<const le_uint64_t *>(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<uint64_t> cib_oid_list;
std::vector<uint8_t> 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<cib_addr_block_t *>(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]);
}
}
+85
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "Global.h"
#include "BTree.h"
#include "DiskStruct.h"
#include "Device.h"
#include "CheckPointMap.h"
#include "ApfsNodeMapperBTree.h"
#include "KeyMgmt.h"
#include <cstdint>
#include <vector>
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<uint8_t> 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;
};
+847
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cassert>
#include <cstring>
#include "ApfsDir.h"
#include "ApfsVolume.h"
#include "ApfsContainer.h"
#include "BTree.h"
#include "Util.h"
#ifndef _MSC_VER
template<size_t L>
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<j_inode_val_t *>(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<const uint8_t *>(bte.val);
const j_inode_val_t *obj = reinterpret_cast<const j_inode_val_t *>(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<const xf_blob_t *>(obj->xfields);
const x_field_t *xf = reinterpret_cast<const x_field_t *>(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<const uint64_t *>(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<const uint64_t *>(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<const uint32_t *>(xdata));
res.optional_present_flags |= Inode::INO_HAS_DOCUMENT_ID;
break;
case INO_EXT_TYPE_NAME:
res.name.assign(reinterpret_cast<const char *>(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<const uint64_t *>(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<const j_dstream_t *>(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<const uint64_t *>(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<DirRec> &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<j_drec_hashed_key_t *>(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<j_drec_key_t *>(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<const uint8_t *>(bte.key), bte.key_len, "entry key");
DumpBuffer(reinterpret_cast<const uint8_t *>(bte.val), bte.val_len, "entry val");
}
k = reinterpret_cast<const j_key_t *>(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<const j_drec_hashed_key_t *>(bte.key);
e.hash = hk->name_len_and_hash;
e.name = reinterpret_cast<const char *>(hk->name);
}
else
{
const j_drec_key_t *rk = reinterpret_cast<const j_drec_key_t *>(bte.key);
e.hash = 0;
e.name = reinterpret_cast<const char *>(rk->name);
}
// assert(res.val_len == sizeof(APFS_Name));
v = reinterpret_cast<const j_drec_val_t *>(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<const xf_blob_t *>(v->xfields);
const x_field_t *xf = reinterpret_cast<const x_field_t *>(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<const uint64_t *>(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<j_drec_hashed_key_t *>(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<const uint8_t *>(name), static_cast<uint16_t>(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<const char *>(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<j_drec_key_t *>(srch_key_buf);
skey->hdr.obj_id_and_type = parent_id | (static_cast<uint64_t>(APFS_TYPE_DIR_REC) << OBJ_TYPE_SHIFT);
skey->name_len = static_cast<uint16_t>(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<const char *>(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<const j_drec_val_t *>(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<uint8_t *>(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<const fext_tree_key_t *>(e.key);
fext_val = reinterpret_cast<const fext_tree_val_t *>(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<const j_file_extent_key_t *>(e.key);
ext_val = reinterpret_cast<const j_file_extent_val_t *>(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<std::string>& 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<const j_xattr_key_t *>(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<const char *>(ekey->name));
it.next();
}
return true;
}
bool ApfsDir::GetAttribute(std::vector<uint8_t>& data, uint64_t inode, const char* name)
{
uint8_t skey_buf[0x500];
j_xattr_key_t *skey = reinterpret_cast<j_xattr_key_t *>(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<int16_t>(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<const j_xattr_val_t *>(res.val);
// adata = reinterpret_cast<const uint8_t *>(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<const j_xattr_dstream_t *>(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<j_xattr_key_t *>(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<int16_t>(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<const j_xattr_val_t *>(res.val);
attr.flags = xv->flags;
attr.xdata_len = xv->xdata_len;
if (xv->flags & XATTR_DATA_STREAM)
{
xs = reinterpret_cast<const j_xattr_dstream_t *>(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<ApfsDir *>(context);
uint64_t ks = *reinterpret_cast<const le_uint64_t *>(skey);
uint64_t ke = *reinterpret_cast<const le_uint64_t *>(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<const j_drec_hashed_key_t *>(skey);
const j_drec_hashed_key_t *e = reinterpret_cast<const j_drec_hashed_key_t *>(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<const j_drec_key_t *>(skey);
const j_drec_key_t *e = reinterpret_cast<const j_drec_key_t *>(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<const j_file_extent_key_t *>(skey);
const j_file_extent_key_t *e = reinterpret_cast<const j_file_extent_key_t *>(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<const j_xattr_key_t *>(skey);
const j_xattr_key_t *e = reinterpret_cast<const j_xattr_key_t *>(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<const j_file_info_key_t *>(skey);
const j_file_info_key_t *e = reinterpret_cast<const j_file_info_key_t *>(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<const fext_tree_key_t *>(skey);
const fext_tree_key_t *e = reinterpret_cast<const fext_tree_key_t *>(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;
}
+147
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
#include <vector>
#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<DirRec> &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<std::string> &names, uint64_t inode);
bool GetAttribute(std::vector<uint8_t> &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<uint8_t> m_tmp_blk;
};
+29
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#include "ApfsNodeMapper.h"
ApfsNodeMapper::ApfsNodeMapper()
{
}
ApfsNodeMapper::~ApfsNodeMapper()
{
}
+40
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#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;
};
+137
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#include <cassert>
#include <cstring>
#include <iostream>
#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<const omap_key_t *>(skey);
const omap_key_t *ekey_map = reinterpret_cast<const omap_key_t *>(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<uint8_t> 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<const omap_key_t *>(res.key);
res_val = reinterpret_cast<const omap_val_t *>(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;
}
+45
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#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;
};
+29
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <cstdint>
// 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;
+394
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#include <cstring>
#include <vector>
#include <iostream>
#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<uint8_t> 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<uint8_t> 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<const j_snap_metadata_val_t *>(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<uint8_t> 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<const omap_phys_t*>(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<const j_snap_metadata_key_t *>(bte.key);
sm_val = reinterpret_cast<const j_snap_metadata_val_t *>(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<const omap_key_t*>(bte.key);
ov = reinterpret_cast<const omap_val_t*>(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<const j_key_t*>(skey);
const j_key_t *ke = reinterpret_cast<const j_key_t*>(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<const j_snap_name_key_t*>(skey);
ske = reinterpret_cast<const j_snap_name_key_t*>(ekey);
return apfs_strncmp(ske->name, ske->name_len, sks->name, sks->name_len);
break;
}
return 0;
}
+71
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <cstdint>
#include "DiskStruct.h"
#include "ApfsNodeMapperBTree.h"
#include "BTree.h"
#include <Crypto/AesXts.h>
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<const char *>(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;
};
+759
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#include <cassert>
#include <cstring>
#include <cstdio>
#include <cinttypes>
#include <iostream>
#include <iomanip>
#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<const uint64_t *>(skey);
uint64_t ke = *reinterpret_cast<const uint64_t *>(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<BTreeNode> &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<const btree_node_phys_t *>(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> BTreeNode::CreateNode(BTree & tree, const uint8_t * block, size_t blocksize, paddr_t paddr, const std::shared_ptr<BTreeNode> &parent, uint32_t parent_index)
{
const btree_node_phys_t *btn = reinterpret_cast<const btree_node_phys_t *>(block);
if (btn->btn_flags & BTNODE_FIXED_KV_SIZE)
return std::make_shared<BTreeNodeFix>(tree, block, blocksize, paddr, parent, parent_index);
else
return std::make_shared<BTreeNodeVar>(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<BTreeNode> &parent, uint32_t parent_index) :
BTreeNode(tree, block, blocksize, paddr, parent, parent_index)
{
m_entries = reinterpret_cast<const kvoff_t *>(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<BTreeNode> &parent, uint32_t parent_index) :
BTreeNode(tree, block, blocksize, paddr, parent, parent_index)
{
m_entries = reinterpret_cast<const kvloc_t *>(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<BTreeNode> 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<BTreeNode> node(m_root_node);
BTreeEntry e;
if (m_debug)
{
// std::cout << std::hex << "BTree::Lookup: key=" << *reinterpret_cast<const uint64_t *>(key) << " root=" << node->nodeid() << std::endl;
std::cout << "BTree::Lookup: ";
DumpHex(std::cout, reinterpret_cast<const uint8_t *>(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<const uint8_t*>(e.val), e.val_len, 32);
oid_parent = node->nodeid();
if (node->flags() & BTNODE_HASHED) {
const btn_index_node_val_t *binv = reinterpret_cast<const btn_index_node_val_t*>(e.val);
oid = binv->binv_child_oid + m_oid;
} else {
assert(e.val_len == sizeof(oid_t));
oid = *reinterpret_cast<const oid_t *>(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<BTreeNode> node(m_root_node);
BTreeEntry e;
if (m_debug)
std::cout << std::hex << "BTree::GetIterator: key=" << *reinterpret_cast<const uint64_t *>(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<const btn_index_node_val_t*>(e.val);
oid = binv->binv_child_oid + m_oid;
} else {
assert(e.val_len == sizeof(oid_t));
oid = *reinterpret_cast<const oid_t *>(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<BTreeNode> 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<const btn_index_node_val_t*>(e.val);
oid = binv->binv_child_oid + m_oid;
} else {
oid = *reinterpret_cast<const oid_t *>(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<BTreeNode> &node)
{
size_t k;
size_t cnt;
BTreeEntry e;
std::shared_ptr<BTreeNode> 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<const btn_index_node_val_t *>(e.val);
oid_child = binv->binv_child_oid + m_oid;
} else {
oid_child = *reinterpret_cast<const uint64_t *>(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<BTreeNode> BTree::GetNode(oid_t oid, const std::shared_ptr<BTreeNode> &parent, uint32_t parent_index)
{
std::shared_ptr<BTreeNode> 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<uint8_t> 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<BTreeNode> &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<BTreeNode>& 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<const uint8_t *>(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<const uint8_t *>(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<BTreeNode> &node, uint32_t index)
{
m_tree = tree;
m_node = node;
m_index = index;
}
BTreeIterator::~BTreeIterator()
{
}
void BTreeIterator::Setup(BTree* tree, const std::shared_ptr<BTreeNode>& 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<BTreeNode> 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<BTreeNode> BTreeIterator::next_node()
{
std::shared_ptr<BTreeNode> 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<BTreeNode>();
while (node->level() > 0) {
node->GetEntry(e, pidx);
if (node->flags() & BTNODE_HASHED) {
const btn_index_node_val_t *binv = reinterpret_cast<const btn_index_node_val_t *>(e.val);
oid = binv->binv_child_oid + m_tree->m_oid;
} else {
oid = *reinterpret_cast<const uint64_t *>(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;
}
+209
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vector>
#include <map>
#include <memory>
#include <mutex>
#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<BTreeNode> m_node;
};
class BTreeNode
{
protected:
BTreeNode(BTree &tree, const uint8_t *block, size_t blocksize, paddr_t paddr, const std::shared_ptr<BTreeNode> &parent, uint32_t parent_index);
public:
static std::shared_ptr<BTreeNode> CreateNode(BTree &tree, const uint8_t *block, size_t blocksize, paddr_t paddr, const std::shared_ptr<BTreeNode> &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<BTreeNode> &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<uint8_t> &block() const { return m_block; }
protected:
std::vector<uint8_t> 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<BTreeNode> 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<BTreeNode> &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<BTreeNode> &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<BTreeNode> &node);
uint32_t Find(const std::shared_ptr<BTreeNode> &node, const void *key, size_t key_size, BTCompareFunc func, void *context);
int FindBin(const std::shared_ptr<BTreeNode> &node, const void *key, size_t key_size, BTCompareFunc func, void *context, FindMode mode);
std::shared_ptr<BTreeNode> GetNode(oid_t oid, const std::shared_ptr<BTreeNode> &parent, uint32_t parent_index);
ApfsContainer &m_container;
ApfsVolume *m_volume;
std::shared_ptr<BTreeNode> 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<uint64_t, std::shared_ptr<BTreeNode>> m_nodes;
std::mutex m_mutex;
#endif
};
class BTreeIterator
{
public:
BTreeIterator();
BTreeIterator(BTree *tree, const std::shared_ptr<BTreeNode> &node, uint32_t index);
~BTreeIterator();
bool next();
void reset();
bool GetEntry(BTreeEntry &res) const;
void Setup(BTree *tree, const std::shared_ptr<BTreeNode> &node, uint32_t index);
private:
BTree *m_tree;
std::shared_ptr<BTreeNode> m_node;
uint32_t m_index;
std::shared_ptr<BTreeNode> next_node();
};
+2203
查看文件
文件差异过大,未显示。 加载差异
+117
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
#include <vector>
#include <ostream>
#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;
};
+86
查看文件
@@ -0,0 +1,86 @@
#include <cassert>
#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<const checkpoint_map_phys_t *>(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<const checkpoint_map_phys_t *>(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);
}
+27
查看文件
@@ -0,0 +1,27 @@
#pragma once
#include <vector>
#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<uint8_t> m_cpm_data;
oid_t m_cpm_oid;
uint32_t m_blksize;
};
+97
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#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;
}
+47
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <cstddef>
#include <cstdint>
#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;
};
+341
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#include <iomanip>
#include <iostream>
#include <cstring>
#include <cassert>
#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<uint8_t> &decompressed, const std::vector<uint8_t> &compressed)
{
if (compressed.size() < sizeof(CompressionHeader))
return false;
const CompressionHeader *hdr = reinterpret_cast<const CompressionHeader *>(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<uint8_t> 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<const CmpfRsrc *>(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<const uint32_t *>(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;
}
+36
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vector>
#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<uint8_t> &decompressed, const std::vector<uint8_t> &compressed);
+111
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#include <cstring>
#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;
}
+45
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <cstdint>
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;
};
+527
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#include <cstring>
#include <vector>
#include <iostream>
#include <iomanip>
#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<const uint8_t *>(&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<char *>(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 &sect = 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<char> 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<const MishHeader *>(data);
const MishEntry *entry = reinterpret_cast<const MishEntry *>(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
}
+85
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <cstdint>
#include <fstream>
#include <string>
#include <vector>
#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<DmgSection> 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
};
+94
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#ifdef __linux__
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <linux/fs.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#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
+45
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifdef __linux__
#include <cstddef>
#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
+109
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#ifdef __APPLE__
#include <unistd.h>
#include <sys/disk.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#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, &sector_count);
ioctl(m_device, DKIOCGETBLOCKSIZE, &sector_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
+44
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#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
+188
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#include <cstring>
#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<uint8_t *>(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;
}
+26
查看文件
@@ -0,0 +1,26 @@
#pragma once
#include <vector>
#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<uint64_t> m_band_offset;
uint64_t m_size;
uint64_t m_band_size;
DiskImageFile m_img;
};
+154
查看文件
@@ -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<uint8_t *>(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;
}
+30
查看文件
@@ -0,0 +1,30 @@
#pragma once
#include <vector>
#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<uint32_t> m_block_map;
FILE *m_vdi;
};
+62
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#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<char *>(data), len);
// TODO: Fehlerbehandlung ...
return true;
}
#endif
+46
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifdef _WIN32
#include <fstream>
#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
+91
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#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<const DISK_GEOMETRY_EX *>(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
+47
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifdef _WIN32
#include "Device.h"
// #define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <tchar.h>
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
+518
查看文件
@@ -0,0 +1,518 @@
#include <cstring>
#include <vector>
#include <iostream>
#include "Global.h"
#include "Endian.h"
#include <Crypto/Crypto.h>
#include <Crypto/TripleDes.h>
#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<char *>(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<uint8_t *>(data);
if (off & mask)
{
blkid = static_cast<uint32_t>(off / m_crypt_blocksize);
blkid = bswap_be(blkid);
m_image.seekg(m_crypt_offset + (off & ~mask));
m_image.read(reinterpret_cast<char *>(buffer), m_crypt_blocksize);
HMAC_SHA1(m_hmac_key, 0x14, reinterpret_cast<const uint8_t *>(&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<uint32_t>(off / m_crypt_blocksize);
blkid = bswap_be(blkid);
m_image.seekg(m_crypt_offset + (off & ~mask));
m_image.read(reinterpret_cast<char *>(buffer), m_crypt_blocksize);
HMAC_SHA1(m_hmac_key, 0x14, reinterpret_cast<const uint8_t *>(&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<char *>(&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<const uint8_t *>(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<uint8_t> data;
std::vector<uint8_t> 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<char *>(data.data()), data.size());
hdr = reinterpret_cast<const DmgCryptHeaderV2 *>(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<const DmgKeyPointer *>(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<char *>(kdata.data()), kdata.size());
keydata = reinterpret_cast<const DmgKeyData *>(kdata.data());
PBKDF2_HMAC_SHA1(reinterpret_cast<const uint8_t *>(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;
}
+39
查看文件
@@ -0,0 +1,39 @@
#pragma once
#include <cstdint>
#include <fstream>
#include <Crypto/Aes.h>
#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;
};
+1515
查看文件
文件差异过大,未显示。 加载差异
+217
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
/*
This file is for handling the different endiannesses used by Apple.
Also helps making the driver run on big-endian architectures.
*/
#pragma once
#include <cstdint>
// This should later be done by some configuration ...
#define APFS_LITTLE_ENDIAN
#undef APFS_BIG_ENDIAN
#ifdef _MSC_VER
// Definitions for Visual Studio
#include <intrin.h>
#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 <byteswap.h>
#include <endian.h>
#endif
#ifdef __APPLE__
// Definitions for macOS
#include <libkern/OSByteOrder.h>
#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<typename T>
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<typename T>
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
+36
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <cstdint>
// 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
};
+200
查看文件
@@ -0,0 +1,200 @@
#include <cstdio>
#include <cstring>
#include <cinttypes>
#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<PMAP_GptHeader *>(m_hdr_data.data());
if (hdr->Signature != 0x5452415020494645) {
m_hdr_data.resize(0x1000);
dev.Read(m_hdr_data.data(), 0x1000, 0x1000);
hdr = reinterpret_cast<PMAP_GptHeader *>(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<size_t>(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<const PMAP_GptHeader *>(m_hdr_data.data());
m_map = reinterpret_cast<const PMAP_Entry *>(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");
}
}
+35
查看文件
@@ -0,0 +1,35 @@
#pragma once
#include <cstdint>
#include <vector>
#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<uint8_t> m_hdr_data;
std::vector<uint8_t> m_entry_data;
const PMAP_GptHeader *m_hdr;
const PMAP_Entry *m_map;
unsigned int m_sector_size;
};
+741
查看文件
@@ -0,0 +1,741 @@
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
#include "ApfsContainer.h"
#include <Crypto/Asn1Der.h>
#include <Crypto/AesXts.h>
#include <Crypto/Sha256.h>
#include <Crypto/Crypto.h>
#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<const uint8_t *>(&mk->mk_locker);
m_data.assign(data, data + mk->mk_locker.kl_nbytes);
m_kl = reinterpret_cast<kb_locker_t *>(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<const keybag_entry_t *>(ptr);
len = (kb->ke_keylen + sizeof(keybag_entry_t) + 0x0F) & ~0xF;
ptr += len;
}
kb = reinterpret_cast<const keybag_entry_t *>(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<const keybag_entry_t *>(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<int>(vek.unk_82.unk_06) << ' ' << setw(2) << static_cast<int>(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<const prange_t *>(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<int>(kek.unk_82.unk_06) << ' ' << setw(2) << static_cast<int>(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<const uint8_t *>(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<const char *>(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<const prange_t *>(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<const char *>(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<const prange_t *>(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<const uint8_t *>(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<const prange_t *>(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<uint8_t> 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<const media_keybag_t *>(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;
}
+68
查看文件
@@ -0,0 +1,68 @@
#pragma once
#include <cstdint>
#include <vector>
#include <string>
#include <iosfwd>
#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<uint8_t> 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;
};
+476
查看文件
@@ -0,0 +1,476 @@
#include <string>
#include "PList.h"
PLObject::PLObject()
{
}
PLObject::~PLObject()
{
}
const PLInteger * PLObject::toInt() const
{
return dynamic_cast<const PLInteger *>(this);
}
const PLString * PLObject::toString() const
{
return dynamic_cast<const PLString *>(this);
}
const PLData * PLObject::toData() const
{
return dynamic_cast<const PLData *>(this);
}
const PLArray * PLObject::toArray() const
{
return dynamic_cast<const PLArray *>(this);
}
const PLDict * PLObject::toDict() const
{
return dynamic_cast<const PLDict *>(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<PLObject *>::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<std::string, PLObject*>::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<std::string, PLObject *>::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 </key>");
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 </integer>.");
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 </string>.");
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 </data>.");
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<uint8_t>& 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;
}
+182
查看文件
@@ -0,0 +1,182 @@
#pragma once
#include <vector>
#include <map>
#include <memory>
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<uint8_t> 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<PLObject *> &array() const { return m_array; }
private:
std::vector<PLObject *> 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<std::string, PLObject *> &dict() const { return m_dict; }
private:
std::map<std::string, PLObject *> 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<uint8_t> &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;
};
+328
查看文件
@@ -0,0 +1,328 @@
#include "Unicode.h"
#include <cstdint>
#include <cstddef>
#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<char32_t> &nfd, const std::vector<char32_t> &in, bool case_fold)
{
char32_t ch;
size_t k;
size_t out_size;
int rc;
std::vector<uint8_t> 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;
}
+10
查看文件
@@ -0,0 +1,10 @@
#pragma once
#include <cstdint>
#include <cstddef>
#include <vector>
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<char32_t> &out, const std::vector<char32_t> &in, bool case_fold);
+1027
查看文件
文件差异过大,未显示。 加载差异
+792
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#include "Unicode.h"
#include <cassert>
#include <iostream>
#include <iomanip>
#include <vector>
#include <sstream>
#if defined(__linux__) || defined(__APPLE__)
#include <termios.h>
#endif
#include <stdio.h>
#include "Util.h"
#include "Crc32.h"
#include <zlib.h>
#include <bzlib.h>
extern "C" {
#include <lzfse.h>
#include <lzvn_decode_base.h>
}
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<uint64_t>(sum2) << 32) | static_cast<uint64_t>(sum1);
}
bool VerifyBlock(const void *block, size_t size)
{
uint64_t cs;
const uint32_t * const data = reinterpret_cast<const uint32_t *>(block);
size /= sizeof(uint32_t);
cs = *reinterpret_cast<const uint64_t *>(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<const uint64_t *>(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<unsigned int>(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<unsigned>(uuid[k]);
st << '-';
for (k = 4; k < 6; k++)
st << setw(2) << static_cast<unsigned>(uuid[k]);
st << '-';
for (k = 6; k < 8; k++)
st << setw(2) << static_cast<unsigned>(uuid[k]);
st << '-';
for (k = 8; k < 10; k++)
st << setw(2) << static_cast<unsigned>(uuid[k]);
st << '-';
for (k = 10; k < 16; k++)
st << setw(2) << static_cast<unsigned>(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<unsigned int>(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<char>(str[k]);
else
st << '.';
st << std::endl;
}
uint32_t HashFilename(const uint8_t* utf8str, uint16_t name_len, bool case_fold)
{
std::vector<char32_t> utf32;
std::vector<char32_t> 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<const uint8_t *>(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<char32_t> s1_u32;
std::vector<char32_t> s2_u32;
std::vector<char32_t> s1_u32_nfd;
std::vector<char32_t> 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<char32_t> &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<uint8_t *>(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<size_t>(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<char *>(reinterpret_cast<const char *>(src));
strm.avail_in = src_size;
strm.next_out = reinterpret_cast<char *>(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);
}
}
+62
查看文件
@@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <cstdint>
#include <cstddef>
#include <string>
#include <ostream>
#include <vector>
#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<char32_t> &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)));