Skip to content

Commit b85e957

Browse files
committed
process multiple blocks
1 parent bee8654 commit b85e957

File tree

1 file changed

+58
-59
lines changed

1 file changed

+58
-59
lines changed

Diff for: src/Microsoft.DotNet.XHarness.TestRunners.Xunit/WasmXmlResultWriter.cs

+58-59
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Microsoft.DotNet.XHarness.TestRunners.Xunit;
1515

1616
internal class WasmXmlResultWriter
1717
{
18-
#if !NET || DEBUG
18+
#if DEBUG
1919
public static void WriteOnSingleLine(XElement assembliesElement)
2020
{
2121
using var ms = new MemoryStream();
@@ -45,66 +45,65 @@ public int TransformBlock(
4545
byte[] inputBuffer, int inputOffset, int inputCount,
4646
byte[] outputBuffer, int outputOffset)
4747
{
48-
int totalBytesWritten = 0;
49-
int inputProcessed = 0;
50-
51-
while (inputProcessed < inputCount)
48+
int inputBlocks = Math.DivRem(inputCount, InputBlockSize, out int inputRemainder);
49+
50+
if (inputBlocks == 0)
51+
ThrowHelper.ThrowArgumentOutOfRange(ThrowHelper.ExceptionArgument.inputCount);
52+
53+
if (outputBuffer == null)
54+
ThrowHelper.ThrowArgumentNull(ThrowHelper.ExceptionArgument.outputBuffer);
55+
56+
if (inputRemainder != 0)
57+
ThrowHelper.ThrowArgumentOutOfRange(ThrowHelper.ExceptionArgument.inputCount);
58+
59+
/*
60+
Input Buffer ("hi mom"):
61+
+-----+-----+-----+-----+-----+-----+
62+
| 'h' | 'i' | ' ' | 'm' | 'o' | 'm' |
63+
+-----+-----+-----+-----+-----+-----+
64+
|104 |105 | 32 |109 |111 |109 |
65+
+-----+-----+-----+-----+-----+-----+
66+
67+
Base64 Encoding Process:
68+
- 'hi ' -> 'aGkg'
69+
- 'mom' -> 'bW9t'
70+
71+
Base64 Encoded Output:
72+
| |base64Written | | base64Written |
73+
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
74+
| \0 | \0 | \0 | \0 |'a' |'G' |'k' |'g' | \0 | \0 | \0 | \0 |'b' |'W' |'9' |'t' |
75+
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
76+
| 0 | 0 | 0 | 0 | 97 | 71 |107 |103 | 0 | 0 | 0 | 0 | 98 | 87 | 57 |116 |
77+
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
78+
79+
Expanded Output Buffer (UTF-16 Encoding):
80+
| outputChars | outputChars |
81+
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
82+
| \0 |'a' | \0 |'G' | \0 |'k' | \0 |'g' | \0 |'b' | \0 |'W' | \0 |'9' | \0 |'t' |
83+
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
84+
| 0 | 97 | 0 | 71 | 0 |107 | 0 |103 | 0 | 98 | 0 | 87 | 0 | 57 | 0 |116 |
85+
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
86+
87+
*/
88+
89+
// Calculate positions in the output buffer
90+
int base64OutputStart = outputOffset + OutputBlockSize / 2;
91+
92+
// write Base64 transformation directly to the second half of the output buffer
93+
int base64BytesWritten = _base64Transform.TransformBlock(
94+
inputBuffer, inputOffset, inputCount,
95+
outputBuffer, base64OutputStart);
96+
97+
var base64Written = outputBuffer.AsSpan(base64OutputStart, base64BytesWritten);
98+
var outputChars = outputBuffer.AsSpan(outputOffset, OutputBlockSize);
99+
for (int i = 0; i < base64BytesWritten; i++)
52100
{
53-
int bytesToProcess = Math.Min(InputBlockSize, inputCount - inputProcessed);
54-
55-
/*
56-
Input Buffer ("hi mom"):
57-
+-----+-----+-----+-----+-----+-----+
58-
| 'h' | 'i' | ' ' | 'm' | 'o' | 'm' |
59-
+-----+-----+-----+-----+-----+-----+
60-
|104 |105 | 32 |109 |111 |109 |
61-
+-----+-----+-----+-----+-----+-----+
62-
63-
Base64 Encoding Process:
64-
- 'hi ' -> 'aGkg'
65-
- 'mom' -> 'bW9t'
66-
67-
Base64 Encoded Output:
68-
| |base64Written | | base64Written |
69-
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
70-
| \0 | \0 | \0 | \0 |'a' |'G' |'k' |'g' | \0 | \0 | \0 | \0 |'b' |'W' |'9' |'t' |
71-
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
72-
| 0 | 0 | 0 | 0 | 97 | 71 |107 |103 | 0 | 0 | 0 | 0 | 98 | 87 | 57 |116 |
73-
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
74-
75-
Expanded Output Buffer (UTF-16 Encoding):
76-
| outputChars | outputChars |
77-
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
78-
| \0 |'a' | \0 |'G' | \0 |'k' | \0 |'g' | \0 |'b' | \0 |'W' | \0 |'9' | \0 |'t' |
79-
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
80-
| 0 | 97 | 0 | 71 | 0 |107 | 0 |103 | 0 | 98 | 0 | 87 | 0 | 57 | 0 |116 |
81-
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
82-
83-
*/
84-
85-
// Calculate positions in the output buffer
86-
int outputStart = outputOffset + totalBytesWritten;
87-
int base64OutputStart = outputStart + OutputBlockSize / 2;
88-
89-
// write Base64 transformation directly to the second half of the output buffer
90-
int base64BytesWritten = _base64Transform.TransformBlock(
91-
inputBuffer, inputOffset + inputProcessed, bytesToProcess,
92-
outputBuffer, base64OutputStart);
93-
94-
var base64Written = outputBuffer.AsSpan(base64OutputStart, base64BytesWritten);
95-
var outputChars = outputBuffer.AsSpan(outputStart, OutputBlockSize);
96-
for (int i = 0; i < base64BytesWritten; i++)
97-
{
98-
// Expand each ascii byte to a char write it in the same logical position
99-
// as a char in outputChars eventually filling the output buffer
100-
BitConverter.TryWriteBytes(outputChars.Slice(i * 2), (char)base64Written[i]);
101-
}
102-
103-
inputProcessed += bytesToProcess;
104-
totalBytesWritten += base64BytesWritten * 2;
101+
// Expand each ascii byte to a char write it in the same logical position
102+
// as a char in outputChars eventually filling the output buffer
103+
BitConverter.TryWriteBytes(outputChars.Slice(i * 2), (char)base64Written[i]);
105104
}
106105

107-
return totalBytesWritten;
106+
return base64BytesWritten * 2;
108107
}
109108

110109
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
@@ -146,7 +145,7 @@ public static void WriteOnSingleLine(XElement assembliesElement)
146145
// string interpolation logic.
147146
Span<char> charData = MemoryMarshal.Cast<byte,char>(bytes);
148147

149-
// Output the result
148+
// Output the result and the the ascii length of the data
150149
Console.WriteLine($"STARTRESULTXML {charData.Length} {charData} ENDRESULTXML");
151150
Console.WriteLine($"Finished writing {charData.Length} bytes of RESULTXML");
152151
}

0 commit comments

Comments
 (0)