From bb9ffa9c796b7df28b046ff5179c76eca6d1c755 Mon Sep 17 00:00:00 2001 From: Curtis Wensley Date: Sun, 11 Sep 2022 19:12:07 -0700 Subject: [PATCH] Check for EOF instead of always throwing exceptions when loading files --- Source/Pablo/Formats/Character/Types/Adf.cs | 2 +- Source/Pablo/Formats/Character/Types/Ansi.cs | 2 +- Source/Pablo/Formats/Character/Types/Ansi.load.cs | 2 +- Source/Pablo/Formats/Character/Types/Atascii.cs | 4 ++-- Source/Pablo/Formats/Character/Types/Avatar.cs | 2 +- Source/Pablo/Formats/Character/Types/Binary.cs | 2 +- Source/Pablo/Formats/Character/Types/CG.cs | 4 ++-- Source/Pablo/Formats/Character/Types/CtrlA.cs | 2 +- Source/Pablo/Formats/Character/Types/Tundra.cs | 2 +- Source/Pablo/Formats/Pix/FormatPix.cs | 2 +- Source/Pablo/Formats/Rip/FormatRip.cs | 2 +- Source/Pablo/Formats/Rip/LidgrenExtensions.cs | 2 +- Source/Pablo/StreamExtensions.cs | 15 +++++++++++++++ 13 files changed, 29 insertions(+), 14 deletions(-) create mode 100644 Source/Pablo/StreamExtensions.cs diff --git a/Source/Pablo/Formats/Character/Types/Adf.cs b/Source/Pablo/Formats/Character/Types/Adf.cs index 764d91f..0146827 100644 --- a/Source/Pablo/Formats/Character/Types/Adf.cs +++ b/Source/Pablo/Formats/Character/Types/Adf.cs @@ -54,7 +54,7 @@ public override void Load (Stream fs, CharacterDocument doc, CharacterHandler ha page.Font.Load (br); WaitEventArgs args = new WaitEventArgs (); - while (true) { + while (!fs.IsEOF()) { p.X = rClip.Left; for (int x=0; x<80; x++) { byte ch = br.ReadByte (); diff --git a/Source/Pablo/Formats/Character/Types/Ansi.cs b/Source/Pablo/Formats/Character/Types/Ansi.cs index a43162f..0353f2d 100644 --- a/Source/Pablo/Formats/Character/Types/Ansi.cs +++ b/Source/Pablo/Formats/Character/Types/Ansi.cs @@ -49,7 +49,7 @@ public override bool DetectAnimation (Stream stream) var bs = new BufferedStream (stream, 1024); var br = new BinaryReader (bs); int read = 0; - while (read < 2048 && true) { + while (read < 2048 && !stream.IsEOF()) { byte curByte = br.ReadByte (); read++; if (curByte == 27) { diff --git a/Source/Pablo/Formats/Character/Types/Ansi.load.cs b/Source/Pablo/Formats/Character/Types/Ansi.load.cs index 3482060..7dc45d4 100644 --- a/Source/Pablo/Formats/Character/Types/Ansi.load.cs +++ b/Source/Pablo/Formats/Character/Types/Ansi.load.cs @@ -498,7 +498,7 @@ public void Load(Stream fs, Canvas canvas, Palette palette, WaitEventHandler onW { var args = new WaitEventArgs(); byte curByte = br.ReadByte(); - while (true && (!fs.CanSeek || fs.Position < fs.Length)) + while (!fs.IsEOF()) { if (onWait != null) onWait(this, args); diff --git a/Source/Pablo/Formats/Character/Types/Atascii.cs b/Source/Pablo/Formats/Character/Types/Atascii.cs index 0e80d5b..a2c6313 100644 --- a/Source/Pablo/Formats/Character/Types/Atascii.cs +++ b/Source/Pablo/Formats/Character/Types/Atascii.cs @@ -52,7 +52,7 @@ public override bool DetectAnimation(Stream stream) { var br = new BinaryReader(stream); - while (true) + while (!stream.IsEOF()) { byte b = br.ReadByte(); switch (b) @@ -154,7 +154,7 @@ public override void Load(Stream fs, CharacterDocument document, CharacterHandle var ce = new CanvasElement(32, 7); p = rClip.Location; var args = new WaitEventArgs(); - while (true) + while (!fs.IsEOF()) { document.OnWait(args); if (args.Exit) diff --git a/Source/Pablo/Formats/Character/Types/Avatar.cs b/Source/Pablo/Formats/Character/Types/Avatar.cs index 98f1779..ceb494c 100644 --- a/Source/Pablo/Formats/Character/Types/Avatar.cs +++ b/Source/Pablo/Formats/Character/Types/Avatar.cs @@ -69,7 +69,7 @@ public override void Load(Stream fs, CharacterDocument doc, CharacterHandler han try { WaitEventArgs args = new WaitEventArgs(); - while (true) + while (!fs.IsEOF()) { doc.OnWait(args); if (args.Exit) diff --git a/Source/Pablo/Formats/Character/Types/Binary.cs b/Source/Pablo/Formats/Character/Types/Binary.cs index 54c6964..d686d6b 100644 --- a/Source/Pablo/Formats/Character/Types/Binary.cs +++ b/Source/Pablo/Formats/Character/Types/Binary.cs @@ -111,7 +111,7 @@ public override void Load(Stream fs, CharacterDocument doc, CharacterHandler han CanvasElement ce = new CanvasElement(32, 7); p = rClip.Location; WaitEventArgs args = new WaitEventArgs(); - while (true) + while (!fs.IsEOF()) { doc.OnWait(args); if (args.Exit) diff --git a/Source/Pablo/Formats/Character/Types/CG.cs b/Source/Pablo/Formats/Character/Types/CG.cs index 6f2726a..8d2e3a7 100644 --- a/Source/Pablo/Formats/Character/Types/CG.cs +++ b/Source/Pablo/Formats/Character/Types/CG.cs @@ -52,7 +52,7 @@ public override bool DetectAnimation(Stream stream) { var br = new BinaryReader(stream); - while (true) + while (!stream.IsEOF()) { byte b = br.ReadByte(); switch (b) @@ -153,7 +153,7 @@ public override void Load(Stream fs, CharacterDocument document, CharacterHandle var ce = new CanvasElement(32, 7); p = rClip.Location; var args = new WaitEventArgs(); - while (true) + while (!fs.IsEOF()) { document.OnWait(args); if (args.Exit) diff --git a/Source/Pablo/Formats/Character/Types/CtrlA.cs b/Source/Pablo/Formats/Character/Types/CtrlA.cs index 2ed47b2..0f6910b 100644 --- a/Source/Pablo/Formats/Character/Types/CtrlA.cs +++ b/Source/Pablo/Formats/Character/Types/CtrlA.cs @@ -167,7 +167,7 @@ public void Load (Stream stream, CharacterDocument doc, Handler handler) p = rClip.Location; attr = CanvasElement.Default.Attribute; try { - while (true) { + while (!stream.IsEOF()) { var b = br.ReadByte (); if (b == 1) { diff --git a/Source/Pablo/Formats/Character/Types/Tundra.cs b/Source/Pablo/Formats/Character/Types/Tundra.cs index 9329ef4..445e009 100644 --- a/Source/Pablo/Formats/Character/Types/Tundra.cs +++ b/Source/Pablo/Formats/Character/Types/Tundra.cs @@ -186,7 +186,7 @@ protected static Point Load(Stream stream, Canvas canvas, Palette pal) { var br = new BinaryReader(stream); var attr = new Attribute(0, 0); - while (true) + while (!stream.IsEOF()) { var command = br.ReadByte(); diff --git a/Source/Pablo/Formats/Pix/FormatPix.cs b/Source/Pablo/Formats/Pix/FormatPix.cs index d8aeb20..90f18f3 100644 --- a/Source/Pablo/Formats/Pix/FormatPix.cs +++ b/Source/Pablo/Formats/Pix/FormatPix.cs @@ -35,7 +35,7 @@ public override void Load(Stream fs, Document document) try { - while (true) + while (!fs.IsEOF()) { b = br.ReadByte(); switch ((char)b) diff --git a/Source/Pablo/Formats/Rip/FormatRip.cs b/Source/Pablo/Formats/Rip/FormatRip.cs index 37e8fe9..9e9d48d 100644 --- a/Source/Pablo/Formats/Rip/FormatRip.cs +++ b/Source/Pablo/Formats/Rip/FormatRip.cs @@ -57,7 +57,7 @@ public void Load (Stream stream, RipDocument document, RipHandler handler) try { var args = new WaitEventArgs (); - while (true) { + while (!stream.IsEOF()) { if (document.AnimateView) { document.OnWait (args); if (args.Exit) diff --git a/Source/Pablo/Formats/Rip/LidgrenExtensions.cs b/Source/Pablo/Formats/Rip/LidgrenExtensions.cs index 3ccc8c8..f79c3ad 100644 --- a/Source/Pablo/Formats/Rip/LidgrenExtensions.cs +++ b/Source/Pablo/Formats/Rip/LidgrenExtensions.cs @@ -34,7 +34,7 @@ public static void ReadCommands (this NetIncomingMessage message, RipDocument do var stream = message.ReadStream (); var reader = new BinaryReader (stream); try { - while (true) { + while (!stream.IsEOF()) { char b = (char)reader.ReadRipByte (); if (b == '|') { diff --git a/Source/Pablo/StreamExtensions.cs b/Source/Pablo/StreamExtensions.cs new file mode 100644 index 0000000..e103ce3 --- /dev/null +++ b/Source/Pablo/StreamExtensions.cs @@ -0,0 +1,15 @@ +using System.IO; + +namespace Pablo +{ + static class StreamExtensions + { + public static bool IsEOF(this Stream stream) + { + if (!stream.CanSeek) + return false; + return stream.Position >= stream.Length; + } + } +} +