2015-02-17 23:40:44 +00:00
|
|
|
|
// plist-cil - An open source library to parse and generate property lists for .NET
|
|
|
|
|
|
// Copyright (C) 2015 Natalia Portillo
|
|
|
|
|
|
//
|
|
|
|
|
|
// This code is based on:
|
|
|
|
|
|
// plist - An open source library to parse and generate property lists
|
|
|
|
|
|
// Copyright (C) 2014 Daniel Dreibrodt
|
|
|
|
|
|
//
|
|
|
|
|
|
// 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;
|
2018-06-19 15:18:51 +02:00
|
|
|
|
using System.Buffers.Binary;
|
2015-02-17 23:40:44 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Claunia.PropertyList
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2015-02-20 03:45:45 +00:00
|
|
|
|
/// An UID. Only found in binary property lists that are keyed archives.
|
2015-02-17 23:40:44 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// @author Daniel Dreibrodt
|
2015-02-19 23:54:19 +00:00
|
|
|
|
/// @author Natalia Portillo
|
2015-02-17 23:40:44 +00:00
|
|
|
|
public class UID : NSObject
|
|
|
|
|
|
{
|
2018-06-19 15:18:51 +02:00
|
|
|
|
readonly ulong value;
|
2015-02-17 23:40:44 +00:00
|
|
|
|
|
2015-02-20 02:23:39 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="Claunia.PropertyList.UID"/> class.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="name">Name.</param>
|
|
|
|
|
|
/// <param name="bytes">Bytes.</param>
|
2018-06-19 22:54:05 +02:00
|
|
|
|
[Obsolete("UIDs have not meaningful names")]
|
2018-06-19 15:18:51 +02:00
|
|
|
|
public UID(String name, ReadOnlySpan<byte> bytes)
|
2015-02-17 23:40:44 +00:00
|
|
|
|
{
|
2018-06-19 15:18:51 +02:00
|
|
|
|
if (bytes.Length != 1 && bytes.Length != 2 && bytes.Length != 4 && bytes.Length != 8)
|
2017-05-20 01:31:50 +01:00
|
|
|
|
throw new ArgumentException("Type argument is not valid.");
|
2018-06-19 22:54:05 +02:00
|
|
|
|
this.value = (ulong)BinaryPropertyListParser.ParseLong(bytes);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="Claunia.PropertyList.UID"/> class.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="bytes">Bytes.</param>
|
|
|
|
|
|
public UID(ReadOnlySpan<byte> bytes)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (bytes.Length != 1 && bytes.Length != 2 && bytes.Length != 4 && bytes.Length != 8)
|
|
|
|
|
|
throw new ArgumentException("Type argument is not valid.");
|
2018-06-19 15:18:51 +02:00
|
|
|
|
this.value = (ulong)BinaryPropertyListParser.ParseLong(bytes);
|
2015-02-17 23:40:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-20 01:31:50 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="Claunia.PropertyList.UID"/> class using an unsigned 8-bit number.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="name">Name.</param>
|
|
|
|
|
|
/// <param name="number">Unsigned 8-bit number.</param>
|
2018-06-19 22:54:05 +02:00
|
|
|
|
[Obsolete("UIDs have no meaningful names")]
|
2017-05-20 01:31:50 +01:00
|
|
|
|
public UID(String name, byte number)
|
|
|
|
|
|
{
|
2018-06-19 15:18:51 +02:00
|
|
|
|
this.value = number;
|
2018-06-19 22:54:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="Claunia.PropertyList.UID"/> class using an unsigned 8-bit number.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="number">Unsigned 8-bit number.</param>
|
|
|
|
|
|
public UID(byte number)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.value = number;
|
2017-05-20 01:31:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="Claunia.PropertyList.UID"/> class using a signed 8-bit number.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="name">Name.</param>
|
|
|
|
|
|
/// <param name="number">Unsigned 8-bit number.</param>
|
2018-06-19 22:54:05 +02:00
|
|
|
|
[Obsolete("UIDs must be unsigned values")]
|
2017-05-20 01:31:50 +01:00
|
|
|
|
public UID(String name, sbyte number)
|
|
|
|
|
|
{
|
2018-06-19 15:18:51 +02:00
|
|
|
|
this.value = (ulong)number;
|
2017-05-20 01:31:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="Claunia.PropertyList.UID"/> class using an unsigned 16-bit number.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="name">Name.</param>
|
|
|
|
|
|
/// <param name="number">Unsigned 16-bit number.</param>
|
2018-06-19 22:54:05 +02:00
|
|
|
|
[Obsolete("UIDs have no meaningful names")]
|
2017-05-20 01:31:50 +01:00
|
|
|
|
public UID(String name, ushort number)
|
|
|
|
|
|
{
|
2018-06-19 15:18:51 +02:00
|
|
|
|
this.value = number;
|
2018-06-19 22:54:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="Claunia.PropertyList.UID"/> class using an unsigned 16-bit number.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="name">Name.</param>
|
|
|
|
|
|
/// <param name="number">Unsigned 16-bit number.</param>
|
|
|
|
|
|
public UID(ushort number)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.value = number;
|
2017-05-20 01:31:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="Claunia.PropertyList.UID"/> class using a signed 16-bit number.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="name">Name.</param>
|
|
|
|
|
|
/// <param name="number">Signed 16-bit number.</param>
|
2018-06-19 22:54:05 +02:00
|
|
|
|
[Obsolete("UIDs must be unsigned values")]
|
2017-05-20 01:31:50 +01:00
|
|
|
|
public UID(String name, short number)
|
|
|
|
|
|
{
|
2018-06-19 15:18:51 +02:00
|
|
|
|
this.value = (ulong)number;
|
2017-05-20 01:31:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="Claunia.PropertyList.UID"/> class using an unsigned 32-bit number.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="name">Name.</param>
|
|
|
|
|
|
/// <param name="number">Unsigned 32-bit number.</param>
|
2018-06-19 22:54:05 +02:00
|
|
|
|
[Obsolete("UIDs have no meaningful names")]
|
2017-05-20 01:31:50 +01:00
|
|
|
|
public UID(String name, uint number)
|
|
|
|
|
|
{
|
2018-06-19 15:18:51 +02:00
|
|
|
|
this.value = number;
|
2018-06-19 22:54:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="Claunia.PropertyList.UID"/> class using an unsigned 32-bit number.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="number">Unsigned 32-bit number.</param>
|
|
|
|
|
|
public UID(uint number)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.value = number;
|
2017-05-20 01:31:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="Claunia.PropertyList.UID"/> class using a signed 32-bit number.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="name">Name.</param>
|
|
|
|
|
|
/// <param name="number">Signed 32-bit number.</param>
|
2018-06-19 22:54:05 +02:00
|
|
|
|
[Obsolete("UIDs must be unsigned values")]
|
2017-05-20 01:31:50 +01:00
|
|
|
|
public UID(String name, int number)
|
|
|
|
|
|
{
|
2018-06-19 15:18:51 +02:00
|
|
|
|
this.value = (ulong)number;
|
2017-05-20 01:31:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="Claunia.PropertyList.UID"/> class using an unsigned 64-bit number.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="name">Name.</param>
|
|
|
|
|
|
/// <param name="number">Unsigned 64-bit number.</param>
|
2018-06-19 22:54:05 +02:00
|
|
|
|
[Obsolete("UIDs have no meaningful names")]
|
2017-05-20 01:31:50 +01:00
|
|
|
|
public UID(String name, ulong number)
|
|
|
|
|
|
{
|
2018-06-19 15:18:51 +02:00
|
|
|
|
this.value = number;
|
2018-06-19 22:54:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="Claunia.PropertyList.UID"/> class using an unsigned 64-bit number.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="number">Unsigned 64-bit number.</param>
|
|
|
|
|
|
public UID(ulong number)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.value = number;
|
2017-05-20 01:31:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="Claunia.PropertyList.UID"/> class using a signed 64-bit number.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="name">Name.</param>
|
|
|
|
|
|
/// <param name="number">Signed 64-bit number.</param>
|
2018-06-19 22:54:05 +02:00
|
|
|
|
[Obsolete("UIDs must be unsigned values")]
|
2017-05-20 01:31:50 +01:00
|
|
|
|
public UID(String name, long number)
|
|
|
|
|
|
{
|
2018-06-19 15:18:51 +02:00
|
|
|
|
this.value = (ulong)number;
|
2017-05-20 01:31:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-02-20 02:23:39 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the bytes.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <value>The bytes.</value>
|
2015-02-17 23:40:44 +00:00
|
|
|
|
public byte[] Bytes
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
2018-06-19 22:54:05 +02:00
|
|
|
|
byte[] bytes = new byte[this.ByteCount];
|
2018-06-19 15:18:51 +02:00
|
|
|
|
this.GetBytes(bytes);
|
2015-02-17 23:40:44 +00:00
|
|
|
|
return bytes;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-06-19 22:54:05 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the number of bytes required to represent this <see cref="UID"/>.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public int ByteCount
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (this.value <= byte.MaxValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (this.value <= ushort.MaxValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
return 2;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (this.value <= uint.MaxValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
return 4;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return 8;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Writes the bytes required to represent this <see cref="UID"/> to a byte span.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="bytes">
|
|
|
|
|
|
/// The byte span to which to write the byte representation of this UID.
|
|
|
|
|
|
/// </param>
|
2018-06-19 15:18:51 +02:00
|
|
|
|
public void GetBytes(Span<byte> bytes)
|
|
|
|
|
|
{
|
2018-06-19 22:54:05 +02:00
|
|
|
|
switch (this.ByteCount)
|
2018-06-19 15:18:51 +02:00
|
|
|
|
{
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
bytes[0] = (byte)this.value;
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
|
|
BinaryPrimitives.WriteUInt16BigEndian(bytes, (ushort)this.value);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
|
|
BinaryPrimitives.WriteUInt32BigEndian(bytes, (uint)this.value);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
|
|
BinaryPrimitives.WriteUInt64BigEndian(bytes, this.value);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-02-20 02:23:39 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the name.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <value>The name.</value>
|
2018-06-19 22:54:05 +02:00
|
|
|
|
[Obsolete("UIDs have no meaningful names")]
|
2015-02-17 23:40:44 +00:00
|
|
|
|
public string Name
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
2018-06-19 22:54:05 +02:00
|
|
|
|
return this.value.ToString();
|
2015-02-17 23:40:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// There is no XML representation specified for UIDs.
|
|
|
|
|
|
/// In this implementation UIDs are represented as strings in the XML output.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="xml">The xml StringBuilder</param>
|
|
|
|
|
|
/// <param name="level">The indentation level</param>
|
2015-02-20 00:00:26 +00:00
|
|
|
|
internal override void ToXml(StringBuilder xml, int level)
|
|
|
|
|
|
{
|
2015-02-17 23:40:44 +00:00
|
|
|
|
Indent(xml, level);
|
|
|
|
|
|
xml.Append("<string>");
|
2018-06-19 22:54:05 +02:00
|
|
|
|
Span<byte> bytes = stackalloc byte[this.ByteCount];
|
2018-06-19 17:13:34 +02:00
|
|
|
|
this.GetBytes(bytes);
|
2015-02-20 00:42:40 +00:00
|
|
|
|
foreach (byte b in bytes)
|
2015-02-17 23:40:44 +00:00
|
|
|
|
xml.Append(String.Format("{0:x2}", b));
|
|
|
|
|
|
xml.Append("</string>");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-02-20 00:00:26 +00:00
|
|
|
|
internal override void ToBinary(BinaryPropertyListWriter outPlist)
|
|
|
|
|
|
{
|
2018-06-19 22:54:05 +02:00
|
|
|
|
outPlist.Write(0x80 + this.ByteCount - 1);
|
|
|
|
|
|
Span<byte> bytes = stackalloc byte[this.ByteCount];
|
2018-06-19 17:13:34 +02:00
|
|
|
|
this.GetBytes(bytes);
|
2015-02-19 17:41:33 +00:00
|
|
|
|
outPlist.Write(bytes);
|
|
|
|
|
|
}
|
2015-02-17 23:40:44 +00:00
|
|
|
|
|
2015-02-20 00:00:26 +00:00
|
|
|
|
internal override void ToASCII(StringBuilder ascii, int level)
|
|
|
|
|
|
{
|
2015-02-17 23:40:44 +00:00
|
|
|
|
Indent(ascii, level);
|
|
|
|
|
|
ascii.Append("\"");
|
2018-06-19 22:54:05 +02:00
|
|
|
|
Span<byte> bytes = stackalloc byte[this.ByteCount];
|
2018-06-19 17:13:34 +02:00
|
|
|
|
this.GetBytes(bytes);
|
2015-02-20 00:42:40 +00:00
|
|
|
|
foreach (byte b in bytes)
|
2015-02-17 23:40:44 +00:00
|
|
|
|
ascii.Append(String.Format("{0:x2}", b));
|
|
|
|
|
|
ascii.Append("\"");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-02-20 00:00:26 +00:00
|
|
|
|
internal override void ToASCIIGnuStep(StringBuilder ascii, int level)
|
|
|
|
|
|
{
|
2015-02-17 23:40:44 +00:00
|
|
|
|
ToASCII(ascii, level);
|
|
|
|
|
|
}
|
2015-02-19 21:30:01 +00:00
|
|
|
|
|
2015-02-20 02:23:39 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Determines whether the specified <see cref="Claunia.PropertyList.NSObject"/> is equal to the current <see cref="Claunia.PropertyList.UID"/>.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="obj">The <see cref="Claunia.PropertyList.NSObject"/> to compare with the current <see cref="Claunia.PropertyList.UID"/>.</param>
|
|
|
|
|
|
/// <returns><c>true</c> if the specified <see cref="Claunia.PropertyList.NSObject"/> is equal to the current
|
|
|
|
|
|
/// <see cref="Claunia.PropertyList.UID"/>; otherwise, <c>false</c>.</returns>
|
2015-02-19 21:30:01 +00:00
|
|
|
|
public override bool Equals(NSObject obj)
|
2018-06-19 17:13:34 +02:00
|
|
|
|
{
|
|
|
|
|
|
return Equals((object)obj);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
|
public override bool Equals(object obj)
|
2015-02-19 21:30:01 +00:00
|
|
|
|
{
|
2018-06-19 15:18:51 +02:00
|
|
|
|
var uid = obj as UID;
|
2015-02-19 21:30:01 +00:00
|
|
|
|
|
2018-06-19 15:18:51 +02:00
|
|
|
|
if (uid == null)
|
2015-02-19 21:30:01 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
|
2018-06-19 22:54:05 +02:00
|
|
|
|
return uid.value == value;
|
2015-02-19 21:30:01 +00:00
|
|
|
|
}
|
2018-06-19 17:13:34 +02:00
|
|
|
|
|
2018-06-19 22:54:05 +02:00
|
|
|
|
/// <inheritdoc/>
|
2018-06-19 17:13:34 +02:00
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
|
{
|
2018-06-19 22:54:05 +02:00
|
|
|
|
return this.value.GetHashCode();
|
2018-06-19 17:13:34 +02:00
|
|
|
|
}
|
2018-06-26 13:59:12 +02:00
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
|
{
|
|
|
|
|
|
return $"{this.value} (UID)";
|
|
|
|
|
|
}
|
2015-02-17 23:40:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|