Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeugma440 committed Aug 26, 2023
1 parent 392603a commit 64adcdb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
7 changes: 2 additions & 5 deletions ATL/AudioData/IO/AA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,11 @@ protected override bool read(Stream source, ReadTagParams readTagParams)
protected override int write(TagData tag, Stream s, string zone)
{
int result = -1; // Default : leave as is
byte[] intBuffer;

if (zone.Equals(ZONE_TAGS))
{
long nbTagsOffset = s.Position;
intBuffer = StreamUtils.EncodeInt32(0);
s.Write(intBuffer, 0, 4); // Number of tags; will be rewritten at the end of the method
s.Write(StreamUtils.EncodeInt32(0)); // Number of tags; will be rewritten at the end of the method

// Mapped textual fields
IDictionary<Field, string> map = tag.ToMap();
Expand Down Expand Up @@ -394,8 +392,7 @@ protected override int write(TagData tag, Stream s, string zone)
}

s.Seek(nbTagsOffset, SeekOrigin.Begin);
intBuffer = StreamUtils.EncodeBEInt32(result);
s.Write(intBuffer, 0, 4); // Number of tags
s.Write(StreamUtils.EncodeBEInt32(result)); // Number of tags
}
if (zone.Equals(ZONE_IMAGE))
{
Expand Down
23 changes: 9 additions & 14 deletions ATL/Utils/StreamUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ namespace ATL
{
/// <summary>
/// Misc. utilities used by binary readers
///
/// TODO : Benchmark against System.Buffers.Binary.BinaryPrimitives
/// </summary>
internal static partial class StreamUtils
{
Expand Down Expand Up @@ -372,24 +370,24 @@ public static string ReadNullTerminatedStringFixed(BufferedBinaryReader r, Encod
/// <returns>The string read, without the zeroes at its end</returns>
private static string readNullTerminatedString(Stream r, Encoding encoding, int limit, bool moveStreamToLimit)
{
int nbChars = (encoding.Equals(Encoding.BigEndianUnicode) || encoding.Equals(Encoding.Unicode)) ? 2 : 1;
int nbChars = encoding.Equals(Encoding.BigEndianUnicode) || encoding.Equals(Encoding.Unicode) ? 2 : 1;
byte[] readBytes = new byte[limit > 0 ? limit : 100];
byte[] buffer = new byte[2];
int nbRead = 0;
long streamLength = r.Length;
long initialPos = r.Position;
long streamPos = initialPos;

while (streamPos < streamLength && ((0 == limit) || (nbRead < limit)))
while (streamPos < streamLength && (0 == limit || nbRead < limit))
{
// Read the size of a character
r.Read(buffer, 0, nbChars);

if ((1 == nbChars) && (0 == buffer[0])) // Null character read for single-char encodings
if (1 == nbChars && 0 == buffer[0]) // Null character read for single-char encodings
{
break;
}
else if ((2 == nbChars) && (0 == buffer[0]) && (0 == buffer[1])) // Null character read for two-char encodings
else if (2 == nbChars && 0 == buffer[0] && 0 == buffer[1]) // Null character read for two-char encodings
{
break;
}
Expand Down Expand Up @@ -424,7 +422,7 @@ public static int DecodeSynchSafeInt(byte[] bytes)

for (int i = 0; i < bytes.Length; i++)
{
result += bytes[i] * (int)Math.Floor(Math.Pow(2, (7 * (bytes.Length - 1 - i))));
result += bytes[i] * (int)Math.Floor(Math.Pow(2, 7 * (bytes.Length - 1 - i)));
}
return result;
}
Expand Down Expand Up @@ -458,11 +456,10 @@ public static byte[] EncodeSynchSafeInt(int value, int nbBytes)
{
if (nbBytes < 1 || nbBytes > 5) throw new ArgumentException("nbBytes has to be 1 to 5; found : " + nbBytes);
byte[] result = new byte[nbBytes];
int range;

for (int i = 0; i < nbBytes; i++)
{
range = 7 * (nbBytes - 1 - i);
int range = 7 * (nbBytes - 1 - i);
result[i] = (byte)((value & (0x7F << range)) >> range);
}

Expand Down Expand Up @@ -637,7 +634,7 @@ public static double ExtendedToDouble(byte[] extended)
// IEEE754/numerical_comp_guide/ncg_math.doc.html
int s = extended[9] >> 7;

int e = (((extended[9] & 0x7f) << 8) | (extended[8]));
int e = ((extended[9] & 0x7f) << 8) | extended[8];

int j = extended[7] >> 7;
long f = extended[7] & 0x7f;
Expand Down Expand Up @@ -668,8 +665,7 @@ public static double ExtendedToDouble(byte[] extended)
e += 1023 - 16383;

// Out of range - too large
if (e > 2047)
return Double.NaN;
if (e > 2047) return Double.NaN;

// Out of range - too small
if (e < 0)
Expand Down Expand Up @@ -903,7 +899,6 @@ public static async Task CopySameStreamAsync(Stream s, long offsetFrom, long off
if (offsetFrom == offsetTo) return;

byte[] data = new byte[bufferSize];
int bufSize;
long written = 0;
bool forward = offsetTo > offsetFrom;
long nbIterations = (long)Math.Ceiling(length * 1f / bufferSize);
Expand All @@ -912,7 +907,7 @@ public static async Task CopySameStreamAsync(Stream s, long offsetFrom, long off

while (written < length)
{
bufSize = Math.Min(bufferSize, toInt(length - written));
int bufSize = Math.Min(bufferSize, toInt(length - written));
if (forward)
{
s.Seek(offsetFrom + length - written - bufSize, SeekOrigin.Begin);
Expand Down

0 comments on commit 64adcdb

Please sign in to comment.