Skip to content

Commit 18987ed

Browse files
authored
add shiftjis as possible data type for symbols (#95)
* add shiftjis as possible data type for symbols * usage of anyhow:bail! -> bail! * revise output of sjis strings * rename shiftjis internally, symbols now uses sjis instead of shiftjis * remove sjis decoding error check as the output is a comment * run cargo fmt
1 parent 614d4f2 commit 18987ed

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ strip = "debuginfo"
2525
codegen-units = 1
2626

2727
[dependencies]
28+
encoding_rs = "0.8"
2829
aes = "0.8"
2930
anyhow = { version = "1.0", features = ["backtrace"] }
3031
ar = { git = "https://github.com/bjorn3/rust-ar.git", branch = "write_symbol_table" }

src/obj/symbols.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,10 @@ pub enum ObjDataKind {
175175
Float,
176176
Double,
177177
String,
178+
ShiftJIS,
178179
String16,
179180
StringTable,
181+
ShiftJISTable,
180182
String16Table,
181183
Int,
182184
Short,

src/util/asm.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,38 @@ where W: Write + ?Sized {
668668
Ok(())
669669
}
670670

671+
use encoding_rs::SHIFT_JIS;
672+
673+
fn write_string_shiftjis<W>(w: &mut W, data: &[u8]) -> Result<()>
674+
where W: Write + ?Sized {
675+
if data.last() != Some(&0x00) {
676+
bail!("Non-terminated Shift-JIS string");
677+
}
678+
679+
let raw_data = &data[..data.len() - 1];
680+
681+
// Decode then write SJIS as comment above byte array
682+
let (cow, _, _) = SHIFT_JIS.decode(raw_data);
683+
write!(w, "\t# ")?;
684+
for c in cow.chars() {
685+
match c {
686+
'#' => write!(w, "\\#")?,
687+
_ => write!(w, "{}", c)?,
688+
}
689+
}
690+
691+
write!(w, "\n\t.byte ")?;
692+
for (i, &b) in data.iter().enumerate() {
693+
write!(w, "0x{:02X}", b)?;
694+
if i + 1 != data.len() {
695+
write!(w, ", ")?;
696+
}
697+
}
698+
699+
writeln!(w)?;
700+
Ok(())
701+
}
702+
671703
fn write_string16<W>(w: &mut W, data: &[u16]) -> Result<()>
672704
where W: Write + ?Sized {
673705
if matches!(data.last(), Some(&b) if b == 0) {
@@ -705,6 +737,12 @@ where W: Write + ?Sized {
705737
ObjDataKind::String => {
706738
return write_string(w, data);
707739
}
740+
ObjDataKind::ShiftJIS => {
741+
if data.is_empty() || data.last() != Some(&0x00) {
742+
anyhow::bail!("Non-terminated Shift-JIS string");
743+
}
744+
return write_string_shiftjis(w, data);
745+
}
708746
ObjDataKind::String16 => {
709747
if data.len() % 2 != 0 {
710748
bail!("Attempted to write wstring with length {:#X}", data.len());
@@ -734,6 +772,12 @@ where W: Write + ?Sized {
734772
}
735773
return Ok(());
736774
}
775+
ObjDataKind::ShiftJISTable => {
776+
for slice in data.split_inclusive(|&b| b == 0) {
777+
write_string_shiftjis(w, slice)?;
778+
}
779+
return Ok(());
780+
}
737781
_ => {}
738782
}
739783
let chunk_size = match data_kind {
@@ -742,7 +786,9 @@ where W: Write + ?Sized {
742786
ObjDataKind::Byte | ObjDataKind::Byte8 | ObjDataKind::Double => 8,
743787
ObjDataKind::String
744788
| ObjDataKind::String16
789+
| ObjDataKind::ShiftJIS
745790
| ObjDataKind::StringTable
791+
| ObjDataKind::ShiftJISTable
746792
| ObjDataKind::String16Table => unreachable!(),
747793
};
748794
for chunk in remain.chunks(chunk_size) {

src/util/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,10 @@ fn symbol_data_kind_to_str(kind: ObjDataKind) -> Option<&'static str> {
329329
ObjDataKind::Float => Some("float"),
330330
ObjDataKind::Double => Some("double"),
331331
ObjDataKind::String => Some("string"),
332+
ObjDataKind::ShiftJIS => Some("sjis"),
332333
ObjDataKind::String16 => Some("wstring"),
333334
ObjDataKind::StringTable => Some("string_table"),
335+
ObjDataKind::ShiftJISTable => Some("sjis_table"),
334336
ObjDataKind::String16Table => Some("wstring_table"),
335337
ObjDataKind::Int => Some("int"),
336338
ObjDataKind::Short => Some("short"),
@@ -382,8 +384,10 @@ fn symbol_data_kind_from_str(s: &str) -> Option<ObjDataKind> {
382384
"float" => Some(ObjDataKind::Float),
383385
"double" => Some(ObjDataKind::Double),
384386
"string" => Some(ObjDataKind::String),
387+
"sjis" => Some(ObjDataKind::ShiftJIS),
385388
"wstring" => Some(ObjDataKind::String16),
386389
"string_table" => Some(ObjDataKind::StringTable),
390+
"sjis_table" => Some(ObjDataKind::ShiftJISTable),
387391
"wstring_table" => Some(ObjDataKind::String16Table),
388392
"int" => Some(ObjDataKind::Int),
389393
"short" => Some(ObjDataKind::Short),

0 commit comments

Comments
 (0)