private void ParseDecryptionData(IBinaryReader reader, out ulong checksum, out int ivLen, out byte[] iv, out int datLen, out byte[] dat) { checksum = reader.ReadUInt64() ^ DemutatedKeys["antitamper"].DemutatedULongs[0]; reader.ReadInt32(); // sn reader.ReadInt32(); // snLen iv = reader.ReadBytes(ivLen = reader.ReadInt32() ^ DemutatedKeys["antitamper"].DemutatedInts[0]); dat = reader.ReadBytes(datLen = reader.ReadInt32() ^ DemutatedKeys["antitamper"].DemutatedInts[1]); }
public void ReadBytesDataFromShortArray() { var buffer = new byte[] { 0, 1, 2 }; IBinaryReader reader = BinaryHelper.CreateReader(buffer); Assert.IsNotNull(reader); CollectionAssert.AreEqual(new byte[] { 0, 1 }, reader.ReadBytes(2)); // should trigger exception here reader.ReadBytes(2); Assert.Fail("Expected exception before"); }
IResourceData ReadResourceData(List <UserResourceType> userTypes, int size) { uint code = ReadUInt32(reader); switch ((ResourceTypeCode)code) { case ResourceTypeCode.Null: return(resourceDataCreator.CreateNull()); case ResourceTypeCode.String: return(resourceDataCreator.Create(reader.ReadString())); case ResourceTypeCode.Boolean: return(resourceDataCreator.Create(reader.ReadBoolean())); case ResourceTypeCode.Char: return(resourceDataCreator.Create((char)reader.ReadUInt16())); case ResourceTypeCode.Byte: return(resourceDataCreator.Create(reader.ReadByte())); case ResourceTypeCode.SByte: return(resourceDataCreator.Create(reader.ReadSByte())); case ResourceTypeCode.Int16: return(resourceDataCreator.Create(reader.ReadInt16())); case ResourceTypeCode.UInt16: return(resourceDataCreator.Create(reader.ReadUInt16())); case ResourceTypeCode.Int32: return(resourceDataCreator.Create(reader.ReadInt32())); case ResourceTypeCode.UInt32: return(resourceDataCreator.Create(reader.ReadUInt32())); case ResourceTypeCode.Int64: return(resourceDataCreator.Create(reader.ReadInt64())); case ResourceTypeCode.UInt64: return(resourceDataCreator.Create(reader.ReadUInt64())); case ResourceTypeCode.Single: return(resourceDataCreator.Create(reader.ReadSingle())); case ResourceTypeCode.Double: return(resourceDataCreator.Create(reader.ReadDouble())); case ResourceTypeCode.Decimal: return(resourceDataCreator.Create(reader.ReadDecimal())); case ResourceTypeCode.DateTime: return(resourceDataCreator.Create(new DateTime(reader.ReadInt64()))); case ResourceTypeCode.TimeSpan: return(resourceDataCreator.Create(new TimeSpan(reader.ReadInt64()))); case ResourceTypeCode.ByteArray: return(resourceDataCreator.Create(reader.ReadBytes(reader.ReadInt32()))); case ResourceTypeCode.Stream: return(resourceDataCreator.CreateStream(reader.ReadBytes(reader.ReadInt32()))); default: int userTypeIndex = (int)(code - (uint)ResourceTypeCode.UserTypes); if (userTypeIndex < 0 || userTypeIndex >= userTypes.Count) { throw new ResourceReaderException(string.Format("Invalid resource data code: {0}", code)); } return(resourceDataCreator.CreateSerialized(reader.ReadBytes(size))); } }
public void Test_Reading_A_Byte_Array() { var expected = new byte[] { 0x01, 0x02, 0x03 }; SetupStreamWithArray(expected); reader .ReadBytes(expected.Length) .Should() .Equal(expected); stream.Position.Should().Be(expected.Length); }
public static DecrypterV105 Create(IBinaryReader reader) { reader.Position = 0; if (reader.Length < 0xA4) { return(null); } var key = reader.ReadBytes(0x94); if (!Utils.Compare(reader.ReadBytes(8), ilpPublicKeyToken)) { return(null); } return(new DecrypterV105(new Version(reader.ReadByte(), reader.ReadByte(), reader.ReadByte(), reader.ReadByte()), key)); }
private static bool ReadCustomTypeArray(IBinaryReader reader, int size, out object result) { CustomTypeInfo info; byte typeCode = reader.ReadByte(); if (!CustomTypeCache.TryGet(typeCode, out info) && !Protocol.AllowRawCustomValues) { if (log.IsDebugEnabled) { log.DebugFormat("Could not find custom type for type code {0}", new object[] { typeCode }); } result = null; return(false); } if ((size < 0) || ((size * 2) > (reader.BaseStream.Length - reader.BaseStream.Position))) { if (log.IsDebugEnabled) { log.DebugFormat("Invalid length for array of type {2}: length={0}, bytesLeft={1}", new object[] { size, reader.BaseStream.Length - reader.BaseStream.Position, info.Type }); } result = null; return(false); } if (info != null) { Array array = Array.CreateInstance(info.Type, size); for (short i = 0; i < size; i = (short)(i + 1)) { int length = reader.ReadInt16(); byte[] arg = reader.ReadBytes(length); object obj2 = info.DeserializeFunction(arg); array.SetValue(obj2, (int)i); } result = array; } else { RawCustomArray array2 = new RawCustomArray(typeCode, size); for (short j = 0; j < size; j = (short)(j + 1)) { int num5 = reader.ReadInt16(); byte[] buffer2 = reader.ReadBytes(num5); array2[j] = buffer2; } result = array2; } return(true); }
public static DecrypterV106 Create(IBinaryReader reader) { try { int keyXorOffs7 = (ReadByteAt(reader, 0) ^ ReadByteAt(reader, 2)) + 2; reader.Position = keyXorOffs7 + (ReadByteAt(reader, 1) ^ ReadByteAt(reader, keyXorOffs7)); int sha1DataLen = reader.Read7BitEncodedInt32() + 0x80; int keyXorOffs6 = (int)reader.Position; int encryptedOffs = (int)reader.Position + sha1DataLen; var sha1Data = reader.ReadBytes(sha1DataLen); uint crc32 = CRC32.CheckSum(sha1Data); reader.Position = reader.Length - 0x18; uint origCrc32 = reader.ReadUInt32(); if (crc32 != origCrc32) { return(null); } var key0 = DeobUtils.Sha1Sum(sha1Data); // 1.0.6.0 var key6 = GetKey(reader, key0, keyXorOffs6); // 1.0.6.6 var key7 = GetKey(reader, key0, keyXorOffs7); // 1.0.6.7 return(new DecrypterV106(key0, key6, key7, encryptedOffs)); } catch (IOException) { return(null); } }
/// <summary> /// Tries to read a custom type. /// </summary> /// <param name="reader">The reader.</param> /// <param name="result">The result.</param> /// <returns>True if the custom type could be readed successfully; otherwise false.</returns> public static bool ReadCustomType(IBinaryReader reader, out object result) { CustomTypeInfo info; byte typeCode = reader.ReadByte(); if (!CustomTypeCache.TryGet(typeCode, out info) && !Protocol.AllowRawCustomValues) { if (log.IsDebugEnabled) { log.DebugFormat("Could not find custom type for type code {0}", new object[] { typeCode }); } result = null; return(false); } short length = reader.ReadInt16(); if ((length < 0) || ((reader.BaseStream.Length - reader.BaseStream.Position) < length)) { if (log.IsDebugEnabled) { log.DebugFormat("Invalid length for custom type: length={0}, bytesLeft={1}", new object[] { length, reader.BaseStream.Length - reader.BaseStream.Position }); } result = null; return(false); } byte[] arg = reader.ReadBytes(length); result = (info != null) ? info.DeserializeFunction(arg) : new RawCustomValue(typeCode, arg); return(true); }
/// <summary> /// Reads a string /// </summary> /// <param name="reader">Reader</param> /// <param name="offset">Offset of string</param> /// <returns>The string or <c>null</c> if we could not read it</returns> private static string ReadString(IBinaryReader reader, uint offset) { reader.Position = offset; if (!reader.CanRead(2)) { return(null); } int size = reader.ReadUInt16(); int sizeInBytes = size * 2; if (!reader.CanRead(sizeInBytes)) { return(null); } var stringData = reader.ReadBytes(sizeInBytes); try { return(Encoding.Unicode.GetString(stringData)); } catch { return(null); } }
private byte[] ReadBytesFor(PlyType dataType) { var typeSize = PlyTypeConverter.GetTypeSize(dataType); var readBytes = _binaryReader.ReadBytes(typeSize); return(readBytes.FlipIfTrue(_reverseByteOrder)); }
/// <summary> /// Reads all remaining bytes /// </summary> /// <param name="reader">this</param> /// <returns>All remaining bytes</returns> public static byte[] ReadRemainingBytes(this IBinaryReader reader) { if (reader.Position >= reader.Length) { return(new byte[0]); } return(reader.ReadBytes((int)(reader.Length - reader.Position))); }
static MethodBodyHeader ParseMethodBody2(IBinaryReader reader, out byte[] code, out byte[] extraSections) { var mbHeader = new MethodBodyHeader(); uint codeOffset; byte b = Peek(reader); if ((b & 3) == 2) { mbHeader.flags = 2; mbHeader.maxStack = 8; mbHeader.codeSize = (uint)(reader.ReadByte() >> 2); mbHeader.localVarSigTok = 0; codeOffset = 1; } else if ((b & 7) == 3) { mbHeader.flags = reader.ReadUInt16(); codeOffset = (uint)(4 * (mbHeader.flags >> 12)); if (codeOffset != 12) { throw new InvalidMethodBody(); } mbHeader.maxStack = reader.ReadUInt16(); mbHeader.codeSize = reader.ReadUInt32(); if (mbHeader.codeSize > int.MaxValue) { throw new InvalidMethodBody(); } mbHeader.localVarSigTok = reader.ReadUInt32(); if (mbHeader.localVarSigTok != 0 && (mbHeader.localVarSigTok >> 24) != 0x11) { throw new InvalidMethodBody(); } } else { throw new InvalidMethodBody(); } if (mbHeader.codeSize + codeOffset > reader.Length) { throw new InvalidMethodBody(); } code = reader.ReadBytes((int)mbHeader.codeSize); if ((mbHeader.flags & 8) != 0) { extraSections = ReadExtraSections2(reader); } else { extraSections = null; } return(mbHeader); }
public void Test_Reading_An_Offline_String() { IDataNode dataNode = new StringDataNode(); string value = "value"; binaryReader.ReadByte().Returns((byte)value.Length, (byte)0); binaryReader.ReadBytes(value.Length).Returns(Encoding.ASCII.GetBytes(value)); object result = dataNode.Read(binaryReader); result.Should().Be(value); Received.InOrder(() => { binaryReader.ReadByte(); binaryReader.ReadBytes(value.Length); binaryReader.ReadByte(); }); }
private string ReadOfflineString(IBinaryReader binaryReader) { byte length = binaryReader.ReadByte(); string result = binaryReader.ReadBytes(length) .Let(Encoding.ASCII.GetString); // Read string termination character binaryReader.ReadByte(); return(result); }
/// <summary> /// Reads <c>byte[]</c> from the stream. /// </summary> /// <param name="reader">Binary reader.</param> /// <param name="length">Number of elements to be read.</param> public static byte[] ReadByteArray(this IBinaryReader reader, int length) { byte[] result = new byte[length]; fixed(byte *b = result) { reader.ReadBytes(b, (uint)length); } return(result); }
/// <summary> /// Reads <c>ushort[]</c> from the stream. /// </summary> /// <param name="reader">Binary reader.</param> /// <param name="length">Number of elements to be read.</param> public static ushort[] ReadUshortArray(this IBinaryReader reader, int length) { ushort[] result = new ushort[length]; fixed(ushort *b = result) { reader.ReadBytes((byte *)b, (uint)length * 2); // 2 = sizeof(ushort) } return(result); }
/// <summary> /// Reads <c>uint[]</c> from the stream. /// </summary> /// <param name="reader">Binary reader.</param> /// <param name="length">Number of elements to be read.</param> public static uint[] ReadUintArray(this IBinaryReader reader, int length) { uint[] result = new uint[length]; fixed(uint *b = result) { reader.ReadBytes((byte *)b, (uint)length * 4); // 4 = sizeof(uint) } return(result); }
static byte[] ReadExtraSections2(IBinaryReader reader) { Align(reader, 4); int startPos = (int)reader.Position; ParseSection(reader); int size = (int)reader.Position - startPos; reader.Position = startPos; return(reader.ReadBytes(size)); }
public Trace ReadTrace(long traceIndex) { if (traceIndex > TraceCount) { throw new ArgumentException($"Cannot read a trace with trace index: {traceIndex}, when file only contains {TraceCount} traces"); } var dataStartIndex = TextHeaderBytesCount + BinaryHeaderBytesCount + TextHeaderBytesCount * (TextHeaders.Count() - 1); var initStreamPosition = dataStartIndex + (240 + BinaryFileHeader.SamplesPerTraceOfFile * this.dataSampleSize) * traceIndex; binaryReader.BaseStream.Seek(initStreamPosition, SeekOrigin.Begin); var traceHeaderByteArr = binaryReader.ReadBytes(TraceHeaderBytesCount); var trHeader = TraceHeader.From(traceHeaderByteArr, BitConverter); var traceDataBytesSz = BinaryFileHeader.SamplesPerTraceOfFile * this.dataSampleSize; var traceData = traceDataReadingFunc(BinaryFileHeader.SamplesPerTraceOfFile); var seismicTrace = new Trace(trHeader, traceData); return(seismicTrace); }
private byte[] CreateStreamsBuffer(IBinaryReader reader) { var tmpBuffer = new List <byte>(); foreach (var stream in ModMD.MetaData.AllStreams) { var size = stream.StreamHeader.StreamSize; reader.Position = (long)stream.StartOffset; tmpBuffer.AddRange(reader.ReadBytes((int)size)); } return(tmpBuffer.ToArray()); }
public static GetImageDataResponse Decode(byte id, IBinaryReader reader) { if (id != 2) { throw new ArgumentOutOfRangeException(nameof(id)); } uint num1 = reader.ReadUInt32(); uint num2 = reader.ReadUInt32(); ushort num3 = reader.ReadUInt16(); byte[] data = reader.ReadBytes(num3); return(new GetImageDataResponse((int)num1, (int)num2, data)); }
public List <CsvmMethodData> read() { int numMethods = reader.ReadInt32(); if (numMethods < 0) { throw new ApplicationException("Invalid number of methods"); } var methods = new List <CsvmMethodData>(numMethods); for (int i = 0; i < numMethods; i++) { var csvmMethod = new CsvmMethodData(); csvmMethod.Guid = new Guid(reader.ReadBytes(16)); csvmMethod.Token = reader.ReadInt32(); csvmMethod.Locals = reader.ReadBytes(reader.ReadInt32()); csvmMethod.Instructions = reader.ReadBytes(reader.ReadInt32()); csvmMethod.Exceptions = reader.ReadBytes(reader.ReadInt32()); methods.Add(csvmMethod); } return(methods); }
private string ReadInlineString(IBinaryReader binaryReader) { byte[] bytes = binaryReader.ReadBytes(length); int posOfNullCharacter = 0; // Get the position of the first null character while (posOfNullCharacter < bytes.Length && bytes[posOfNullCharacter] != 0) { posOfNullCharacter++; } return(bytes.Take(posOfNullCharacter) .ToArray() .Let(Encoding.ASCII.GetString)); }
public void Test_Reading_From_A_BinaryReader() { string expected = nameof(expected); childNode.Read(binaryReader).Returns(expected); object result = paddingNode.Read(binaryReader); Received.InOrder(() => { childNode.Read(binaryReader); binaryReader.ReadBytes(PADDING_SIZE); }); result.Should().Be(expected); }
/// <summary> /// Reads a byte array from a specified <see cref="T:ExitGames.IO.IBinaryReader"/> /// </summary> /// <param name="reader">The <see cref="T:ExitGames.IO.IBinaryReader"/> to read from.</param> /// <param name="byteArray">When this method returns true, contains the byte array that was read from the <see cref="T:ExitGames.IO.IBinaryReader"/>.</param> /// <returns>True if the byte array was successfully read; otherwise false.</returns> public static bool ReadByteArray(IBinaryReader reader, out object byteArray) { int length = reader.ReadInt32(); if ((length < 0) || ((reader.BaseStream.Length - reader.BaseStream.Position) < length)) { if (log.IsDebugEnabled) { log.DebugFormat("Invalid length for byte array: length={0}, bytesLeft={1}", new object[] { length, reader.BaseStream.Length - reader.BaseStream.Position }); } byteArray = null; return(false); } byteArray = reader.ReadBytes(length); return(true); }
/// <summary> /// Reads an <see cref="T:System.String"/> from a specified <see cref="T:ExitGames.IO.IBinaryReader"/> /// </summary> /// <param name="reader">The <see cref="T:ExitGames.IO.IBinaryReader"/> to read from.</param> /// <param name="result">When this method returns true, contains the string that was read from the <see cref="T:ExitGames.IO.IBinaryReader"/>.</param> /// <returns>True if the value was successfully read; otherwise false.</returns> public static bool ReadString(IBinaryReader reader, out string result) { int length = reader.ReadInt16(); if ((length < 0) || ((reader.BaseStream.Length - reader.BaseStream.Position) < length)) { if (log.IsDebugEnabled) { log.DebugFormat("Invalid length for string: length={0}, bytesLeft={1}", new object[] { length, reader.BaseStream.Length - reader.BaseStream.Position }); } result = null; return(false); } byte[] bytes = reader.ReadBytes(length); result = Encoding.UTF8.GetString(bytes); return(true); }
UTF8String ReadUTF8String() { if (reader.ReadByte() == 0xFF) { return(null); } reader.Position--; uint len; if (!reader.ReadCompressedUInt32(out len)) { throw new CABlobParserException("Could not read compressed UInt32"); } if (len == 0) { return(UTF8String.Empty); } return(new UTF8String(reader.ReadBytes((int)len))); }
public byte[] Decrypt() { if (reader.ReadUInt32() != 0x58455245) { throw new InvalidDataException("Invalid EREX sig"); } if (reader.ReadInt32() > 1) { throw new ApplicationException("Invalid EREX file"); } byte flags = reader.ReadByte(); bool isEncrypted = (flags & 1) != 0; bool isDeflated = (flags & 2) != 0; int length = reader.ReadInt32(); if (length < 0) { throw new ApplicationException("Invalid length"); } if (isEncrypted) { ReadKey(); } if (isDeflated) { reader = Inflate(length); } if (isEncrypted) { reader = Decrypt(length); } return(reader.ReadBytes(length)); }
public virtual bool Decrypt(IBinaryReader fileDataReader, DumpedMethod dm) { if (fileDataReader.ReadByte() != 0x2A) { return(false); // Not a RET } methodsDataReader.Position = fileDataReader.ReadCompressedUInt32(); dm.mhCodeSize = methodsDataReader.ReadCompressedUInt32(); dm.code = methodsDataReader.ReadBytes((int)dm.mhCodeSize); if ((dm.mhFlags & 8) != 0) { dm.extraSections = MethodBodyParser.ReadExtraSections(methodsDataReader); } if (!DecryptCode(dm)) { return(false); } return(true); }
private static FragmentList Decode(IBinaryReader patchReader) { var l = new FragmentList(); while(patchReader.BaseStream.Position != patchReader.BaseStream.Length) { var start = patchReader.ReadUInt32(); var end = patchReader.ReadUInt32(); var len = patchReader.ReadUInt32(); var data = patchReader.ReadBytes((int)len); var f = new Fragment { Start = (int)start, End = (int)end, Length = (int)len, Data = data }; //Console.WriteLine("chunk:" + start + " " + end + " " + len); l.Add(f); } return l; }
private IEnumerable<HgChunk> ReadBundleGroup(IBinaryReader binaryReader) { const uint nullChunkMaxSize = 4; const int chunkHeaderSize = 84; uint length; while((length = binaryReader.ReadUInt32()) > nullChunkMaxSize) { var nodeID = new HgNodeID(binaryReader.ReadBytes(20)); var firstParentNodeID = new HgNodeID(binaryReader.ReadBytes(20)); var secondParentNodeID = new HgNodeID(binaryReader.ReadBytes(20)); var changesetNodeID = new HgNodeID(binaryReader.ReadBytes(20)); var dataLength = (int)length - chunkHeaderSize; var data = new byte[dataLength]; binaryReader.Read(data, 0, dataLength); var chunk = new HgChunk(nodeID, firstParentNodeID, secondParentNodeID, changesetNodeID, data); yield return chunk; } // while }
public static DecrypterV106 Create(IBinaryReader reader) { try { int keyXorOffs7 = (ReadByteAt(reader, 0) ^ ReadByteAt(reader, 2)) + 2; reader.Position = keyXorOffs7 + (ReadByteAt(reader, 1) ^ ReadByteAt(reader, keyXorOffs7)); int sha1DataLen = reader.Read7BitEncodedInt32() + 0x80; int keyXorOffs6 = (int)reader.Position; int encryptedOffs = (int)reader.Position + sha1DataLen; var sha1Data = reader.ReadBytes(sha1DataLen); uint crc32 = CRC32.CheckSum(sha1Data); reader.Position = reader.Length - 0x18; uint origCrc32 = reader.ReadUInt32(); if (crc32 != origCrc32) return null; var key0 = DeobUtils.Sha1Sum(sha1Data); // 1.0.6.0 var key6 = GetKey(reader, key0, keyXorOffs6); // 1.0.6.6 var key7 = GetKey(reader, key0, keyXorOffs7); // 1.0.6.7 return new DecrypterV106(key0, key6, key7, encryptedOffs); } catch (IOException) { return null; } }
public static DecrypterV105 Create(IBinaryReader reader) { reader.Position = 0; if (reader.Length < 0xA4) return null; var key = reader.ReadBytes(0x94); if (!Utils.Compare(reader.ReadBytes(8), ilpPublicKeyToken)) return null; return new DecrypterV105(new Version(reader.ReadByte(), reader.ReadByte(), reader.ReadByte(), reader.ReadByte()), key); }
static MethodBodyHeader ParseMethodBody2(IBinaryReader reader, out byte[] code, out byte[] extraSections) { var mbHeader = new MethodBodyHeader(); uint codeOffset; byte b = Peek(reader); if ((b & 3) == 2) { mbHeader.flags = 2; mbHeader.maxStack = 8; mbHeader.codeSize = (uint)(reader.ReadByte() >> 2); mbHeader.localVarSigTok = 0; codeOffset = 1; } else if ((b & 7) == 3) { mbHeader.flags = reader.ReadUInt16(); codeOffset = (uint)(4 * (mbHeader.flags >> 12)); if (codeOffset != 12) throw new InvalidMethodBody(); mbHeader.maxStack = reader.ReadUInt16(); mbHeader.codeSize = reader.ReadUInt32(); if (mbHeader.codeSize > int.MaxValue) throw new InvalidMethodBody(); mbHeader.localVarSigTok = reader.ReadUInt32(); if (mbHeader.localVarSigTok != 0 && (mbHeader.localVarSigTok >> 24) != 0x11) throw new InvalidMethodBody(); } else throw new InvalidMethodBody(); if (mbHeader.codeSize + codeOffset > reader.Length) throw new InvalidMethodBody(); code = reader.ReadBytes((int)mbHeader.codeSize); if ((mbHeader.flags & 8) != 0) extraSections = ReadExtraSections2(reader); else extraSections = null; return mbHeader; }
/// <summary> /// Reads a string /// </summary> /// <param name="reader">Reader</param> /// <param name="offset">Offset of string</param> /// <returns>The string or <c>null</c> if we could not read it</returns> static string ReadString(IBinaryReader reader, uint offset) { reader.Position = offset; if (!reader.CanRead(2)) return null; int size = reader.ReadUInt16(); int sizeInBytes = size * 2; if (!reader.CanRead(sizeInBytes)) return null; var stringData = reader.ReadBytes(sizeInBytes); try { return Encoding.Unicode.GetString(stringData); } catch { return null; } }
PdbCustomDebugInfo ReadEncLambdaAndClosureMap(long recPosEnd) { var data = reader.ReadBytes((int)(recPosEnd - reader.Position)); return(new PdbEditAndContinueLambdaMapCustomDebugInfo(data)); }
static byte[] ReadExtraSections2(IBinaryReader reader) { Align(reader, 4); int startPos = (int)reader.Position; ParseSection(reader); int size = (int)reader.Position - startPos; reader.Position = startPos; return reader.ReadBytes(size); }