Skip to content

Commit

Permalink
Fix 64 bit layout bug, and add defences against cousins
Browse files Browse the repository at this point in the history
  • Loading branch information
Dijji committed Apr 16, 2020
1 parent 96489d9 commit 8ce209a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
2 changes: 1 addition & 1 deletion LayoutsU4K.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct BBTENTRYUnicode4K
public UInt16 cbStored;
public UInt16 cbInflated;
public UInt16 cRef;
public UInt32 dwPadding;
public UInt16 wPadding;
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
Expand Down
20 changes: 10 additions & 10 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void OpenFile(string fileName)
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Error reading xst file");
MessageBox.Show(ex.ToString(), "Error reading xst file");
}
})
// When loading completes, update the UI using the UI thread
Expand Down Expand Up @@ -156,7 +156,7 @@ private void treeFolders_SelectedItemChanged(object sender, RoutedPropertyChange
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Unexpected error reading messages");
MessageBox.Show(ex.ToString(), "Unexpected error reading messages");
}
}

Expand All @@ -175,7 +175,7 @@ private void listMessages_SelectionChanged(object sender, SelectionChangedEventA
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Error reading message details");
MessageBox.Show(ex.ToString(), "Error reading message details");
}
}
view.SetMessage(m);
Expand Down Expand Up @@ -213,7 +213,7 @@ private void listRecipients_SelectionChanged(object sender, SelectionChangedEven
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Error reading recipient");
MessageBox.Show(ex.ToString(), "Error reading recipient");
}
}

Expand All @@ -225,7 +225,7 @@ private void listAttachments_SelectionChanged(object sender, SelectionChangedEve
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Error reading attachment");
MessageBox.Show(ex.ToString(), "Error reading attachment");
}
}

Expand All @@ -249,7 +249,7 @@ private void exportEmail_Executed(object sender, ExecutedRoutedEventArgs e)
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Error exporting email");
MessageBox.Show(ex.ToString(), "Error exporting email");
}
}
}
Expand Down Expand Up @@ -345,7 +345,7 @@ private void ExportEmailProperties(IEnumerable<Message> messages)
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Error exporting properties");
MessageBox.Show(ex.ToString(), "Error exporting properties");
}
})
// When exporting completes, update the UI using the UI thread
Expand Down Expand Up @@ -588,7 +588,7 @@ private void ShowMessage(Message m)
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Error reading message body");
MessageBox.Show(ex.ToString(), "Error reading message body");
}
}

Expand Down Expand Up @@ -778,7 +778,7 @@ private string SaveAttachmentToTemporaryFile(Attachment a)
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Error saving attachment");
MessageBox.Show(ex.ToString(), "Error saving attachment");
return null;
}
}
Expand Down Expand Up @@ -867,7 +867,7 @@ private void saveAttachmentAs_Executed(object sender, ExecutedRoutedEventArgs e)
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Error saving attachment");
MessageBox.Show(ex.ToString(), "Error saving attachment");
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ public static unsafe T[] MapArray<T>(byte[] buffer, int offset, int count)
return temp;
}

// Map part of an unsafe data buffer onto the specified type T
public static unsafe T MapType<T>(byte* buffer, int buflen, int offset)
// Map part of an unsafe data buffer onto the specified type T
// The optional entry size can be offered as a crosscheck on the accuracy of the layouts
public static unsafe T MapType<T>(byte* buffer, int buflen, int offset, int entrySize = 0)
{
int size = Marshal.SizeOf(typeof(T));
if (entrySize != 0 && size > entrySize)
throw new XstException($"Mapping error: requested map of {typeof(T).Name} whose size is {size} but entry size is only {entrySize}");
if (offset < 0 || offset + size > buflen)
throw new XstException($"Out of bounds error attempting to map {typeof(T).Name} from buffer length {buflen} at offset {offset}");
return (T)Marshal.PtrToStructure(new IntPtr(buffer + offset), typeof(T));
Expand Down
18 changes: 9 additions & 9 deletions NDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ private void ReadBTPageUnicode(FileStream fs, ulong fileOffset, TreeIntermediate
BTENTRYUnicode e;
unsafe
{
e = Map.MapType<BTENTRYUnicode>(p.rgentries, LayoutsU.BTPAGEEntryBytes, i * p.cbEnt);
e = Map.MapType<BTENTRYUnicode>(p.rgentries, LayoutsU.BTPAGEEntryBytes, i * p.cbEnt, p.cbEnt);
}
var inter = new TreeIntermediate { Key = e.btkey };
parent.Children.Add(inter);
Expand All @@ -260,7 +260,7 @@ private void ReadBTPageUnicode(FileStream fs, ulong fileOffset, TreeIntermediate
{
unsafe
{
var e = Map.MapType<NBTENTRYUnicode>(p.rgentries, LayoutsU.BTPAGEEntryBytes, i * p.cbEnt);
var e = Map.MapType<NBTENTRYUnicode>(p.rgentries, LayoutsU.BTPAGEEntryBytes, i * p.cbEnt, p.cbEnt);
var nb = new Node { Key = e.nid.dwValue, Type = e.nid.nidType, DataBid = e.bidData, SubDataBid = e.bidSub, Parent = e.nidParent };
parent.Children.Add(nb);
}
Expand All @@ -269,7 +269,7 @@ private void ReadBTPageUnicode(FileStream fs, ulong fileOffset, TreeIntermediate
{
unsafe
{
var e = Map.MapType<BBTENTRYUnicode>(p.rgentries, LayoutsU.BTPAGEEntryBytes, i * p.cbEnt);
var e = Map.MapType<BBTENTRYUnicode>(p.rgentries, LayoutsU.BTPAGEEntryBytes, i * p.cbEnt, p.cbEnt);
parent.Children.Add(new DataRef { Key = e.BREF.bid, Offset = e.BREF.ib, Length = e.cb });
}
}
Expand All @@ -293,7 +293,7 @@ private void ReadBTPageUnicode4K(FileStream fs, ulong fileOffset, TreeIntermedia
BTENTRYUnicode e;
unsafe
{
e = Map.MapType<BTENTRYUnicode>(p.rgentries, LayoutsU4K.BTPAGEEntryBytes, i * p.cbEnt);
e = Map.MapType<BTENTRYUnicode>(p.rgentries, LayoutsU4K.BTPAGEEntryBytes, i * p.cbEnt, p.cbEnt);
}
var inter = new TreeIntermediate { Key = e.btkey };
parent.Children.Add(inter);
Expand All @@ -313,7 +313,7 @@ private void ReadBTPageUnicode4K(FileStream fs, ulong fileOffset, TreeIntermedia
{
unsafe
{
var e = Map.MapType<NBTENTRYUnicode>(p.rgentries, LayoutsU4K.BTPAGEEntryBytes, i * p.cbEnt);
var e = Map.MapType<NBTENTRYUnicode>(p.rgentries, LayoutsU4K.BTPAGEEntryBytes, i * p.cbEnt, p.cbEnt);
var nb = new Node { Key = e.nid.dwValue, Type = e.nid.nidType, DataBid = e.bidData, SubDataBid = e.bidSub, Parent = e.nidParent };
parent.Children.Add(nb);
}
Expand All @@ -322,7 +322,7 @@ private void ReadBTPageUnicode4K(FileStream fs, ulong fileOffset, TreeIntermedia
{
unsafe
{
var e = Map.MapType<BBTENTRYUnicode4K>(p.rgentries, LayoutsU4K.BTPAGEEntryBytes, i * p.cbEnt);
var e = Map.MapType<BBTENTRYUnicode4K>(p.rgentries, LayoutsU4K.BTPAGEEntryBytes, i * p.cbEnt, p.cbEnt);
parent.Children.Add(new DataRef { Key = e.BREF.bid, Offset = e.BREF.ib, Length = e.cbStored, InflatedLength = e.cbInflated });
}
}
Expand All @@ -345,7 +345,7 @@ private void ReadBTPageANSI(FileStream fs, ulong fileOffset, TreeIntermediate pa
BTENTRYANSI e;
unsafe
{
e = Map.MapType<BTENTRYANSI>(p.rgentries, LayoutsA.BTPAGEEntryBytes, i * p.cbEnt);
e = Map.MapType<BTENTRYANSI>(p.rgentries, LayoutsA.BTPAGEEntryBytes, i * p.cbEnt, p.cbEnt);
}
var inter = new TreeIntermediate { Key = e.btkey };
parent.Children.Add(inter);
Expand All @@ -365,7 +365,7 @@ private void ReadBTPageANSI(FileStream fs, ulong fileOffset, TreeIntermediate pa
{
unsafe
{
var e = Map.MapType<NBTENTRYANSI>(p.rgentries, LayoutsA.BTPAGEEntryBytes, i * p.cbEnt);
var e = Map.MapType<NBTENTRYANSI>(p.rgentries, LayoutsA.BTPAGEEntryBytes, i * p.cbEnt, p.cbEnt);
var nb = new Node { Key = e.nid.dwValue, Type = e.nid.nidType, DataBid = e.bidData, SubDataBid = e.bidSub, Parent = e.nidParent };
parent.Children.Add(nb);
}
Expand All @@ -374,7 +374,7 @@ private void ReadBTPageANSI(FileStream fs, ulong fileOffset, TreeIntermediate pa
{
unsafe
{
var e = Map.MapType<BBTENTRYANSI>(p.rgentries, LayoutsA.BTPAGEEntryBytes, i * p.cbEnt);
var e = Map.MapType<BBTENTRYANSI>(p.rgentries, LayoutsA.BTPAGEEntryBytes, i * p.cbEnt, p.cbEnt);
parent.Children.Add(new DataRef { Key = e.BREF.bid, Offset = e.BREF.ib, Length = e.cb });
}
}
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.9.0.2")]
[assembly: AssemblyVersion("1.9.0.3")]

0 comments on commit 8ce209a

Please sign in to comment.