// dnlib: See LICENSE.txt for more info using System; using dnlib.IO; namespace dnlib.DotNet.MD { /// /// Represents the #US stream /// public sealed class USStream : HeapStream { /// public USStream() { } /// public USStream(DataReaderFactory mdReaderFactory, uint metadataBaseOffset, StreamHeader streamHeader) : base(mdReaderFactory, metadataBaseOffset, streamHeader) { } /// /// Reads a unicode string /// /// Offset of unicode string /// A string or null if is invalid public string Read(uint offset) { if (offset == 0) return string.Empty; if (!IsValidOffset(offset)) return null; var reader = dataReader; reader.Position = offset; if (!reader.TryReadCompressedUInt32(out uint length)) return null; if (!reader.CanRead(length)) return null; try { return reader.ReadUtf16String((int)(length / 2)); } catch (OutOfMemoryException) { throw; } catch { // It's possible that an exception is thrown when converting a char* to // a string. If so, return an empty string. return string.Empty; } } /// /// Reads data just like , but returns an empty string if /// offset is invalid /// /// Offset of unicode string /// The string public string ReadNoNull(uint offset) => Read(offset) ?? string.Empty; } }