Skip to content

Commit

Permalink
Improve error messages in Xiami
Browse files Browse the repository at this point in the history
  • Loading branch information
davidxuang committed Dec 13, 2023
1 parent b9c0f65 commit d026083
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion MusicDecrypto.Library/DecryptoBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public async ValueTask<Info> DecryptAsync()

private async ValueTask<Info> GetMetadataAsync(bool firstRun = true)
{
if (_audioType == AudioTypes.Undefined) _audioType = ((ReadOnlySpan<byte>)_buffer.AsSpan()).SniffAudioType();
if (_audioType == AudioTypes.Undefined) _audioType = _buffer.AsSpan().SniffAudioType();
_buffer.Name = "buffer" + _audioType.GetExtension();
_buffer.ResetPosition();

Expand Down
4 changes: 2 additions & 2 deletions MusicDecrypto.Library/Media/Extensions/MediaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal static class MediaExtensions
_ => throw new InvalidDataException("Undefined music type."),
};

internal static AudioTypes SniffAudioType(this ReadOnlySpan<byte> data)
internal static AudioTypes SniffAudioType(this Span<byte> data)
{
return data switch
{
Expand Down Expand Up @@ -66,7 +66,7 @@ internal static AudioTypes SniffAudioType(this ReadOnlySpan<byte> data)
_ => throw new InvalidDataException("Undefined image type."),
};

internal static ImageTypes SniffImageType(this ReadOnlySpan<byte> data)
internal static ImageTypes SniffImageType(this Span<byte> data)
{
return data switch
{
Expand Down
2 changes: 1 addition & 1 deletion MusicDecrypto.Library/Vendor/NetEase/Decrypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ protected override async ValueTask<bool> ProcessMetadataOverrideAsync(Tag tag)
}
}

var coverType = _coverBuffer is null ? ImageTypes.Undefined : ((ReadOnlySpan<byte>)_coverBuffer.AsSpan()).SniffImageType();
var coverType = _coverBuffer is null ? ImageTypes.Undefined : _coverBuffer.AsSpan().SniffImageType();
if (coverType == ImageTypes.Undefined)
{
_coverBuffer = null;
Expand Down
2 changes: 1 addition & 1 deletion MusicDecrypto.Library/Vendor/Tencent/QmcDecrypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ protected override async ValueTask<bool> ProcessMetadataOverrideAsync(Tag tag)
{
new Picture(new ByteVector(coverBuffer))
{
MimeType = ((ReadOnlySpan<byte>)coverBuffer.AsSpan()).SniffImageType().GetMime(),
MimeType = coverBuffer.AsSpan().SniffImageType().GetMime(),
Type = PictureType.FrontCover,
}
};
Expand Down
3 changes: 2 additions & 1 deletion MusicDecrypto.Library/Vendor/Xiami/Decrypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using MusicDecrypto.Library.Media;
using MusicDecrypto.Library.Media.Extensions;

namespace MusicDecrypto.Library.Vendor.Xiami;

Expand All @@ -20,7 +21,7 @@ public Decrypto(MarshalMemoryStream buffer, string name, WarnHandler? warn, Audi
if (!_reader.ReadBytes(8).AsSpan(0, 4).SequenceEqual(_magic) || !_reader.ReadBytes(4).SequenceEqual(_separator))
{
throw new InvalidDataException(
Path.GetExtension(_oldName).TrimStart('.') == "xm"
buffer.AsSpan().SniffAudioType() == AudioTypes.Undefined
? "File header is unexpected."
: "File seems unencrypted.");
}
Expand Down

0 comments on commit d026083

Please sign in to comment.