format-info.js 1.1 KB

1234567891011121314151617181920212223242526272829
  1. var Utils = require('./utils')
  2. var G15 = (1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0)
  3. var G15_MASK = (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1)
  4. var G15_BCH = Utils.getBCHDigit(G15)
  5. /**
  6. * Returns format information with relative error correction bits
  7. *
  8. * The format information is a 15-bit sequence containing 5 data bits,
  9. * with 10 error correction bits calculated using the (15, 5) BCH code.
  10. *
  11. * @param {Number} errorCorrectionLevel Error correction level
  12. * @param {Number} mask Mask pattern
  13. * @return {Number} Encoded format information bits
  14. */
  15. exports.getEncodedBits = function getEncodedBits (errorCorrectionLevel, mask) {
  16. var data = ((errorCorrectionLevel.bit << 3) | mask)
  17. var d = data << 10
  18. while (Utils.getBCHDigit(d) - G15_BCH >= 0) {
  19. d ^= (G15 << (Utils.getBCHDigit(d) - G15_BCH))
  20. }
  21. // xor final data with mask pattern in order to ensure that
  22. // no combination of Error Correction Level and data mask pattern
  23. // will result in an all-zero data string
  24. return ((data << 10) | d) ^ G15_MASK
  25. }