Skip to content

Commit 5c6fdfd

Browse files
committed
Fix structured append parity
1 parent 8f1e981 commit 5c6fdfd

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

QrCodeGenerator/StructuredAppend.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,16 @@ private static Tuple<DataSegment, DataSegment> SplitSegment(DataSegment segment,
296296
);
297297
}
298298

299+
/// <summary>
300+
/// Calculates the parity of the data, i.e. the XOR of all its bytes.
301+
/// <para>
302+
/// Every QR code of the sequence carries it, so that a scanner can tell whether the codes
303+
/// it has collected belong together (see ISO/IEC 18004, section 8.4.1).
304+
/// </para>
305+
/// </summary>
299306
private static byte CalculateParity(byte[] data)
300307
{
301-
return data.Aggregate<byte, byte>(0, (current, value) => (byte)(current ^ (byte)(value >> 8)));
308+
return data.Aggregate<byte, byte>(0, (current, value) => (byte)(current ^ value));
302309
}
303310

304311
}

QrCodeGeneratorTest/StructuredAppendTest.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ public void EncodeVeryLongStringInMultipleCodes()
4949
}
5050
}
5151

52+
[Fact]
53+
public void StructuredAppendHeadersCarryDataParity()
54+
{
55+
var text = RandomData.MakeAlphanumericString(3000, seed: 1001);
56+
var data = Encoding.GetEncoding("ISO-8859-1").GetBytes(text);
57+
var expectedParity = data.Aggregate<byte, byte>(0, (current, value) => (byte)(current ^ value));
58+
59+
var qrCodes = StructuredAppend.BuildSegments(data, 29, QrCode.Ecc.Medium, ECI.Latin1, false);
60+
61+
Assert.NotEqual(0, expectedParity);
62+
Assert.All(qrCodes, segments => Assert.Equal(expectedParity, segments[0].StructuredAppendParity));
63+
}
64+
5265
[Fact]
5366
public void RejectTooLongString()
5467
{

0 commit comments

Comments
 (0)