Skip to content

Add full XI5Ticket support and improve ticket handling #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions BackendServices/CastleLibrary/XI5/Reader/TicketReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
using System;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XI5.Types;

namespace XI5.Reader
{
public class TicketReader : BinaryReader
{
public TicketReader(Stream input) : base(input)
{ }

#region Big Endian Shenanigans

// credit where credit is due: https://stackoverflow.com/a/71048495

public override short ReadInt16() => BinaryPrimitives.ReadInt16BigEndian(ReadBytes(2));
public override int ReadInt32() => BinaryPrimitives.ReadInt32BigEndian(ReadBytes(4));
public override long ReadInt64() => BinaryPrimitives.ReadInt64BigEndian(ReadBytes(8));

public override ushort ReadUInt16() => BinaryPrimitives.ReadUInt16BigEndian(ReadBytes(2));
public override uint ReadUInt32() => BinaryPrimitives.ReadUInt32BigEndian(ReadBytes(4));
public override ulong ReadUInt64() => BinaryPrimitives.ReadUInt64BigEndian(ReadBytes(8));

#endregion

internal TicketVersion ReadTicketVersion() => new TicketVersion((byte)(ReadByte() >> 4), ReadByte());

internal ushort ReadTicketHeader()
{
// Ticket header
ReadBytes(4); // Header
return ReadUInt16(); // Ticket length
}

public void DetermineTicketFormat()
{
TicketVersion version = ReadTicketVersion();
Console.Write($"Ticket version {version.Major}.{version.Minor}");
ushort length = ReadTicketHeader();
Console.WriteLine($", length is {length} bytes");

while (this.BaseStream.Position <= length)
{
DetermineSectionFormat();
}

long unreadBytes = this.BaseStream.Length - this.BaseStream.Position;
if (unreadBytes != 0)
{
Console.WriteLine($"!!! Unread bytes: {unreadBytes} !!!");
}
}

private void DetermineSectionFormat()
{
TicketDataSection section = ReadTicketSectionHeader();
Console.WriteLine($" {section.Type} Section, length is {section.Length} @ offset {section.Position}");

if (section.Type == TicketDataSectionType.DateOfBirth)
{
this.BaseStream.Position += section.Length;
}

int endSpot = section.Length + section.Position;
while (this.BaseStream.Position <= endSpot)
{
DetermineData();
}
}

private void DetermineData()
{
TicketData data = ReadTicketData(TicketDataType.Empty);
if ((ushort)data.Type >> 8 == 0x30) // if data type starts with section header
{
this.BaseStream.Position -= sizeof(ushort) * 2; // roll back read from data
DetermineSectionFormat(); // read the section
return;
}
this.ReadBytes(data.Length);
Console.WriteLine($" {data.Type}, length is {data.Length}");
}

internal TicketDataSection ReadTicketSectionHeader()
{
long position = this.BaseStream.Position;

byte sectionHeader = this.ReadByte();
if (sectionHeader != 0x30)
{
throw new FormatException($"Expected 0x30 for section header, was {sectionHeader}. Offset is {this.BaseStream.Position}");
}

TicketDataSectionType type = (TicketDataSectionType)this.ReadByte();
ushort length = this.ReadUInt16();

return new TicketDataSection(type, length, position);
}

private TicketData ReadTicketData(TicketDataType expectedType)
{
TicketData data = new TicketData((TicketDataType)ReadUInt16(), ReadUInt16());
if (data.Type != expectedType && expectedType != TicketDataType.Empty)
throw new FormatException($"Expected data type to be {expectedType}, was really {data.Type} ({(int)data.Type})");

return data;
}

internal byte[] ReadTicketBinaryData(TicketDataType type = TicketDataType.Binary)
=> ReadBytes(ReadTicketData(type).Length);
internal string ReadTicketStringData(TicketDataType type = TicketDataType.String)
=> Encoding.Default.GetString(ReadTicketBinaryData(type)).TrimEnd('\0');

internal uint ReadTicketUInt32Data()
{
ReadTicketData(TicketDataType.UInt32);
return ReadUInt32();
}

internal ulong ReadTicketUInt64Data()
{
ReadTicketData(TicketDataType.UInt64);
return ReadUInt64();
}

internal DateTimeOffset ReadTicketTimestampData()
{
ReadTicketData(TicketDataType.Timestamp);
return DateTimeOffset.FromUnixTimeMilliseconds((long)ReadUInt64());
}

internal void SkipTicketEmptyData(int sections = 1)
{
for (int i = 0; i < sections; i++) ReadTicketData(TicketDataType.Empty);
}
}
}
106 changes: 55 additions & 51 deletions BackendServices/CastleLibrary/XI5/StreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,55 @@
using System;
using System.IO;

namespace XI5
{
internal static class StreamExtensions
{
public static uint ReadUInt(this Stream stream)
{
byte[] buffer = new byte[4];
stream.Read(buffer, 0, 4);
if (BitConverter.IsLittleEndian)
Array.Reverse(buffer);
return BitConverter.ToUInt32(buffer, 0);
}

public static ulong ReadULong(this Stream stream)
{
byte[] buffer = new byte[8];
stream.Read(buffer, 0, 8);
if (BitConverter.IsLittleEndian)
Array.Reverse(buffer);
return BitConverter.ToUInt64(buffer, 0);
}

public static ushort ReadUShort(this Stream stream)
{
byte[] buffer = new byte[2];
stream.Read(buffer, 0, 2);
if (BitConverter.IsLittleEndian)
Array.Reverse(buffer);
return BitConverter.ToUInt16(buffer, 0);
}

public static bool ReadAll(this Stream stream, byte[] buffer, int startIndex, int count)
{
if (stream == null)
return false;

int offset = 0;
while (offset < count)
{
int readCount = stream.Read(buffer, startIndex + offset, count - offset);
if (readCount == 0)
return false;
offset += readCount;
}
return true;
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XI5
{
internal static class StreamExtensions
{
public static uint ReadUInt(this Stream stream)
{
byte[] buffer = new byte[4];
stream.Read(buffer, 0, 4);
if (BitConverter.IsLittleEndian)
Array.Reverse(buffer);
return BitConverter.ToUInt32(buffer, 0);
}

public static ulong ReadULong(this Stream stream)
{
byte[] buffer = new byte[8];
stream.Read(buffer, 0, 8);
if (BitConverter.IsLittleEndian)
Array.Reverse(buffer);
return BitConverter.ToUInt64(buffer, 0);
}

public static ushort ReadUShort(this Stream stream)
{
byte[] buffer = new byte[2];
stream.Read(buffer, 0, 2);
if (BitConverter.IsLittleEndian)
Array.Reverse(buffer);
return BitConverter.ToUInt16(buffer, 0);
}

public static bool ReadAll(this Stream stream, byte[] buffer, int startIndex, int count)
{
if (stream == null)
return false;

int offset = 0;
while (offset < count)
{
int readCount = stream.Read(buffer, startIndex + offset, count - offset);
if (readCount == 0)
return false;
offset += readCount;
}
return true;
}
}
}
36 changes: 36 additions & 0 deletions BackendServices/CastleLibrary/XI5/Types/Parsers/TicketParser21.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XI5.Reader;

namespace XI5.Types.Parsers
{
internal static class TicketParser21
{
internal static void ParseTicket(XI5Ticket ticket, TicketReader reader)
{
ticket.SerialId = reader.ReadTicketStringData(TicketDataType.Binary);

ticket.IssuerId = reader.ReadTicketUInt32Data();

ticket.IssuedDate = reader.ReadTicketTimestampData();
ticket.ExpiryDate = reader.ReadTicketTimestampData();

ticket.UserId = reader.ReadTicketUInt64Data();
ticket.Username = reader.ReadTicketStringData();

ticket.Country = reader.ReadTicketStringData(TicketDataType.Binary); // No I am not going to brazil
ticket.Domain = reader.ReadTicketStringData();

ticket.ServiceId = reader.ReadTicketStringData(TicketDataType.Binary);
ticket.TitleId = XI5Ticket.ServiceIdRegex.Matches(ticket.ServiceId)[0].ToString();

ticket.Status = reader.ReadUInt32();

// Skip padding section in ticket
reader.SkipTicketEmptyData(3);
}
}
}
51 changes: 51 additions & 0 deletions BackendServices/CastleLibrary/XI5/Types/Parsers/TicketParser30.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XI5.Reader;

namespace XI5.Types.Parsers
{
internal static class TicketParser30
{
internal static void ParseTicket(XI5Ticket ticket, TicketReader reader)
{
ticket.SerialId = reader.ReadTicketStringData(TicketDataType.Binary);

ticket.IssuerId = reader.ReadTicketUInt32Data();

ticket.IssuedDate = reader.ReadTicketTimestampData();
ticket.ExpiryDate = reader.ReadTicketTimestampData();

ticket.UserId = reader.ReadTicketUInt64Data();
ticket.Username = reader.ReadTicketStringData();

ticket.Country = reader.ReadTicketStringData(TicketDataType.Binary);
ticket.Domain = reader.ReadTicketStringData();

ticket.ServiceId = reader.ReadTicketStringData(TicketDataType.Binary);
ticket.TitleId = XI5Ticket.ServiceIdRegex.Matches(ticket.ServiceId)[0].ToString();

TicketDataSection header = reader.ReadTicketSectionHeader();
if (header.Type != TicketDataSectionType.DateOfBirth)
{
throw new FormatException($"Expected section to be {nameof(TicketDataSectionType.DateOfBirth)}, " +
$"was really {header.Type} ({(int)header.Type})");
}

reader.ReadUInt32(); // Birthdate
reader.SkipTicketEmptyData(2); // Padding?

Console.WriteLine(reader.BaseStream.Position);
header = reader.ReadTicketSectionHeader();
if (header.Type != TicketDataSectionType.Age)
{
throw new FormatException($"Expected section to be {nameof(TicketDataSectionType.Age)}, " +
$"was really {header.Type} ({(int)header.Type})");
}

reader.SkipTicketEmptyData();
}
}
}
20 changes: 20 additions & 0 deletions BackendServices/CastleLibrary/XI5/Types/TicketData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XI5.Types
{
public readonly struct TicketData
{
public readonly TicketDataType Type;
public readonly ushort Length;

public TicketData(TicketDataType type, ushort length)
{
Type = type;
Length = length;
}
}
}
22 changes: 22 additions & 0 deletions BackendServices/CastleLibrary/XI5/Types/TicketDataSection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XI5.Types
{
public readonly struct TicketDataSection
{
public TicketDataSectionType Type { get; }
public ushort Length { get; }
public int Position { get; }

public TicketDataSection(TicketDataSectionType type, ushort length, long position)
{
Type = type;
Length = length;
Position = (int)position;
}
}
}
16 changes: 16 additions & 0 deletions BackendServices/CastleLibrary/XI5/Types/TicketDataSectionType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XI5.Types
{
public enum TicketDataSectionType : byte
{
Body = 0,
Footer = 2,
Age = 16,
DateOfBirth = 17,
}
}
Loading