getNumDataBytesAndNumECBytesForBlockID static method

void getNumDataBytesAndNumECBytesForBlockID(
  1. int numTotalBytes,
  2. int numDataBytes,
  3. int numRSBlocks,
  4. int blockID,
  5. Int32List numDataBytesInBlock,
  6. Int32List numECBytesInBlock,
)

Get number of data bytes and number of error correction bytes for block id "blockID". Store the result in "numDataBytesInBlock", and "numECBytesInBlock". See table 12 in 8.5.1 of JISX0510:2004 (p.30)

Implementation

static void getNumDataBytesAndNumECBytesForBlockID(
    int numTotalBytes,
    int numDataBytes,
    int numRSBlocks,
    int blockID,
    Int32List numDataBytesInBlock,
    Int32List numECBytesInBlock) {
  if (blockID >= numRSBlocks) {
    throw WriterException('Block ID too large');
  }
  // numRsBlocksInGroup2 = 196 % 5 = 1
  var numRsBlocksInGroup2 = numTotalBytes % numRSBlocks;
  // numRsBlocksInGroup1 = 5 - 1 = 4
  var numRsBlocksInGroup1 = numRSBlocks - numRsBlocksInGroup2;
  // numTotalBytesInGroup1 = 196 / 5 = 39
  var numTotalBytesInGroup1 = numTotalBytes ~/ numRSBlocks;
  // numTotalBytesInGroup2 = 39 + 1 = 40
  var numTotalBytesInGroup2 = numTotalBytesInGroup1 + 1;
  // numDataBytesInGroup1 = 66 / 5 = 13
  var numDataBytesInGroup1 = numDataBytes ~/ numRSBlocks;
  // numDataBytesInGroup2 = 13 + 1 = 14
  var numDataBytesInGroup2 = numDataBytesInGroup1 + 1;
  // numEcBytesInGroup1 = 39 - 13 = 26
  var numEcBytesInGroup1 = numTotalBytesInGroup1 - numDataBytesInGroup1;
  // numEcBytesInGroup2 = 40 - 14 = 26
  var numEcBytesInGroup2 = numTotalBytesInGroup2 - numDataBytesInGroup2;
  // Sanity checks.
  // 26 = 26
  if (numEcBytesInGroup1 != numEcBytesInGroup2) {
    throw WriterException('EC bytes mismatch');
  }
  // 5 = 4 + 1.
  if (numRSBlocks != numRsBlocksInGroup1 + numRsBlocksInGroup2) {
    throw WriterException('RS blocks mismatch');
  }
  // 196 = (13 + 26) * 4 + (14 + 26) * 1
  if (numTotalBytes !=
      ((numDataBytesInGroup1 + numEcBytesInGroup1) * numRsBlocksInGroup1) +
          ((numDataBytesInGroup2 + numEcBytesInGroup2) *
              numRsBlocksInGroup2)) {
    throw WriterException('Total bytes mismatch');
  }

  if (blockID < numRsBlocksInGroup1) {
    numDataBytesInBlock[0] = numDataBytesInGroup1;
    numECBytesInBlock[0] = numEcBytesInGroup1;
  } else {
    numDataBytesInBlock[0] = numDataBytesInGroup2;
    numECBytesInBlock[0] = numEcBytesInGroup2;
  }
}