Skip to content

Commit

Permalink
Return a copy of the ArrayPoolBufferWriter buffer in Ascii85, AsciiHe…
Browse files Browse the repository at this point in the history
…x and RunLength filters and fix #964
  • Loading branch information
BobLd committed Dec 31, 2024
1 parent 366fc4f commit 2b14ae0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
68 changes: 63 additions & 5 deletions src/UglyToad.PdfPig.Tests/Filters/Ascii85FilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ public void DecodesWikipediaExample()

var result = filter.Decode(bytes, dictionary, 0);

var text = Encoding.ASCII.GetString(result.ToArray());
#if !NET
string text = Encoding.ASCII.GetString(result.ToArray());
#else
string text = Encoding.ASCII.GetString(result.Span);
#endif

Assert.Equal("Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, " +
"that by a perseverance of delight in the continued and indefatigable generation of knowledge, " +
Expand All @@ -37,7 +41,11 @@ public void ReplacesZWithEmptyBytes()

var result = filter.Decode(bytes, dictionary, 1);

var text = Encoding.ASCII.GetString(result.ToArray());
#if !NET
string text = Encoding.ASCII.GetString(result.ToArray());
#else
string text = Encoding.ASCII.GetString(result.Span);
#endif

Assert.Equal("Man \0\0\0\0is d", text);
}
Expand Down Expand Up @@ -104,8 +112,12 @@ public void DecodesEncodedPdfContent()

var result = filter.Decode(Encoding.ASCII.GetBytes(input), dictionary, 0);

var text = Encoding.ASCII.GetString(result.ToArray());

#if !NET
string text = Encoding.ASCII.GetString(result.ToArray());
#else
string text = Encoding.ASCII.GetString(result.Span);
#endif

Assert.Equal(PdfContent.Replace("\r\n", "\n"), text);
}

Expand All @@ -124,11 +136,57 @@ public void DecodesEncodedPdfContentMissingEndOfDataSymbol()

var result = filter.Decode(Encoding.ASCII.GetBytes(input), dictionary, 0);

var text = Encoding.ASCII.GetString(result.ToArray());
#if !NET
string text = Encoding.ASCII.GetString(result.ToArray());
#else
string text = Encoding.ASCII.GetString(result.Span);
#endif

Assert.Equal(PdfContent.Replace("\r\n", "\n"), text);
}

[Fact]
public void DecodeParallel()
{
Parallel.For(0, 100_000, i =>
{
if (i % 2 == 0)
{
var bytes = Encoding.ASCII.GetBytes("9jqo^zBlbD-");

var result = filter.Decode(bytes, dictionary, 1);

#if !NET
string text = Encoding.ASCII.GetString(result.ToArray());
#else
string text = Encoding.ASCII.GetString(result.Span);
#endif

Assert.Equal("Man \0\0\0\0is d", text);
}
else
{
const string input =
@"0d&.mDdmGg4?O`>9P&*SFD)dS2E2gC4pl@QEb/Zr$8N_r$:7]!01IZ=0eskNAdU47<+?7h+B3Ol2_m!C+?)#1+B1
`9>:<KhASu!rA7]9oF*)G6@;U'.@ps6t@V$[&ART*lARTXoCj@HP2DlU*/0HBI+B1r?0H_r%1a#ac$<nof.3LB""+=MAS+D58'ATD3qCj@.
F@;@;70ea^uAKYi.Eb-A7E+*6f+EV:*DBN1?0ek+_+B1r?<%9""=ASu!rA7]9oF*)G6@;U'<.3MT)$8<SS1,pCU6jd-H;e7
C#1,U1&Ft""Og2'=;YEa`c,ASu!rA8,po+Dk\3BQ%F&+CT;%+CQ]A1,'h!Ft""Oh2'=;UBl%3eCh4`'DBMbD7O]H>0H_br.:""&q8d[6p/M
T()<(%'A;f?Ma+CT;%+E_a:A0>K&EZek1D/aN,F)u&6DBNA*A0>f4BOu4*+EM76E,9eK+B3(_<%9""p.!0AMEb031ATMF#F<G%,DIIR2+Cno
&@3B9%+CT.1.3LK*+=KNS6V0ilAoD^,@<=+N>p**=$</Jt-rY&$AKYo'+EV:.+Cf>,E,oN2F(oQ1+D#G#De*R""B-;&&FD,T'F!+n3AKY4b
F*22=@:F%a+=SF4C'moi+=Li?EZeh0FD)e-@<>p#@;]TuBl.9kATKCFGA(],AKYo5BOu4*+CT;%+C#7pF_Pr+@VfTuDf0B:+=SF4C'moi+=
Li?EZek1DKKT1F`2DD/TboKAKY](@:s.m/h%oBC'mC/$>""*cF*)G6@;Q?_DIdZpC&~>";

var result = filter.Decode(Encoding.ASCII.GetBytes(input), dictionary, 0);

#if !NET
string text = Encoding.ASCII.GetString(result.ToArray());
#else
string text = Encoding.ASCII.GetString(result.Span);
#endif

Assert.Equal(PdfContent.Replace("\r\n", "\n"), text);
}
});
}
}
}
4 changes: 2 additions & 2 deletions src/UglyToad.PdfPig/Filters/Ascii85Filter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public sealed class Ascii85Filter : IFilter
/// <inheritdoc />
public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken streamDictionary, int filterIndex)
{
var asciiBuffer = new byte[5];
Span<byte> asciiBuffer = stackalloc byte[5];

var index = 0;

Expand Down Expand Up @@ -96,7 +96,7 @@ public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken str
WriteData(asciiBuffer, index, writer);
}

return writer.WrittenMemory;
return writer.WrittenMemory.ToArray();
}

private static void WriteData(Span<byte> ascii, int index, ArrayPoolBufferWriter<byte> writer)
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Filters/AsciiHexDecodeFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken str
WriteHexToByte(pair, writer);
}

return writer.WrittenMemory;
return writer.WrittenMemory.ToArray();
}

private static void WriteHexToByte(ReadOnlySpan<byte> hexBytes, ArrayPoolBufferWriter<byte> writer)
Expand Down
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig/Filters/RunLengthFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public ReadOnlyMemory<byte> Decode(ReadOnlySpan<byte> input, DictionaryToken str
}
}

return output.WrittenMemory;
return output.WrittenMemory.ToArray();
}
}
}

0 comments on commit 2b14ae0

Please sign in to comment.