public ReadContext ReadHeader(IFormattedReader r)
        {
            var rc = new ReadContext(r);

            try
            {
                var bom = r.ReadInt64();
                if (bom != 0x0d0e0a0402080500)
                {
                    throw new FileFormatException("Expected Signature not found. Either this file is not a TeaFile or the byte order (endianness) differs between the machine were the file was written and the local machine. Expected:'{0:x}'\nFound:   '{1:x}'\n".Formatted(0x0d0e0a0402080500, bom));
                }
                rc.ItemAreaStart = r.ReadInt64();
                rc.ItemAreaEnd   = r.ReadInt64();
                rc.SectionCount  = r.ReadInt64();

                for (int i = 0; i < rc.SectionCount; i++)
                {
                    int sectionId         = r.ReadInt32();
                    int nextSectionOffset = r.ReadInt32();
                    int nextSectionStart  = (int)r.Position + nextSectionOffset;
                    if (nextSectionStart > rc.ItemAreaStart)
                    {
                        throw new FileFormatException("NextSectionOffset of section number {0} is wrong: Next Section Start would be beyond ItemStart.".Formatted(i));
                    }

                    ISectionFormatter sectionFormatter = this.sectionFormatters.FirstOrDefault(f => f.Id == sectionId);
                    if (sectionFormatter != null)
                    {
                        sectionFormatter.Read(rc);
                        if (r.Position > nextSectionStart)
                        {
                            throw new FileFormatException("Section read too many bytes from the stream. SectionId:{0} SectionName:{1}".Formatted(sectionId, sectionFormatter.GetType().Name.Replace("Formatter", "")));
                        }
                    }

                    int bytesToSkip = nextSectionStart - (int)r.Position;
                    if (bytesToSkip < 0)
                    {
                        throw new FileFormatException("Reading sections from the file header failed. Section with id {0} reads more bytes than reserved for that section".Formatted(sectionId));
                    }
                    r.SkipBytes(bytesToSkip);
                }

                if (r.Position > rc.ItemAreaStart)
                {
                    throw new FileFormatException("Stream position is behind start of item area.");
                }
                r.SkipBytes((int)rc.ItemAreaStart - (int)r.Position);

                if (rc.ItemAreaStart != r.Position)
                {
                    throw new FileFormatException("Stream Position could not be set to start of item area.");
                }
                return(rc);
            }
            catch (FileFormatException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // communicate any unexpected Header Read Error (e.g. an error while reading from disc)
                throw new FileFormatException("Error reading TeaFile Header: {0}".Formatted(ex.Message), ex);
            }
        }
예제 #2
0
 public void AddSectionFormatter(ISectionFormatter formatter)
 {
     this.sectionFormatters = this.sectionFormatters.Union(formatter.ToEnumerable()).ToArray();
 }
 public void AddSectionFormatter(ISectionFormatter formatter)
 {
     this.sectionFormatters = this.sectionFormatters.Union(formatter.ToEnumerable()).ToArray();
 }