Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ strip = "debuginfo"
codegen-units = 1

[dependencies]
encoding_rs = "0.8"
aes = "0.8"
anyhow = { version = "1.0", features = ["backtrace"] }
ar = { git = "https://github.com/bjorn3/rust-ar.git", branch = "write_symbol_table" }
Expand Down
2 changes: 2 additions & 0 deletions src/obj/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,10 @@ pub enum ObjDataKind {
Float,
Double,
String,
ShiftJIS,
String16,
StringTable,
ShiftJISTable,
String16Table,
Int,
Short,
Expand Down
46 changes: 46 additions & 0 deletions src/util/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,38 @@ where W: Write + ?Sized {
Ok(())
}

use encoding_rs::SHIFT_JIS;

fn write_string_shiftjis<W>(w: &mut W, data: &[u8]) -> Result<()>
where W: Write + ?Sized {
if data.last() != Some(&0x00) {
bail!("Non-terminated Shift-JIS string");
}

let raw_data = &data[..data.len() - 1];

// Decode then write SJIS as comment above byte array
let (cow, _, _) = SHIFT_JIS.decode(raw_data);
write!(w, "\t# ")?;
for c in cow.chars() {
match c {
'#' => write!(w, "\\#")?,
_ => write!(w, "{}", c)?,
}
}

write!(w, "\n\t.byte ")?;
for (i, &b) in data.iter().enumerate() {
write!(w, "0x{:02X}", b)?;
if i + 1 != data.len() {
write!(w, ", ")?;
}
}

writeln!(w)?;
Ok(())
}

fn write_string16<W>(w: &mut W, data: &[u16]) -> Result<()>
where W: Write + ?Sized {
if matches!(data.last(), Some(&b) if b == 0) {
Expand Down Expand Up @@ -705,6 +737,12 @@ where W: Write + ?Sized {
ObjDataKind::String => {
return write_string(w, data);
}
ObjDataKind::ShiftJIS => {
if data.is_empty() || data.last() != Some(&0x00) {
anyhow::bail!("Non-terminated Shift-JIS string");
}
return write_string_shiftjis(w, data);
}
ObjDataKind::String16 => {
if data.len() % 2 != 0 {
bail!("Attempted to write wstring with length {:#X}", data.len());
Expand Down Expand Up @@ -734,6 +772,12 @@ where W: Write + ?Sized {
}
return Ok(());
}
ObjDataKind::ShiftJISTable => {
for slice in data.split_inclusive(|&b| b == 0) {
write_string_shiftjis(w, slice)?;
}
return Ok(());
}
_ => {}
}
let chunk_size = match data_kind {
Expand All @@ -742,7 +786,9 @@ where W: Write + ?Sized {
ObjDataKind::Byte | ObjDataKind::Byte8 | ObjDataKind::Double => 8,
ObjDataKind::String
| ObjDataKind::String16
| ObjDataKind::ShiftJIS
| ObjDataKind::StringTable
| ObjDataKind::ShiftJISTable
| ObjDataKind::String16Table => unreachable!(),
};
for chunk in remain.chunks(chunk_size) {
Expand Down
4 changes: 4 additions & 0 deletions src/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,10 @@ fn symbol_data_kind_to_str(kind: ObjDataKind) -> Option<&'static str> {
ObjDataKind::Float => Some("float"),
ObjDataKind::Double => Some("double"),
ObjDataKind::String => Some("string"),
ObjDataKind::ShiftJIS => Some("sjis"),
ObjDataKind::String16 => Some("wstring"),
ObjDataKind::StringTable => Some("string_table"),
ObjDataKind::ShiftJISTable => Some("sjis_table"),
ObjDataKind::String16Table => Some("wstring_table"),
ObjDataKind::Int => Some("int"),
ObjDataKind::Short => Some("short"),
Expand Down Expand Up @@ -382,8 +384,10 @@ fn symbol_data_kind_from_str(s: &str) -> Option<ObjDataKind> {
"float" => Some(ObjDataKind::Float),
"double" => Some(ObjDataKind::Double),
"string" => Some(ObjDataKind::String),
"sjis" => Some(ObjDataKind::ShiftJIS),
"wstring" => Some(ObjDataKind::String16),
"string_table" => Some(ObjDataKind::StringTable),
"sjis_table" => Some(ObjDataKind::ShiftJISTable),
"wstring_table" => Some(ObjDataKind::String16Table),
"int" => Some(ObjDataKind::Int),
"short" => Some(ObjDataKind::Short),
Expand Down