#include #include #include #include #include #include #include #include #include #include #include "FFS.h" using namespace emscripten; static double timePointToUnix(std::chrono::system_clock::time_point tp); static std::string normalizePath(const std::string &path); static std::string joinPath(const std::vector &parts); static std::vector splitPath(const std::string &input); static std::string getParentDir(const std::string &path); static std::string getBaseName(const std::string &path); static std::string toLower(const std::string &s); static const auto logger = FFSLogInterface::build(); EMSCRIPTEN_BINDINGS(libffshit) { class_("FFS") .constructor<>() .function("getPlatform", &FFS::getPlatform) .function("getIMEI", &FFS::getIMEI) .function("getModel", &FFS::getModel) .function("getWarnings", &FFS::getWarnings) .function("readDir", &FFS::readDir) .function("readFile", &FFS::readFile) .function("open", &FFS::open) .function("stat", &FFS::stat) .function("close", &FFS::close); register_vector("EntryArray"); register_vector("StringArray"); value_object("FileData") .field("data", &FFS::FileData::data) .field("size", &FFS::FileData::size); value_object("Entry") .field("name", &FFS::Entry::name) .field("path", &FFS::Entry::path) .field("size", &FFS::Entry::size) .field("timestamp", &FFS::Entry::timestamp) .field("isFile", &FFS::Entry::isFile) .field("isDirectory", &FFS::Entry::isDirectory) .field("isReadonly", &FFS::Entry::isReadonly) .field("isHidden", &FFS::Entry::isHidden) .field("isSystem", &FFS::Entry::isSystem); value_object("Options") .field("isOldSearchAlgorithm", &FFS::Options::isOldSearchAlgorithm) .field("searchStartAddress", &FFS::Options::searchStartAddress) .field("platform", &FFS::Options::platform) .field("skipBroken", &FFS::Options::skipBroken) .field("skipDuplicates", &FFS::Options::skipDuplicates) .field("debug", &FFS::Options::debug) .field("verboseProcessing", &FFS::Options::verboseProcessing) .field("verboseHeaders", &FFS::Options::verboseHeaders) .field("verboseData", &FFS::Options::verboseData); }; FFS::FFS() = default; void FFS::open(uintptr_t ptr, size_t size, const FFS::Options &options) { // Only the first init() takes effect, the interface is always installed to collect warnings FULLFLASH::Log::Logger::init(logger); logger->debug = options.debug; logger->warnings.clear(); try { auto *data = reinterpret_cast(ptr); if (options.platform == "auto") { m_fullflash = FULLFLASH::FULLFLASH::build(data, size); const auto &detector = m_fullflash->get_detector(); m_platform = detector.get_platform(); if (m_platform == FULLFLASH::Platform::Type::UNK) throw FULLFLASH::Exception("Unknown platform"); m_fullflash->load_partitions(options.isOldSearchAlgorithm, options.searchStartAddress); m_partitions = m_fullflash->get_partitions(); if (m_partitions->get_fs_platform() != detector.get_platform()) m_platform = m_partitions->get_fs_platform(); } else { m_platform = FULLFLASH::Platform::StringToType.at(options.platform); m_fullflash = FULLFLASH::FULLFLASH::build(data, size, m_platform); m_fullflash->load_partitions(options.isOldSearchAlgorithm, options.searchStartAddress); m_partitions = m_fullflash->get_partitions(); } m_filesystem = FULLFLASH::Filesystem::build(m_platform, m_partitions); m_filesystem->log_verbose_processing(options.verboseProcessing); m_filesystem->log_verbose_headers(options.verboseHeaders); m_filesystem->log_verbose_data(options.verboseData); m_filesystem->load(options.skipBroken, options.skipDuplicates); m_rootDir = m_filesystem->get_root(); m_warnings = std::move(logger->warnings); } catch (const std::bad_alloc &) { close(); throw std::runtime_error("Not enough memory to load the fullflash"); } catch (const FULLFLASH::Partitions::Exception &e) { close(); throw std::runtime_error("[FULLFLASH::Partitions::Exception] " + e.what()); } catch (const FULLFLASH::Filesystem::Exception &e) { close(); throw std::runtime_error("[FULLFLASH::Filesystem::Exception] " + e.what()); } catch (const FULLFLASH::Exception &e) { close(); throw std::runtime_error("[FULLFLASH::Exception] " + e.what()); } catch (const FULLFLASH::BaseException &e) { close(); throw std::runtime_error("[FULLFLASH::BaseException] " + e.what()); } catch(...) { close(); throw; } } void FFS::close() { m_rootDir.reset(); m_filesystem.reset(); m_partitions.reset(); m_warnings.clear(); } FFS::~FFS() { close(); FULLFLASH::Log::Logger::init(nullptr); } std::string FFS::getPlatform() const { if (!m_partitions) throw std::runtime_error("FFS is closed."); return FULLFLASH::Platform::TypeToString.at(m_platform); } std::string FFS::getIMEI() const { if (!m_partitions) throw std::runtime_error("FFS is closed."); return m_fullflash->get_detector().get_imei(); } std::string FFS::getModel() const { if (!m_partitions) throw std::runtime_error("FFS is closed."); return m_fullflash->get_detector().get_model(); } std::vector FFS::getWarnings() const { if (!m_partitions) throw std::runtime_error("FFS is closed."); return m_warnings; } FFS::FileData FFS::readFile(const std::string &path) const { if (!m_partitions) throw std::runtime_error("FFS is closed."); const auto dirOrFile = getDirOrFilePtr(path); if (!dirOrFile.file) return { .data = 0, .size = 0 }; const auto data = dirOrFile.file->get_data(); const auto dataSize = data.get_size(); auto *buffer = static_cast(malloc(dataSize)); memcpy(buffer, data.get_data().get(), dataSize); return { .data = reinterpret_cast(buffer), .size = dataSize }; } std::tuple FFS::getDirPtr(const std::string &path) const { FULLFLASH::Filesystem::Directory::Ptr dir = m_rootDir; const auto parts = splitPath(toLower(path)); std::vector canonicalPathParts; if (parts.empty()) return { dir, "/" }; // canonicalPathParts.push_back(dir->get_name()); for (const auto &part: parts) { bool found = false; for (const auto &subdir: dir->get_subdirs()) { if (toLower(subdir->get_name()) == part) { dir = subdir; canonicalPathParts.push_back(dir->get_name()); found = true; break; } } if (!found) return { nullptr, "" }; } return { dir, joinPath(canonicalPathParts) }; } FFS::Entry FFS::stat(const std::string &path) const { if (!m_partitions) throw std::runtime_error("FFS is closed."); auto parentPath = getParentDir(path); const auto dirOrFile = getDirOrFilePtr(path); if (dirOrFile.dir) { Entry entry = {}; fillAttributes(&entry, dirOrFile.dir, parentPath); return entry; } if (dirOrFile.file) { Entry entry = {}; fillAttributes(&entry, dirOrFile.file, parentPath); return entry; } return {}; } FFS::DirOrFile FFS::getDirOrFilePtr(const std::string &path) const { const auto normalizedPath = normalizePath(path); if (normalizedPath == "/") return { .dir = m_rootDir, .canonicalPath = "/" }; const auto [parentDir, parentDirCanonicalPath] = getDirPtr(getParentDir(normalizedPath)); if (!parentDir) return {}; const auto baseNameLC = getBaseName(toLower(path)); for (const auto &subdir: parentDir->get_subdirs()) { if (toLower(subdir->get_name()) == baseNameLC) return { .dir = subdir, .canonicalPath = parentDirCanonicalPath + "/" + subdir->get_name() }; } for (const auto &file: parentDir->get_files()) { if (toLower(file->get_name()) == baseNameLC) return { .file = file, .canonicalPath = parentDirCanonicalPath + "/" + file->get_name() }; } return {}; } std::vector FFS::readDir(const std::string &path) const { if (!m_partitions) throw std::runtime_error("FFS is closed."); const auto [parentDir, parentDirCanonicalPath] = getDirPtr(path); std::vector entries; if (parentDir) { for (const auto &subdir: parentDir->get_subdirs()) { entries.resize(entries.size() + 1); auto &entry = entries.back(); fillAttributes(&entry, subdir, parentDirCanonicalPath); } for (const auto &file: parentDir->get_files()) { entries.resize(entries.size() + 1); auto &entry = entries.back(); fillAttributes(&entry, file, parentDirCanonicalPath); } } return entries; } void FFS::fillAttributes(Entry *entry, const FULLFLASH::Filesystem::File::Ptr &file, const std::string &parentPath) { entry->name = file->get_name(); entry->path = parentPath; entry->timestamp = timePointToUnix(file->get_timestamp()) * 1000; entry->size = file->get_size(); entry->isFile = true; entry->isReadonly = file->get_attributes().is_readonly(); entry->isHidden = file->get_attributes().is_hidden(); entry->isSystem = file->get_attributes().is_system(); } void FFS::fillAttributes(Entry *entry, const FULLFLASH::Filesystem::Directory::Ptr &dir, const std::string &parentPath) const { entry->name = dir == m_rootDir ? "" : dir->get_name(); entry->path = parentPath; entry->timestamp = timePointToUnix(dir->get_timestamp()) * 1000; entry->isDirectory = true; entry->isReadonly = dir->get_attributes().is_readonly(); entry->isHidden = dir->get_attributes().is_hidden(); entry->isSystem = dir->get_attributes().is_system(); } static std::string normalizePath(const std::string &path) { return joinPath(splitPath(path)); } static std::string getBaseName(const std::string &path) { const auto parts = splitPath(path); return parts.empty() ? "" : parts.back(); } static std::string getParentDir(const std::string &path) { auto parts = splitPath(path); if (!parts.empty()) parts.pop_back(); return joinPath(parts); } static std::vector splitPath(const std::string &input) { if (input.empty() || input[0] != '/') throw std::invalid_argument("Path must be absolute"); std::vector parts; std::string part; size_t i = 1; for (; i <= input.size(); ++i) { if (i == input.size() || input[i] == '/') { if (!part.empty()) { if (part == ".") { // skip } else if (part == "..") { if (!parts.empty()) { parts.pop_back(); } } else { parts.push_back(part); } part.clear(); } while (i + 1 < input.size() && input[i + 1] == '/') i++; } else { part += input[i]; } } return parts; } static std::string joinPath(const std::vector &parts) { if (parts.empty()) return "/"; std::string result; for (const auto &part: parts) result += "/" + part; return result; } static double timePointToUnix(std::chrono::system_clock::time_point tp) { return static_cast(std::chrono::duration_cast(tp.time_since_epoch()).count()); } static std::string toLower(const std::string &s) { std::string result = s; std::transform(result.begin(), result.end(), result.begin(), [](const char c) { return std::tolower(static_cast(c)); }); return result; }