Skip to content

Commit

Permalink
Add some properly typed Exception catching...
Browse files Browse the repository at this point in the history
  • Loading branch information
AdmiralCurtiss committed Mar 25, 2015
1 parent c6e4a2d commit b526fae
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
9 changes: 7 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ static bool PatchArm9( System.IO.FileStream nds, uint pos, uint len ) {
System.IO.File.WriteAllBytes( "arm9-dec.bin", decData );
#endif
}
} catch ( Exception ) {
} catch ( blzDecodingException ) {
compressed = false;
}
}
Expand Down Expand Up @@ -306,7 +306,12 @@ static bool PatchOverlay( System.IO.FileStream nds, uint pos, uint len ) {

bool compressed = ( compressedBitmask & 0x01 ) == 0x01;
if ( compressed ) {
decData = blz.BLZ_Decode( data );
try {
decData = blz.BLZ_Decode( data );
} catch ( blzDecodingException ) {
decData = data;
compressed = false;
}
} else {
decData = data;
}
Expand Down
24 changes: 11 additions & 13 deletions blz.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
using System.Text;

namespace WfcPatcher {
class blzDecodingException : Exception {
public blzDecodingException() : base() { }
public blzDecodingException( string message ) : base( message ) { }
public blzDecodingException( string message, Exception inner ) : base( message, inner ) { }
}

class blz {
/*----------------------------------------------------------------------------*/
/*-- blz.c - Bottom LZ coding for Nintendo GBA/DS --*/
Expand Down Expand Up @@ -168,25 +174,17 @@ public byte[] BLZ_Decode( byte[] pak_buffer ) {

inc_len = BitConverter.ToUInt32( pak_buffer, (int)pak_len - 4 );
if ( inc_len == 0 ) {
throw new Exception( "Not coded file!" );
enc_len = 0;
dec_len = pak_len;
pak_len = 0;
raw_len = dec_len;

fileWasNotCompressed = true;
//Console.WriteLine();
return pak_buffer;
throw new blzDecodingException( "Not coded file!" );
} else {
if ( pak_len < 8 ) throw new Exception( "File has a bad header" );
if ( pak_len < 8 ) throw new blzDecodingException( "File has a bad header" );
hdr_len = pak_buffer[pak_len - 5];
if ( ( hdr_len < 0x08 ) || ( hdr_len > 0x0B ) ) throw new Exception( "Bad header length" );
if ( pak_len <= hdr_len ) throw new Exception( "Bad length" );
if ( ( hdr_len < 0x08 ) || ( hdr_len > 0x0B ) ) throw new blzDecodingException( "Bad header length" );
if ( pak_len <= hdr_len ) throw new blzDecodingException( "Bad length" );
enc_len = BitConverter.ToUInt32( pak_buffer, (int)pak_len - 8 ) & 0x00FFFFFF;
dec_len = pak_len - enc_len;
pak_len = enc_len - hdr_len;
raw_len = dec_len + enc_len + inc_len;
if ( raw_len > RAW_MAXIM ) throw new Exception( "Bad decoded length" );
if ( raw_len > RAW_MAXIM ) throw new blzDecodingException( "Bad decoded length" );
}

raw_buffer = Memory( (int)raw_len, 1 );
Expand Down

0 comments on commit b526fae

Please sign in to comment.