Skip to content

Instantly share code, notes, and snippets.

@MikuroXina
Last active October 26, 2022 12:13
Show Gist options
  • Select an option

  • Save MikuroXina/93e24f27ddcf0fed4be86470bfbd7b2b to your computer and use it in GitHub Desktop.

Select an option

Save MikuroXina/93e24f27ddcf0fed4be86470bfbd7b2b to your computer and use it in GitHub Desktop.
The decomposer, converting the ani file to ico files.
use std::{env, fs, io::Write, path::PathBuf};
const ICON_MAGIC: &[u8] = b"icon";
fn next_partition(buf: &[u8]) -> Option<usize> {
buf.windows(ICON_MAGIC.len())
.position(|win| win == ICON_MAGIC)
}
fn hoge() -> Result<(), Box<dyn std::error::Error + 'static>> {
let path = PathBuf::from(
env::args()
.nth(1)
.expect("Please give .ani file path as an argument"),
);
let buf = fs::read(&path)?;
let mut buf = &buf[..];
let mut count = 0;
while let Some(idx) = next_partition(buf) {
let end = next_partition(&buf[idx + 1..]).unwrap_or(buf.len());
count += 1;
let mut out_file_name = path.file_name().unwrap().to_os_string();
out_file_name.push(format!("{:04}", count));
let mut out_path = path.clone();
out_path.set_file_name(out_file_name);
out_path.set_extension("ico");
let mut out = fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(out_path)?;
let mut chunk_idx = 8;
while idx + chunk_idx + 4 <= end {
out.write_all(&[if chunk_idx == 10 {
0x01
} else {
buf[idx + chunk_idx]
}])?;
chunk_idx += 1;
}
if idx + chunk_idx <= end {
out.write_all(&[buf[idx + chunk_idx]])?;
}
if end - idx - chunk_idx <= 3 {
let i = idx + chunk_idx + 1;
out.write_all(&buf[i..i + 2])?;
}
buf = &buf[idx + 1..];
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment