2018-07-23 23:25:43 +01:00
|
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
|
// Aaru Data Preservation Suite
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : Write.cs
|
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
|
//
|
|
|
|
|
|
// Component : Disk image plugins.
|
|
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Writes Basic Lisa Utility disk images.
|
|
|
|
|
|
//
|
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// This library is free software; you can redistribute it and/or modify
|
|
|
|
|
|
// it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
|
// published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
|
//
|
|
|
|
|
|
// This library is distributed in the hope that it will be useful, but
|
|
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
|
|
//
|
|
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
|
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2022-12-03 16:07:10 +00:00
|
|
|
|
// Copyright © 2011-2023 Natalia Portillo
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2023-10-05 01:05:23 +01:00
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
2020-02-27 00:33:26 +00:00
|
|
|
|
using Aaru.CommonTypes;
|
2022-12-15 22:21:07 +00:00
|
|
|
|
using Aaru.CommonTypes.AaruMetadata;
|
2020-02-27 00:33:26 +00:00
|
|
|
|
using Aaru.CommonTypes.Enums;
|
|
|
|
|
|
using Aaru.CommonTypes.Structs;
|
|
|
|
|
|
using Aaru.Decoders;
|
2020-07-20 15:43:52 +01:00
|
|
|
|
using Aaru.Helpers;
|
2020-02-27 00:33:26 +00:00
|
|
|
|
using Version = Aaru.CommonTypes.Interop.Version;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2023-10-06 01:16:28 +01:00
|
|
|
|
namespace Aaru.Images;
|
2022-11-15 15:58:43 +00:00
|
|
|
|
|
2023-10-05 01:05:23 +01:00
|
|
|
|
[SuppressMessage("ReSharper", "UnusedType.Global")]
|
2022-03-06 13:29:38 +00:00
|
|
|
|
public sealed partial class Blu
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2023-10-03 23:34:59 +01:00
|
|
|
|
#region IWritableImage Members
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public bool Create(string path, MediaType mediaType, Dictionary<string, string> options, ulong sectors,
|
2023-10-03 23:34:59 +01:00
|
|
|
|
uint sectorSize)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(sectorSize != 512)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Unsupported_sector_size;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(sectors > 0xFFFFFF)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Too_many_sectors;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(!SupportedMediaTypes.Contains(mediaType))
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = string.Format(Localization.Unsupported_media_format_0, mediaType);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_imageInfo = new ImageInfo
|
|
|
|
|
|
{
|
|
|
|
|
|
MediaType = mediaType,
|
|
|
|
|
|
SectorSize = sectorSize,
|
|
|
|
|
|
Sectors = sectors
|
|
|
|
|
|
};
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
_writingStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch(IOException e)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = string.Format(Localization.Could_not_create_new_image_file_exception_0, e.Message);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
IsWriting = true;
|
|
|
|
|
|
ErrorMessage = null;
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public bool WriteMediaTag(byte[] data, MediaTagType tag)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Writing_media_tags_is_not_supported;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public bool WriteSector(byte[] data, ulong sectorAddress)
|
|
|
|
|
|
{
|
|
|
|
|
|
int longSectorSize = _imageInfo.MediaType == MediaType.PriamDataTower ? 536 : 532;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(!IsWriting)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Tried_to_write_on_a_non_writable_image;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(data.Length != 512)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Incorrect_data_size;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(sectorAddress >= _imageInfo.Sectors)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Tried_to_write_past_image_size;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2023-10-03 23:34:59 +01:00
|
|
|
|
_writingStream.Seek(longSectorSize + (long)sectorAddress * longSectorSize, SeekOrigin.Begin);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_writingStream.Write(data, 0, data.Length);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
ErrorMessage = "";
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public bool WriteSectors(byte[] data, ulong sectorAddress, uint length)
|
|
|
|
|
|
{
|
|
|
|
|
|
int longSectorSize = _imageInfo.MediaType == MediaType.PriamDataTower ? 536 : 532;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(!IsWriting)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Tried_to_write_on_a_non_writable_image;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(data.Length % 512 != 0)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Incorrect_data_size;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(sectorAddress + length > _imageInfo.Sectors)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Tried_to_write_past_image_size;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2023-10-03 23:34:59 +01:00
|
|
|
|
_writingStream.Seek(longSectorSize + (long)sectorAddress * longSectorSize, SeekOrigin.Begin);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_writingStream.Write(data, 0, data.Length);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
ErrorMessage = "";
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public bool WriteSectorLong(byte[] data, ulong sectorAddress)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!IsWriting)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Tried_to_write_on_a_non_writable_image;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(sectorAddress >= _imageInfo.Sectors)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Tried_to_write_past_image_size;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int longSectorSize = _imageInfo.MediaType == MediaType.PriamDataTower ? 536 : 532;
|
|
|
|
|
|
|
|
|
|
|
|
byte[] oldTag;
|
|
|
|
|
|
byte[] newTag;
|
|
|
|
|
|
|
|
|
|
|
|
switch(data.Length - 512)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Sony tag, convert to Profile
|
|
|
|
|
|
case 12 when longSectorSize == 532:
|
|
|
|
|
|
oldTag = new byte[12];
|
|
|
|
|
|
Array.Copy(data, 512, oldTag, 0, 12);
|
|
|
|
|
|
newTag = LisaTag.DecodeSonyTag(oldTag)?.ToProfile().GetBytes();
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
// Sony tag, convert to Priam
|
|
|
|
|
|
case 12:
|
|
|
|
|
|
oldTag = new byte[12];
|
|
|
|
|
|
Array.Copy(data, 512, oldTag, 0, 12);
|
|
|
|
|
|
newTag = LisaTag.DecodeSonyTag(oldTag)?.ToPriam().GetBytes();
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
// Profile tag, copy to Profile
|
|
|
|
|
|
case 20 when longSectorSize == 532:
|
|
|
|
|
|
newTag = new byte[20];
|
|
|
|
|
|
Array.Copy(data, 512, newTag, 0, 20);
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
// Profile tag, convert to Priam
|
|
|
|
|
|
case 20:
|
|
|
|
|
|
oldTag = new byte[20];
|
|
|
|
|
|
Array.Copy(data, 512, oldTag, 0, 20);
|
|
|
|
|
|
newTag = LisaTag.DecodeProfileTag(oldTag)?.ToPriam().GetBytes();
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
// Priam tag, convert to Profile
|
|
|
|
|
|
case 24 when longSectorSize == 532:
|
|
|
|
|
|
oldTag = new byte[24];
|
|
|
|
|
|
Array.Copy(data, 512, oldTag, 0, 24);
|
|
|
|
|
|
newTag = LisaTag.DecodePriamTag(oldTag)?.ToProfile().GetBytes();
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
// Priam tag, copy to Priam
|
|
|
|
|
|
case 24:
|
|
|
|
|
|
newTag = new byte[24];
|
|
|
|
|
|
Array.Copy(data, 512, newTag, 0, 24);
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 0:
|
|
|
|
|
|
newTag = null;
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Incorrect_data_size;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
return false;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
newTag ??= new byte[longSectorSize - 512];
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2023-10-03 23:34:59 +01:00
|
|
|
|
_writingStream.Seek(longSectorSize + (long)sectorAddress * longSectorSize, SeekOrigin.Begin);
|
|
|
|
|
|
_writingStream.Write(data, 0, 512);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_writingStream.Write(newTag, 0, newTag.Length);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
ErrorMessage = "";
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public bool WriteSectorsLong(byte[] data, ulong sectorAddress, uint length)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!IsWriting)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Tried_to_write_on_a_non_writable_image;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(sectorAddress + length > _imageInfo.Sectors)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Tried_to_write_past_image_size;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
int longSectorSize = _imageInfo.MediaType == MediaType.PriamDataTower ? 536 : 532;
|
|
|
|
|
|
long givenSectorSize = data.Length / length;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
switch(givenSectorSize)
|
|
|
|
|
|
{
|
|
|
|
|
|
case 536:
|
|
|
|
|
|
case 532:
|
|
|
|
|
|
case 524:
|
2023-10-03 23:34:59 +01:00
|
|
|
|
case 512:
|
|
|
|
|
|
break;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
default:
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Incorrect_data_size;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
for(uint i = 0; i < length; i++)
|
|
|
|
|
|
{
|
2018-07-23 23:25:43 +01:00
|
|
|
|
byte[] oldTag;
|
|
|
|
|
|
byte[] newTag;
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
switch(givenSectorSize - 512)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
|
|
|
|
|
// Sony tag, convert to Profile
|
|
|
|
|
|
case 12 when longSectorSize == 532:
|
|
|
|
|
|
oldTag = new byte[12];
|
2023-10-03 23:34:59 +01:00
|
|
|
|
Array.Copy(data, givenSectorSize * i + 512, oldTag, 0, 12);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
newTag = LisaTag.DecodeSonyTag(oldTag)?.ToProfile().GetBytes();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
break;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// Sony tag, convert to Priam
|
2020-11-11 04:19:18 +00:00
|
|
|
|
case 12:
|
2018-07-23 23:25:43 +01:00
|
|
|
|
oldTag = new byte[12];
|
2023-10-03 23:34:59 +01:00
|
|
|
|
Array.Copy(data, givenSectorSize * i + 512, oldTag, 0, 12);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
newTag = LisaTag.DecodeSonyTag(oldTag)?.ToPriam().GetBytes();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
break;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// Profile tag, copy to Profile
|
|
|
|
|
|
case 20 when longSectorSize == 532:
|
|
|
|
|
|
newTag = new byte[20];
|
2023-10-03 23:34:59 +01:00
|
|
|
|
Array.Copy(data, givenSectorSize * i + 512, newTag, 0, 20);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
break;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// Profile tag, convert to Priam
|
2020-11-11 04:19:18 +00:00
|
|
|
|
case 20:
|
2018-07-23 23:25:43 +01:00
|
|
|
|
oldTag = new byte[20];
|
2023-10-03 23:34:59 +01:00
|
|
|
|
Array.Copy(data, givenSectorSize * i + 512, oldTag, 0, 20);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
newTag = LisaTag.DecodeProfileTag(oldTag)?.ToPriam().GetBytes();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
break;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// Priam tag, convert to Profile
|
|
|
|
|
|
case 24 when longSectorSize == 532:
|
|
|
|
|
|
oldTag = new byte[24];
|
2023-10-03 23:34:59 +01:00
|
|
|
|
Array.Copy(data, givenSectorSize * i + 512, oldTag, 0, 24);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
newTag = LisaTag.DecodePriamTag(oldTag)?.ToProfile().GetBytes();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
break;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// Priam tag, copy to Priam
|
2020-11-11 04:19:18 +00:00
|
|
|
|
case 24:
|
2018-07-23 23:25:43 +01:00
|
|
|
|
newTag = new byte[24];
|
2023-10-03 23:34:59 +01:00
|
|
|
|
Array.Copy(data, givenSectorSize * i + 512, newTag, 0, 24);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case 0:
|
|
|
|
|
|
newTag = null;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
break;
|
|
|
|
|
|
default:
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Incorrect_data_size;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 13:20:25 +01:00
|
|
|
|
newTag ??= new byte[longSectorSize - 512];
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2023-10-03 23:34:59 +01:00
|
|
|
|
_writingStream.Seek(longSectorSize + (long)sectorAddress * longSectorSize, SeekOrigin.Begin);
|
|
|
|
|
|
_writingStream.Write(data, (int)(givenSectorSize * i), 512);
|
|
|
|
|
|
_writingStream.Write(newTag, 0, newTag.Length);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
ErrorMessage = "";
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public bool Close()
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!IsWriting)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Image_is_not_opened_for_writing;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
byte[] markerTag = Encoding.UTF8.GetBytes("Aaru " + Version.GetVersion());
|
|
|
|
|
|
byte[] driveName;
|
2023-10-03 23:34:59 +01:00
|
|
|
|
var driveType = new byte[3];
|
2022-03-06 13:29:38 +00:00
|
|
|
|
byte[] driveBlocks = BigEndianBitConverter.GetBytes((uint)_imageInfo.Sectors);
|
|
|
|
|
|
int longSectorSize = _imageInfo.MediaType == MediaType.PriamDataTower ? 536 : 532;
|
|
|
|
|
|
byte[] blockSize = BigEndianBitConverter.GetBytes((ushort)longSectorSize);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
switch(_imageInfo.MediaType)
|
|
|
|
|
|
{
|
|
|
|
|
|
case MediaType.AppleProfile when _imageInfo.Sectors == 0x4C00:
|
|
|
|
|
|
driveName = Encoding.ASCII.GetBytes(PROFILE10_NAME);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
break;
|
|
|
|
|
|
case MediaType.AppleWidget when _imageInfo.Sectors == 0x4C00:
|
|
|
|
|
|
driveType[1] = 0x01;
|
|
|
|
|
|
driveName = Encoding.ASCII.GetBytes(PROFILE10_NAME);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
break;
|
|
|
|
|
|
case MediaType.PriamDataTower when _imageInfo.Sectors == 0x22C7C:
|
|
|
|
|
|
driveType[1] = 0xFF;
|
|
|
|
|
|
driveName = Encoding.ASCII.GetBytes(PRIAM_NAME);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
driveName = Encoding.ASCII.GetBytes(PROFILE_NAME);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_writingStream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
_writingStream.Write(driveName, 0, driveName.Length >= 0xD ? 0xD : driveName.Length);
|
|
|
|
|
|
_writingStream.Seek(0xD, SeekOrigin.Begin);
|
|
|
|
|
|
_writingStream.Write(driveType, 0, 3);
|
|
|
|
|
|
_writingStream.Seek(0x12, SeekOrigin.Begin);
|
|
|
|
|
|
_writingStream.Write(driveBlocks, 1, 3);
|
|
|
|
|
|
_writingStream.Seek(0x15, SeekOrigin.Begin);
|
|
|
|
|
|
_writingStream.Write(blockSize, 1, 2);
|
|
|
|
|
|
_writingStream.Seek(512, SeekOrigin.Begin);
|
2020-07-20 21:11:32 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_writingStream.Write(markerTag, 0,
|
|
|
|
|
|
markerTag.Length >= longSectorSize - 512 ? longSectorSize - 512 : markerTag.Length);
|
2020-07-20 21:11:32 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_writingStream.Flush();
|
|
|
|
|
|
_writingStream.Close();
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
IsWriting = false;
|
|
|
|
|
|
ErrorMessage = "";
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
2022-12-15 22:21:07 +00:00
|
|
|
|
public bool SetImageInfo(ImageInfo imageInfo) => true;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public bool SetGeometry(uint cylinders, uint heads, uint sectorsPerTrack) => true;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public bool WriteSectorTag(byte[] data, ulong sectorAddress, SectorTagType tag)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Unsupported_feature;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public bool WriteSectorsTag(byte[] data, ulong sectorAddress, uint length, SectorTagType tag)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Unsupported_feature;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
2022-12-15 22:21:07 +00:00
|
|
|
|
public bool SetDumpHardware(List<DumpHardware> dumpHardware) => false;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
2022-12-15 22:21:07 +00:00
|
|
|
|
public bool SetMetadata(Metadata metadata) => false;
|
2023-10-03 23:34:59 +01:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|