[SignalingServer] Optimize dependent modules
[platform/framework/web/wrtjs.git] / signaling_server / service / node_modules / qrcode / lib / core / version.js
1 var Utils = require('./utils')
2 var ECCode = require('./error-correction-code')
3 var ECLevel = require('./error-correction-level')
4 var Mode = require('./mode')
5 var VersionCheck = require('./version-check')
6 var isArray = require('isarray')
7
8 // Generator polynomial used to encode version information
9 var G18 = (1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0)
10 var G18_BCH = Utils.getBCHDigit(G18)
11
12 function getBestVersionForDataLength (mode, length, errorCorrectionLevel) {
13   for (var currentVersion = 1; currentVersion <= 40; currentVersion++) {
14     if (length <= exports.getCapacity(currentVersion, errorCorrectionLevel, mode)) {
15       return currentVersion
16     }
17   }
18
19   return undefined
20 }
21
22 function getReservedBitsCount (mode, version) {
23   // Character count indicator + mode indicator bits
24   return Mode.getCharCountIndicator(mode, version) + 4
25 }
26
27 function getTotalBitsFromDataArray (segments, version) {
28   var totalBits = 0
29
30   segments.forEach(function (data) {
31     var reservedBits = getReservedBitsCount(data.mode, version)
32     totalBits += reservedBits + data.getBitsLength()
33   })
34
35   return totalBits
36 }
37
38 function getBestVersionForMixedData (segments, errorCorrectionLevel) {
39   for (var currentVersion = 1; currentVersion <= 40; currentVersion++) {
40     var length = getTotalBitsFromDataArray(segments, currentVersion)
41     if (length <= exports.getCapacity(currentVersion, errorCorrectionLevel, Mode.MIXED)) {
42       return currentVersion
43     }
44   }
45
46   return undefined
47 }
48
49 /**
50  * Returns version number from a value.
51  * If value is not a valid version, returns defaultValue
52  *
53  * @param  {Number|String} value        QR Code version
54  * @param  {Number}        defaultValue Fallback value
55  * @return {Number}                     QR Code version number
56  */
57 exports.from = function from (value, defaultValue) {
58   if (VersionCheck.isValid(value)) {
59     return parseInt(value, 10)
60   }
61
62   return defaultValue
63 }
64
65 /**
66  * Returns how much data can be stored with the specified QR code version
67  * and error correction level
68  *
69  * @param  {Number} version              QR Code version (1-40)
70  * @param  {Number} errorCorrectionLevel Error correction level
71  * @param  {Mode}   mode                 Data mode
72  * @return {Number}                      Quantity of storable data
73  */
74 exports.getCapacity = function getCapacity (version, errorCorrectionLevel, mode) {
75   if (!VersionCheck.isValid(version)) {
76     throw new Error('Invalid QR Code version')
77   }
78
79   // Use Byte mode as default
80   if (typeof mode === 'undefined') mode = Mode.BYTE
81
82   // Total codewords for this QR code version (Data + Error correction)
83   var totalCodewords = Utils.getSymbolTotalCodewords(version)
84
85   // Total number of error correction codewords
86   var ecTotalCodewords = ECCode.getTotalCodewordsCount(version, errorCorrectionLevel)
87
88   // Total number of data codewords
89   var dataTotalCodewordsBits = (totalCodewords - ecTotalCodewords) * 8
90
91   if (mode === Mode.MIXED) return dataTotalCodewordsBits
92
93   var usableBits = dataTotalCodewordsBits - getReservedBitsCount(mode, version)
94
95   // Return max number of storable codewords
96   switch (mode) {
97     case Mode.NUMERIC:
98       return Math.floor((usableBits / 10) * 3)
99
100     case Mode.ALPHANUMERIC:
101       return Math.floor((usableBits / 11) * 2)
102
103     case Mode.KANJI:
104       return Math.floor(usableBits / 13)
105
106     case Mode.BYTE:
107     default:
108       return Math.floor(usableBits / 8)
109   }
110 }
111
112 /**
113  * Returns the minimum version needed to contain the amount of data
114  *
115  * @param  {Segment} data                    Segment of data
116  * @param  {Number} [errorCorrectionLevel=H] Error correction level
117  * @param  {Mode} mode                       Data mode
118  * @return {Number}                          QR Code version
119  */
120 exports.getBestVersionForData = function getBestVersionForData (data, errorCorrectionLevel) {
121   var seg
122
123   var ecl = ECLevel.from(errorCorrectionLevel, ECLevel.M)
124
125   if (isArray(data)) {
126     if (data.length > 1) {
127       return getBestVersionForMixedData(data, ecl)
128     }
129
130     if (data.length === 0) {
131       return 1
132     }
133
134     seg = data[0]
135   } else {
136     seg = data
137   }
138
139   return getBestVersionForDataLength(seg.mode, seg.getLength(), ecl)
140 }
141
142 /**
143  * Returns version information with relative error correction bits
144  *
145  * The version information is included in QR Code symbols of version 7 or larger.
146  * It consists of an 18-bit sequence containing 6 data bits,
147  * with 12 error correction bits calculated using the (18, 6) Golay code.
148  *
149  * @param  {Number} version QR Code version
150  * @return {Number}         Encoded version info bits
151  */
152 exports.getEncodedBits = function getEncodedBits (version) {
153   if (!VersionCheck.isValid(version) || version < 7) {
154     throw new Error('Invalid QR Code version')
155   }
156
157   var d = version << 12
158
159   while (Utils.getBCHDigit(d) - G18_BCH >= 0) {
160     d ^= (G18 << (Utils.getBCHDigit(d) - G18_BCH))
161   }
162
163   return (version << 12) | d
164 }