use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
pub const MAGIC: &[u8; 8] = b"SHUNSTMP";
pub const FORMAT_REV: u32 = 1;
pub const TRAILER_LEN: usize = 64;
pub const SHELL_LOCALES: [&str; 8] = ["en", "zh-Hans", "zh-Hant", "ja", "ko", "fr", "ru", "es"];
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Trailer {
pub offset: u64,
pub len: u64,
pub sha256: [u8; 32],
}
impl Trailer {
pub fn generic() -> [u8; TRAILER_LEN] {
let mut t = [0u8; TRAILER_LEN];
t[..8].copy_from_slice(MAGIC);
t[8..12].copy_from_slice(&FORMAT_REV.to_le_bytes());
t[16..24].copy_from_slice(&0u64.to_le_bytes());
t[24..32].copy_from_slice(&0u64.to_le_bytes());
t[32..64].copy_from_slice(&Sha256::digest([]));
t
}
}
pub fn parse_trailer(exe: &[u8]) -> Result<Trailer, String> {
if exe.len() < TRAILER_LEN {
return Err(format!(
"too short for a stamp trailer: {} bytes",
exe.len()
));
}
let mut tail = [0u8; TRAILER_LEN];
tail.copy_from_slice(&exe[exe.len() - TRAILER_LEN..]);
parse_tail(&tail, exe.len() as u64)
}
pub fn parse_tail(tail: &[u8; TRAILER_LEN], total_len: u64) -> Result<Trailer, String> {
if total_len < TRAILER_LEN as u64 {
return Err(format!("too short for a stamp trailer: {total_len} bytes"));
}
let t = &tail[..];
if &t[..8] != MAGIC {
return Err("no stamp trailer magic — not a shun shell binary".into());
}
let rev = u32::from_le_bytes(t[8..12].try_into().unwrap());
if rev != FORMAT_REV {
return Err(format!(
"stamp container format rev {rev} unsupported — this build understands rev {FORMAT_REV}"
));
}
let flags = u32::from_le_bytes(t[12..16].try_into().unwrap());
if flags != 0 {
return Err(format!("unknown trailer flags {flags:#x}"));
}
let offset = u64::from_le_bytes(t[16..24].try_into().unwrap());
let len = u64::from_le_bytes(t[24..32].try_into().unwrap());
let mut sha256 = [0u8; 32];
sha256.copy_from_slice(&t[32..64]);
if len == 0 {
return Ok(Trailer {
offset,
len,
sha256,
});
}
let end = offset.saturating_add(len);
if end != total_len - TRAILER_LEN as u64 {
return Err(format!(
"container [{offset}, {end}) does not abut the trailer at {} — corrupt stamp",
total_len - TRAILER_LEN as u64
));
}
Ok(Trailer {
offset,
len,
sha256,
})
}
pub fn verify_container(trailer: &Trailer, container: &[u8]) -> Result<(), String> {
if container.len() as u64 != trailer.len {
return Err(format!(
"container length {} != trailer's {}",
container.len(),
trailer.len
));
}
let got = Sha256::digest(container);
if got.as_slice() != trailer.sha256 {
return Err("container sha256 mismatch — the stamped exe is corrupt".into());
}
Ok(())
}
pub fn build_trailer(offset: u64, container: &[u8]) -> [u8; TRAILER_LEN] {
let mut t = [0u8; TRAILER_LEN];
t[..8].copy_from_slice(MAGIC);
t[8..12].copy_from_slice(&FORMAT_REV.to_le_bytes());
t[16..24].copy_from_slice(&offset.to_le_bytes());
t[24..32].copy_from_slice(&(container.len() as u64).to_le_bytes());
t[32..64].copy_from_slice(&Sha256::digest(container));
t
}
pub mod entry {
pub const CONFIG: &str = "shun-config.json";
pub const STEPS: &str = "shun-steps.json";
pub const LICENSE_DOCS: &str = "shun-license-docs.json";
pub const LOGO: &str = "shun-logo.bin";
pub const LOGO_KIND: &str = "shun-logo-kind.txt";
pub const META: &str = "shun-meta.json";
pub const PAYLOAD: &str = "shun-payload.bin";
}
pub fn pack_container(entries: &[(&str, &[u8])]) -> Result<Vec<u8>, String> {
let mut builder = tar::Builder::new(Vec::new());
for &(name, data) in entries.iter() {
let mut header = tar::Header::new_gnu();
header.set_size(data.len() as u64);
header.set_mode(0o644);
header.set_mtime(0);
builder
.append_data(&mut header, name, data)
.map_err(|e| format!("container entry {name}: {e}"))?;
}
builder
.into_inner()
.map_err(|e| format!("container finalize: {e}"))
}
#[derive(Debug, Clone)]
pub struct ContainerIndex {
ranges: BTreeMap<String, (u64, u64)>,
}
impl ContainerIndex {
pub fn parse(container: &[u8]) -> Result<Self, String> {
let mut ranges = BTreeMap::new();
let mut pos = 0usize;
while pos + 512 <= container.len() {
let header = &container[pos..pos + 512];
if header.iter().all(|&b| b == 0) {
break; }
if &header[257..262] != b"ustar" {
return Err(format!(
"container entry at {pos}: not a tar header (long-name formats unsupported)"
));
}
let name_end = header[..100].iter().position(|&b| b == 0).unwrap_or(100);
let name = String::from_utf8_lossy(&header[..name_end]).into_owned();
let size = parse_size(&header[124..136])
.map_err(|e| format!("container entry '{name}': {e}"))?;
let data = (pos + 512) as u64;
let padded = size.div_ceil(512) * 512;
if data + size > container.len() as u64 {
return Err(format!("container entry '{name}': truncated payload"));
}
ranges.insert(name, (data, size));
pos = (data + padded) as usize;
}
Ok(Self { ranges })
}
pub fn range(&self, name: &str) -> Option<(u64, u64)> {
self.ranges.get(name).copied()
}
pub fn data<'a>(&self, container: &'a [u8], name: &str) -> Option<&'a [u8]> {
self.range(name)
.map(|(off, len)| &container[off as usize..(off + len) as usize])
}
}
fn parse_size(bytes: &[u8]) -> Result<u64, String> {
if let Some(&first) = bytes.first() {
if first & 0x80 != 0 {
let mut v: u64 = (first & 0x7f) as u64;
for &b in &bytes[1..] {
v = (v << 8) | b as u64;
}
return Ok(v);
}
}
let s = String::from_utf8_lossy(bytes);
let trimmed = s.trim_matches(char::from(0)).trim();
if trimmed.is_empty() {
return Ok(0);
}
u64::from_str_radix(trimmed, 8).map_err(|e| format!("bad octal size '{trimmed}': {e}"))
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StampMeta {
pub shun_version: String,
pub product_name: String,
pub product_version: String,
pub variant: Option<String>,
pub flavor: String,
pub log_name: String,
}
pub struct StampParts {
pub config_json: String,
pub steps_json: String,
pub license_docs_json: String,
pub logo_bytes: Vec<u8>,
pub logo_kind: String,
pub payload_archive: Vec<u8>,
pub meta: StampMeta,
}
impl StampParts {
pub fn pack_container(&self) -> Result<Vec<u8>, String> {
let meta = self.meta_bytes();
let entries: [(&str, &[u8]); 7] = [
(entry::CONFIG, self.config_json.as_bytes()),
(entry::STEPS, self.steps_json.as_bytes()),
(entry::LICENSE_DOCS, self.license_docs_json.as_bytes()),
(entry::LOGO, &self.logo_bytes),
(entry::LOGO_KIND, self.logo_kind.as_bytes()),
(entry::META, &meta),
(entry::PAYLOAD, &self.payload_archive),
];
pack_container(&entries)
}
fn meta_bytes(&self) -> Vec<u8> {
serde_json::to_vec_pretty(&self.meta).expect("stamp meta serializes")
}
}
#[derive(Debug, Clone)]
pub struct StampIdentity {
pub company: String,
pub description: String,
pub internal_name: String,
pub version: String,
pub ico: Option<Vec<u8>>,
}
#[cfg(windows)]
pub fn stamp_shell(
shell_exe: &Path,
out_path: &Path,
parts: &StampParts,
identity: &StampIdentity,
) -> Result<(), String> {
let exe = std::fs::read(shell_exe)
.map_err(|e| format!("read shell binary {}: {e}", shell_exe.display()))?;
let trailer_at_end = exe.len() >= TRAILER_LEN
&& &exe[exe.len() - TRAILER_LEN..exe.len() - TRAILER_LEN + 8] == MAGIC;
let input_body_len = if trailer_at_end {
let trailer = parse_trailer(&exe)?;
if trailer.len != 0 {
return Err(format!(
"shell binary {} already carries a container ({} bytes) — stamp the GENERIC shell",
shell_exe.display(),
trailer.len
));
}
exe.len() - TRAILER_LEN
} else if exe.starts_with(b"MZ") {
exe.len()
} else {
return Err(format!(
"{} is neither a PE image nor a shun shell binary",
shell_exe.display()
));
};
let tmp = out_path.with_extension("stamping.exe");
std::fs::write(&tmp, &exe[..input_body_len])
.map_err(|e| format!("write staging copy {}: {e}", tmp.display()))?;
stamp_win::patch_identity(&tmp, identity)?;
let exe = std::fs::read(&tmp).map_err(|e| format!("read patched exe: {e}"))?;
let _ = std::fs::remove_file(&tmp);
let body_len = match parse_trailer(&exe) {
Ok(trailer) if trailer.len == 0 => exe.len() - TRAILER_LEN,
Ok(_) => {
return Err(
"the identity-patched exe unexpectedly carries a non-empty container".into(),
);
}
Err(_) => exe.len(),
};
let mut out = exe;
out.truncate(body_len);
let offset = body_len as u64;
let container = parts.pack_container()?;
let trailer = build_trailer(offset, &container);
out.extend_from_slice(&container);
out.extend_from_slice(&trailer);
if let Some(parent) = out_path.parent() {
std::fs::create_dir_all(parent).map_err(|e| format!("create {}: {e}", parent.display()))?;
}
std::fs::write(out_path, &out)
.map_err(|e| format!("write artifact {}: {e}", out_path.display()))
}
#[cfg(not(windows))]
pub fn stamp_shell(
_shell_exe: &Path,
_out_path: &Path,
_parts: &StampParts,
_identity: &StampIdentity,
) -> Result<(), String> {
Err(
"stamping requires Windows: the version resource and icon are patched with the OS \
resource editor (BeginUpdateResourceW). Run the stamp on a Windows host — the shun \
release and product release CI both do."
.into(),
)
}
#[cfg(windows)]
#[path = "stamp_win.rs"]
pub mod stamp_win;
pub mod verinfo {
use super::StampIdentity;
use std::collections::BTreeMap;
const LANG_ID: u16 = 0x0409;
const CHARSET_ID: u16 = 0x04b0;
fn push_wstr(out: &mut Vec<u8>, s: &str) {
for unit in s.encode_utf16() {
out.extend_from_slice(&unit.to_le_bytes());
}
out.extend_from_slice(&0u16.to_le_bytes());
}
fn pad32(out: &mut Vec<u8>) {
while out.len() % 4 != 0 {
out.push(0);
}
}
fn header(total: u16, value_len: u16, value_type: u16, key: &str, out: &mut Vec<u8>) {
out.extend_from_slice(&total.to_le_bytes());
out.extend_from_slice(&value_len.to_le_bytes());
out.extend_from_slice(&value_type.to_le_bytes());
push_wstr(out, key);
pad32(out);
}
fn key_field_len(s: &str, prefix: usize) -> usize {
let bytes = (s.encode_utf16().count() + 1) * 2;
let aligned_end = (prefix + bytes).div_ceil(4) * 4;
aligned_end - prefix
}
fn string_block(key: &str, value: &str) -> Vec<u8> {
let value_len = (value.encode_utf16().count() + 1) * 2;
let mut body = Vec::new();
push_wstr(&mut body, value);
pad32(&mut body);
let mut out = Vec::new();
header(
(6 + key_field_len(key, 6) + body.len()) as u16,
value_len as u16,
1,
key,
&mut out,
);
out.extend_from_slice(&body);
out
}
fn packed_version(version: &str) -> (u32, u32) {
let mut parts = [0u16; 4];
for (i, part) in version.split('.').take(4).enumerate() {
parts[i] = part.trim().parse().unwrap_or(0);
}
let ms = ((parts[0] as u32) << 16) | parts[1] as u32;
let ls = ((parts[2] as u32) << 16) | parts[3] as u32;
(ms, ls)
}
pub fn build(ident: &StampIdentity) -> Vec<u8> {
let (file_ms, file_ls) = packed_version(&ident.version);
let fixed: [u32; 13] = [
0xfeef04bd, 0x0001_0000, file_ms,
file_ls,
file_ms,
file_ls,
0x0000_003f, 0, 0x0004_0004, 1, 0, 0, 0, ];
let strings: [(&str, &str); 7] = [
("CompanyName", &ident.company),
("FileDescription", &ident.description),
("FileVersion", &ident.version),
("InternalName", &ident.internal_name),
("OriginalFilename", &format!("{}.exe", ident.internal_name)),
("ProductName", &ident.description),
("ProductVersion", &ident.version),
];
let mut blocks = Vec::new();
for (key, value) in strings {
blocks.extend_from_slice(&string_block(key, value));
}
let lang_key = format!("{LANG_ID:04x}{CHARSET_ID:04x}");
let table_len = 6 + key_field_len(&lang_key, 6) + blocks.len();
let mut table = Vec::new();
header(table_len as u16, 0, 1, &lang_key, &mut table);
table.extend_from_slice(&blocks);
let sfi_len = 6 + key_field_len("StringFileInfo", 6) + table.len();
let mut sfi = Vec::new();
header(sfi_len as u16, 0, 1, "StringFileInfo", &mut sfi);
sfi.extend_from_slice(&table);
let var_value: [u8; 4] = [
LANG_ID.to_le_bytes()[0],
LANG_ID.to_le_bytes()[1],
CHARSET_ID.to_le_bytes()[0],
CHARSET_ID.to_le_bytes()[1],
];
let var_body_len = 6 + key_field_len("Translation", 6) + var_value.len();
let mut var_body = Vec::new();
header(
var_body_len as u16,
var_value.len() as u16,
1,
"Translation",
&mut var_body,
);
var_body.extend_from_slice(&var_value);
let vfi_len = 6 + key_field_len("VarFileInfo", 6) + var_body.len();
let mut vfi = Vec::new();
header(vfi_len as u16, 0, 1, "VarFileInfo", &mut vfi);
vfi.extend_from_slice(&var_body);
let total =
6 + key_field_len("VS_VERSION_INFO", 6) + fixed.len() * 4 + sfi.len() + vfi.len();
let mut out = Vec::with_capacity(total);
header(total as u16, 52, 0, "VS_VERSION_INFO", &mut out);
for word in fixed {
out.extend_from_slice(&word.to_le_bytes());
}
out.extend_from_slice(&sfi);
out.extend_from_slice(&vfi);
out
}
pub fn parse_strings(block: &[u8]) -> Result<BTreeMap<String, String>, String> {
let mut out = BTreeMap::new();
let total = u16_at(block, 0)? as usize;
let children_at = 6 + key_field_len("VS_VERSION_INFO", 6) + 52;
parse_children(block, children_at, total, &mut out)?;
Ok(out)
}
fn u16_at(b: &[u8], pos: usize) -> Result<u16, String> {
b.get(pos..pos + 2)
.map(|s| u16::from_le_bytes(s.try_into().unwrap()))
.ok_or_else(|| "truncated u16".to_string())
}
fn read_wstr(b: &[u8], pos: &mut usize) -> Result<String, String> {
let mut units = Vec::new();
while *pos + 2 <= b.len() {
let unit = u16_at(b, *pos)?;
*pos += 2;
if unit == 0 {
return Ok(String::from_utf16_lossy(&units));
}
units.push(unit);
}
Err("unterminated wide string".into())
}
fn parse_children(
b: &[u8],
mut pos: usize,
end: usize,
out: &mut BTreeMap<String, String>,
) -> Result<(), String> {
while pos + 6 <= end {
let total = u16_at(b, pos)? as usize;
let value_len = u16_at(b, pos + 2)? as usize;
if total < 6 || pos + total > end {
return Err(format!("block at {pos}: bad length {total} (end {end})"));
}
let mut cur = pos + 6;
let key = read_wstr(b, &mut cur)?;
pad(&mut cur);
if value_len == 0 {
parse_children(b, cur, pos + total, out)?;
} else if key == "Translation" {
} else {
let value = read_wstr(b, &mut cur)?;
out.insert(key, value);
}
pos += total;
}
Ok(())
}
fn pad(pos: &mut usize) {
*pos = pos.div_ceil(4) * 4;
}
}
use crate::config::{BackgroundSpec, ShunConfig, ThemeConfig, WallpaperSourceSpec};
pub fn resolved_config(
manifest: &Path,
variant: Option<&str>,
) -> Result<(ShunConfig, PathBuf), String> {
let mut config = ShunConfig::from_any(manifest)
.map_err(|e| format!("manifest {}: {e}", manifest.display()))?;
if let Some(variant) = variant {
config
.apply_variant(variant)
.map_err(|e| format!("variant {variant}: {e}"))?;
}
let manifest_dir = manifest
.parent()
.map(|d| d.to_path_buf())
.unwrap_or_else(|| PathBuf::from("."));
embed_theme_images(&mut config, &manifest_dir)?;
Ok((config, manifest_dir))
}
pub fn steps_json(config: &ShunConfig, manifest_dir: &Path) -> Result<String, String> {
let steps = config
.resolve_steps(manifest_dir, None)
.map_err(|e| format!("wizard pipeline: {e}"))?;
serde_json::to_vec_pretty(&steps)
.map(|v| String::from_utf8_lossy(&v).into_owned())
.map_err(|e| format!("steps serialize: {e}"))
}
pub fn license_docs_json(config: &ShunConfig, manifest_dir: &Path) -> Result<String, String> {
let mut license_docs: BTreeMap<String, Vec<crate::config::ResolvedLicenseDoc>> =
BTreeMap::new();
for locale in SHELL_LOCALES {
let locale_steps = config
.resolve_steps(manifest_dir, Some(locale))
.map_err(|e| format!("wizard pipeline ({locale}): {e}"))?;
if let Some(license) = locale_steps
.iter()
.find(|step| step.kind == crate::config::StepKind::License)
{
license_docs.insert(locale.to_string(), license.licenses.clone());
}
}
serde_json::to_vec_pretty(&license_docs)
.map(|v| String::from_utf8_lossy(&v).into_owned())
.map_err(|e| format!("license docs serialize: {e}"))
}
pub fn payload_archive(config: &ShunConfig, manifest_dir: &Path) -> Result<Vec<u8>, String> {
let payload_relative = config
.payload
.clone()
.ok_or_else(|| "the manifest declares no payload directory".to_string())?;
let payload_dir = manifest_dir.join(payload_relative);
crate::payload::pack_directory(&payload_dir)
.map_err(|e| format!("payload {}: {e}", payload_dir.display()))
}
pub fn logo_parts(config: &ShunConfig, manifest_dir: &Path) -> Result<(Vec<u8>, String), String> {
let Some(logo) = config.product.logo.as_ref() else {
return Ok((Vec::new(), "none".into()));
};
let bytes = std::fs::read(manifest_dir.join(logo))
.map_err(|e| format!("logo {}: {e}", manifest_dir.join(logo).display()))?;
if bytes.is_empty() {
return Ok((Vec::new(), "none".into()));
}
let kind = Path::new(logo)
.extension()
.map(|e| e.to_string_lossy().into_owned())
.unwrap_or_else(|| "none".into())
.to_lowercase();
Ok((bytes, kind))
}
pub fn resolve_parts(
manifest: &Path,
variant: Option<&str>,
flavor: Option<String>,
) -> Result<StampParts, String> {
let (config, manifest_dir) = resolved_config(manifest, variant)?;
let steps_json = steps_json(&config, &manifest_dir)?;
let license_docs_json = license_docs_json(&config, &manifest_dir)?;
let payload_archive = payload_archive(&config, &manifest_dir)?;
let (logo_bytes, logo_kind) = logo_parts(&config, &manifest_dir)?;
let log_name: String = config
.product
.name
.chars()
.map(|c| {
if c.is_alphanumeric() || c == '-' || c == '_' {
c
} else {
'-'
}
})
.collect();
Ok(StampParts {
config_json: serde_json::to_vec_pretty(&config)
.map(|v| String::from_utf8_lossy(&v).into_owned())
.map_err(|e| format!("config serialize: {e}"))?,
steps_json,
license_docs_json,
logo_bytes,
logo_kind,
payload_archive,
meta: StampMeta {
shun_version: env!("CARGO_PKG_VERSION").to_string(),
product_name: config.product.name.clone(),
product_version: config.product.version.clone(),
variant: variant.map(str::to_string),
flavor: flavor.unwrap_or_default(),
log_name,
},
})
}
#[cfg(feature = "cli")]
pub fn logo_ico_bytes(logo: &[u8]) -> Result<Vec<u8>, String> {
use image::ExtendedColorType;
use image::codecs::ico::{IcoEncoder, IcoFrame};
use image::imageops::FilterType;
let logo = image::load_from_memory(logo)
.map_err(|e| format!("the product logo does not decode as an image: {e}"))?;
let mut frames = Vec::new();
for size in [16u32, 24, 32, 48, 64, 128, 256] {
let square = square_rgba(&logo, size, FilterType::Lanczos3);
frames.push(
IcoFrame::as_png(square.as_raw(), size, size, ExtendedColorType::Rgba8)
.map_err(|e| format!("icon frame {size}px: {e}"))?,
);
}
let mut out = Vec::new();
IcoEncoder::new(&mut out)
.encode_images(&frames)
.map_err(|e| format!("the logo ICO encodes: {e}"))?;
Ok(out)
}
#[cfg(feature = "cli")]
fn square_rgba(
logo: &image::DynamicImage,
size: u32,
filter: image::imageops::FilterType,
) -> image::RgbaImage {
let scaled = if logo.width() >= logo.height() {
logo.resize_exact(size, (size * logo.height() / logo.width()).max(1), filter)
} else {
logo.resize_exact((size * logo.width() / logo.height()).max(1), size, filter)
};
let mut canvas = image::RgbaImage::new(size, size);
let x = (size - scaled.width()) / 2;
let y = (size - scaled.height()) / 2;
image::imageops::overlay(&mut canvas, &scaled, x.into(), y.into());
canvas
}
pub fn embed_theme_images(config: &mut ShunConfig, manifest_dir: &Path) -> Result<(), String> {
use base64::Engine as _;
let Some(theme) = config.shell.as_mut().and_then(|shell| shell.theme.as_mut()) else {
return Ok(());
};
if let Some(wallpaper) = theme.wallpaper.as_mut() {
for source in wallpaper.sources.iter_mut() {
let (kind, path_ref) = match source {
WallpaperSourceSpec::Video { video } => ("video", video),
WallpaperSourceSpec::Image { image } => ("image", image),
WallpaperSourceSpec::Pipeline { .. } => continue,
};
if path_ref.starts_with("https://") || path_ref.starts_with("http://") {
continue;
}
let path = manifest_dir.join(path_ref.clone());
let bytes = std::fs::read(&path)
.map_err(|e| format!("wallpaper {kind} {}: {e}", path.display()))?;
let mime = match path
.extension()
.and_then(|e| e.to_str())
.unwrap_or("")
.to_ascii_lowercase()
.as_str()
{
"mp4" | "m4v" => "video/mp4",
"webm" => "video/webm",
"png" => "image/png",
"jpg" | "jpeg" => "image/jpeg",
"webp" => "image/webp",
"gif" => "image/gif",
other => return Err(format!("wallpaper {kind} {other}: unsupported format")),
};
let url = format!(
"data:{mime};base64,{}",
base64::engine::general_purpose::STANDARD.encode(bytes)
);
println!("cargo:rerun-if-changed={}", path.display());
*path_ref = url;
}
}
let ThemeConfig {
background,
rail_background,
pane_background,
..
} = theme;
for spec in [background, rail_background, pane_background]
.into_iter()
.flatten()
{
if let BackgroundSpec::Image { image } = spec {
let path = manifest_dir.join(image.clone());
let bytes = std::fs::read(&path)
.map_err(|e| format!("theme wallpaper {}: {e}", path.display()))?;
let mime = match path
.extension()
.and_then(|e| e.to_str())
.unwrap_or("")
.to_ascii_lowercase()
.as_str()
{
"png" => "image/png",
"jpg" | "jpeg" => "image/jpeg",
"webp" => "image/webp",
"gif" => "image/gif",
"bmp" => "image/bmp",
other => return Err(format!("theme wallpaper {other}: unsupported format")),
};
let url = format!(
"data:{mime};base64,{}",
base64::engine::general_purpose::STANDARD.encode(bytes)
);
println!("cargo:rerun-if-changed={}", path.display());
*spec = BackgroundSpec::DataUrl(url);
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn generic_trailer_roundtrips_through_parse() {
let mut exe = b"MZ fake shell".to_vec();
let t = Trailer::generic();
exe.extend_from_slice(&t);
let parsed = parse_trailer(&exe).expect("generic trailer parses");
assert_eq!(parsed.offset, 0);
assert_eq!(parsed.len, 0);
verify_container(&parsed, &[]).expect("empty container verifies");
}
#[test]
fn stamped_image_roundtrips() {
let mut exe = b"MZ fake shell bytes".to_vec();
let entries: Vec<(&str, &[u8])> = vec![
(entry::CONFIG, br#"{ "product": { "name": "Demo" } }"#),
(entry::PAYLOAD, &[7u8; 1500]), ];
let container = pack_container(&entries).expect("pack");
let offset = exe.len() as u64;
let trailer = build_trailer(offset, &container);
exe.extend_from_slice(&container);
exe.extend_from_slice(&trailer);
let parsed = parse_trailer(&exe).expect("trailer parses");
let container = &exe[parsed.offset as usize..(parsed.offset + parsed.len) as usize];
verify_container(&parsed, container).expect("sha verifies");
let index = ContainerIndex::parse(container).expect("index parses");
assert_eq!(
index.data(container, entry::CONFIG).unwrap(),
&br#"{ "product": { "name": "Demo" } }"#[..]
);
assert_eq!(index.data(container, entry::PAYLOAD).unwrap(), &[7u8; 1500]);
assert!(index.data(container, entry::LOGO).is_none());
}
#[test]
fn parse_rejects_non_shell() {
let exe = b"MZ nothing here".to_vec();
assert!(parse_trailer(&exe).is_err());
}
#[test]
fn parse_rejects_unknown_rev() {
let mut t = Trailer::generic();
t[8..12].copy_from_slice(&99u32.to_le_bytes());
let mut exe = b"MZ".to_vec();
exe.extend_from_slice(&t);
let err = parse_trailer(&exe).unwrap_err();
assert!(err.contains("rev 99"), "{err}");
}
#[test]
fn parse_rejects_container_not_abutting_trailer() {
let container = pack_container(&[(entry::CONFIG, b"x")]).unwrap();
let mut exe = b"MZ body".to_vec();
let offset = exe.len() as u64;
let mut trailer = build_trailer(offset, &container);
exe.extend_from_slice(&container);
trailer[24..32].copy_from_slice(&((container.len() as u64) + 5).to_le_bytes());
exe.extend_from_slice(&trailer);
assert!(parse_trailer(&exe).is_err());
}
#[test]
fn meta_json_roundtrips() {
let meta = StampMeta {
shun_version: "0.6.33".into(),
product_name: "WoWSP".into(),
product_version: "0.6.9".into(),
variant: Some("webview2".into()),
flavor: "webview2".into(),
log_name: "WoWSP".into(),
};
let json = serde_json::to_vec(&meta).unwrap();
let back: StampMeta = serde_json::from_slice(&json).unwrap();
assert_eq!(back.product_name, "WoWSP");
assert_eq!(back.variant.as_deref(), Some("webview2"));
}
#[test]
fn version_info_builds_and_parses_back() {
let ident = StampIdentity {
company: "smoke".into(),
description: "SmokeTest Installer".into(),
internal_name: "smoketest-1.2.3".into(),
version: "1.2.3".into(),
ico: None,
};
let block = verinfo::build(&ident);
let strings = verinfo::parse_strings(&block).expect("version block parses");
assert_eq!(
strings.get("ProductName").map(String::as_str),
Some("SmokeTest Installer")
);
assert_eq!(
strings.get("FileVersion").map(String::as_str),
Some("1.2.3")
);
assert_eq!(
strings.get("OriginalFilename").map(String::as_str),
Some("smoketest-1.2.3.exe")
);
assert_eq!(
strings.get("CompanyName").map(String::as_str),
Some("smoke")
);
assert_eq!(
strings.get("InternalName").map(String::as_str),
Some("smoketest-1.2.3")
);
}
}