internal 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); }
public static VariantDictionary Deserialize(byte[] pb) { if (pb == null) { Debug.Assert(false); return(null); } VariantDictionary d = new VariantDictionary(); using (MemoryStream ms = new MemoryStream(pb, false)) { ushort uVersion = MemUtil.BytesToUInt16(MemUtil.Read(ms, 2)); if ((uVersion & VdmCritical) > (VdVersion & VdmCritical)) { throw new FormatException(KLRes.FileNewVerReq); } while (true) { int iType = ms.ReadByte(); if (iType < 0) { throw new EndOfStreamException(KLRes.FileCorrupted); } byte btType = (byte)iType; if (btType == (byte)VdType.None) { break; } int cbName = MemUtil.BytesToInt32(MemUtil.Read(ms, 4)); byte[] pbName = MemUtil.Read(ms, cbName); if (pbName.Length != cbName) { throw new EndOfStreamException(KLRes.FileCorrupted); } string strName = StrUtil.Utf8.GetString(pbName); int cbValue = MemUtil.BytesToInt32(MemUtil.Read(ms, 4)); byte[] pbValue = MemUtil.Read(ms, cbValue); if (pbValue.Length != cbValue) { throw new EndOfStreamException(KLRes.FileCorrupted); } switch (btType) { case (byte)VdType.UInt32: if (cbValue == 4) { d.SetUInt32(strName, MemUtil.BytesToUInt32(pbValue)); } else { Debug.Assert(false); } break; case (byte)VdType.UInt64: if (cbValue == 8) { d.SetUInt64(strName, MemUtil.BytesToUInt64(pbValue)); } else { Debug.Assert(false); } break; case (byte)VdType.Bool: if (cbValue == 1) { d.SetBool(strName, (pbValue[0] != 0)); } else { Debug.Assert(false); } break; case (byte)VdType.Int32: if (cbValue == 4) { d.SetInt32(strName, MemUtil.BytesToInt32(pbValue)); } else { Debug.Assert(false); } break; case (byte)VdType.Int64: if (cbValue == 8) { d.SetInt64(strName, MemUtil.BytesToInt64(pbValue)); } else { Debug.Assert(false); } break; case (byte)VdType.String: d.SetString(strName, StrUtil.Utf8.GetString(pbValue)); break; case (byte)VdType.ByteArray: d.SetByteArray(strName, pbValue); break; default: Debug.Assert(false); // Unknown type break; } } Debug.Assert(ms.ReadByte() < 0); } return(d); }
private static string ReadFile(BinaryReader br, PlgxPluginInfo plgx, IStatusLogger slStatus) { uint uSig1 = br.ReadUInt32(); uint uSig2 = br.ReadUInt32(); uint uVersion = br.ReadUInt32(); if ((uSig1 != PlgxSignature1) || (uSig2 != PlgxSignature2)) { return(null); // Ignore file, don't throw } if ((uVersion & PlgxVersionMask) > (PlgxVersion & PlgxVersionMask)) { throw new PlgxException(KLRes.FileVersionUnsupported); } string strPluginPath = null; string strTmpRoot = null; bool? obContent = null; string strBuildPre = null, strBuildPost = null; while (true) { KeyValuePair <ushort, byte[]> kvp = ReadObject(br); if (kvp.Key == PlgxEOF) { break; } else if (kvp.Key == PlgxFileUuid) { plgx.FileUuid = new PwUuid(kvp.Value); } else if (kvp.Key == PlgxBaseFileName) { plgx.BaseFileName = StrUtil.Utf8.GetString(kvp.Value); } else if (kvp.Key == PlgxCreationTime) { } // Ignore else if (kvp.Key == PlgxGeneratorName) { } else if (kvp.Key == PlgxGeneratorVersion) { } else if (kvp.Key == PlgxPrereqKP) { ulong uReq = MemUtil.BytesToUInt64(kvp.Value); if (uReq > PwDefs.FileVersion64) { throw new PlgxException(KLRes.FileNewVerReq); } } else if (kvp.Key == PlgxPrereqNet) { ulong uReq = MemUtil.BytesToUInt64(kvp.Value); ulong uInst = WinUtil.GetMaxNetFrameworkVersion(); if ((uInst != 0) && (uReq > uInst)) { throw new PlgxException(KPRes.NewerNetRequired); } } else if (kvp.Key == PlgxPrereqOS) { string strOS = "," + WinUtil.GetOSStr() + ","; string strReq = "," + StrUtil.Utf8.GetString(kvp.Value) + ","; if (strReq.IndexOf(strOS, StrUtil.CaseIgnoreCmp) < 0) { throw new PlgxException(KPRes.PluginOperatingSystemUnsupported); } } else if (kvp.Key == PlgxPrereqPtr) { uint uReq = MemUtil.BytesToUInt32(kvp.Value); if (uReq > (uint)IntPtr.Size) { throw new PlgxException(KPRes.PluginOperatingSystemUnsupported); } } else if (kvp.Key == PlgxBuildPre) { strBuildPre = StrUtil.Utf8.GetString(kvp.Value); } else if (kvp.Key == PlgxBuildPost) { strBuildPost = StrUtil.Utf8.GetString(kvp.Value); } else if (kvp.Key == PlgxBeginContent) { if (obContent.HasValue) { throw new PlgxException(KLRes.FileCorrupted); } string strCached = PlgxCache.GetCacheFile(plgx, true, false); if (!string.IsNullOrEmpty(strCached) && plgx.AllowCached) { strPluginPath = strCached; break; } if (slStatus != null) { slStatus.SetText(KPRes.PluginsCompilingAndLoading, LogStatusType.Info); } obContent = true; if (plgx.LogStream != null) { plgx.LogStream.WriteLine("Content:"); } } else if (kvp.Key == PlgxFile) { if (!obContent.HasValue || !obContent.Value) { throw new PlgxException(KLRes.FileCorrupted); } if (strTmpRoot == null) { strTmpRoot = CreateTempDirectory(); } ExtractFile(kvp.Value, strTmpRoot, plgx); } else if (kvp.Key == PlgxEndContent) { if (!obContent.HasValue || !obContent.Value) { throw new PlgxException(KLRes.FileCorrupted); } obContent = false; } else { Debug.Assert(false); } } if ((strPluginPath == null) && plgx.AllowCompile) { strPluginPath = Compile(strTmpRoot, plgx, strBuildPre, strBuildPost); } return(strPluginPath); }
public ulong GetRandomUInt64() { byte[] pb = GetRandomBytes(8); return(MemUtil.BytesToUInt64(pb)); }