protected byte[] VariablesToBytes(List <DatabaseVariable> ltVariables, int iLength)
        {
            byte[] abBuffer, abReturn = new byte[Crc24.ciCrc24Length + iLength];
            int    iFrom, iTo = Crc24.ciCrc24Length;
            Crc24  Crc24Computed;

            foreach (DatabaseVariable Variable in ltVariables)
            {
                abReturn[iTo++] = Variable.bTag;
                abBuffer        = Variable.GetValue();
                for (iFrom = 0; iFrom < Variable.iLength; iFrom++)
                {
                    abReturn[iTo++] = abBuffer[iFrom];
                }
            }

            if (iTo == Crc24.ciCrc24Length + iLength)
            {
                Crc24Computed = new Crc24(abReturn, Crc24.ciCrc24Length);
                abReturn[0]   = (byte)((Crc24Computed.iCrc24 >> 16) & 0xff);
                abReturn[1]   = (byte)((Crc24Computed.iCrc24 >> 8) & 0xff);
                abReturn[2]   = (byte)(Crc24Computed.iCrc24 & 0xff);
                return(abReturn);
            }
            else
            {
                throw new FormatException("Failed to complete EncryptedDatabase.VariablesToBytes().");
            }
        }
        public new List <DatabaseObject> Read(string sTable)
        {
            int iBytesRead, iCrc24Value, iOffset = Crc24.ciCrc24Length;

            byte[]                abDecrypted;
            Crc24                 Crc24Computed;
            DatabaseObject        NewObject;
            DatabaseVariable      NewVariable;
            List <Row>            ltRows;
            List <DatabaseObject> ltReturn = new List <DatabaseObject>();

            _eState = nState.OK;

            if (sTable == "system")
            {
                ltRows = base.Read(sTable, ciIdSystemMinPropertyId, int.MaxValue);
            }
            else
            {
                ltRows = base.Read(sTable);
            }

            foreach (Row NewRow in ltRows)
            {
                if (_eState != nState.OK)
                {
                    break;
                }

                NewObject   = null;
                NewVariable = FetchEmptyDatabaseVariable();
                abDecrypted = _Cryptography.DecryptAes(NewRow.abData, _abAesKey);

                if ((abDecrypted == null) || (abDecrypted.Length < (Crc24.ciCrc24Length + 2)))
                {
                    _eState = nState.CouldNotRead;
                }
                else
                {
                    iCrc24Value   = (abDecrypted[0] << 16) | (abDecrypted[1] << 8) | abDecrypted[2];
                    Crc24Computed = new Crc24(abDecrypted, Crc24.ciCrc24Length);
                    if (iCrc24Value != Crc24Computed.iCrc24)
                    {
                        _eState = nState.CouldNotRead;
                    }
                    else
                    {
                        while ((iBytesRead = NewVariable.SetValue(abDecrypted, iOffset)) > 0)
                        {
                            _ltVariables.Add(NewVariable);
                            NewVariable = FetchEmptyDatabaseVariable();
                            iOffset    += iBytesRead;
                        }

                        switch (sTable)
                        {
                        case "system": NewObject = new Property(NewRow.iId, _ltVariables); break;
                        }

                        if (NewObject != null)
                        {
                            ltReturn.Add(NewObject);
                        }

                        _ltVariables.Add(NewVariable);
                    }
                }
                RecycleDatabaseVariables();
            }
            return(ltReturn);
        }
Exemple #3
0
        /// <summary></summary>
        /// <param name=""></param>
        /// <param name=""></param>
        public byte[] Parse(string sArmor, bool isKey)
        {
            byte[] abCrc24, abReturn = null;
            int    i, iCrc24Value;
            Crc24  Crc24Computed;
            BytesAndTextUtility StringConverter;
            nArmorParserState   eArmorParserState = nArmorParserState.Start;
            string sArmoredData, sChecksum, sHeader;

            string[] asLines;

            if (string.IsNullOrEmpty(sArmor))
            {
                _eStatus = nStatus.ParseError;
            }
            else
            {
                asLines      = sArmor.Split(asDelimiters, StringSplitOptions.None);
                sArmoredData = sChecksum = sHeader = string.Empty;

                for (i = 0; i < asLines.Length; i++)
                {
                    switch (eArmorParserState)
                    {
                    case nArmorParserState.Start:
                        eArmorParserState = isKey ? ParseCheckForKeyHeader(asLines[i]) : ParseCheckForMessageHeader(asLines[i]);
                        if (eArmorParserState == nArmorParserState.Header)
                        {
                            sHeader = asLines[i];
                        }
                        break;

                    case nArmorParserState.Header:
                    case nArmorParserState.KeyValue: eArmorParserState = ParseCheckForBlankLine(asLines[i]); break;

                    case nArmorParserState.BlankLine:
                    case nArmorParserState.ArmoredData:
                        eArmorParserState = ParseCheckForData(asLines[i], sHeader);
                        if (eArmorParserState == nArmorParserState.ArmoredData)
                        {
                            sArmoredData += asLines[i];
                        }
                        else if (eArmorParserState == nArmorParserState.Checksum)
                        {
                            sChecksum = asLines[i];
                        }
                        break;

                    case nArmorParserState.Checksum: eArmorParserState = ParseCheckForFooter(asLines[i], sHeader); break;
                    }
                }

                if (eArmorParserState == nArmorParserState.Success)
                {
                    StringConverter = new BytesAndTextUtility(sArmoredData);
                    abReturn        = StringConverter.abBase64StringBytes;

                    if (abReturn == null)
                    {
                        _eStatus = nStatus.ParseError;
                    }
                    else
                    {
                        _eStatus = nStatus.OK;

                        if (!string.IsNullOrEmpty(sChecksum))
                        {
                            StringConverter = new BytesAndTextUtility(sChecksum.Substring(1));
                            abCrc24         = StringConverter.abBase64StringBytes;

                            if ((abCrc24 == null) || (abCrc24.Length != Crc24.ciCrc24Length))
                            {
                                _eStatus = nStatus.ParseError;
                                abReturn = null;
                            }
                            else
                            {
                                iCrc24Value   = (abCrc24[0] << 16) | (abCrc24[1] << 8) | abCrc24[2];
                                Crc24Computed = new Crc24(abReturn);
                                if (iCrc24Value != Crc24Computed.iCrc24)
                                {
                                    _eStatus = nStatus.CrcError;
                                    abReturn = null;
                                }
                            }
                        }
                    }
                }
                else
                {
                    _eStatus = nStatus.ParseError;
                }
            }
            return(abReturn);
        }