Skip to content

Commit 1178ff0

Browse files
committed
Fix reading of malformed hdlr boxes
1 parent ae10009 commit 1178ff0

4 files changed

Lines changed: 12 additions & 21 deletions

File tree

AAXClean.nuspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
</metadata>
2424
<dependencies>
2525
<group targetFramework="net9.0">
26-
<dependency id="Mpeg4Lib" version="2.0.1" />
26+
<dependency id="Mpeg4Lib" version="2.0.1.2" />
2727
<dependency id="Microsoft.SourceLink.GitHub" version="8.0.0" />
2828
</group>
2929
<group targetFramework="net8.0">
30-
<dependency id="Mpeg4Lib" version="2.0.1" />
30+
<dependency id="Mpeg4Lib" version="2.0.1.2" />
3131
<dependency id="Microsoft.SourceLink.GitHub" version="8.0.0" />
3232
</group>
3333
</dependencies>

src/AAXClean/AAXClean.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
5-
<Version>2.0.1.1</Version>
5+
<Version>2.0.1.2</Version>
66
<Authors>MrGneissGuy</Authors>
77
<Company>Just Me, Inc.</Company>
88
<Description>Decrypts Audible aax and aaxc files to mp4.</Description>

src/Mpeg4Lib/Boxes/HdlrBox.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ namespace Mpeg4Lib.Boxes;
88

99
public class HdlrBox : FullBox
1010
{
11-
public bool HasNullTerminator { get; set; }
12-
public override long RenderSize => base.RenderSize + 20 + Encoding.UTF8.GetByteCount(HandlerName) + (HasNullTerminator ? 1 : 0);
11+
public int NullTerminatorCount { get; set; }
12+
public override long RenderSize => base.RenderSize + 20 + Encoding.UTF8.GetByteCount(HandlerName) + NullTerminatorCount;
1313
public uint PreDefined { get; }
1414
public string HandlerType { get; }
1515
private readonly byte[] Reserved;
@@ -22,20 +22,12 @@ public HdlrBox(Stream file, BoxHeader header, IBox? parent) : base(file, header,
2222
HandlerType = Encoding.UTF8.GetString(file.ReadBlock(4));
2323
Reserved = file.ReadBlock(12);
2424

25-
List<byte> blist = new();
26-
while (file.Position < endPos)
27-
{
28-
byte lastByte = (byte)file.ReadByte();
25+
var readToEnd = file.ReadBlock((int)(endPos - file.Position));
2926

30-
if (lastByte == 0)
31-
{
32-
HasNullTerminator = true;
33-
break;
34-
}
27+
for (int i = readToEnd.Length - 1; i >= 0 && readToEnd[i] == 0; i--)
28+
NullTerminatorCount++;
3529

36-
blist.Add(lastByte);
37-
}
38-
HandlerName = Encoding.UTF8.GetString(blist.ToArray());
30+
HandlerName = Encoding.UTF8.GetString(readToEnd, 0, readToEnd.Length - NullTerminatorCount);
3931
}
4032

4133
private HdlrBox(string type, string? name, IBox parent)
@@ -48,7 +40,7 @@ private HdlrBox(string type, string? name, IBox parent)
4840
HandlerType = type;
4941
Reserved = new byte[12];
5042
HandlerName = name ?? "";
51-
HasNullTerminator = true;
43+
NullTerminatorCount = 1;
5244
}
5345

5446
public static HdlrBox Create(string type, string? name, byte[] reservedData, IBox parent)
@@ -70,7 +62,6 @@ protected override void Render(Stream file)
7062
file.Write(Encoding.UTF8.GetBytes(HandlerType));
7163
file.Write(Reserved);
7264
file.Write(Encoding.UTF8.GetBytes(HandlerName));
73-
if (HasNullTerminator)
74-
file.WriteByte(0);
65+
file.Write(new byte[NullTerminatorCount]);
7566
}
7667
}

src/Mpeg4Lib/Mpeg4Lib.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
5-
<Version>2.0.1.1</Version>
5+
<Version>2.0.1.2</Version>
66
<Authors>MrGneissGuy</Authors>
77
<Company>Just Me, Inc.</Company>
88
<Description>Parsees mpeg-4 files</Description>

0 commit comments

Comments
 (0)