gitea

Development moved to Codeberg

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
package qr

type block struct {
	data []byte
	ecc  []byte
}
type blockList []*block

func splitToBlocks(data <-chan byte, vi *versionInfo) blockList {
	result := make(blockList, vi.NumberOfBlocksInGroup1+vi.NumberOfBlocksInGroup2)

	for b := 0; b < int(vi.NumberOfBlocksInGroup1); b++ {
		blk := new(block)
		blk.data = make([]byte, vi.DataCodeWordsPerBlockInGroup1)
		for cw := 0; cw < int(vi.DataCodeWordsPerBlockInGroup1); cw++ {
			blk.data[cw] = <-data
		}
		blk.ecc = ec.calcECC(blk.data, vi.ErrorCorrectionCodewordsPerBlock)
		result[b] = blk
	}

	for b := 0; b < int(vi.NumberOfBlocksInGroup2); b++ {
		blk := new(block)
		blk.data = make([]byte, vi.DataCodeWordsPerBlockInGroup2)
		for cw := 0; cw < int(vi.DataCodeWordsPerBlockInGroup2); cw++ {
			blk.data[cw] = <-data
		}
		blk.ecc = ec.calcECC(blk.data, vi.ErrorCorrectionCodewordsPerBlock)
		result[int(vi.NumberOfBlocksInGroup1)+b] = blk
	}

	return result
}

func (bl blockList) interleave(vi *versionInfo) []byte {
	var maxCodewordCount int
	if vi.DataCodeWordsPerBlockInGroup1 > vi.DataCodeWordsPerBlockInGroup2 {
		maxCodewordCount = int(vi.DataCodeWordsPerBlockInGroup1)
	} else {
		maxCodewordCount = int(vi.DataCodeWordsPerBlockInGroup2)
	}
	resultLen := (vi.DataCodeWordsPerBlockInGroup1+vi.ErrorCorrectionCodewordsPerBlock)*vi.NumberOfBlocksInGroup1 +
		(vi.DataCodeWordsPerBlockInGroup2+vi.ErrorCorrectionCodewordsPerBlock)*vi.NumberOfBlocksInGroup2

	result := make([]byte, 0, resultLen)
	for i := 0; i < maxCodewordCount; i++ {
		for b := 0; b < len(bl); b++ {
			if len(bl[b].data) > i {
				result = append(result, bl[b].data[i])
			}
		}
	}
	for i := 0; i < int(vi.ErrorCorrectionCodewordsPerBlock); i++ {
		for b := 0; b < len(bl); b++ {
			result = append(result, bl[b].ecc[i])
		}
	}
	return result
}