getNumDataBytesAndNumECBytesForBlockID static method

void getNumDataBytesAndNumECBytesForBlockID(
  1. int numTotalBytes,
  2. int numDataBytes,
  3. int numRSBlocks,
  4. int blockID,
  5. List<int> numDataBytesInBlock,
  6. List<int> 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,
  List<int> numDataBytesInBlock,
  List<int> numECBytesInBlock,
) {
  if (blockID >= numRSBlocks) {
    throw WriterException('Block ID too large');
  }
  // numRsBlocksInGroup2 = 196 % 5 = 1
  final numRsBlocksInGroup2 = numTotalBytes % numRSBlocks;
  // numRsBlocksInGroup1 = 5 - 1 = 4
  final numRsBlocksInGroup1 = numRSBlocks - numRsBlocksInGroup2;
  // numTotalBytesInGroup1 = 196 / 5 = 39
  final numTotalBytesInGroup1 = numTotalBytes ~/ numRSBlocks;
  // numTotalBytesInGroup2 = 39 + 1 = 40
  final numTotalBytesInGroup2 = numTotalBytesInGroup1 + 1;
  // numDataBytesInGroup1 = 66 / 5 = 13
  final numDataBytesInGroup1 = numDataBytes ~/ numRSBlocks;
  // numDataBytesInGroup2 = 13 + 1 = 14
  final numDataBytesInGroup2 = numDataBytesInGroup1 + 1;
  // numEcBytesInGroup1 = 39 - 13 = 26
  final numEcBytesInGroup1 = numTotalBytesInGroup1 - numDataBytesInGroup1;
  // numEcBytesInGroup2 = 40 - 14 = 26
  final 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;
  }
}