#cache #ld #parser #define #format #endianness #glibc

ldcache_rs

根据glibc中定义的多种格式解析ld.so.cache的库

1 个不稳定版本

0.1.0 2021年6月24日

#1954解析器实现

MIT 许可证

70KB
396 代码行

ld_cache_rs

一个简单的Rust解析器,用于解析ld.so.cache,无需任何C绑定

根据glibc中定义的多种格式解析ld.so.cache的库 此库不使用任何C绑定或尝试创建与数据匹配的结构,它使用纯解析,从而允许定义字节序。由于解析ld.so.cache对访问符号很有用,因此解析既严格又快速。如果您想制作一个价格低廉的ld.so.cache解析器,简单调用 strings /etc/ld.so.cache 即可。

使用方法

通常使用 Cache::new() 加载默认缓存(/etc/ld.so.cache),但如果您需要加载文件而不是默认文件,我们通过 Cache::parse(buf: &[u8], endianness: TargetEndian) 支持,其中字节序是三个值之一,大端、小端和本地。

use ldcache_rs::{Cache, CacheError, Entry};
use std::collections::hash_map::Iter;
fn main(){
    /// parse /etc/ld.so.cache with Native Endianness
    let cache:Result<Cache,CacheError>=Cache::new();
    let cache=cache.unwrap();
    /// Utility function, does the contains check on the entries with the full lib name
    let ok:bool=cache.contains("key");
    /// Utility function, get the entry based on the full lib name
    let entry:Option<&Entry>=cache.get("key");
    /// Utility function, get the paths of the lib based on the full lib name
    let paths:Option<Vec<&str>>=cache.get_paths("key");
    /// Utility function, get the first path of the lib based on the full lib name
    let path:Option<&str>=cache.get_path("key");
    /// Utility function, create an iterator over the entries
    let iter:Iter<'_, String, Entry>=cache.iter();
    /// Utility function, return a boolean indicating if there is a partial match
    /// As this utility will iterate over all elements, if you need the element please
    /// use get_partial or get_path_partial
    let ok_partial:bool=cache.contains_partial("key");
    /// Utility function, return the first element that contains the key inside the full lib
    /// name (partial match)
    let entry_partial:Option<&Entry>=cache.get_partial("key");
    /// Utility function, return the first lib paths for which the full lib
    /// name contains the key (partial match)
    let paths_partial:Option<Vec<&str>>=cache.get_paths_partial("key");
    /// Utility function, return the first lib path for which the full lib
    /// name contains the key (partial match)
    let path_partial:Option<&str>=cache.get_path_partial("key");
}

依赖关系

~310–780KB
~18K SLoC