interleaveWithECBytes static method
BitArray
interleaveWithECBytes()
Interleave "bits" with corresponding error correction bytes. On success, store the result in "result". The interleave rule is complicated. See 8.6 of JISX0510:2004 (p.37) for details.
Implementation
static BitArray interleaveWithECBytes(
BitArray bits, int numTotalBytes, int numDataBytes, int numRSBlocks) {
// "bits" must have "getNumDataBytes" bytes of data.
if (bits.sizeInBytes != numDataBytes) {
throw WriterException('Number of bits and data bytes does not match');
}
// Step 1. Divide data bytes into blocks and generate error correction bytes for them. We'll
// store the divided data bytes blocks and error correction bytes blocks into "blocks".
var dataBytesOffset = 0;
var maxNumDataBytes = 0;
var maxNumEcBytes = 0;
// Since, we know the number of reedsolmon blocks, we can initialize the vector with the number.
var blocks = <BlockPair>[];
for (var i = 0; i < numRSBlocks; ++i) {
var numDataBytesInBlock = Int32List(1);
var numEcBytesInBlock = Int32List(1);
getNumDataBytesAndNumECBytesForBlockID(numTotalBytes, numDataBytes,
numRSBlocks, i, numDataBytesInBlock, numEcBytesInBlock);
var size = numDataBytesInBlock[0];
var dataBytes = Int8List(size);
bits.toBytes(8 * dataBytesOffset, dataBytes, 0, size);
var ecBytes = generateECBytes(dataBytes, numEcBytesInBlock[0]);
blocks.add(BlockPair(dataBytes, ecBytes));
maxNumDataBytes = math.max(maxNumDataBytes, size);
maxNumEcBytes = math.max(maxNumEcBytes, ecBytes.length);
dataBytesOffset += numDataBytesInBlock[0];
}
if (numDataBytes != dataBytesOffset) {
throw WriterException('Data bytes does not match offset');
}
var result = BitArray();
// First, place data blocks.
for (var i = 0; i < maxNumDataBytes; ++i) {
for (var block in blocks) {
var dataBytes = block.dataBytes;
if (i < dataBytes.length) {
result.appendBits(dataBytes[i], 8);
}
}
}
// Then, place error correction blocks.
for (var i = 0; i < maxNumEcBytes; ++i) {
for (var block in blocks) {
var ecBytes = block.errorCorrectionBytes;
if (i < ecBytes.length) {
result.appendBits(ecBytes[i], 8);
}
}
}
if (numTotalBytes != result.sizeInBytes) {
// Should be same.
throw WriterException(
'Interleaving error: $numTotalBytes and ${result.sizeInBytes} differ.');
}
return result;
}