@@ -2,6 +2,7 @@ use alloc::{borrow::Cow, boxed::Box, format, string::String, vec::Vec};
22use core:: { ffi:: CStr , fmt, fmt:: Debug } ;
33
44use anyhow:: { Result , bail} ;
5+ use encoding_rs:: SHIFT_JIS ;
56use object:: Endian as _;
67
78use crate :: {
@@ -57,13 +58,18 @@ impl fmt::Display for DataType {
5758impl DataType {
5859 pub fn display_labels ( & self , endian : object:: Endianness , bytes : & [ u8 ] ) -> Vec < String > {
5960 let mut strs = Vec :: new ( ) ;
60- for literal in self . display_literals ( endian, bytes) {
61- strs. push ( format ! ( "{}: {}" , self , literal) )
61+ for ( literal, label_override) in self . display_literals ( endian, bytes) {
62+ let label = label_override. unwrap_or_else ( || format ! ( "{}" , self ) ) ;
63+ strs. push ( format ! ( "{}: {}" , label, literal) )
6264 }
6365 strs
6466 }
6567
66- pub fn display_literals ( & self , endian : object:: Endianness , bytes : & [ u8 ] ) -> Vec < String > {
68+ pub fn display_literals (
69+ & self ,
70+ endian : object:: Endianness ,
71+ bytes : & [ u8 ] ,
72+ ) -> Vec < ( String , Option < String > ) > {
6773 let mut strs = Vec :: new ( ) ;
6874 if self . required_len ( ) . is_some_and ( |l| bytes. len ( ) < l) {
6975 log:: warn!(
@@ -87,56 +93,72 @@ impl DataType {
8793 match self {
8894 DataType :: Int8 => {
8995 let i = i8:: from_ne_bytes ( bytes. try_into ( ) . unwrap ( ) ) ;
90- strs. push ( format ! ( "{:#x}" , i) ) ;
96+ strs. push ( ( format ! ( "{:#x}" , i) , None ) ) ;
9197
9298 if i < 0 {
93- strs. push ( format ! ( "{:#x}" , ReallySigned ( i) ) ) ;
99+ strs. push ( ( format ! ( "{:#x}" , ReallySigned ( i) ) , None ) ) ;
94100 }
95101 }
96102 DataType :: Int16 => {
97103 let i = endian. read_i16_bytes ( bytes. try_into ( ) . unwrap ( ) ) ;
98- strs. push ( format ! ( "{:#x}" , i) ) ;
104+ strs. push ( ( format ! ( "{:#x}" , i) , None ) ) ;
99105
100106 if i < 0 {
101- strs. push ( format ! ( "{:#x}" , ReallySigned ( i) ) ) ;
107+ strs. push ( ( format ! ( "{:#x}" , ReallySigned ( i) ) , None ) ) ;
102108 }
103109 }
104110 DataType :: Int32 => {
105111 let i = endian. read_i32_bytes ( bytes. try_into ( ) . unwrap ( ) ) ;
106- strs. push ( format ! ( "{:#x}" , i) ) ;
112+ strs. push ( ( format ! ( "{:#x}" , i) , None ) ) ;
107113
108114 if i < 0 {
109- strs. push ( format ! ( "{:#x}" , ReallySigned ( i) ) ) ;
115+ strs. push ( ( format ! ( "{:#x}" , ReallySigned ( i) ) , None ) ) ;
110116 }
111117 }
112118 DataType :: Int64 => {
113119 let i = endian. read_i64_bytes ( bytes. try_into ( ) . unwrap ( ) ) ;
114- strs. push ( format ! ( "{:#x}" , i) ) ;
120+ strs. push ( ( format ! ( "{:#x}" , i) , None ) ) ;
115121
116122 if i < 0 {
117- strs. push ( format ! ( "{:#x}" , ReallySigned ( i) ) ) ;
123+ strs. push ( ( format ! ( "{:#x}" , ReallySigned ( i) ) , None ) ) ;
118124 }
119125 }
120126 DataType :: Float => {
121127 let bytes: [ u8 ; 4 ] = bytes. try_into ( ) . unwrap ( ) ;
122- strs. push ( format ! ( "{:?}f" , match endian {
123- object:: Endianness :: Little => f32 :: from_le_bytes( bytes) ,
124- object:: Endianness :: Big => f32 :: from_be_bytes( bytes) ,
125- } ) ) ;
128+ strs. push ( (
129+ format ! ( "{:?}f" , match endian {
130+ object:: Endianness :: Little => f32 :: from_le_bytes( bytes) ,
131+ object:: Endianness :: Big => f32 :: from_be_bytes( bytes) ,
132+ } ) ,
133+ None ,
134+ ) ) ;
126135 }
127136 DataType :: Double => {
128137 let bytes: [ u8 ; 8 ] = bytes. try_into ( ) . unwrap ( ) ;
129- strs. push ( format ! ( "{:?}" , match endian {
130- object:: Endianness :: Little => f64 :: from_le_bytes( bytes) ,
131- object:: Endianness :: Big => f64 :: from_be_bytes( bytes) ,
132- } ) ) ;
138+ strs. push ( (
139+ format ! ( "{:?}" , match endian {
140+ object:: Endianness :: Little => f64 :: from_le_bytes( bytes) ,
141+ object:: Endianness :: Big => f64 :: from_be_bytes( bytes) ,
142+ } ) ,
143+ None ,
144+ ) ) ;
133145 }
134146 DataType :: Bytes => {
135- strs. push ( format ! ( "{:#?}" , bytes) ) ;
147+ strs. push ( ( format ! ( "{:#?}" , bytes) , None ) ) ;
136148 }
137149 DataType :: String => {
138150 if let Ok ( cstr) = CStr :: from_bytes_until_nul ( bytes) {
139- strs. push ( format ! ( "{:?}" , cstr) ) ;
151+ strs. push ( ( format ! ( "{:?}" , cstr) , None ) ) ;
152+ }
153+ if let Some ( nul_idx) = bytes. iter ( ) . position ( |& c| c == b'\0' ) {
154+ let ( cow, _, had_errors) = SHIFT_JIS . decode ( & bytes[ ..nul_idx] ) ;
155+ if !had_errors {
156+ let str = format ! ( "{:?}" , cow) ;
157+ // Only add the Shift JIS string if it's different from the ASCII string.
158+ if !strs. iter ( ) . any ( |x| x. 0 == str) {
159+ strs. push ( ( str, Some ( "Shift JIS" . into ( ) ) ) ) ;
160+ }
161+ }
140162 }
141163 }
142164 }
0 commit comments