/// <summary> /// Reads a null-terminated UTF-16 string /// </summary> /// <param name="offset">Offset of the string</param> /// <returns>The UTF-16 string</returns> public static async Task <string> ReadNullTerminatedUnicodeStringAsync(this IReadOnlyBinaryDataAccessor accessor, long index) { int length = 0; while (await accessor.ReadAsync(index + length * 2) != 0 || await accessor.ReadAsync(index + length * 2 + 1) != 0) { length += 1; } return(accessor.ReadUnicodeString(index, length)); }
/// <summary> /// Reads an unsigned 64 bit big endian integer /// </summary> /// <param name="offset">Offset of the integer to read.</param> /// <returns>The integer from the given location</returns> public static async Task <UInt64> ReadUInt64BigEndianAsync(this IReadOnlyBinaryDataAccessor accessor, long offset) { var bytes = await accessor.ReadAsync(offset, 8); Array.Reverse(bytes); return(BitConverter.ToUInt64(bytes, 0)); }
/// <summary> /// Reads a null-terminated using the given encoding /// </summary> /// <param name="offset">Offset of the string</param> /// <returns>The string at the given location</returns> public static async Task <string> ReadNullTerminatedStringAsync(this IReadOnlyBinaryDataAccessor accessor, long index, Encoding e) { // The null character we're looking for var nullCharSequence = e.GetBytes(Convert.ToChar(0x0).ToString()); // Find the length of the string as determined by the location of the null-char sequence int length = 0; while (!(await accessor.ReadAsync(index + length * nullCharSequence.Length, nullCharSequence.Length)).All(x => x == 0)) { length += 1; } return(accessor.ReadString(index, length, e)); }
/// <summary> /// Reads a signed 32 bit little endian integer /// </summary> /// <param name="offset">Offset of the integer to read.</param> /// <returns>The integer from the given location</returns> public static async Task <Int32> ReadInt32Async(this IReadOnlyBinaryDataAccessor accessor, long offset) { return(BitConverter.ToInt32(await accessor.ReadAsync(offset, 4), 0)); }
/// <summary> /// Reads a string using the given encoding /// </summary> /// <param name="offset">Offset of the string</param> /// <param name="length">Length in characters of the string</param> /// <returns>The UTF-16 string at the given offset</returns> public static async Task <string> ReadStringAsync(this IReadOnlyBinaryDataAccessor accessor, long index, int length, Encoding e) { return(e.GetString(await accessor.ReadAsync(index, length), 0, length)); }
/// <summary> /// Reads an unsigned 64 bit little endian integer /// </summary> /// <param name="offset">Offset of the integer to read.</param> /// <returns>The integer from the given location</returns> public static async Task <UInt64> ReadUInt64Async(this IReadOnlyBinaryDataAccessor accessor, long offset) { return(BitConverter.ToUInt64(await accessor.ReadAsync(offset, 8), 0)); }