@@ -72,6 +72,11 @@ pub enum XlsError {
72
72
Art ( & ' static str ) ,
73
73
/// Worksheet not found
74
74
WorksheetNotFound ( String ) ,
75
+ /// Invalid iFmt value
76
+ InvalidFormat {
77
+ /// iFmt value, See 2.4.126 Format
78
+ ifmt : u16 ,
79
+ } ,
75
80
}
76
81
77
82
from_err ! ( std:: io:: Error , XlsError , Io ) ;
@@ -109,6 +114,7 @@ impl std::fmt::Display for XlsError {
109
114
#[ cfg( feature = "picture" ) ]
110
115
XlsError :: Art ( s) => write ! ( f, "Invalid art record '{s}'" ) ,
111
116
XlsError :: WorksheetNotFound ( name) => write ! ( f, "Worksheet '{name}' not found" ) ,
117
+ XlsError :: InvalidFormat { ifmt } => write ! ( f, "Invalid ifmt value: '{ifmt}'" ) ,
112
118
}
113
119
}
114
120
}
@@ -339,12 +345,12 @@ impl<RS: Read + Seek> Xls<RS> {
339
345
}
340
346
}
341
347
// 2.4.126 FORMATTING
342
- 0x041E => {
343
- let Ok ( ( idx, format) ) = parse_format ( & mut r , & encoding ) else {
344
- continue ;
345
- } ;
346
- formats . insert ( idx , format ) ;
347
- }
348
+ 0x041E => match parse_format ( & mut r , & encoding , biff ) {
349
+ Ok ( ( idx, format) ) => {
350
+ formats . insert ( idx , format ) ;
351
+ }
352
+ Err ( e ) => log :: warn! ( "{e}" ) ,
353
+ } ,
348
354
// XFS
349
355
0x00E0 => {
350
356
xfs. push ( parse_xf ( & r) ?) ;
@@ -913,25 +919,26 @@ fn parse_xf(r: &Record<'_>) -> Result<u16, XlsError> {
913
919
///
914
920
/// See: https://learn.microsoft.com/ru-ru/openspecs/office_file_formats/ms-xls/300280fd-e4fe-4675-a924-4d383af48d3b
915
921
/// 2.4.126
916
- fn parse_format ( r : & mut Record < ' _ > , encoding : & XlsEncoding ) -> Result < ( u16 , CellFormat ) , XlsError > {
917
- if r. data . len ( ) < 5 {
922
+ fn parse_format (
923
+ r : & mut Record < ' _ > ,
924
+ encoding : & XlsEncoding ,
925
+ biff : Biff ,
926
+ ) -> Result < ( u16 , CellFormat ) , XlsError > {
927
+ if r. data . len ( ) < 2 {
918
928
return Err ( XlsError :: Len {
919
929
typ : "format" ,
920
- expected : 5 ,
930
+ expected : 2 ,
921
931
found : r. data . len ( ) ,
922
932
} ) ;
923
933
}
934
+ let ifmt = read_u16 ( r. data ) ;
935
+ match ifmt {
936
+ 5 ..=8 | 23 ..=26 | 41 ..=44 | 63 ..=66 | 164 ..=382 => ( ) ,
937
+ _ => return Err ( XlsError :: InvalidFormat { ifmt } ) ,
938
+ }
924
939
925
- let idx = read_u16 ( r. data ) ;
926
-
927
- // TODO: check if this can be replaced with parse_string()
928
- let cch = read_u16 ( & r. data [ 2 ..] ) as usize ;
929
- let high_byte = r. data [ 4 ] & 0x1 != 0 ;
930
- r. data = & r. data [ 5 ..] ;
931
- let mut s = String :: with_capacity ( cch) ;
932
- encoding. decode_to ( r. data , cch, & mut s, Some ( high_byte) ) ;
933
-
934
- Ok ( ( idx, detect_custom_number_format ( & s) ) )
940
+ let s = parse_string ( & r. data [ 2 ..] , encoding, biff) ?;
941
+ Ok ( ( ifmt, detect_custom_number_format ( & s) ) )
935
942
}
936
943
937
944
/// Decode XLUnicodeRichExtendedString.
0 commit comments