encoder.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. highestRequiredMode := d.classifyDataModes()
  152. // Optimise segments.
  153. err := d.optimiseDataModes()
  154. if err != nil {
  155. return nil, err
  156. }
  157. // Check if a single byte encoded segment would be more efficient.
  158. optimizedLength := 0
  159. for _, s := range d.optimised {
  160. length, err := d.encodedLength(s.dataMode, len(s.data))
  161. if err != nil {
  162. return nil, err
  163. }
  164. optimizedLength += length
  165. }
  166. singleByteSegmentLength, err := d.encodedLength(highestRequiredMode, len(d.data))
  167. if err != nil {
  168. return nil, err
  169. }
  170. if singleByteSegmentLength <= optimizedLength {
  171. d.optimised = []segment{segment{dataMode: highestRequiredMode, data: d.data}}
  172. }
  173. // Encode data.
  174. encoded := bitset.New()
  175. for _, s := range d.optimised {
  176. d.encodeDataRaw(s.data, s.dataMode, encoded)
  177. }
  178. return encoded, nil
  179. }
  180. // classifyDataModes classifies the raw data into unoptimised segments.
  181. // e.g. "123ZZ#!#!" =>
  182. // [numeric, 3, "123"] [alphanumeric, 2, "ZZ"] [byte, 4, "#!#!"].
  183. //
  184. // Returns the highest data mode needed to encode the data. e.g. for a mixed
  185. // numeric/alphanumeric input, the highest is alphanumeric.
  186. //
  187. // dataModeNone < dataModeNumeric < dataModeAlphanumeric < dataModeByte
  188. func (d *dataEncoder) classifyDataModes() dataMode {
  189. var start int
  190. mode := dataModeNone
  191. highestRequiredMode := mode
  192. for i, v := range d.data {
  193. newMode := dataModeNone
  194. switch {
  195. case v >= 0x30 && v <= 0x39:
  196. newMode = dataModeNumeric
  197. case v == 0x20 || v == 0x24 || v == 0x25 || v == 0x2a || v == 0x2b || v ==
  198. 0x2d || v == 0x2e || v == 0x2f || v == 0x3a || (v >= 0x41 && v <= 0x5a):
  199. newMode = dataModeAlphanumeric
  200. default:
  201. newMode = dataModeByte
  202. }
  203. if newMode != mode {
  204. if i > 0 {
  205. d.actual = append(d.actual, segment{dataMode: mode, data: d.data[start:i]})
  206. start = i
  207. }
  208. mode = newMode
  209. }
  210. if newMode > highestRequiredMode {
  211. highestRequiredMode = newMode
  212. }
  213. }
  214. d.actual = append(d.actual, segment{dataMode: mode, data: d.data[start:len(d.data)]})
  215. return highestRequiredMode
  216. }
  217. // optimiseDataModes optimises the list of segments to reduce the overall output
  218. // encoded data length.
  219. //
  220. // The algorithm coalesces adjacent segments. segments are only coalesced when
  221. // the Data Modes are compatible, and when the coalesced segment has a shorter
  222. // encoded length than separate segments.
  223. //
  224. // Multiple segments may be coalesced. For example a string of alternating
  225. // alphanumeric/numeric segments ANANANANA can be optimised to just A.
  226. func (d *dataEncoder) optimiseDataModes() error {
  227. for i := 0; i < len(d.actual); {
  228. mode := d.actual[i].dataMode
  229. numChars := len(d.actual[i].data)
  230. j := i + 1
  231. for j < len(d.actual) {
  232. nextNumChars := len(d.actual[j].data)
  233. nextMode := d.actual[j].dataMode
  234. if nextMode > mode {
  235. break
  236. }
  237. coalescedLength, err := d.encodedLength(mode, numChars+nextNumChars)
  238. if err != nil {
  239. return err
  240. }
  241. seperateLength1, err := d.encodedLength(mode, numChars)
  242. if err != nil {
  243. return err
  244. }
  245. seperateLength2, err := d.encodedLength(nextMode, nextNumChars)
  246. if err != nil {
  247. return err
  248. }
  249. if coalescedLength < seperateLength1+seperateLength2 {
  250. j++
  251. numChars += nextNumChars
  252. } else {
  253. break
  254. }
  255. }
  256. optimised := segment{dataMode: mode,
  257. data: make([]byte, 0, numChars)}
  258. for k := i; k < j; k++ {
  259. optimised.data = append(optimised.data, d.actual[k].data...)
  260. }
  261. d.optimised = append(d.optimised, optimised)
  262. i = j
  263. }
  264. return nil
  265. }
  266. // encodeDataRaw encodes data in dataMode. The encoded data is appended to
  267. // encoded.
  268. func (d *dataEncoder) encodeDataRaw(data []byte, dataMode dataMode, encoded *bitset.Bitset) {
  269. modeIndicator := d.modeIndicator(dataMode)
  270. charCountBits := d.charCountBits(dataMode)
  271. // Append mode indicator.
  272. encoded.Append(modeIndicator)
  273. // Append character count.
  274. encoded.AppendUint32(uint32(len(data)), charCountBits)
  275. // Append data.
  276. switch dataMode {
  277. case dataModeNumeric:
  278. for i := 0; i < len(data); i += 3 {
  279. charsRemaining := len(data) - i
  280. var value uint32
  281. bitsUsed := 1
  282. for j := 0; j < charsRemaining && j < 3; j++ {
  283. value *= 10
  284. value += uint32(data[i+j] - 0x30)
  285. bitsUsed += 3
  286. }
  287. encoded.AppendUint32(value, bitsUsed)
  288. }
  289. case dataModeAlphanumeric:
  290. for i := 0; i < len(data); i += 2 {
  291. charsRemaining := len(data) - i
  292. var value uint32
  293. for j := 0; j < charsRemaining && j < 2; j++ {
  294. value *= 45
  295. value += encodeAlphanumericCharacter(data[i+j])
  296. }
  297. bitsUsed := 6
  298. if charsRemaining > 1 {
  299. bitsUsed = 11
  300. }
  301. encoded.AppendUint32(value, bitsUsed)
  302. }
  303. case dataModeByte:
  304. for _, b := range data {
  305. encoded.AppendByte(b, 8)
  306. }
  307. }
  308. }
  309. // modeIndicator returns the segment header bits for a segment of type dataMode.
  310. func (d *dataEncoder) modeIndicator(dataMode dataMode) *bitset.Bitset {
  311. switch dataMode {
  312. case dataModeNumeric:
  313. return d.numericModeIndicator
  314. case dataModeAlphanumeric:
  315. return d.alphanumericModeIndicator
  316. case dataModeByte:
  317. return d.byteModeIndicator
  318. default:
  319. log.Panic("Unknown data mode")
  320. }
  321. return nil
  322. }
  323. // charCountBits returns the number of bits used to encode the length of a data
  324. // segment of type dataMode.
  325. func (d *dataEncoder) charCountBits(dataMode dataMode) int {
  326. switch dataMode {
  327. case dataModeNumeric:
  328. return d.numNumericCharCountBits
  329. case dataModeAlphanumeric:
  330. return d.numAlphanumericCharCountBits
  331. case dataModeByte:
  332. return d.numByteCharCountBits
  333. default:
  334. log.Panic("Unknown data mode")
  335. }
  336. return 0
  337. }
  338. // encodedLength returns the number of bits required to encode n symbols in
  339. // dataMode.
  340. //
  341. // The number of bits required is affected by:
  342. // - QR code type - Mode Indicator length.
  343. // - Data mode - number of bits used to represent data length.
  344. // - Data mode - how the data is encoded.
  345. // - Number of symbols encoded.
  346. //
  347. // An error is returned if the mode is not supported, or the length requested is
  348. // too long to be represented.
  349. func (d *dataEncoder) encodedLength(dataMode dataMode, n int) (int, error) {
  350. modeIndicator := d.modeIndicator(dataMode)
  351. charCountBits := d.charCountBits(dataMode)
  352. if modeIndicator == nil {
  353. return 0, errors.New("mode not supported")
  354. }
  355. maxLength := (1 << uint8(charCountBits)) - 1
  356. if n > maxLength {
  357. return 0, errors.New("length too long to be represented")
  358. }
  359. length := modeIndicator.Len() + charCountBits
  360. switch dataMode {
  361. case dataModeNumeric:
  362. length += 10 * (n / 3)
  363. if n%3 != 0 {
  364. length += 1 + 3*(n%3)
  365. }
  366. case dataModeAlphanumeric:
  367. length += 11 * (n / 2)
  368. length += 6 * (n % 2)
  369. case dataModeByte:
  370. length += 8 * n
  371. }
  372. return length, nil
  373. }
  374. // encodeAlphanumericChar returns the QR Code encoded value of v.
  375. //
  376. // v must be a QR Code defined alphanumeric character: 0-9, A-Z, SP, $%*+-./ or
  377. // :. The characters are mapped to values in the range 0-44 respectively.
  378. func encodeAlphanumericCharacter(v byte) uint32 {
  379. c := uint32(v)
  380. switch {
  381. case c >= '0' && c <= '9':
  382. // 0-9 encoded as 0-9.
  383. return c - '0'
  384. case c >= 'A' && c <= 'Z':
  385. // A-Z encoded as 10-35.
  386. return c - 'A' + 10
  387. case c == ' ':
  388. return 36
  389. case c == '$':
  390. return 37
  391. case c == '%':
  392. return 38
  393. case c == '*':
  394. return 39
  395. case c == '+':
  396. return 40
  397. case c == '-':
  398. return 41
  399. case c == '.':
  400. return 42
  401. case c == '/':
  402. return 43
  403. case c == ':':
  404. return 44
  405. default:
  406. log.Panicf("encodeAlphanumericCharacter() with non alphanumeric char %v.", v)
  407. }
  408. return 0
  409. }