Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeugma440 committed Nov 8, 2024
1 parent fc42ed8 commit 6871023
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
27 changes: 11 additions & 16 deletions ATL/AudioData/IO/Ogg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ namespace ATL.AudioData.IO
partial class Ogg : VorbisTagHolder, IMetaDataIO, IAudioDataIO
{
// Contents of the file
private const int CONTENTS_UNSUPPORTED = -1; // Unsupported
private const int CONTENTS_VORBIS = 0; // Vorbis
private const int CONTENTS_OPUS = 1; // Opus
private const int CONTENTS_FLAC = 2; // FLAC
Expand Down Expand Up @@ -123,21 +122,20 @@ public void ReadFromStream(Stream r)
{
Offset = r.Position;
byte[] buffer = new byte[8];
r.Read(buffer, 0, 4);
ID = new byte[4];
Array.Copy(buffer, ID, 4);
r.Read(buffer, 0, 2);
if (r.Read(ID, 0, 4) < 4) return;
if (r.Read(buffer, 0, 2) < 2) return;
StreamVersion = buffer[0];
TypeFlag = buffer[1];
r.Read(buffer, 0, 8);
if (r.Read(buffer, 0, 8) < 8) return;
AbsolutePosition = StreamUtils.DecodeUInt64(buffer);
r.Read(buffer, 0, 4);
if (r.Read(buffer, 0, 4) < 4) return;
StreamId = StreamUtils.DecodeInt32(buffer);
r.Read(buffer, 0, 4);
if (r.Read(buffer, 0, 4) < 4) return;
PageNumber = StreamUtils.DecodeInt32(buffer);
r.Read(buffer, 0, 4);
if (r.Read(buffer, 0, 4) < 4) return;
Checksum = StreamUtils.DecodeUInt32(buffer);
r.Read(buffer, 0, 1);
if (r.Read(buffer, 0, 1) < 1) return;
Segments = buffer[0];

LacingValues = new byte[Segments];
Expand Down Expand Up @@ -407,11 +405,8 @@ private static ulong getSamples(BufferedBinaryReader source)
source.Seek(20, SeekOrigin.Current);
nbLacingValues = source.ReadByte();
nextPageOffset = 0;
source.Read(lacingValues, 0, nbLacingValues);
for (int i = 0; i < nbLacingValues; i++)
{
nextPageOffset += lacingValues[i];
}
int read = source.Read(lacingValues, 0, nbLacingValues);
for (int i = 0; i < read; i++) nextPageOffset += lacingValues[i];
}
else
{
Expand Down Expand Up @@ -979,7 +974,7 @@ private static void generatePageCrc32(Stream s, IEnumerable<KeyValuePair<long, i
{
s.Seek(kv.Key, SeekOrigin.Begin);
if (data.Length < kv.Value) data = new byte[kv.Value]; // Enlarge only if needed; max size is 0xffff
s.Read(data, 0, kv.Value);
if (s.Read(data, 0, kv.Value) < kv.Value) return;
uint crc = OggCRC32.CalculateCRC(0, data, (uint)kv.Value);
// Write CRC value at the dedicated location within the OGG header
s.Seek(kv.Key + 22, SeekOrigin.Begin);
Expand Down Expand Up @@ -1007,7 +1002,7 @@ private static bool renumberRemainingPages(Stream s, long nextPageOffset, int wr
s.Seek(nextPageOffset, SeekOrigin.Begin);
int dataSize = header.HeaderSize + header.GetPageSize();
if (data.Length < dataSize) data = new byte[dataSize]; // Only realloc when size is insufficient
s.Read(data, 0, dataSize);
if (s.Read(data, 0, dataSize) < dataSize) return false;

// Checksum has to include its own location, as if it were 0
data[22] = 0;
Expand Down
12 changes: 6 additions & 6 deletions ATL/Utils/ImageUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,9 @@ public static ImageProperties GetImageProperties(byte[] imageData, ImageFormat f
else
{
// Read IHDR chunk
s.Read(intData, 0, 4);
if (s.Read(intData, 0, 4) < 4) break;
props.Width = StreamUtils.DecodeBEInt32(intData);
s.Read(intData, 0, 4);
if (s.Read(intData, 0, 4) < 4) break;
props.Height = StreamUtils.DecodeBEInt32(intData);
props.ColorDepth = r.ReadByte();
int colorType = r.ReadByte();
Expand Down Expand Up @@ -420,9 +420,9 @@ public static ImageProperties GetImageProperties(byte[] imageData, ImageFormat f
// Skip frame length
s.Seek(2, SeekOrigin.Current);
bitsPerSample = r.ReadByte();
s.Read(shortData, 0, 2);
if (s.Read(shortData, 0, 2) < 2) break;
props.Height = StreamUtils.DecodeBEUInt16(shortData);
s.Read(shortData, 0, 2);
if (s.Read(shortData, 0, 2) < 2) break;
props.Width = StreamUtils.DecodeBEUInt16(shortData);
byte nbComponents = r.ReadByte();
props.ColorDepth = bitsPerSample * nbComponents;
Expand Down Expand Up @@ -480,9 +480,9 @@ private static uint findPngChunk(Stream s, byte[] chunkID, long limit)

while (s.Position < limit)
{
s.Read(intData, 0, 4); // Chunk Size
if (s.Read(intData, 0, 4) < 4) break; // Chunk Size
chunkSize = StreamUtils.DecodeBEUInt32(intData);
s.Read(intData, 0, 4); // Chunk ID
if (s.Read(intData, 0, 4) < 4) break; // Chunk ID
foundChunk = intData.SequenceEqual(chunkID);
if (foundChunk) return chunkSize;

Expand Down
28 changes: 15 additions & 13 deletions ATL/Utils/StreamUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public static Encoding GetEncodingFromFileBOM(FileStream file)
{
Encoding result;
byte[] bom = new byte[4]; // Get the byte-order mark, if there is one
file.Read(bom, 0, 4);
if (file.Read(bom, 0, 4) < 4) return Settings.DefaultTextEncoding;
if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf) // utf-8
{
result = Encoding.UTF8;
Expand Down Expand Up @@ -414,7 +414,7 @@ private static string readNullTerminatedString(Stream r, Encoding encoding, int
while (streamPos < streamLength && (0 == limit || nbRead < limit))
{
// Read the size of a character
r.Read(buffer, 0, nbChars);
if (r.Read(buffer, 0, nbChars) < nbChars) break;

if (1 == nbChars && 0 == buffer[0]) // Null character read for single-char encodings
{
Expand Down Expand Up @@ -536,6 +536,7 @@ public static bool FindSequence(Stream stream, byte[] sequence, long limit = 0)
int bytesToRead;
int iSequence = 0;
int readBytes = 0;
int read;
long initialPos = stream.Position;

int remainingBytes = (int)((limit > 0) ? Math.Min(stream.Length - stream.Position, limit) : stream.Length - stream.Position);
Expand All @@ -544,9 +545,9 @@ public static bool FindSequence(Stream stream, byte[] sequence, long limit = 0)
{
bytesToRead = Math.Min(remainingBytes, BUFFER_SIZE);

stream.Read(readBuffer, 0, bytesToRead);
read = stream.Read(readBuffer, 0, bytesToRead);

for (int i = 0; i < bytesToRead; i++)
for (int i = 0; i < read; i++)
{
if (sequence[iSequence] == readBuffer[i]) iSequence++;
else if (iSequence > 0) iSequence = 0;
Expand All @@ -559,7 +560,7 @@ public static bool FindSequence(Stream stream, byte[] sequence, long limit = 0)
}

remainingBytes -= bytesToRead;
readBytes += bytesToRead;
readBytes += read;
}

// If we're here, the sequence hasn't been found
Expand All @@ -584,7 +585,7 @@ public static uint ReadBEBits(Stream source, int bitPosition, int bitCount)

// Read a number of bits from file at the given position
source.Seek(bitPosition / 8, SeekOrigin.Begin); // integer division =^ div
source.Read(buffer, 0, buffer.Length);
if (source.Read(buffer, 0, buffer.Length) < buffer.Length) return 0;
uint result = DecodeBEUInt32(buffer);
result = (result << (bitPosition % 8)) >> (32 - bitCount);

Expand Down Expand Up @@ -937,24 +938,25 @@ public static async Task CopySameStreamAsync(Stream s, long offsetFrom, long off
long nbIterations = (long)Math.Ceiling(length * 1f / bufferSize);
long resolution = (long)Math.Ceiling(nbIterations / 10f);
long iteration = 0;
int read;

while (written < length)
{
int bufSize = Math.Min(bufferSize, toInt(length - written));
int toRead = Math.Min(bufferSize, toInt(length - written));
if (forward)
{
s.Seek(offsetFrom + length - written - bufSize, SeekOrigin.Begin);
await s.ReadAsync(data, 0, bufSize);
s.Seek(offsetTo + length - written - bufSize, SeekOrigin.Begin);
s.Seek(offsetFrom + length - written - toRead, SeekOrigin.Begin);
read = await s.ReadAsync(data, 0, toRead);
s.Seek(offsetTo + length - written - read, SeekOrigin.Begin);
}
else
{
s.Seek(offsetFrom + written, SeekOrigin.Begin);
await s.ReadAsync(data, 0, bufSize);
read = await s.ReadAsync(data, 0, toRead);
s.Seek(offsetTo + written, SeekOrigin.Begin);
}
await s.WriteAsync(data, 0, bufSize);
written += bufSize;
await s.WriteAsync(data, 0, read);
written += toRead;

if (progress != null)
{
Expand Down

0 comments on commit 6871023

Please sign in to comment.