// OCTET STRING SNMP data type. // Copyright (C) 2008-2010 Malcolm Crowe, Lex Li, and other contributors. // // Permission is hereby granted, free of charge, to any person obtaining a copy of this // software and associated documentation files (the "Software"), to deal in the Software // without restriction, including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons // to whom the Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. using System; using System.Globalization; using System.IO; using System.Linq; using System.Net.NetworkInformation; using System.Text; // ASN.1 BER encoding library by Malcolm Crowe at the University of the West of Scotland // See http://cis.paisley.ac.uk/crow-ci0 // This is version 0 of the library, please advise me about any bugs // mailto:malcolm.crowe@paisley.ac.uk // Restrictions: It is assumed that no encoding has index length greater than 2^31-1. // UNIVERSAL TYPES // Some of the more unusual Universal encodings are supported but not fully implemented // Should you require these types, as an alternative to changing this code // you can catch the exception that is thrown and examine the contents yourself. // APPLICATION TYPES // If you want to handle Application types systematically, you can derive index class from // Universal, and provide the Creator and Creators methods for your class // You will see an example of how to do this in the Snmplib // CONTEXT AND PRIVATE TYPES // Ad hoc coding can be used for these, as an alterative to derive index class as above. namespace GSF.Net.Snmp { /// /// OctetString type. /// public sealed class OctetString // This namespace has its own concept of string : ISnmpData, IEquatable { private static readonly OctetString EmptyString = new OctetString(string.Empty, Encoding.GetEncoding("ASCII")); // IMPORTANT: use GetEncoding because of CF. private static Encoding _defaultEncoding = Encoding.GetEncoding("ASCII"); private readonly byte[] _raw; private byte[] _length; /// /// Initializes a new instance of the class. /// /// The length. /// The stream. public OctetString(Tuple length, Stream stream) { if (length == null) { throw new ArgumentNullException(nameof(length)); } if (stream == null) { throw new ArgumentNullException(nameof(stream)); } _raw = new byte[length.Item1]; stream.Read(_raw, 0, length.Item1); Encoding = DefaultEncoding; _length = length.Item2; } /// /// Creates an from raw bytes. /// /// Raw bytes public OctetString(byte[] raw) { if (raw == null) { throw new ArgumentNullException(nameof(raw)); } _raw = new byte[raw.Length]; Buffer.BlockCopy(raw, 0, _raw, 0, raw.Length); Encoding = DefaultEncoding; } /// /// Creates an with a specific . This string is treated in specific . /// /// String. /// Encoding. public OctetString(string content, Encoding encoding) { Encoding = encoding; _raw = Encoding.GetBytes(content); } /// /// Creates an with a specific . This string is treated as UTF-16. /// /// String. public OctetString(string content) : this(content, DefaultEncoding) { } /// /// Creates an with a specific . /// /// public OctetString(Levels level) : this(new[] { (byte)level }) { } /// /// Encoding of this /// public Encoding Encoding { get; private set; } /// /// Gets raw bytes. /// /// public byte[] GetRaw() { return _raw; } /// /// Gets the empty string. /// /// The empty. public static OctetString Empty { get { return EmptyString; } } /// /// Returns a that represents this . /// /// public Levels ToLevels() { byte[] bytes = GetRaw(); if (bytes.Length > 1) { throw new InvalidCastException("Length should be 1"); } return (Levels)(bytes[0] & 7); } /// /// Returns a in a hex form that represents this . /// /// public string ToHexString() { StringBuilder result = new StringBuilder(); foreach (byte b in _raw) { result.Append(b.ToString("X2", CultureInfo.InvariantCulture)); } return result.ToString(); } /// /// Returns a in a specific that represents this . /// /// /// public string ToString(Encoding encoding) { if (encoding == null) { throw new ArgumentNullException(nameof(encoding)); } return encoding.GetString(_raw, 0, _raw.Length); // use this call for SL3. } /// /// Returns a in UTF-16 that represents this . /// /// public override string ToString() { return ToString(Encoding); } /// /// Type code. /// public SnmpType TypeCode { get { return SnmpType.OctetString; } } /// /// Appends the bytes to . /// /// The stream. public void AppendBytesTo(Stream stream) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } stream.AppendBytes(TypeCode, _length, _raw); } /// /// Indicates whether the current object is equal to another object of the same type. /// /// An object to compare with this object. /// true if the current object is equal to the parameter; otherwise, false. /// public bool Equals(OctetString other) { return Equals(this, other); } /// /// Determines whether the specified is equal to the current . /// /// The to compare with the current . /// true if the specified is equal to the current ; otherwise, false. /// public override bool Equals(object obj) { return Equals(this, obj as OctetString); } /// /// Serves as a hash function for a particular type. /// /// A hash code for the current . public override int GetHashCode() { return ToString(Encoding.Unicode).GetHashCode(); } /// /// The equality operator. /// /// Left object /// Right object /// /// Returns true if the values of its operands are equal, false otherwise. public static bool operator ==(OctetString left, OctetString right) { return Equals(left, right); } /// /// The inequality operator. /// /// Left object /// Right object /// /// Returns true if the values of its operands are not equal, false otherwise. public static bool operator !=(OctetString left, OctetString right) { return !(left == right); } #if !NETFX_CORE /// /// Converts octets to physical address. /// /// public PhysicalAddress ToPhysicalAddress() { if (_raw.Length != 6) { throw new InvalidCastException(string.Format(CultureInfo.InvariantCulture, "the data length is not equal to 6: {0}", _raw.Length)); } return new PhysicalAddress(_raw); } #endif /// /// Default encoding of type. /// public static Encoding DefaultEncoding { get { return _defaultEncoding; } set { _defaultEncoding = value; } } /// /// The comparison. /// /// Left object /// Right object /// /// Returns true if the values of its operands are not equal, false otherwise. private static bool Equals(OctetString left, OctetString right) { object lo = left; object ro = right; if (lo == ro) { return true; } if (lo == null || ro == null) { return false; } return left._raw.SequenceEqual(right._raw); } internal byte[] GetLengthBytes() { return _length; } internal void SetLengthBytes(byte[] bytes) { _length = bytes; } /// /// Indicates whether the specified object is a null reference (Nothing in Visual Basic) or an string. /// /// A reference. /// true if the parameter is a null reference (Nothing in Visual Basic) or an empty string (""); otherwise, false. public static bool IsNullOrEmpty(OctetString value) { if (value == null) { return true; } if (value == Empty) { return true; } return false; } } // all references here are to ITU-X.690-12/97 }