Skip to content

Commit

Permalink
fix: macos build and libesedb 20230824
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsetkookaburra committed Sep 30, 2023
1 parent 5680350 commit 86c3347
Show file tree
Hide file tree
Showing 1,053 changed files with 55,848 additions and 38,256 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
include:
- os: ubuntu-latest
- os: windows-latest
- os: macos-latest
steps:
- name: Clone Repository
uses: actions/checkout@v3
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode/
target/
Cargo.lock
tests/CacheStorage.edb
Expand Down
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ fn main() {

A `tests/CacheStorage.edb` can be obtained from from `AppData\Local\Packages\Microsoft.Windows.CloudExperienceHost_cw5n1h2txyewy\AppData\CacheStorage`

## Project TODO

* [ ] Document build.rs (modifying for leaf pages, etc)

## License and Acknowledgement

This project is made available under the [LGPL-3.0-or-later](./COPYING.LESSER).
Expand Down
1 change: 1 addition & 0 deletions libesedb-sys/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vscode/
target/
Cargo.lock
16 changes: 15 additions & 1 deletion libesedb-sys/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Rust `-sys` bindings to [`libyal/libesedb`](https://github.com/libyal/libesedb).
## Rebuilding Bindings

```
bindgen libesedb-20210424/include/libesedb.h -o src/bindings.rs -- -Ilibesedb-20210424/include -fparse-all-comments
bindgen libesedb-20230824/include/libesedb.h -o src/bindings.rs -- -Ilibesedb-20230824/include -fparse-all-comments
```

## Configuring Max Leaf-Pages
Expand All @@ -37,6 +37,20 @@ In your Cargo.toml:
LIBESEDB_MAXIMUM_NUMBER_OF_LEAF_PAGES = "32 * 1024"
```

## Updating Bundled libesedb

* Download/Clone head of the `main` branch
* `git clone --branch main --depth 1 https://github.com/libyal/libesedb`
* <https://github.com/libyal/libesedb/archive/refs/heads/main.zip>
* Follow Build Instructions <https://github.com/libyal/libesedb/wiki/Building>
* Run the following
* `./synclibs.sh`
* `./autogen.sh`
* `./configure`
* `make dist`
* Output should include a `.tar.gz` containing the source distribution version
* Note: I used Cygwin on Windows, you will need the packages mentioned here: <https://github.com/libyal/libesedb/wiki/Building#cygwin>

## Authors

```
Expand Down
155 changes: 103 additions & 52 deletions libesedb-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,69 +1,71 @@
/*
* Rust build script for bindings to libesedb
*
* Copyright (C) 2022-2023, Oliver Lenehan ~sunsetkookaburra
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

use cc::Build;
use std::env;
use std::fs::File;
use std::fs::{copy, create_dir_all, File};
use std::io::{BufRead, BufReader, Write};
use std::path::PathBuf;
use std::process::Command;
use walkdir::WalkDir;

const VERSION: &str = "20230824";

fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-env-changed=LIBESEDB_MAXIMUM_NUMBER_OF_LEAF_PAGES");

// docs.rs will attempt to compile, to allow for build scripts that generate
// bindings on the fly.
// We manually generate bindings, so there is no need to recompile the
// vendored C source on docs.rs
// vendored C source for docs.rs
if env::var("DOCS_RS").is_ok() {
println!("cargo:warning=docs.rs build, skipping C source build because of manual bindings.");
println!(
"cargo:warning=docs.rs build, skipping C source build because of manual bindings."
);
return;
}

// There are a couple different approaches to building '*-sys' crates.
// This site provides a great overview and initial starting point:
// https://kornel.ski/rust-sys-crate
// The build script here takes a weird combo hybrid approach that
// hopefully works well all-round.
// On Windows:
// * Compile with `cc` crate
// On Unices:
// * Run configure script
// * Compile with `cc` crate
let mut c = Build::new();

let mut page_tree_orig =
BufReader::new(File::open("libesedb-20210424/libesedb/libesedb_page_tree.c").unwrap())
.lines();
let page_tree = PathBuf::from(env::var("OUT_DIR").unwrap()).join("libesedb_page_tree.c");
let mut page_tree_new = File::create(&page_tree).unwrap();

let max_leaf_pages = env::var("LIBESEDB_MAXIMUM_NUMBER_OF_LEAF_PAGES")
.unwrap_or(String::from("32 * 1024"));
println!("cargo:warning=LIBESEDB_MAXIMUM_NUMBER_OF_LEAF_PAGES set to ({max_leaf_pages})");

while let Some(line) = page_tree_orig.next() {
let line = line.unwrap();
writeln!(page_tree_new, "{line}").unwrap();
if line.starts_with("#include \"esedb_page_values.h\"") {
writeln!(page_tree_new).unwrap();
writeln!(page_tree_new, "#undef LIBESEDB_MAXIMUM_NUMBER_OF_LEAF_PAGES").unwrap();
writeln!(page_tree_new, "#define LIBESEDB_MAXIMUM_NUMBER_OF_LEAF_PAGES ({max_leaf_pages})").unwrap();
}
}
drop(page_tree_new);

c.include("libesedb-20210424/include");
c.include("libesedb-20210424/common");
c.include("libesedb-20210424/libbfio");
c.include("libesedb-20210424/libcdata");
c.include("libesedb-20210424/libcerror");
c.include("libesedb-20210424/libcfile");
c.include("libesedb-20210424/libclocale");
c.include("libesedb-20210424/libcnotify");
c.include("libesedb-20210424/libcpath");
c.include("libesedb-20210424/libcsplit");
c.include("libesedb-20210424/libcthreads");
c.include("libesedb-20210424/libesedb");
c.include("libesedb-20210424/libfcache");
c.include("libesedb-20210424/libfdata");
c.include("libesedb-20210424/libfdatetime");
c.include("libesedb-20210424/libfguid");
c.include("libesedb-20210424/libfmapi");
c.include("libesedb-20210424/libfvalue");
c.include("libesedb-20210424/libfwnt");
c.include("libesedb-20210424/libmapidb");
c.include("libesedb-20210424/libuna");
// Copy source to OUT_DIR, so we can make changes without affecting the
// bundled source tree (allow for reproducibility).
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let src_dir = out_dir.join(format!("libesedb-{VERSION}"));

for ent in WalkDir::new("libesedb-20210424") {
for ent in WalkDir::new(format!("libesedb-{VERSION}")) {
let ent = ent.unwrap();
if ent.file_type().is_file() {
if ent.file_type().is_dir() {
// println!("{:?}", out_dir.join(ent.path()));
create_dir_all(out_dir.join(ent.path())).unwrap();
} else if ent.file_type().is_file() {
copy(ent.path(), out_dir.join(ent.path())).unwrap();
let name = ent.file_name().to_str().unwrap();
if name.ends_with(".c") {
let parent_name = ent
Expand All @@ -75,18 +77,51 @@ fn main() {
.to_str()
.unwrap();
if name == "libesedb_page_tree.c" {
c.file(&page_tree);
let max_leaf_pages = env::var("LIBESEDB_MAXIMUM_NUMBER_OF_LEAF_PAGES")
.unwrap_or(String::from("32 * 1024"));
println!("cargo:warning=LIBESEDB_MAXIMUM_NUMBER_OF_LEAF_PAGES set to ({max_leaf_pages})");
let mut page_tree = File::create(out_dir.join(ent.path())).unwrap();
for line in BufReader::new(File::open(ent.path()).unwrap()).lines() {
let line = line.unwrap();
writeln!(page_tree, "{line}").unwrap();
if line.starts_with("#include \"esedb_page_values.h\"") {
writeln!(page_tree).unwrap();
writeln!(page_tree, "#undef LIBESEDB_MAXIMUM_NUMBER_OF_LEAF_PAGES")
.unwrap();
writeln!(page_tree, "#define LIBESEDB_MAXIMUM_NUMBER_OF_LEAF_PAGES ({max_leaf_pages})").unwrap();
}
}
c.file(ent.path());
} else if parent_name.starts_with("lib") {
c.file(ent.path());
}
}
}
}

if cfg!(target_family = "unix") {
c.define("HAVE_CONFIG_H", None);
c.define("LOCALEDIR", "\"/usr/share/locale\"");
} else if cfg!(target_family = "windows") {
c.include(src_dir.join("include"));
c.include(src_dir.join("common"));
c.include(src_dir.join("libbfio"));
c.include(src_dir.join("libcdata"));
c.include(src_dir.join("libcerror"));
c.include(src_dir.join("libcfile"));
c.include(src_dir.join("libclocale"));
c.include(src_dir.join("libcnotify"));
c.include(src_dir.join("libcpath"));
c.include(src_dir.join("libcsplit"));
c.include(src_dir.join("libcthreads"));
c.include(src_dir.join("libesedb"));
c.include(src_dir.join("libfcache"));
c.include(src_dir.join("libfdata"));
c.include(src_dir.join("libfdatetime"));
c.include(src_dir.join("libfguid"));
c.include(src_dir.join("libfmapi"));
c.include(src_dir.join("libfvalue"));
c.include(src_dir.join("libfwnt"));
c.include(src_dir.join("libmapidb"));
c.include(src_dir.join("libuna"));

if env::var("CARGO_CFG_TARGET_FAMILY").unwrap() == "windows" {
c.define("HAVE_LOCAL_LIBBFIO", "1");
c.define("HAVE_LOCAL_LIBCDATA", "1");
c.define("HAVE_LOCAL_LIBCERROR", "1");
Expand All @@ -105,6 +140,22 @@ fn main() {
c.define("HAVE_LOCAL_LIBFWNT", "1");
c.define("HAVE_LOCAL_LIBMAPIDB", "1");
c.define("HAVE_LOCAL_LIBUNA", "1");
} else if env::var("CARGO_CFG_TARGET_FAMILY").unwrap() == "unix" {
c.define("HAVE_CONFIG_H", None);
c.define("LOCALEDIR", "\"/usr/share/locale\"");

Command::new("chmod")
.arg("+x")
.arg(src_dir.join("configure"))
.status()
.unwrap();
// Run configure on Unices (they have shell)
Command::new(src_dir.join("configure"))
.current_dir(src_dir)
.env("CC", c.get_compiler().cc_env())
.env("CFLAGS", c.get_compiler().cflags_env())
.status()
.unwrap();
}

c.compile("esedb");
Expand Down
19 changes: 16 additions & 3 deletions libesedb-sys/examples/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ fn main() {
exit(1);
}

let filename = CString::new(env::args().nth(1).unwrap_or("Windows.edb".to_string())).unwrap();
let filename =
CString::new(env::args().nth(1).unwrap_or("Windows.edb".to_string())).unwrap();

if libesedb_file_open(file, filename.as_ptr() as _, LIBESEDB_OPEN_READ, &mut error) != 1 {
eprintln!("Unable to open file.");
Expand Down Expand Up @@ -59,12 +60,24 @@ fn main() {
println!("The table name (including null byte) takes up {table_name_size} bytes.");

let mut table_name = vec![0; table_name_size.try_into().unwrap()];
if libesedb_table_get_utf8_name(table, table_name.as_mut_ptr(), table_name.len().try_into().unwrap(), &mut error) != 1 {
if libesedb_table_get_utf8_name(
table,
table_name.as_mut_ptr(),
table_name.len().try_into().unwrap(),
&mut error,
) != 1
{
eprintln!("Unable to get table name.");
libesedb_error_free(&mut error);
}

println!("Table Name: {}", CString::from_vec_with_nul(table_name).unwrap().to_str().unwrap());
println!(
"Table Name: {}",
CString::from_vec_with_nul(table_name)
.unwrap()
.to_str()
.unwrap()
);

if libesedb_file_close(file, &mut error) != 0 {
eprintln!("Unable to close file.");
Expand Down
Loading

0 comments on commit 86c3347

Please sign in to comment.