2022-08-27 06:43:36 +00:00
|
|
|
using System;
|
2016-09-26 23:14:11 +00:00
|
|
|
using System.Collections.Generic;
|
2022-01-03 05:35:59 +00:00
|
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
2016-09-26 23:14:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Generation 4 <see cref="SaveFile"/> object for Pokémon Battle Revolution saves.
|
|
|
|
/// </summary>
|
2024-03-04 05:13:16 +00:00
|
|
|
public sealed class SAV4BR : SaveFile, IBoxDetailName
|
2016-09-26 23:14:11 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
protected internal override string ShortSummary => $"{Version} #{SaveCount:0000}";
|
|
|
|
public override string Extension => string.Empty;
|
2023-01-22 04:02:33 +00:00
|
|
|
public override PersonalTable4 Personal => PersonalTable.DP;
|
2023-03-26 00:55:55 +00:00
|
|
|
public override ReadOnlySpan<ushort> HeldItems => Legal.HeldItems_DP;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
private const int SAVE_COUNT = 4;
|
|
|
|
public const int SIZE_HALF = 0x1C0000;
|
|
|
|
|
|
|
|
public SAV4BR() : base(SaveUtil.SIZE_G4BR)
|
2016-09-26 23:14:11 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
ClearBoxes();
|
|
|
|
}
|
2016-09-26 23:14:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public SAV4BR(byte[] data) : base(data)
|
|
|
|
{
|
|
|
|
InitializeData(data);
|
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private void InitializeData(ReadOnlySpan<byte> data)
|
|
|
|
{
|
|
|
|
Data = DecryptPBRSaveData(data);
|
2016-09-26 23:14:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Detect active save
|
|
|
|
var first = ReadUInt32BigEndian(Data.AsSpan(0x00004C));
|
|
|
|
var second = ReadUInt32BigEndian(Data.AsSpan(0x1C004C));
|
|
|
|
SaveCount = Math.Max(second, first);
|
|
|
|
if (second > first)
|
2019-06-09 02:56:11 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
// swap halves
|
|
|
|
byte[] tempData = new byte[SIZE_HALF];
|
|
|
|
Array.Copy(Data, 0, tempData, 0, SIZE_HALF);
|
|
|
|
Array.Copy(Data, SIZE_HALF, Data, 0, SIZE_HALF);
|
|
|
|
tempData.CopyTo(Data, SIZE_HALF);
|
2019-06-09 02:56:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var names = (string[]) SaveNames;
|
|
|
|
for (int i = 0; i < SAVE_COUNT; i++)
|
2019-06-09 02:56:11 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var name = GetOTName(i);
|
|
|
|
if (string.IsNullOrWhiteSpace(name))
|
|
|
|
name = $"Empty {i + 1}";
|
|
|
|
else if (_currentSlot == -1)
|
|
|
|
_currentSlot = i;
|
|
|
|
names[i] = name;
|
|
|
|
}
|
2016-09-26 23:14:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (_currentSlot == -1)
|
|
|
|
_currentSlot = 0;
|
2016-09-26 23:14:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
CurrentSlot = _currentSlot;
|
|
|
|
}
|
2018-05-23 02:02:56 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary> Amount of times the primary save has been saved </summary>
|
|
|
|
private uint SaveCount;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override byte[] GetFinalData()
|
|
|
|
{
|
|
|
|
SetChecksums();
|
|
|
|
return EncryptPBRSaveData(Data);
|
|
|
|
}
|
2016-09-26 23:14:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Configuration
|
2023-01-22 04:02:33 +00:00
|
|
|
protected override SAV4BR CloneInternal() => new(Write());
|
2016-09-26 23:14:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public readonly IReadOnlyList<string> SaveNames = new string[SAVE_COUNT];
|
2018-02-16 01:05:45 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private int _currentSlot = -1;
|
|
|
|
private const int SIZE_SLOT = 0x6FF00;
|
2018-09-15 05:37:47 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public int CurrentSlot
|
|
|
|
{
|
|
|
|
get => _currentSlot;
|
|
|
|
// 4 save slots, data reading depends on current slot
|
|
|
|
set
|
2018-02-16 01:05:45 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
_currentSlot = value;
|
|
|
|
var ofs = SIZE_SLOT * _currentSlot;
|
|
|
|
Box = ofs + 0x978;
|
|
|
|
Party = ofs + 0x13A54; // first team slot after boxes
|
|
|
|
BoxName = ofs + 0x58674;
|
2016-09-26 23:14:11 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2016-09-26 23:14:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override int SIZE_STORED => PokeCrypto.SIZE_4STORED;
|
|
|
|
protected override int SIZE_PARTY => PokeCrypto.SIZE_4STORED + 4;
|
2023-01-22 04:02:33 +00:00
|
|
|
public override BK4 BlankPKM => new();
|
2022-06-18 18:04:24 +00:00
|
|
|
public override Type PKMType => typeof(BK4);
|
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
public override ushort MaxMoveID => 467;
|
|
|
|
public override ushort MaxSpeciesID => Legal.MaxSpeciesID_4;
|
2022-06-18 18:04:24 +00:00
|
|
|
public override int MaxAbilityID => Legal.MaxAbilityID_4;
|
|
|
|
public override int MaxItemID => Legal.MaxItemID_4_HGSS;
|
|
|
|
public override int MaxBallID => Legal.MaxBallID_4;
|
2024-02-23 03:20:54 +00:00
|
|
|
public override GameVersion MaxGameID => Legal.MaxGameID_4;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
2023-09-28 05:15:59 +00:00
|
|
|
public override int MaxEV => EffortValues.Max255;
|
2024-02-23 03:20:54 +00:00
|
|
|
public override byte Generation => 4;
|
2022-06-18 18:04:24 +00:00
|
|
|
public override EntityContext Context => EntityContext.Gen4;
|
2024-05-12 19:46:58 +00:00
|
|
|
public override int MaxStringLengthTrainer => 7;
|
2022-11-25 01:42:17 +00:00
|
|
|
public override int MaxStringLengthNickname => 10;
|
2022-06-18 18:04:24 +00:00
|
|
|
public override int MaxMoney => 999999;
|
|
|
|
public override int Language => (int)LanguageID.English; // prevent KOR from inhabiting
|
|
|
|
|
|
|
|
public override int BoxCount => 18;
|
|
|
|
|
|
|
|
public override int PartyCount
|
|
|
|
{
|
|
|
|
get
|
2018-05-19 21:42:21 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
int ctr = 0;
|
|
|
|
for (int i = 0; i < 6; i++)
|
2019-02-02 07:08:03 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (Data[GetPartyOffset(i) + 4] != 0) // sanity
|
|
|
|
ctr++;
|
2019-02-02 07:08:03 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return ctr;
|
2018-05-19 21:42:21 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
protected set
|
2016-09-26 23:14:11 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
// Ignore, value is calculated
|
2016-09-26 23:14:11 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Checksums
|
|
|
|
protected override void SetChecksums()
|
|
|
|
{
|
|
|
|
SetChecksum(Data, 0x0000000, 0x0000100, 0x000008);
|
|
|
|
SetChecksum(Data, 0x0000000, SIZE_HALF, SIZE_HALF - 0x80);
|
|
|
|
SetChecksum(Data, SIZE_HALF, 0x0000100, SIZE_HALF + 0x000008);
|
|
|
|
SetChecksum(Data, SIZE_HALF, SIZE_HALF, SIZE_HALF + SIZE_HALF - 0x80);
|
|
|
|
}
|
2017-10-18 06:19:34 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public override bool ChecksumsValid => IsChecksumsValid(Data);
|
|
|
|
public override string ChecksumInfo => $"Checksums valid: {ChecksumsValid}.";
|
2016-09-26 23:14:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static bool IsChecksumsValid(Span<byte> sav)
|
|
|
|
{
|
|
|
|
return VerifyChecksum(sav, 0x0000000, 0x0000100, 0x000008)
|
|
|
|
&& VerifyChecksum(sav, 0x0000000, SIZE_HALF, SIZE_HALF - 0x80)
|
|
|
|
&& VerifyChecksum(sav, SIZE_HALF, 0x0000100, SIZE_HALF + 0x000008)
|
|
|
|
&& VerifyChecksum(sav, SIZE_HALF, SIZE_HALF, SIZE_HALF + SIZE_HALF - 0x80);
|
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Trainer Info
|
2024-02-23 03:20:54 +00:00
|
|
|
public override GameVersion Version { get => GameVersion.BATREV; set { } }
|
2016-09-26 23:14:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private string GetOTName(int slot)
|
|
|
|
{
|
2024-03-21 13:56:28 +00:00
|
|
|
var ofs = 0x390 + (SIZE_SLOT * slot);
|
2022-06-18 18:04:24 +00:00
|
|
|
var span = Data.AsSpan(ofs, 16);
|
|
|
|
return GetString(span);
|
|
|
|
}
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
private void SetOTName(int slot, ReadOnlySpan<char> name)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2024-03-21 13:56:28 +00:00
|
|
|
var ofs = 0x390 + (SIZE_SLOT * slot);
|
2022-06-18 18:04:24 +00:00
|
|
|
var span = Data.AsSpan(ofs, 16);
|
2023-01-22 04:02:33 +00:00
|
|
|
SetString(span, name, 7, StringConverterOption.ClearZero);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-02-21 05:52:22 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public string CurrentOT { get => GetOTName(_currentSlot); set => SetOTName(_currentSlot, value); }
|
2019-02-21 05:52:22 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Storage
|
|
|
|
public override int GetPartyOffset(int slot) => Party + (SIZE_PARTY * slot);
|
|
|
|
public override int GetBoxOffset(int box) => Box + (SIZE_STORED * box * 30);
|
2016-09-26 23:43:52 +00:00
|
|
|
|
2024-03-21 13:56:28 +00:00
|
|
|
public override uint Money
|
|
|
|
{
|
|
|
|
get => (uint)((Data[(_currentSlot * SIZE_SLOT) + 0x12861] << 16) | (Data[(_currentSlot * SIZE_SLOT) + 0x12862] << 8) | Data[(_currentSlot * SIZE_SLOT) + 0x12863]);
|
|
|
|
set
|
|
|
|
{
|
|
|
|
Data[(_currentSlot * SIZE_SLOT) + 0x12861] = (byte)((value >> 16) & 0xFF);
|
|
|
|
Data[(_currentSlot * SIZE_SLOT) + 0x12862] = (byte)((value >> 8) & 0xFF);
|
|
|
|
Data[(_currentSlot * SIZE_SLOT) + 0x12863] = (byte)(value & 0xFF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
public override ushort TID16
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
get => (ushort)((Data[(_currentSlot * SIZE_SLOT) + 0x12867] << 8) | Data[(_currentSlot * SIZE_SLOT) + 0x12860]);
|
2022-06-18 18:04:24 +00:00
|
|
|
set
|
2021-01-23 06:55:55 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
Data[(_currentSlot * SIZE_SLOT) + 0x12867] = (byte)(value >> 8);
|
|
|
|
Data[(_currentSlot * SIZE_SLOT) + 0x12860] = (byte)(value & 0xFF);
|
2021-01-23 06:55:55 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2021-01-23 06:55:55 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
public override ushort SID16
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
get => (ushort)((Data[(_currentSlot * SIZE_SLOT) + 0x12865] << 8) | Data[(_currentSlot * SIZE_SLOT) + 0x12866]);
|
2022-06-18 18:04:24 +00:00
|
|
|
set
|
2021-01-23 06:55:55 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
Data[(_currentSlot * SIZE_SLOT) + 0x12865] = (byte)(value >> 8);
|
|
|
|
Data[(_currentSlot * SIZE_SLOT) + 0x12866] = (byte)(value & 0xFF);
|
2021-01-23 06:55:55 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2021-01-23 06:55:55 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Save file does not have Box Name / Wallpaper info
|
|
|
|
private int BoxName = -1;
|
|
|
|
private const int BoxNameLength = 0x28;
|
2018-09-15 05:37:47 +00:00
|
|
|
|
2023-03-26 06:14:50 +00:00
|
|
|
private Span<byte> GetBoxNameSpan(int box)
|
|
|
|
{
|
|
|
|
int ofs = BoxName + (box * BoxNameLength);
|
|
|
|
return Data.AsSpan(ofs, BoxNameLength);
|
|
|
|
}
|
|
|
|
|
2024-03-04 05:13:16 +00:00
|
|
|
public string GetBoxName(int box)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
if (BoxName < 0)
|
2024-03-04 05:13:16 +00:00
|
|
|
return BoxDetailNameExtensions.GetDefaultBoxNameCaps(box);
|
2022-06-18 18:04:24 +00:00
|
|
|
|
2023-03-26 06:14:50 +00:00
|
|
|
var span = GetBoxNameSpan(box);
|
2022-06-18 18:04:24 +00:00
|
|
|
if (ReadUInt16BigEndian(span) == 0)
|
2024-03-04 05:13:16 +00:00
|
|
|
return BoxDetailNameExtensions.GetDefaultBoxNameCaps(box);
|
2023-03-26 06:14:50 +00:00
|
|
|
return GetString(span);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-05-23 02:02:56 +00:00
|
|
|
|
2024-03-04 05:13:16 +00:00
|
|
|
public void SetBoxName(int box, ReadOnlySpan<char> value)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
if (BoxName < 0)
|
|
|
|
return;
|
2018-05-23 23:30:22 +00:00
|
|
|
|
2023-03-26 06:14:50 +00:00
|
|
|
var span = GetBoxNameSpan(box);
|
2022-06-18 18:04:24 +00:00
|
|
|
if (ReadUInt16BigEndian(span) == 0)
|
|
|
|
return;
|
2018-05-23 23:30:22 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
SetString(span, value, BoxNameLength / 2, StringConverterOption.ClearZero);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2016-09-26 23:43:52 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
protected override BK4 GetPKM(byte[] data)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
if (data.Length != SIZE_STORED)
|
|
|
|
Array.Resize(ref data, SIZE_STORED);
|
|
|
|
return BK4.ReadUnshuffle(data);
|
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override byte[] DecryptPKM(byte[] data) => data;
|
2016-09-26 23:14:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override void SetDex(PKM pk) { /* There's no PokéDex */ }
|
2018-09-15 05:37:47 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override void SetPKM(PKM pk, bool isParty = false)
|
|
|
|
{
|
|
|
|
var pk4 = (BK4)pk;
|
|
|
|
// Apply to this Save File
|
2024-02-23 23:01:36 +00:00
|
|
|
pk4.UpdateHandler(this);
|
|
|
|
pk.RefreshChecksum();
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2016-09-26 23:14:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override void SetPartyValues(PKM pk, bool isParty)
|
|
|
|
{
|
|
|
|
if (pk is G4PKM g4)
|
|
|
|
g4.Sanity = isParty ? (ushort)0xC000 : (ushort)0x4000;
|
|
|
|
}
|
2018-05-19 21:42:21 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static byte[] DecryptPBRSaveData(ReadOnlySpan<byte> input)
|
|
|
|
{
|
|
|
|
byte[] output = new byte[input.Length];
|
|
|
|
Span<ushort> keys = stackalloc ushort[4];
|
|
|
|
for (int offset = 0; offset < SaveUtil.SIZE_G4BR; offset += SIZE_HALF)
|
2016-09-26 23:14:11 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var inSlice = input.Slice(offset, SIZE_HALF);
|
|
|
|
var outSlice = output.AsSpan(offset, SIZE_HALF);
|
2022-03-26 20:28:29 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// First 8 bytes are the encryption keys for this chunk.
|
|
|
|
var keySlice = inSlice[..(keys.Length * 2)];
|
|
|
|
GeniusCrypto.ReadKeys(keySlice, keys);
|
2022-03-26 20:28:29 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Copy over the keys to the result.
|
|
|
|
keySlice.CopyTo(outSlice);
|
2022-03-26 20:28:29 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Decrypt the input, result stored in output.
|
|
|
|
Range r = new(8, SIZE_HALF);
|
|
|
|
GeniusCrypto.Decrypt(inSlice[r], outSlice[r], keys);
|
2016-09-26 23:14:11 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return output;
|
|
|
|
}
|
2016-09-26 23:14:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static byte[] EncryptPBRSaveData(ReadOnlySpan<byte> input)
|
|
|
|
{
|
|
|
|
byte[] output = new byte[input.Length];
|
|
|
|
Span<ushort> keys = stackalloc ushort[4];
|
|
|
|
for (int offset = 0; offset < SaveUtil.SIZE_G4BR; offset += SIZE_HALF)
|
2016-09-26 23:14:11 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var inSlice = input.Slice(offset, SIZE_HALF);
|
|
|
|
var outSlice = output.AsSpan(offset, SIZE_HALF);
|
2022-03-26 20:28:29 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// First 8 bytes are the encryption keys for this chunk.
|
|
|
|
var keySlice = inSlice[..(keys.Length * 2)];
|
|
|
|
GeniusCrypto.ReadKeys(keySlice, keys);
|
2022-03-26 20:28:29 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Copy over the keys to the result.
|
|
|
|
keySlice.CopyTo(outSlice);
|
2022-03-26 20:28:29 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Decrypt the input, result stored in output.
|
|
|
|
Range r = new(8, SIZE_HALF);
|
|
|
|
GeniusCrypto.Encrypt(inSlice[r], outSlice[r], keys);
|
2016-09-26 23:14:11 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return output;
|
|
|
|
}
|
2016-09-26 23:14:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static bool VerifyChecksum(Span<byte> input, int offset, int len, int chkOffset)
|
|
|
|
{
|
|
|
|
// Read original checksum data, and clear it for recomputing
|
|
|
|
Span<uint> originalChecksums = stackalloc uint[16];
|
|
|
|
var checkSpan = input.Slice(chkOffset, 4 * originalChecksums.Length);
|
|
|
|
for (int i = 0; i < originalChecksums.Length; i++)
|
2016-09-26 23:14:11 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var chk = checkSpan.Slice(i * 4, 4);
|
|
|
|
originalChecksums[i] = ReadUInt32BigEndian(chk);
|
|
|
|
}
|
|
|
|
checkSpan.Clear();
|
2016-09-26 23:14:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Compute current checksum of the specified span
|
|
|
|
Span<uint> checksums = stackalloc uint[16];
|
|
|
|
var span = input.Slice(offset, len);
|
|
|
|
ComputeChecksums(span, checksums);
|
2016-09-26 23:14:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Restore original checksums
|
|
|
|
WriteChecksums(checkSpan, originalChecksums);
|
2016-09-26 23:14:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Check if they match
|
|
|
|
return checksums.SequenceEqual(originalChecksums);
|
|
|
|
}
|
2016-09-26 23:14:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static void SetChecksum(Span<byte> input, int offset, int len, int chkOffset)
|
|
|
|
{
|
|
|
|
// Wipe Checksum region.
|
|
|
|
var checkSpan = input.Slice(chkOffset, 4 * 16);
|
|
|
|
checkSpan.Clear();
|
2016-09-26 23:14:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Compute current checksum of the specified span
|
|
|
|
Span<uint> checksums = stackalloc uint[16];
|
|
|
|
var span = input.Slice(offset, len);
|
|
|
|
ComputeChecksums(span, checksums);
|
2022-03-26 20:28:29 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
WriteChecksums(checkSpan, checksums);
|
|
|
|
}
|
2022-03-26 20:28:29 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static void WriteChecksums(Span<byte> span, Span<uint> checksums)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < checksums.Length; i++)
|
2022-03-26 20:28:29 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var dest = span[(i * 4)..];
|
|
|
|
WriteUInt32BigEndian(dest, checksums[i]);
|
2022-03-26 20:28:29 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2016-09-26 23:14:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static void ComputeChecksums(Span<byte> span, Span<uint> checksums)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < span.Length; i += 2)
|
2022-03-26 20:28:29 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
uint value = ReadUInt16BigEndian(span[i..]);
|
|
|
|
for (int c = 0; c < checksums.Length; c++)
|
|
|
|
checksums[c] += ((value >> c) & 1);
|
2016-09-26 23:14:11 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2017-04-09 21:06:50 +00:00
|
|
|
|
2024-04-23 05:57:17 +00:00
|
|
|
public override string GetString(ReadOnlySpan<byte> data)
|
|
|
|
=> StringConverter4GC.GetStringUnicode(data);
|
|
|
|
public override int LoadString(ReadOnlySpan<byte> data, Span<char> destBuffer)
|
|
|
|
=> StringConverter4GC.LoadStringUnicode(data, destBuffer);
|
|
|
|
public override int SetString(Span<byte> destBuffer, ReadOnlySpan<char> value, int maxLength, StringConverterOption option)
|
|
|
|
=> StringConverter4GC.SetStringUnicode(value, destBuffer, maxLength, option);
|
2016-09-26 23:14:11 +00:00
|
|
|
}
|