internal static void WriteHeaderField(BinaryWriter bwOut,
                                              Kdb4HeaderFieldID kdbID, byte[] pbData)
        {
            Debug.Assert(bwOut != null);
            if (bwOut == null)
            {
                throw new ArgumentNullException("bwOut");
            }

            bwOut.Write((byte)kdbID);

            if (pbData != null)
            {
                ushort uLength = (ushort)pbData.Length;
                bwOut.Write(uLength);

                if (uLength > 0)
                {
                    bwOut.Write(pbData);
                }
            }
            else
            {
                bwOut.Write((ushort)0);
            }
        }
 public void WriteHeaderField(BinaryWriter writer, Kdb4HeaderFieldID headerId, byte[] data)
 {
     var x = new BinaryWriter(new MemoryStream());
     
     writer.Write((byte)headerId);
     writer.Write((ushort)data.Length);
     Write(writer, data);
 }
Example #3
0
        private bool ReadHeaderField(BinaryReaderEx brSource)
        {
            Debug.Assert(brSource != null);
            if (brSource == null)
            {
                throw new ArgumentNullException("brSource");
            }

            byte   btFieldID = brSource.ReadByte();
            ushort uSize     = MemUtil.BytesToUInt16(brSource.ReadBytes(2));

            byte[] pbData = null;
            if (uSize > 0)
            {
                string strPrevExcpText = brSource.ReadExceptionText;
                brSource.ReadExceptionText = KLRes.FileHeaderEndEarly;

                pbData = brSource.ReadBytes(uSize);

                brSource.ReadExceptionText = strPrevExcpText;
            }

            bool bResult            = true;
            Kdb4HeaderFieldID kdbID = (Kdb4HeaderFieldID)btFieldID;

            switch (kdbID)
            {
            case Kdb4HeaderFieldID.EndOfHeader:
                bResult = false;                         // Returning false indicates end of header
                break;

            case Kdb4HeaderFieldID.CipherID:
                SetCipher(pbData);
                break;

            case Kdb4HeaderFieldID.CompressionFlags:
                SetCompressionFlags(pbData);
                break;

            case Kdb4HeaderFieldID.MasterSeed:
                m_pbMasterSeed = pbData;
                CryptoRandom.Instance.AddEntropy(pbData);
                break;

            case Kdb4HeaderFieldID.TransformSeed:
                m_pbTransformSeed = pbData;
                CryptoRandom.Instance.AddEntropy(pbData);
                break;

            case Kdb4HeaderFieldID.TransformRounds:
                m_pwDatabase.KeyEncryptionRounds = MemUtil.BytesToUInt64(pbData);
                break;

            case Kdb4HeaderFieldID.EncryptionIV:
                m_pbEncryptionIV = pbData;
                break;

            case Kdb4HeaderFieldID.ProtectedStreamKey:
                m_pbProtectedStreamKey = pbData;
                CryptoRandom.Instance.AddEntropy(pbData);
                break;

            case Kdb4HeaderFieldID.StreamStartBytes:
                m_pbStreamStartBytes = pbData;
                break;

            case Kdb4HeaderFieldID.InnerRandomStreamID:
                SetInnerRandomStreamID(pbData);
                break;

            default:
                Debug.Assert(false);
                if (m_slLogger != null)
                {
                    m_slLogger.SetText(KLRes.UnknownHeaderId + @": " +
                                       kdbID.ToString() + "!", LogStatusType.Warning);
                }
                break;
            }

            return(bResult);
        }
Example #4
0
        private static void WriteHeaderField(BinaryWriter bwOut,
            Kdb4HeaderFieldID kdbID, byte[] pbData)
        {
            Debug.Assert(bwOut != null);
            if(bwOut == null) throw new ArgumentNullException("bwOut");

            bwOut.Write((byte)kdbID);

            if(pbData != null)
            {
                ushort uLength = (ushort)pbData.Length;
                bwOut.Write(uLength);

                if(uLength > 0) bwOut.Write(pbData);
            }
            else bwOut.Write((ushort)0);
        }
Example #5
0
        void ReadDbHeader(Stream encryptedDatabase)
        {
            BinaryReader source = new BinaryReader(encryptedDatabase);

            {
                byte[] pbSig1 = source.ReadBytes(4);
                uint   uSig1  = BytesToUInt32(pbSig1);
                byte[] pbSig2 = source.ReadBytes(4);
                uint   uSig2  = BytesToUInt32(pbSig2);

                if ((uSig1 == FileSignatureOld1) && (uSig2 == FileSignatureOld2))
                {
                    throw new Exception("Old format");
                }

                if ((uSig1 == FileSignature1) && (uSig2 == FileSignature2))
                {
                }
                else if ((uSig1 == FileSignaturePreRelease1) && (uSig2 == FileSignaturePreRelease2))
                {
                }
                else
                {
                    throw new Exception("Unknown format: File signature invalid");
                }

                byte[] pb       = source.ReadBytes(4);
                uint   uVersion = BytesToUInt32(pb);
                if ((uVersion & FileVersionCriticalMask) > (FileVersion32 & FileVersionCriticalMask))
                {
                    throw new Exception("Unknown format: File version unsupported");
                }


                bool endReached = false;

                while (!endReached)
                {
                    byte   btFieldID = source.ReadByte();
                    ushort uSize     = BytesToUInt16(source.ReadBytes(2));

                    byte[] pbData = null;
                    if (uSize > 0)
                    {
                        pbData = source.ReadBytes(uSize);
                    }

                    Kdb4HeaderFieldID kdbID = (Kdb4HeaderFieldID)btFieldID;
                    switch (kdbID)
                    {
                    case Kdb4HeaderFieldID.EndOfHeader:
                        endReached = true;      // end of header
                        break;

                    case Kdb4HeaderFieldID.MasterSeed:
                        _masterSeed = pbData;
                        break;

                    case Kdb4HeaderFieldID.TransformSeed:
                        _transformSeed = pbData;
                        break;

                    case Kdb4HeaderFieldID.TransformRounds:
                        _transformRounds = BytesToUInt64(pbData);
                        break;

                    case Kdb4HeaderFieldID.EncryptionIV:
                        _initializationVectors = pbData;
                        break;

                    case Kdb4HeaderFieldID.StreamStartBytes:
                        _expectedStartBytes = pbData;
                        break;
                    }
                }
            }

            if (_transformRounds == 0)
            {
                throw new Exception();
            }
            if (_initializationVectors == null)
            {
                throw new Exception();
            }
            if (_expectedStartBytes == null)
            {
                throw new Exception();
            }
        }