private static string DeserializePayload(Core.Utility.BinaryReader reader) { var payloadSize = (int)reader.BaseStream.Length; var bytes = reader.ReadBytes(payloadSize); var payload = System.Text.Encoding.UTF8.GetString(bytes); return(payload); }
private static void CheckHeader(Core.Utility.BinaryReader reader, ref int bytesRead) { var identifier = reader.ReadBytes(MagicIdentifier.Length); bytesRead += identifier.Length; if (!identifier.SequenceEqual(MagicIdentifier)) { throw new InvalidOperationException(Resources.Strings.GZipHeaderError_InvalidMagic); } }
/// <summary> /// Deserializes the payload. /// </summary> /// <param name="reader">The binary reader containing the data to deserialize to create the object.</param> /// <returns>The number of bytes deserialized.</returns> /// <remarks>The default implementation merely validates that the payload is correct. Specific subclasses may override this implementation if /// any specific data from the payload is desired.</remarks> /// <exception cref="System.IO.EndOfStreamException">Thrown if the data stream does not contain enough data as specified by payload length.</exception> /// <exception cref="System.IO.InvalidDataException">Thrown if the payload checksum differs from the expected checksum.</exception> protected virtual int DeserializePayload(Core.Utility.BinaryReader reader) { var payload = reader.ReadBytes(Length); if (payload.Length < Length) { throw new System.IO.EndOfStreamException(); } ValidatePayloadCrc(payload); return(Length); }
private void ValidateHeaderCrc(Core.Utility.BinaryReader reader, int bytesRead) { reader.BaseStream.Seek(-bytesRead, System.IO.SeekOrigin.Current); var data = reader.ReadBytes(bytesRead - 1); var crc8 = Crc8.OfBlock(data); reader.BaseStream.Seek(bytesRead, System.IO.SeekOrigin.Begin); if (crc8 != Crc) { throw new System.IO.InvalidDataException(string.Format(CultureInfo.CurrentCulture, Resources.Strings.InvalidDataBlockChecksumFormat, crc8, Crc)); } }
/// <inheritdoc /> public override int Deserialize(Core.Utility.BinaryReader reader) { var header = reader.ReadBytes(MagicKey.Length); var bytesRead = header.Length; if (!header.SequenceEqual(MagicKey)) { throw new INTV.Core.UnexpectedFileTypeException("LUIGI"); } Version = reader.ReadByte(); bytesRead += sizeof(byte); if (Version > LuigiFileHeader.CurrentVersion) { // Perhaps throwing here is a bad idea... throw new System.InvalidOperationException(string.Format(System.Globalization.CultureInfo.CurrentCulture, Resources.Strings.UnsupportedLuigiVersion_Format, Version)); } Features = (LuigiFeatureFlags)reader.ReadUInt64(); bytesRead += sizeof(LuigiFeatureFlags); if (Version > 0) { Features2 = (LuigiFeatureFlags2)reader.ReadUInt64(); bytesRead += sizeof(LuigiFeatureFlags2); var uidBytes = reader.ReadBytes(VersionOneUidSize); if (uidBytes.Length < sizeof(ulong)) { throw new System.IO.EndOfStreamException(); } Uid = BitConverter.ToUInt64(uidBytes, 0); bytesRead += sizeof(ulong); var originalFormatKey = uidBytes.Skip(OriginalRomCrc32Size).Take(OriginalRomKeySize); var originalCrcData = uidBytes.Take(OriginalRomCrc32Size).ToArray(); if (originalFormatKey.SequenceEqual(RomKey)) { OriginalRomFormat = RomFormat.Rom; OriginalRomCrc32 = BitConverter.ToUInt32(originalCrcData, 0); } else { OriginalRomFormat = RomFormat.Bin; OriginalRomCrc32 = BitConverter.ToUInt32(originalCrcData, 0); if (!originalFormatKey.SequenceEqual(BinKey)) { OriginalCfgCrc32 = BitConverter.ToUInt32(originalFormatKey.ToArray(), 0); } } } Reserved = reader.ReadBytes(ReservedHeaderBytesSize); bytesRead += ReservedHeaderBytesSize; Crc = reader.ReadByte(); bytesRead += HeaderChecksumSize; ValidateHeaderBytesRead(bytesRead); ValidateHeaderCrc(reader, bytesRead); _deserializeByteCount = bytesRead; return(bytesRead); }