not really known
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.

336 lines
8.8 KiB

  1. /*
  2. Ported to JavaScript by Lazar Laszlo 2011
  3. lazarsoft@gmail.com, www.lazarsoft.info
  4. */
  5. /*
  6. *
  7. * Copyright 2007 ZXing authors
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. function QRCodeDataBlockReader(blocks, version, numErrorCorrectionCode)
  22. {
  23. this.blockPointer = 0;
  24. this.bitPointer = 7;
  25. this.dataLength = 0;
  26. this.blocks = blocks;
  27. this.numErrorCorrectionCode = numErrorCorrectionCode;
  28. if (version <= 9)
  29. this.dataLengthMode = 0;
  30. else if (version >= 10 && version <= 26)
  31. this.dataLengthMode = 1;
  32. else if (version >= 27 && version <= 40)
  33. this.dataLengthMode = 2;
  34. this.getNextBits = function( numBits)
  35. {
  36. var bits = 0;
  37. if (numBits < this.bitPointer + 1)
  38. {
  39. // next word fits into current data block
  40. var mask = 0;
  41. for (var i = 0; i < numBits; i++)
  42. {
  43. mask += (1 << i);
  44. }
  45. mask <<= (this.bitPointer - numBits + 1);
  46. bits = (this.blocks[this.blockPointer] & mask) >> (this.bitPointer - numBits + 1);
  47. this.bitPointer -= numBits;
  48. return bits;
  49. }
  50. else if (numBits < this.bitPointer + 1 + 8)
  51. {
  52. // next word crosses 2 data blocks
  53. var mask1 = 0;
  54. for (var i = 0; i < this.bitPointer + 1; i++)
  55. {
  56. mask1 += (1 << i);
  57. }
  58. bits = (this.blocks[this.blockPointer] & mask1) << (numBits - (this.bitPointer + 1));
  59. this.blockPointer++;
  60. bits += ((this.blocks[this.blockPointer]) >> (8 - (numBits - (this.bitPointer + 1))));
  61. this.bitPointer = this.bitPointer - numBits % 8;
  62. if (this.bitPointer < 0)
  63. {
  64. this.bitPointer = 8 + this.bitPointer;
  65. }
  66. return bits;
  67. }
  68. else if (numBits < this.bitPointer + 1 + 16)
  69. {
  70. // next word crosses 3 data blocks
  71. var mask1 = 0; // mask of first block
  72. var mask3 = 0; // mask of 3rd block
  73. //bitPointer + 1 : number of bits of the 1st block
  74. //8 : number of the 2nd block (note that use already 8bits because next word uses 3 data blocks)
  75. //numBits - (bitPointer + 1 + 8) : number of bits of the 3rd block
  76. for (var i = 0; i < this.bitPointer + 1; i++)
  77. {
  78. mask1 += (1 << i);
  79. }
  80. var bitsFirstBlock = (this.blocks[this.blockPointer] & mask1) << (numBits - (this.bitPointer + 1));
  81. this.blockPointer++;
  82. var bitsSecondBlock = this.blocks[this.blockPointer] << (numBits - (this.bitPointer + 1 + 8));
  83. this.blockPointer++;
  84. for (var i = 0; i < numBits - (this.bitPointer + 1 + 8); i++)
  85. {
  86. mask3 += (1 << i);
  87. }
  88. mask3 <<= 8 - (numBits - (this.bitPointer + 1 + 8));
  89. var bitsThirdBlock = (this.blocks[this.blockPointer] & mask3) >> (8 - (numBits - (this.bitPointer + 1 + 8)));
  90. bits = bitsFirstBlock + bitsSecondBlock + bitsThirdBlock;
  91. this.bitPointer = this.bitPointer - (numBits - 8) % 8;
  92. if (this.bitPointer < 0)
  93. {
  94. this.bitPointer = 8 + this.bitPointer;
  95. }
  96. return bits;
  97. }
  98. else
  99. {
  100. return 0;
  101. }
  102. }
  103. this.NextMode=function()
  104. {
  105. if ((this.blockPointer > this.blocks.length - this.numErrorCorrectionCode - 2))
  106. return 0;
  107. else
  108. return this.getNextBits(4);
  109. }
  110. this.getDataLength=function( modeIndicator)
  111. {
  112. var index = 0;
  113. while (true)
  114. {
  115. if ((modeIndicator >> index) == 1)
  116. break;
  117. index++;
  118. }
  119. return this.getNextBits(qrcode.sizeOfDataLengthInfo[this.dataLengthMode][index]);
  120. }
  121. this.getRomanAndFigureString=function( dataLength)
  122. {
  123. var length = dataLength;
  124. var intData = 0;
  125. var strData = "";
  126. var tableRomanAndFigure = new Array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' ', '$', '%', '*', '+', '-', '.', '/', ':');
  127. do
  128. {
  129. if (length > 1)
  130. {
  131. intData = this.getNextBits(11);
  132. var firstLetter = Math.floor(intData / 45);
  133. var secondLetter = intData % 45;
  134. strData += tableRomanAndFigure[firstLetter];
  135. strData += tableRomanAndFigure[secondLetter];
  136. length -= 2;
  137. }
  138. else if (length == 1)
  139. {
  140. intData = this.getNextBits(6);
  141. strData += tableRomanAndFigure[intData];
  142. length -= 1;
  143. }
  144. }
  145. while (length > 0);
  146. return strData;
  147. }
  148. this.getFigureString=function( dataLength)
  149. {
  150. var length = dataLength;
  151. var intData = 0;
  152. var strData = "";
  153. do
  154. {
  155. if (length >= 3)
  156. {
  157. intData = this.getNextBits(10);
  158. if (intData < 100)
  159. strData += "0";
  160. if (intData < 10)
  161. strData += "0";
  162. length -= 3;
  163. }
  164. else if (length == 2)
  165. {
  166. intData = this.getNextBits(7);
  167. if (intData < 10)
  168. strData += "0";
  169. length -= 2;
  170. }
  171. else if (length == 1)
  172. {
  173. intData = this.getNextBits(4);
  174. length -= 1;
  175. }
  176. strData += intData;
  177. }
  178. while (length > 0);
  179. return strData;
  180. }
  181. this.get8bitByteArray=function( dataLength)
  182. {
  183. var length = dataLength;
  184. var intData = 0;
  185. var output = new Array();
  186. do
  187. {
  188. intData = this.getNextBits(8);
  189. output.push( intData);
  190. length--;
  191. }
  192. while (length > 0);
  193. return output;
  194. }
  195. this.getKanjiString=function( dataLength)
  196. {
  197. var length = dataLength;
  198. var intData = 0;
  199. var unicodeString = "";
  200. do
  201. {
  202. intData = this.getNextBits(13);
  203. var lowerByte = intData % 0xC0;
  204. var higherByte = intData / 0xC0;
  205. var tempWord = (higherByte << 8) + lowerByte;
  206. var shiftjisWord = 0;
  207. if (tempWord + 0x8140 <= 0x9FFC)
  208. {
  209. // between 8140 - 9FFC on Shift_JIS character set
  210. shiftjisWord = tempWord + 0x8140;
  211. }
  212. else
  213. {
  214. // between E040 - EBBF on Shift_JIS character set
  215. shiftjisWord = tempWord + 0xC140;
  216. }
  217. //var tempByte = new Array(0,0);
  218. //tempByte[0] = (sbyte) (shiftjisWord >> 8);
  219. //tempByte[1] = (sbyte) (shiftjisWord & 0xFF);
  220. //unicodeString += new String(SystemUtils.ToCharArray(SystemUtils.ToByteArray(tempByte)));
  221. unicodeString += String.fromCharCode(shiftjisWord);
  222. length--;
  223. }
  224. while (length > 0);
  225. return unicodeString;
  226. }
  227. this.parseECIValue = function ()
  228. {
  229. var intData = 0;
  230. var firstByte = this.getNextBits(8);
  231. if ((firstByte & 0x80) == 0) {
  232. intData = firstByte & 0x7F;
  233. }
  234. if ((firstByte & 0xC0) == 0x80) {
  235. // two bytes
  236. var secondByte = this.getNextBits(8);
  237. intData = ((firstByte & 0x3F) << 8) | secondByte;
  238. }
  239. if ((firstByte & 0xE0) == 0xC0) {
  240. // three bytes
  241. var secondThirdBytes = this.getNextBits(8);;
  242. intData = ((firstByte & 0x1F) << 16) | secondThirdBytes;
  243. }
  244. return intData;
  245. }
  246. this.__defineGetter__("DataByte", function()
  247. {
  248. var output = new Array();
  249. var MODE_NUMBER = 1;
  250. var MODE_ROMAN_AND_NUMBER = 2;
  251. var MODE_8BIT_BYTE = 4;
  252. var MODE_ECI = 7;
  253. var MODE_KANJI = 8;
  254. do
  255. {
  256. var mode = this.NextMode();
  257. //canvas.println("mode: " + mode);
  258. if (mode == 0)
  259. {
  260. if (output.length > 0)
  261. break;
  262. else
  263. throw "Empty data block";
  264. }
  265. if (mode != MODE_NUMBER && mode != MODE_ROMAN_AND_NUMBER && mode != MODE_8BIT_BYTE && mode != MODE_KANJI && mode != MODE_ECI)
  266. {
  267. throw "Invalid mode: " + mode + " in (block:" + this.blockPointer + " bit:" + this.bitPointer + ")";
  268. }
  269. if(mode == MODE_ECI)
  270. {
  271. var temp_sbyteArray3 = this.parseECIValue();
  272. //output.push(temp_sbyteArray3);
  273. }
  274. else
  275. {
  276. var dataLength = this.getDataLength(mode);
  277. if (dataLength < 1)
  278. throw "Invalid data length: " + dataLength;
  279. switch (mode)
  280. {
  281. case MODE_NUMBER:
  282. var temp_str = this.getFigureString(dataLength);
  283. var ta = new Array(temp_str.length);
  284. for(var j=0;j<temp_str.length;j++)
  285. ta[j]=temp_str.charCodeAt(j);
  286. output.push(ta);
  287. break;
  288. case MODE_ROMAN_AND_NUMBER:
  289. var temp_str = this.getRomanAndFigureString(dataLength);
  290. var ta = new Array(temp_str.length);
  291. for(var j=0;j<temp_str.length;j++)
  292. ta[j]=temp_str.charCodeAt(j);
  293. output.push(ta );
  294. break;
  295. case MODE_8BIT_BYTE:
  296. var temp_sbyteArray3 = this.get8bitByteArray(dataLength);
  297. output.push(temp_sbyteArray3);
  298. break;
  299. case MODE_KANJI:
  300. var temp_str = this.getKanjiString(dataLength);
  301. output.push(temp_str);
  302. break;
  303. }
  304. }
  305. }
  306. while (true);
  307. return output;
  308. });
  309. }