Exemple #1
0
 public override int getFileSize()
 {
     if (Works() &&
         (_Default[0] is SCPSection0))
     {
         SCPSection0 pointers = (SCPSection0)_Default[0];
         int         sum      = Marshal.SizeOf(_CRC) + Marshal.SizeOf(_Length);
         for (int loper = 0; loper < pointers.getNrPointers(); loper++)
         {
             sum += pointers.getLength(loper);
         }
         return(sum);
     }
     return(0);
 }
Exemple #2
0
 public override int Write(byte[] buffer, int offset)
 {
     // Check if format works.
     if (Works())
     {
         _Length = getFileSize();
         if ((buffer != null) &&
             ((offset + _Length) <= buffer.Length) &&
             (_Default[0] is SCPSection0))
         {
             // Write length of file.
             BytesTool.writeBytes(_Length, buffer, offset + Marshal.SizeOf(_CRC), Marshal.SizeOf(_Length), true);
             SCPSection0 pointers = (SCPSection0)_Default[0];
             // Write all sections in format.
             for (int loper = 0; loper < pointers.getNrPointers(); loper++)
             {
                 if (loper < _MinNrSections)
                 {
                     _Default[loper].Write(buffer, offset + pointers.getIndex(loper) - 1);
                 }
                 else if ((pointers.getLength(loper) > SCPSection.Size) &&
                          (_Manufactor[loper - _MinNrSections] != null))
                 {
                     _Manufactor[loper - _MinNrSections].Write(buffer, offset + pointers.getIndex(loper) - 1);
                 }
             }
             // Calculate CRC of byte array.
             CRCTool crctool = new CRCTool();
             crctool.Init(CRCTool.CRCCode.CRC_CCITT);
             _CRC = crctool.CalcCRCITT(buffer, offset + Marshal.SizeOf(_CRC), _Length - Marshal.SizeOf(_CRC));
             BytesTool.writeBytes(_CRC, buffer, offset, Marshal.SizeOf(_CRC), true);
             return(0x0);
         }
         return(0x2);
     }
     return(0x1);
 }
Exemple #3
0
        /// <summary>
        /// Function to set pointers.
        /// </summary>
        public void setPointers()
        {
            if (_Default[0] is SCPSection0)
            {
                SCPSection0 pointers = (SCPSection0)_Default[0];
                pointers.setNrPointers(_MinNrSections + (_Manufactor != null ? _Manufactor.Length : 0));
                int sum = Marshal.SizeOf(_CRC) + Marshal.SizeOf(_Length) + 1;
                for (int loper = 0; loper < _MinNrSections; loper++)
                {
                    ushort id     = (ushort)loper;
                    int    length = _Default[loper].getLength();
                    int    index  = (length > SCPSection.Size ? sum : 0);
                    pointers.setPointer(loper, id, length, index);
                    sum += length;
                }
                if (_Manufactor != null)
                {
                    for (int loper = 0; loper < _Manufactor.Length; loper++)
                    {
                        ushort id     = _Manufactor[loper].getSectionID();
                        int    length = _Manufactor[loper].getLength();
                        int    index  = (length > SCPSection.Size ? sum : 0);
                        pointers.setPointer(_MinNrSections + loper, id, length, index);
                        sum += length;
                    }
                }

                _Length = sum - 1;

                // Determine file Protocol Compatibility Level.
                if (_Default[0].Works() &&
                    _Default[1].Works() &&
                    (_Default[1] is SCPSection1))
                {
                    if (_Default[2].Works() &&
                        _Default[3].Works())
                    {
                        if (_Default[4].Works() &&
                            _Default[5].Works() &&
                            _Default[6].Works())
                        {
                            ((SCPSection1)_Default[1]).setProtocolCompatibilityLevel(SCPSection1.ProtocolCompatibility.CatIV);
                        }
                        else if (_Default[5].Works())
                        {
                            ((SCPSection1)_Default[1]).setProtocolCompatibilityLevel(SCPSection1.ProtocolCompatibility.CatIII);
                        }
                        else if (_Default[6].Works())
                        {
                            ((SCPSection1)_Default[1]).setProtocolCompatibilityLevel(SCPSection1.ProtocolCompatibility.CatII);
                        }
                        else if (_Default[7].Works() &&
                                 _Default[8].Works())
                        {
                            ((SCPSection1)_Default[1]).setProtocolCompatibilityLevel(SCPSection1.ProtocolCompatibility.CatI);
                        }
                    }
                    else if (_Default[7].Works() &&
                             _Default[8].Works())
                    {
                        ((SCPSection1)_Default[1]).setProtocolCompatibilityLevel(SCPSection1.ProtocolCompatibility.CatI);
                    }
                }
            }
        }
Exemple #4
0
        public override int Read(byte[] buffer, int offset)
        {
            // Read in pointers (section0)
            int err = _Default[0].Read(buffer, offset + Marshal.SizeOf(_CRC) + Marshal.SizeOf(_Length), 0);

            if ((err != 0) ||
                !(_Default[0] is SCPSection0))
            {
                return(0x1);
            }
            SCPSection0 pointers = (SCPSection0)_Default[0];
            ushort      nrleads  = 0;

            // set extra space for extra sections.
            if (pointers.getNrPointers() > _MinNrSections)
            {
                _Manufactor = new SCPSection[pointers.getNrPointers() - _MinNrSections];
            }

            System.Text.Encoding usedEncoding = null;

            // read in all section but pointers (section0).
            for (int loper = 1; loper < pointers.getNrPointers(); loper++)
            {
                // Special case for SCP Section 5 and 6 (they need to know the nr of leads used).
                if ((loper < _MinNrSections) &&
                    (_Default[loper] is SCPSection5))
                {
                    ((SCPSection5)_Default[loper]).setNrLeads(nrleads);
                }
                else if ((loper < _MinNrSections) &&
                         (_Default[loper] is SCPSection6))
                {
                    ((SCPSection6)_Default[loper]).setNrLeads(nrleads);
                }
                // Section works if length if greater then size of section header.
                if (pointers.getLength(loper) > 16)
                {
                    if (loper < _MinNrSections)
                    {
                        int ret = _Default[loper].Read(buffer, offset + pointers.getIndex(loper) - 1, pointers.getLength(loper));
                        if (ret != 0)
                        {
                            err |= (0x2 << loper);
                        }

                        if (usedEncoding != null)
                        {
                            _Default[loper].SetEncoding(usedEncoding);
                        }
                    }
                    else
                    {
                        _Manufactor[loper - _MinNrSections] = new SCPSectionUnknown();

                        int ret = _Manufactor[loper - _MinNrSections].Read(buffer, offset + pointers.getIndex(loper) - 1, pointers.getLength(loper));
                        if (ret != 0)
                        {
                            err |= (0x2 << loper);
                        }

                        if (usedEncoding != null)
                        {
                            _Manufactor[loper - _MinNrSections].SetEncoding(usedEncoding);
                        }
                    }
                }

                if ((loper < _MinNrSections) &&
                    (_Default[loper] is SCPSection1))
                {
                    usedEncoding = ((SCPSection1)_Default[loper]).getLanguageSupportCode();

                    _Default[0].SetEncoding(usedEncoding);
                    _Default[1].SetEncoding(usedEncoding);
                }
                else if ((loper < _MinNrSections) &&
                         (_Default[loper] is SCPSection3))
                {
                    nrleads = ((SCPSection3)_Default[loper]).getNrLeads();
                }
            }
            return(err);
        }