Skip to content

Commit

Permalink
Make all exceptions thrown into XstExceptions, so they can be disting…
Browse files Browse the repository at this point in the history
…uished by callers
  • Loading branch information
Dijji committed Sep 4, 2017
1 parent aac6dd8 commit 036d9e9
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 44 deletions.
2 changes: 1 addition & 1 deletion BTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private static TreeNode LookupTreeNode(TreeIntermediate parent, UInt64 key, Acti
if (readDeferred != null)
readDeferred(parent);
else
throw new Exception("Deferred index found, but no reader supplied");
throw new XstException("Deferred index found, but no reader supplied");
}

TreeIntermediate next = null;
Expand Down
4 changes: 2 additions & 2 deletions Crypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ public static void Decrypt(ref byte[] buffer, EbCryptMethod method, UInt32 key,
}

// Throw until the code has been validated
throw new Exception("Decryption of Cyclic algorithm not validated");
throw new XstException("Decryption of Cyclic algorithm not validated");
//break;
}

default:
throw new Exception("Encryption method not known");
throw new XstException("Encryption method not known");
}
}
}
Expand Down
32 changes: 16 additions & 16 deletions LTP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public IEnumerable<NID> ReadTableRowIds(FileStream fs, NID nid)
var blocks = ReadHeapOnNode(fs, nid);
var h = blocks.First();
if (h.bClientSig != EbType.bTypeTC)
throw new Exception("Was expecting a table");
throw new XstException("Was expecting a table");

// Read the table information
var t = MapType<TCINFO>(blocks, h.hidUserRoot);
Expand Down Expand Up @@ -142,7 +142,7 @@ public IEnumerable<T> ReadTable<T>(FileStream fs, BTree<Node> subNodeTree, NID n
BTree<Node> childSubNodeTree;
var rn = ndb.LookupSubNodeAndReadItsSubNodeBtree(fs, subNodeTree, nid, out childSubNodeTree);
if (rn == null)
throw new Exception("Node block does not exist");
throw new XstException("Node block does not exist");

return ReadTableInternal<T>(fs, childSubNodeTree, rn.DataBid, g, idGetter, storeProp);
}
Expand All @@ -162,7 +162,7 @@ private void ReadPropertiesInternal<T>(FileStream fs, BTree<Node> subNodeTree, U
var blocks = ReadHeapOnNode(fs, dataBid);
var h = blocks.First();
if (h.bClientSig != EbType.bTypePC)
throw new Exception("Was expecting a PC");
throw new XstException("Was expecting a PC");

// Read the index of properties
var props = ReadBTHIndex<PCBTH>(blocks, h.hidUserRoot).ToArray();
Expand All @@ -183,7 +183,7 @@ private IEnumerable<Property> ReadAllPropertiesInternal(FileStream fs, BTree<Nod
var blocks = ReadHeapOnNode(fs, dataBid);
var h = blocks.First();
if (h.bClientSig != EbType.bTypePC)
throw new Exception("Was expecting a PC");
throw new XstException("Was expecting a PC");

// Read the index of properties
var props = ReadBTHIndex<PCBTH>(blocks, h.hidUserRoot).ToArray();
Expand Down Expand Up @@ -393,7 +393,7 @@ private IEnumerable<T> ReadTableInternal<T>(FileStream fs, BTree<Node> subNodeTr
var blocks = ReadHeapOnNode(fs, dataBid);
var h = blocks.First();
if (h.bClientSig != EbType.bTypeTC)
throw new Exception("Was expecting a table");
throw new XstException("Was expecting a table");

// Read the table information
var t = MapType<TCINFO>(blocks, h.hidUserRoot);
Expand Down Expand Up @@ -455,7 +455,7 @@ private IEnumerable<T> ReadTableData<T>(FileStream fs, TCINFO t, List<HNDataBloc
{
int blockNum = (int)(index.dwRowIndex / rowsPerBlock);
if (blockNum >= dataBlocks.Count)
throw new Exception("Data block number out of bounds");
throw new XstException("Data block number out of bounds");

var db = dataBlocks[blockNum];

Expand All @@ -468,7 +468,7 @@ private IEnumerable<T> ReadTableData<T>(FileStream fs, TCINFO t, List<HNDataBloc

if (rowOffset + t.rgibTCI_bm > db.Offset + db.Length)
{
throw new Exception("Out of bounds reading table data");
throw new XstException("Out of bounds reading table data");
}

// Read the column existence data
Expand Down Expand Up @@ -516,7 +516,7 @@ private dynamic ReadTableColumnValue(FileStream fs, BTree<Node> subNodeTree, Lis
{
case EpropertyType.PtypInteger32:
if (col.cbData != 4)
throw new Exception("Unexpected property length");
throw new XstException("Unexpected property length");
val = Map.MapType<Int32>(db.Buffer, (int)rowOffset + col.ibData);
break;

Expand All @@ -526,7 +526,7 @@ private dynamic ReadTableColumnValue(FileStream fs, BTree<Node> subNodeTree, Lis

case EpropertyType.PtypBinary:
if (col.cbData != 4)
throw new Exception("Unexpected property length");
throw new XstException("Unexpected property length");
hnid = Map.MapType<HNID>(db.Buffer, (int)rowOffset + col.ibData);

if (!hnid.HasValue)
Expand All @@ -544,7 +544,7 @@ private dynamic ReadTableColumnValue(FileStream fs, BTree<Node> subNodeTree, Lis

case EpropertyType.PtypString: // Unicode string
if (col.cbData != 4)
throw new Exception("Unexpected property length");
throw new XstException("Unexpected property length");
hnid = Map.MapType<HNID>(db.Buffer, (int)rowOffset + col.ibData);

if (!hnid.HasValue)
Expand All @@ -570,7 +570,7 @@ private dynamic ReadTableColumnValue(FileStream fs, BTree<Node> subNodeTree, Lis

case EpropertyType.PtypString8: // Multibyte string in variable encoding
if (col.cbData != 4)
throw new Exception("Unexpected property length");
throw new XstException("Unexpected property length");
hnid = Map.MapType<HNID>(db.Buffer, (int)rowOffset + col.ibData);

if (!hnid.HasValue)
Expand Down Expand Up @@ -598,7 +598,7 @@ private dynamic ReadTableColumnValue(FileStream fs, BTree<Node> subNodeTree, Lis
case EpropertyType.PtypTime:
// In a Table Context, time values are held in line
if (col.cbData != 8)
throw new Exception("Unexpected property length");
throw new XstException("Unexpected property length");
var fileTime = Map.MapType<Int64>(db.Buffer, (int)rowOffset + col.ibData);
try
{
Expand Down Expand Up @@ -663,9 +663,9 @@ private List<RowDataBlock> ReadSubNodeRowDataBlocks(FileStream fs, BTree<Node> s
var blocks = new List<RowDataBlock>();
var n = NDB.LookupSubNode(subNodeTree, nid);
if (n == null)
throw new Exception("Sub node NID not found");
throw new XstException("Sub node NID not found");
if (n.SubDataBid != 0)
throw new Exception("Sub-nodes of sub-nodes not yet implemented");
throw new XstException("Sub-nodes of sub-nodes not yet implemented");

foreach (var buf in ndb.ReadDataBlocks(fs, n.DataBid))
{
Expand All @@ -685,7 +685,7 @@ private List<HNDataBlock> ReadHeapOnNode(FileStream fs, NID nid)
{
var rn = ndb.LookupNode(nid);
if (rn == null)
throw new Exception("Node block does not exist");
throw new XstException("Node block does not exist");
return ReadHeapOnNode(fs, rn.DataBid);
}

Expand Down Expand Up @@ -762,7 +762,7 @@ private byte[] GetBytesForHNID(FileStream fs, List<HNDataBlock> blocks, BTree<No
buf = ndb.ReadSubNodeDataBlock(fs, subNodeTree, hnid.NID);
}
else
throw new Exception("Data storage style not implemented");
throw new XstException("Data storage style not implemented");

return buf;
}
Expand Down
6 changes: 3 additions & 3 deletions Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static unsafe T MapType<T>(byte[] buffer, int offset = 0)
{
int size = Marshal.SizeOf(typeof(T));
if (offset < 0 || offset + size > buffer.Length)
throw new Exception("Out of bounds error attempting to map type");
throw new XstException("Out of bounds error attempting to map type");
GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
T temp = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject() + offset, typeof(T));
handle.Free();
Expand All @@ -36,7 +36,7 @@ public static unsafe T[] MapArray<T>(byte[] buffer, int offset, int count)
T[] temp = new T[count];
int size = Marshal.SizeOf(typeof(T));
if (offset < 0 || offset + count * size > buffer.Length)
throw new Exception("Out of bounds error attempting to map array of types");
throw new XstException("Out of bounds error attempting to map array of types");
GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
for (int i = 0; i < count; i++)
{
Expand All @@ -51,7 +51,7 @@ public static unsafe T MapType<T>(byte* buffer, int buflen, int offset)
{
int size = Marshal.SizeOf(typeof(T));
if (offset < 0 || offset + size > buflen)
throw new Exception("Out of bounds error attempting to map type");
throw new XstException("Out of bounds error attempting to map type");
return (T)Marshal.PtrToStructure(new IntPtr(buffer + offset), typeof(T));
}
}
Expand Down
32 changes: 16 additions & 16 deletions NDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public BTree<Node> ReadSubNodeBtree(FileStream fs, UInt64 subDataBid)
public static Node LookupSubNode(BTree<Node> subNodeTree, NID nid)
{
if (subNodeTree == null)
throw new Exception("No sub node data tree found");
throw new XstException("No sub node data tree found");

return subNodeTree.Lookup(nid.dwValue);
}
Expand All @@ -100,9 +100,9 @@ public byte[] ReadSubNodeDataBlock(FileStream fs, BTree<Node> subNodeTree, NID n
return null;
var n = LookupSubNode(subNodeTree, nid);
if (n == null)
throw new Exception("Node not found in sub node tree");
throw new XstException("Node not found in sub node tree");
if (n.SubDataBid != 0)
throw new Exception("Sub-nodes of sub-nodes not yet implemented");
throw new XstException("Sub-nodes of sub-nodes not yet implemented");
return ReadDataBlock(fs, n.DataBid);
}

Expand Down Expand Up @@ -138,7 +138,7 @@ public Node LookupNodeAndReadItsSubNodeBtree(FileStream fs, NID nid, out BTree<N
subNodeTree = null;
var rn = LookupNode(nid);
if (rn == null)
throw new Exception("Node block does not exist");
throw new XstException("Node block does not exist");
// If there is a sub-node, read its btree so that we can resolve references to nodes in it later
if (rn.SubDataBid != 0)
{
Expand All @@ -153,7 +153,7 @@ public Node LookupSubNodeAndReadItsSubNodeBtree(FileStream fs, BTree<Node> subNo
childSubNodeTree = null;
var rn = LookupSubNode(subNodeTree, nid);
if (rn == null)
throw new Exception("Node block does not exist");
throw new XstException("Node block does not exist");
// If there is a sub-node, read its btree so that we can resolve references to nodes in it later
if (rn.SubDataBid != 0)
{
Expand All @@ -175,7 +175,7 @@ private void ReadHeaderAndIndexes()
var h = Map.ReadType<FileHeader1>(fs);

if (h.dwMagic != 0x4e444221)
throw new Exception("File is not a .ost or .pst file: the magic cookie is missing");
throw new XstException("File is not a .ost or .pst file: the magic cookie is missing");

if (h.wVer == 0x15 || h.wVer == 0x17 )
{
Expand Down Expand Up @@ -206,7 +206,7 @@ private void ReadHeaderAndIndexes()
ReadBTPageANSI(fs, h2.root.BREFBBT.ib, dataTree.Root);
}
else
throw new Exception("Unrecognised header type");
throw new XstException("Unrecognised header type");
}
}

Expand Down Expand Up @@ -274,7 +274,7 @@ private void ReadBTPageUnicode(FileStream fs, ulong fileOffset, TreeIntermediate
}
}
else
throw new Exception("Unexpected page entry type");
throw new XstException("Unexpected page entry type");
}
}
}
Expand Down Expand Up @@ -327,7 +327,7 @@ private void ReadBTPageUnicode4K(FileStream fs, ulong fileOffset, TreeIntermedia
}
}
else
throw new Exception("Unexpected page entry type");
throw new XstException("Unexpected page entry type");
}
}
}
Expand Down Expand Up @@ -379,7 +379,7 @@ private void ReadBTPageANSI(FileStream fs, ulong fileOffset, TreeIntermediate pa
}
}
else
throw new Exception("Unexpected page entry type");
throw new XstException("Unexpected page entry type");
}
}
}
Expand Down Expand Up @@ -438,7 +438,7 @@ private byte[] ReadDataBlockInternal(FileStream fs, UInt64 dataBid, ref int offs
bool first = (buffer == null); // Remember if we're at the top of a potential recursion
var rb = LookupDataBlock(dataBid);
if (rb == null)
throw new Exception("Data block does not exist");
throw new XstException("Data block does not exist");
if (first)
offset = 0;

Expand Down Expand Up @@ -503,7 +503,7 @@ private byte[] ReadDataBlockInternal(FileStream fs, UInt64 dataBid, ref int offs
{
// The recursion is over, check the results
if (offset != buffer.Length)
throw new Exception("Data xblock length mismatch");
throw new XstException("Data xblock length mismatch");

return buffer;
}
Expand All @@ -514,7 +514,7 @@ private byte[] ReadDataBlockInternal(FileStream fs, UInt64 dataBid, ref int offs
private byte[] ReadAndDecompress(FileStream fs, DataRef rb, out int read, byte[] buffer = null, int offset = 0)
{
if (rb == null)
throw new Exception("Data block does not exist");
throw new XstException("Data block does not exist");

if (IsUnicode4K && rb.Length != rb.InflatedLength)
{
Expand All @@ -523,7 +523,7 @@ private byte[] ReadAndDecompress(FileStream fs, DataRef rb, out int read, byte[]
// The first two bytes are a zlib header which DeflateStream does not understand
// They should be 0x789c, the magic code for default compression
if (fs.ReadByte() != 0x78 || fs.ReadByte() != 0x9c)
throw new Exception("Unexpected header in compressed data stream");
throw new XstException("Unexpected header in compressed data stream");

using (DeflateStream decompressionStream = new DeflateStream(fs, CompressionMode.Decompress, true))
{
Expand Down Expand Up @@ -555,7 +555,7 @@ private void ReadSubNodeBtreeUnicode(FileStream fs, UInt64 subDataBid, TreeInter
{
var rb = LookupDataBlock(subDataBid);
if (rb == null)
throw new Exception("SubNode data block does not exist");
throw new XstException("SubNode data block does not exist");

int read;
byte[] buffer = ReadAndDecompress(fs, rb, out read);
Expand Down Expand Up @@ -591,7 +591,7 @@ private void ReadSubNodeBtreeANSI(FileStream fs, UInt64 subDataBid, TreeIntermed
{
var rb = LookupDataBlock(subDataBid);
if (rb == null)
throw new Exception("SubNode data block does not exist");
throw new XstException("SubNode data block does not exist");

int read;
byte[] buffer = ReadAndDecompress(fs, rb, out read);
Expand Down
2 changes: 1 addition & 1 deletion Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.*")]
[assembly: AssemblyVersion("1.3.*")]
8 changes: 4 additions & 4 deletions RtfDecompressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public MemoryStream Decompress(byte[] data, bool enforceCrc = false)
{
var crc = CalculateCrc(data, HeaderLength);
if (crc != header.crc)
throw new ArgumentException("Input stream is corrupt: CRC did not match");
throw new XstException("Input stream is corrupt: CRC did not match");
}

byte[] dictionary = new byte[CircularDictionaryMaxLength];
Expand Down Expand Up @@ -149,7 +149,7 @@ public MemoryStream Decompress(byte[] data, bool enforceCrc = false)
var lower = (word & 0xF) + 2;

if (upper > dictionaryEnd)
throw new ArgumentException("Input stream is corrupt: invalid dictionary reference");
throw new XstException("Input stream is corrupt: invalid dictionary reference");

if (upper == dictionaryWrite)
//special dictionary reference means that decompression is complete
Expand Down Expand Up @@ -182,12 +182,12 @@ public MemoryStream Decompress(byte[] data, bool enforceCrc = false)
}
catch (IndexOutOfRangeException ex)
{
throw new ArgumentException("Input stream is corrupt: index out of range");
throw new XstException("Input stream is corrupt: index out of range");
}
break;

default:
throw new ArgumentException("Input stream is corrupt: unknown compression type");
throw new XstException("Input stream is corrupt: unknown compression type");
}

return null;
Expand Down
9 changes: 8 additions & 1 deletion XstFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public Message OpenAttachedMessage(Attachment a)
return m;
}
else
throw new Exception("Unexpected data type for attached message");
throw new XstException("Unexpected data type for attached message");
}
}
#endregion
Expand Down Expand Up @@ -388,4 +388,11 @@ private void ReadMessageTables(FileStream fs, BTree<Node> subNodeTree, Message m

#endregion
}

public class XstException : Exception
{
public XstException(string message) : base(message)
{
}
}
}

0 comments on commit 036d9e9

Please sign in to comment.