Trying to read event log, trying to fix the exception 'Unable to read beyond end of the stream.'

255 Views Asked by At

I am trying to read an event log file, but each time it attempts to read any file it puts out the exception 'Unable to read beyond end of the stream.' I can't figure out how to fix what is calling the exception.

This is the specific line that is calling the exception: uint length = br.ReadUInt32();

If you need any more information just ask

    public unsafe void Parse(string filename)
    {
        try
        {
            // Open the file
            using (FileStream fs = new FileStream(filename, FileMode.Open))
            {
                // Use BinaryReader to read the file
                using (BinaryReader br = new BinaryReader(fs))
                {
                    //Read the header of the file
                    byte[] header = new byte[sizeof(EventLogHeader)];
                    br.Read(header, 0, header.Length);
                    EventLogHeader _h = new EventLogHeader(header);
                    /*
                    // Validate the file
                    if (!Validate(_h))
                    {
                        this.OnAction("Invalid file format.");
                        return;
                    }
                    */
                    int totalEvents = (int)(_h.NextIndex - 1);
                    this.OnAction(String.Format("Found {0} events", totalEvents));

                    // Read the items
                    EventLogEntry e;
                    int cnt = 0;
                    uint offset = _h.FooterOffset;
                    while (true)
                    {
                        byte[] buff = ReadEntry(br, ref offset);
                        e = ReadEntry(buff);
                        cnt++;
                        DateTime dt = GetTime(e.rec.TimeGenerated);
                        this.OnFoundRecord(
                            new object[] { 
                                Enum.GetName(typeof(EventLogEntryType),e.rec.EventType),
                                dt.ToShortDateString(),
                                dt.ToShortTimeString(),
                                e.SourceName,
                                e.Strings,
                                e.rec.EventCategory,
                                e.rec.EventID,
                                e.UserSid, 
                                e.Computername});
                        if (cnt % 200 == 0) this.OnProgress(cnt, totalEvents);
                        if (offset == 48)
                            break;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            this.OnAction(String.Format("Error Occured! {0}", ex.Message));
        }
        return;
    }

ReadEntry Method:

    private byte[] ReadEntry(BinaryReader br, ref uint endPoint)
    {
        br.BaseStream.Seek(endPoint - 4, SeekOrigin.Begin);
        uint length = br.ReadUInt32();
        endPoint -= length;
        br.BaseStream.Seek(endPoint, SeekOrigin.Begin);
        byte[] buff = new byte[length];
        br.Read(buff, 0, buff.Length);
        return buff;
    }

uint offset = _h.FooterOffset;

0

There are 0 best solutions below