/// <summary> /// Adds a float to the internal object table. /// </summary> /// <param name="value">The value to add.</param> /// <returns>The index of the added value.</returns> private int AddFloat(float value) { if (!_uniques.Contains(value)) { var index = _objectTable.Count; var buffer = BitConverter.GetBytes(value); if (BitConverter.IsLittleEndian) { Array.Reverse(buffer); } var item = new BinaryPlistItem(value); item.Marker.Add((byte)(0x20 | (byte)Math.Log(buffer.Length, 2))); item.SetByteValue(buffer); _objectTable.Add(item); _objectTableSize += item.Size; _uniques.SetIndex(value, index); return(index); } return(_uniques.GetIndex(value)); }
/// <summary> /// Adds a dictionary to the internal object table. /// </summary> /// <param name="value">The value to add.</param> /// <returns>The index of the added value.</returns> private int AddDictionary(IDictionary value) { var index = _objectTable.Count; var dict = new BinaryPlistDictionary(_objectTable, value.Count); var item = new BinaryPlistItem(dict) { IsDictionary = true }; _objectTable.Add(item); foreach (var key in value.Keys) { dict.KeyReference.Add(AddObject(key)); dict.ObjectReference.Add(AddObject(value[key])); _objectRefCount += 2; } if (dict.KeyReference.Count < 15) { item.Marker.Add((byte)(0xD0 | (byte)dict.KeyReference.Count)); } else { item.Marker.Add(0xDF); AddIntegerCount(item.Marker, dict.KeyReference.Count); } _objectTableSize += item.Size; return(index); }
/// <summary> /// Adds a date to the internal object table. /// </summary> /// <param name="value">The value to add.</param> /// <returns>The index of the added value.</returns> private int AddDate(DateTime value) { if (!_uniques.Contains(value)) { var index = _objectTable.Count; var buffer = BitConverter.GetBytes(value.ToUniversalTime().Subtract(ReferenceDate).TotalSeconds); if (BitConverter.IsLittleEndian) { Array.Reverse(buffer); } var item = new BinaryPlistItem(value); item.Marker.Add(0x33); item.SetByteValue(buffer); _objectTable.Add(item); _objectTableSize += item.Size; _uniques.SetIndex(value, index); return(index); } return(_uniques.GetIndex(value)); }
/// <summary> /// Adds an array to the internal object table. /// </summary> /// <param name="value">The value to add.</param> /// <returns>The index of the added value.</returns> private int AddArray(IEnumerable value) { var index = _objectTable.Count; var array = new BinaryPlistArray(_objectTable); var item = new BinaryPlistItem(array) { IsArray = true }; _objectTable.Add(item); foreach (var obj in value) { array.ObjectReference.Add(AddObject(obj)); _objectRefCount++; } if (array.ObjectReference.Count < 15) { item.Marker.Add((byte)(0xA0 | (byte)array.ObjectReference.Count)); } else { item.Marker.Add(0xAF); AddIntegerCount(item.Marker, array.ObjectReference.Count); } _objectTableSize += item.Size; return(index); }
/// <summary> /// Writes an array item to the given <see cref="BinaryWriter" />. /// </summary> /// <param name="writer">The <see cref="BinaryWriter" /> to write to.</param> /// <param name="value">The array item to write.</param> /// <returns>The number of bytes written.</returns> private int WriteArray(BinaryWriter writer, BinaryPlistItem value) { var size = value.Marker.Count; var array = (BinaryPlistArray)value.Value; writer.Write(value.Marker.ToArray()); size += array.ObjectReference.Sum(objectRef => WriteReferenceInteger(writer, objectRef, _objectRefSize)); return(size); }
/// <summary> /// Writes a dictionary item to the given <see cref="BinaryWriter" />. /// </summary> /// <param name="writer">The <see cref="BinaryWriter" /> to write to.</param> /// <param name="value">The dictionary item to write.</param> /// <returns>The number of bytes written.</returns> private int WriteDictionary(BinaryWriter writer, BinaryPlistItem value) { var size = value.Marker.Count; var dict = (BinaryPlistDictionary)value.Value; writer.Write(value.Marker.ToArray()); size += dict.KeyReference.Sum(keyRef => WriteReferenceInteger(writer, keyRef, _objectRefSize)); size += dict.ObjectReference.Sum(objectRef => WriteReferenceInteger(writer, objectRef, _objectRefSize)); return(size); }
/// <summary> /// Adds a string to the internal object table. /// </summary> /// <param name="value">The value to add.</param> /// <returns>The index of the added value.</returns> private int AddString(string value) { if (!_uniques.Contains(value)) { var index = _objectTable.Count; var ascii = value.IsAscii(); byte[] buffer; var item = new BinaryPlistItem(value); if (value.Length < 15) { item.Marker.Add((byte)((byte)(ascii ? 0x50 : 0x60) | (byte)value.Length)); } else { item.Marker.Add((byte)(ascii ? 0x5F : 0x6F)); AddIntegerCount(item.Marker, value.Length); } if (ascii) { buffer = Encoding.ASCII.GetBytes(value); } else { buffer = Encoding.Unicode.GetBytes(value); if (BitConverter.IsLittleEndian) { for (var i = 0; i < buffer.Length; i++) { var l = buffer[i]; buffer[i] = buffer[++i]; buffer[i] = l; } } } item.SetByteValue(buffer); _objectTable.Add(item); _objectTableSize += item.Size; _uniques.SetIndex(value, index); return(index); } return(_uniques.GetIndex(value)); }
/// <summary> /// Adds an integer to the internal object table. /// </summary> /// <param name="value">The value to add.</param> /// <returns>The index of the added value.</returns> private int AddInteger(long value) { if (!_uniques.Contains(value)) { var index = _objectTable.Count; var item = new BinaryPlistItem(value); item.SetByteValue(GetIntegerBytes(value)); item.Marker.Add((byte)(0x10 | (byte)Math.Log(item.ByteValue.Count, 2))); _objectTable.Add(item); _objectTableSize += item.Size; _uniques.SetIndex(value, index); return(index); } return(_uniques.GetIndex(value)); }
/// <summary> /// Adds arbitrary data to the internal object table. /// </summary> /// <param name="value">The value to add.</param> /// <returns>The index of the added value.</returns> private int AddData(object value) { var index = _objectTable.Count; var bufferIndex = 0; if (!(value is byte[] buffer)) { using (var stream = new MemoryStream()) { var formatter = new BinaryFormatter(); formatter.Serialize(stream, value); stream.Position = 0; buffer = new byte[stream.Length]; int count; while (0 < (count = stream.Read(buffer, 0, buffer.Length - bufferIndex))) { bufferIndex += count; } } } var item = new BinaryPlistItem(value); item.SetByteValue(buffer); if (buffer.Length < 15) { item.Marker.Add((byte)(0x40 | (byte)buffer.Length)); } else { item.Marker.Add(0x4F); AddIntegerCount(item.Marker, buffer.Length); } _objectTable.Add(item); _objectTableSize += item.Size; return(index); }
/// <summary> /// Adds a primitive to the internal object table. /// </summary> /// <param name="value">The value to add.</param> /// <returns>The index of the added value.</returns> private int AddPrimitive(bool?value) { if (!value.HasValue || !_uniques.Contains(value.Value)) { var index = _objectTable.Count; var item = new BinaryPlistItem(value); item.Marker.Add(value.HasValue ? value.Value ? (byte)0x9 : (byte)0x8 : (byte)0); _objectTable.Add(item); _objectTableSize += item.Size; if (value.HasValue) { _uniques.SetIndex(value.Value, index); } return(index); } return(_uniques.GetIndex(value.Value)); }
/// <summary> /// Reads the object table from the given reader. /// </summary> /// <param name="reader">The reader to read the object table from.</param> private void ReadObjectTable(BinaryReader reader) { for (var i = 0; i < _objectCount; i++) { reader.BaseStream.Position = _offsetTable[i]; var marker = reader.ReadByte(); // The first half of the byte is the base marker. int size; int intSize; BinaryPlistItem item; switch ((marker & 0xf0) >> 4) { case 0: if (ReadPrimitive(reader, reader.BaseStream.Position - 1, out var primitive)) { _objectTable.Add(new BinaryPlistItem(primitive)); } break; case 1: size = 1 << (marker & 0xf); var parsedInt = ReadInteger(reader, reader.BaseStream.Position, size); if (size < 4) { _objectTable.Add(new BinaryPlistItem((short)parsedInt)); } else if (size < 8) { _objectTable.Add(new BinaryPlistItem((int)parsedInt)); } else { _objectTable.Add(new BinaryPlistItem(parsedInt)); } break; case 2: size = 1 << (marker & 0xf); _objectTable.Add(new BinaryPlistItem(ReadReal(reader, reader.BaseStream.Position, size))); break; case 3: size = marker & 0xf; if (size == 3) { _objectTable.Add(new BinaryPlistItem(ReadDate(reader, reader.BaseStream.Position, 8))); } else { throw new InvalidOperationException("Unsupported date size: " + size.ToBinaryString()); } break; case 4: size = marker & 0xf; if (size == 15) { intSize = 1 << (reader.ReadByte() & 0xf); size = (int)ReadInteger(reader, reader.BaseStream.Position, intSize); } _objectTable.Add(new BinaryPlistItem(ReadData(reader, reader.BaseStream.Position, size))); break; case 5: size = marker & 0xf; if (size == 15) { intSize = 1 << (reader.ReadByte() & 0xf); size = (int)ReadInteger(reader, reader.BaseStream.Position, intSize); } _objectTable.Add(new BinaryPlistItem(ReadAsciiString(reader, reader.BaseStream.Position, size))); break; case 6: size = marker & 0xf; if (size == 15) { intSize = 1 << (reader.ReadByte() & 0xf); size = (int)ReadInteger(reader, reader.BaseStream.Position, intSize); } _objectTable.Add( new BinaryPlistItem(ReadUnicodeString(reader, reader.BaseStream.Position, size))); break; case 8: size = (marker & 0xf) + 1; _objectTable.Add(new BinaryPlistItem(ReadUniqueId(reader, reader.BaseStream.Position, size))); break; case 10: case 12: size = marker & 0xf; if (size == 15) { intSize = 1 << (reader.ReadByte() & 0xf); size = (int)ReadInteger(reader, reader.BaseStream.Position, intSize); } item = new BinaryPlistItem(ReadArray(reader, reader.BaseStream.Position, size)); item.IsArray = true; _objectTable.Add(item); break; case 13: size = marker & 0xf; if (size == 15) { intSize = 1 << (reader.ReadByte() & 0xf); size = (int)ReadInteger(reader, reader.BaseStream.Position, intSize); } item = new BinaryPlistItem(ReadDictionary(reader, reader.BaseStream.Position, size)); item.IsDictionary = true; _objectTable.Add(item); break; default: throw new InvalidOperationException( "An invalid marker was found while reading the object table: " + marker.ToBinaryString()); } } }