You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

456 lines
11 KiB

  1. // go-qrcode
  2. // Copyright 2014 Tom Harwood
  3. package qrcode
  4. import (
  5. "errors"
  6. "log"
  7. bitset "github.com/skip2/go-qrcode/bitset"
  8. )
  9. // Data encoding.
  10. //
  11. // The main data portion of a QR Code consists of one or more segments of data.
  12. // A segment consists of:
  13. //
  14. // - The segment Data Mode: numeric, alphanumeric, or byte.
  15. // - The length of segment in bits.
  16. // - Encoded data.
  17. //
  18. // For example, the string "123ZZ#!#!" may be represented as:
  19. //
  20. // [numeric, 3, "123"] [alphanumeric, 2, "ZZ"] [byte, 4, "#!#!"]
  21. //
  22. // Multiple data modes exist to minimise the size of encoded data. For example,
  23. // 8-bit bytes require 8 bits to encode each, but base 10 numeric data can be
  24. // encoded at a higher density of 3 numbers (e.g. 123) per 10 bits.
  25. //
  26. // Some data can be represented in multiple modes. Numeric data can be
  27. // represented in all three modes, whereas alphanumeric data (e.g. 'A') can be
  28. // represented in alphanumeric and byte mode.
  29. //
  30. // Starting a new segment (to use a different Data Mode) has a cost, the bits to
  31. // state the new segment Data Mode and length. To minimise each QR Code's symbol
  32. // size, an optimisation routine coalesces segment types where possible, to
  33. // reduce the encoded data length.
  34. //
  35. // There are several other data modes available (e.g. Kanji mode) which are not
  36. // implemented here.
  37. // A segment encoding mode.
  38. type dataMode uint8
  39. const (
  40. // Each dataMode is a subset of the subsequent dataMode:
  41. // dataModeNone < dataModeNumeric < dataModeAlphanumeric < dataModeByte
  42. //
  43. // This ordering is important for determining which data modes a character can
  44. // be encoded with. E.g. 'E' can be encoded in both dataModeAlphanumeric and
  45. // dataModeByte.
  46. dataModeNone dataMode = 1 << iota
  47. dataModeNumeric
  48. dataModeAlphanumeric
  49. dataModeByte
  50. )
  51. // dataModeString returns d as a short printable string.
  52. func dataModeString(d dataMode) string {
  53. switch d {
  54. case dataModeNone:
  55. return "none"
  56. case dataModeNumeric:
  57. return "numeric"
  58. case dataModeAlphanumeric:
  59. return "alphanumeric"
  60. case dataModeByte:
  61. return "byte"
  62. }
  63. return "unknown"
  64. }
  65. type dataEncoderType uint8
  66. const (
  67. dataEncoderType1To9 dataEncoderType = iota
  68. dataEncoderType10To26
  69. dataEncoderType27To40
  70. )
  71. // segment is a single segment of data.
  72. type segment struct {
  73. // Data Mode (e.g. numeric).
  74. dataMode dataMode
  75. // segment data (e.g. "abc").
  76. data []byte
  77. }
  78. // A dataEncoder encodes data for a particular QR Code version.
  79. type dataEncoder struct {
  80. // Minimum & maximum versions supported.
  81. minVersion int
  82. maxVersion int
  83. // Mode indicator bit sequences.
  84. numericModeIndicator *bitset.Bitset
  85. alphanumericModeIndicator *bitset.Bitset
  86. byteModeIndicator *bitset.Bitset
  87. // Character count lengths.
  88. numNumericCharCountBits int
  89. numAlphanumericCharCountBits int
  90. numByteCharCountBits int
  91. // The raw input data.
  92. data []byte
  93. // The data classified into unoptimised segments.
  94. actual []segment
  95. // The data classified into optimised segments.
  96. optimised []segment
  97. }
  98. // newDataEncoder constructs a dataEncoder.
  99. func newDataEncoder(t dataEncoderType) *dataEncoder {
  100. d := &dataEncoder{}
  101. switch t {
  102. case dataEncoderType1To9:
  103. d = &dataEncoder{
  104. minVersion: 1,
  105. maxVersion: 9,
  106. numericModeIndicator: bitset.New(b0, b0, b0, b1),
  107. alphanumericModeIndicator: bitset.New(b0, b0, b1, b0),
  108. byteModeIndicator: bitset.New(b0, b1, b0, b0),
  109. numNumericCharCountBits: 10,
  110. numAlphanumericCharCountBits: 9,
  111. numByteCharCountBits: 8,
  112. }
  113. case dataEncoderType10To26:
  114. d = &dataEncoder{
  115. minVersion: 10,
  116. maxVersion: 26,
  117. numericModeIndicator: bitset.New(b0, b0, b0, b1),
  118. alphanumericModeIndicator: bitset.New(b0, b0, b1, b0),
  119. byteModeIndicator: bitset.New(b0, b1, b0, b0),
  120. numNumericCharCountBits: 12,
  121. numAlphanumericCharCountBits: 11,
  122. numByteCharCountBits: 16,
  123. }
  124. case dataEncoderType27To40:
  125. d = &dataEncoder{
  126. minVersion: 27,
  127. maxVersion: 40,
  128. numericModeIndicator: bitset.New(b0, b0, b0, b1),
  129. alphanumericModeIndicator: bitset.New(b0, b0, b1, b0),
  130. byteModeIndicator: bitset.New(b0, b1, b0, b0),
  131. numNumericCharCountBits: 14,
  132. numAlphanumericCharCountBits: 13,
  133. numByteCharCountBits: 16,
  134. }
  135. default:
  136. log.Panic("Unknown dataEncoderType")
  137. }
  138. return d
  139. }
  140. // encode data as one or more segments and return the encoded data.
  141. //
  142. // The returned data does not include the terminator bit sequence.
  143. func (d *dataEncoder) encode(data []byte) (*bitset.Bitset, error) {
  144. d.data = data
  145. d.actual = nil
  146. d.optimised = nil
  147. if len(data) == 0 {
  148. return nil, errors.New("no data to encode")
  149. }
  150. // Classify data into unoptimised segments.
  151. d.classifyDataModes()
  152. // Optimise segments.
  153. err := d.optimiseDataModes()
  154. if err != nil {
  155. return nil, err
  156. }
  157. // Encode data.
  158. encoded := bitset.New()
  159. for _, s := range d.optimised {
  160. d.encodeDataRaw(s.data, s.dataMode, encoded)
  161. }
  162. return encoded, nil
  163. }
  164. // classifyDataModes classifies the raw data into unoptimised segments.
  165. // e.g. "123ZZ#!#!" =>
  166. // [numeric, 3, "123"] [alphanumeric, 2, "ZZ"] [byte, 4, "#!#!"].
  167. func (d *dataEncoder) classifyDataModes() {
  168. var start int
  169. mode := dataModeNone
  170. for i, v := range d.data {
  171. newMode := dataModeNone
  172. switch {
  173. case v >= 0x30 && v <= 0x39:
  174. newMode = dataModeNumeric
  175. case v == 0x20 || v == 0x24 || v == 0x25 || v == 0x2a || v == 0x2b || v ==
  176. 0x2d || v == 0x2e || v == 0x2f || v == 0x3a || (v >= 0x41 && v <= 0x5a):
  177. newMode = dataModeAlphanumeric
  178. default:
  179. newMode = dataModeByte
  180. }
  181. if newMode != mode {
  182. if i > 0 {
  183. d.actual = append(d.actual, segment{dataMode: mode, data: d.data[start:i]})
  184. start = i
  185. }
  186. mode = newMode
  187. }
  188. }
  189. d.actual = append(d.actual, segment{dataMode: mode, data: d.data[start:len(d.data)]})
  190. }
  191. // optimiseDataModes optimises the list of segments to reduce the overall output
  192. // encoded data length.
  193. //
  194. // The algorithm coalesces adjacent segments. segments are only coalesced when
  195. // the Data Modes are compatible, and when the coalesced segment has a shorter
  196. // encoded length than separate segments.
  197. //
  198. // Multiple segments may be coalesced. For example a string of alternating
  199. // alphanumeric/numeric segments ANANANANA can be optimised to just A.
  200. func (d *dataEncoder) optimiseDataModes() error {
  201. for i := 0; i < len(d.actual); {
  202. mode := d.actual[i].dataMode
  203. numChars := len(d.actual[i].data)
  204. j := i + 1
  205. for j < len(d.actual) {
  206. nextNumChars := len(d.actual[j].data)
  207. nextMode := d.actual[j].dataMode
  208. if nextMode > mode {
  209. break
  210. }
  211. coalescedLength, err := d.encodedLength(mode, numChars+nextNumChars)
  212. if err != nil {
  213. return err
  214. }
  215. seperateLength1, err := d.encodedLength(mode, numChars)
  216. if err != nil {
  217. return err
  218. }
  219. seperateLength2, err := d.encodedLength(nextMode, nextNumChars)
  220. if err != nil {
  221. return err
  222. }
  223. if coalescedLength < seperateLength1+seperateLength2 {
  224. j++
  225. numChars += nextNumChars
  226. } else {
  227. break
  228. }
  229. }
  230. optimised := segment{dataMode: mode,
  231. data: make([]byte, 0, numChars)}
  232. for k := i; k < j; k++ {
  233. optimised.data = append(optimised.data, d.actual[k].data...)
  234. }
  235. d.optimised = append(d.optimised, optimised)
  236. i = j
  237. }
  238. return nil
  239. }
  240. // encodeDataRaw encodes data in dataMode. The encoded data is appended to
  241. // encoded.
  242. func (d *dataEncoder) encodeDataRaw(data []byte, dataMode dataMode, encoded *bitset.Bitset) {
  243. modeIndicator := d.modeIndicator(dataMode)
  244. charCountBits := d.charCountBits(dataMode)
  245. // Append mode indicator.
  246. encoded.Append(modeIndicator)
  247. // Append character count.
  248. encoded.AppendUint32(uint32(len(data)), charCountBits)
  249. // Append data.
  250. switch dataMode {
  251. case dataModeNumeric:
  252. for i := 0; i < len(data); i += 3 {
  253. charsRemaining := len(data) - i
  254. var value uint32
  255. bitsUsed := 1
  256. for j := 0; j < charsRemaining && j < 3; j++ {
  257. value *= 10
  258. value += uint32(data[i+j] - 0x30)
  259. bitsUsed += 3
  260. }
  261. encoded.AppendUint32(value, bitsUsed)
  262. }
  263. case dataModeAlphanumeric:
  264. for i := 0; i < len(data); i += 2 {
  265. charsRemaining := len(data) - i
  266. var value uint32
  267. for j := 0; j < charsRemaining && j < 2; j++ {
  268. value *= 45
  269. value += encodeAlphanumericCharacter(data[i+j])
  270. }
  271. bitsUsed := 6
  272. if charsRemaining > 1 {
  273. bitsUsed = 11
  274. }
  275. encoded.AppendUint32(value, bitsUsed)
  276. }
  277. case dataModeByte:
  278. for _, b := range data {
  279. encoded.AppendByte(b, 8)
  280. }
  281. }
  282. }
  283. // modeIndicator returns the segment header bits for a segment of type dataMode.
  284. func (d *dataEncoder) modeIndicator(dataMode dataMode) *bitset.Bitset {
  285. switch dataMode {
  286. case dataModeNumeric:
  287. return d.numericModeIndicator
  288. case dataModeAlphanumeric:
  289. return d.alphanumericModeIndicator
  290. case dataModeByte:
  291. return d.byteModeIndicator
  292. default:
  293. log.Panic("Unknown data mode")
  294. }
  295. return nil
  296. }
  297. // charCountBits returns the number of bits used to encode the length of a data
  298. // segment of type dataMode.
  299. func (d *dataEncoder) charCountBits(dataMode dataMode) int {
  300. switch dataMode {
  301. case dataModeNumeric:
  302. return d.numNumericCharCountBits
  303. case dataModeAlphanumeric:
  304. return d.numAlphanumericCharCountBits
  305. case dataModeByte:
  306. return d.numByteCharCountBits
  307. default:
  308. log.Panic("Unknown data mode")
  309. }
  310. return 0
  311. }
  312. // encodedLength returns the number of bits required to encode n symbols in
  313. // dataMode.
  314. //
  315. // The number of bits required is affected by:
  316. // - QR code type - Mode Indicator length.
  317. // - Data mode - number of bits used to represent data length.
  318. // - Data mode - how the data is encoded.
  319. // - Number of symbols encoded.
  320. //
  321. // An error is returned if the mode is not supported, or the length requested is
  322. // too long to be represented.
  323. func (d *dataEncoder) encodedLength(dataMode dataMode, n int) (int, error) {
  324. modeIndicator := d.modeIndicator(dataMode)
  325. charCountBits := d.charCountBits(dataMode)
  326. if modeIndicator == nil {
  327. return 0, errors.New("mode not supported")
  328. }
  329. maxLength := (1 << uint8(charCountBits)) - 1
  330. if n > maxLength {
  331. return 0, errors.New("length too long to be represented")
  332. }
  333. length := modeIndicator.Len() + charCountBits
  334. switch dataMode {
  335. case dataModeNumeric:
  336. length += 10 * (n / 3)
  337. if n%3 != 0 {
  338. length += 1 + 3*(n%3)
  339. }
  340. case dataModeAlphanumeric:
  341. length += 11 * (n / 2)
  342. length += 6 * (n % 2)
  343. case dataModeByte:
  344. length += 8 * n
  345. }
  346. return length, nil
  347. }
  348. // encodeAlphanumericChar returns the QR Code encoded value of v.
  349. //
  350. // v must be a QR Code defined alphanumeric character: 0-9, A-Z, SP, $%*+-./ or
  351. // :. The characters are mapped to values in the range 0-44 respectively.
  352. func encodeAlphanumericCharacter(v byte) uint32 {
  353. c := uint32(v)
  354. switch {
  355. case c >= '0' && c <= '9':
  356. // 0-9 encoded as 0-9.
  357. return c - '0'
  358. case c >= 'A' && c <= 'Z':
  359. // A-Z encoded as 10-35.
  360. return c - 'A' + 10
  361. case c == ' ':
  362. return 36
  363. case c == '$':
  364. return 37
  365. case c == '%':
  366. return 38
  367. case c == '*':
  368. return 39
  369. case c == '+':
  370. return 40
  371. case c == '-':
  372. return 41
  373. case c == '.':
  374. return 42
  375. case c == '/':
  376. return 43
  377. case c == ':':
  378. return 44
  379. default:
  380. log.Panicf("encodeAlphanumericCharacter() with non alphanumeric char %v.", v)
  381. }
  382. return 0
  383. }