bitcoin-atm
bitcoin atm for pyc inc.
git clone https://9o.is/git/bitcoin-atm.git
datablock.js
(3575B)
1 /*
2 Ported to JavaScript by Lazar Laszlo 2011
3
4 lazarsoft@gmail.com, www.lazarsoft.info
5
6 */
7
8 /*
9 *
10 * Copyright 2007 ZXing authors
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 */
24
25
26 function DataBlock(numDataCodewords, codewords)
27 {
28 this.numDataCodewords = numDataCodewords;
29 this.codewords = codewords;
30
31 this.__defineGetter__("NumDataCodewords", function()
32 {
33 return this.numDataCodewords;
34 });
35 this.__defineGetter__("Codewords", function()
36 {
37 return this.codewords;
38 });
39 }
40
41 DataBlock.getDataBlocks=function(rawCodewords, version, ecLevel)
42 {
43
44 if (rawCodewords.length != version.TotalCodewords)
45 {
46 throw "ArgumentException";
47 }
48
49 // Figure out the number and size of data blocks used by this version and
50 // error correction level
51 var ecBlocks = version.getECBlocksForLevel(ecLevel);
52
53 // First count the total number of data blocks
54 var totalBlocks = 0;
55 var ecBlockArray = ecBlocks.getECBlocks();
56 for (var i = 0; i < ecBlockArray.length; i++)
57 {
58 totalBlocks += ecBlockArray[i].Count;
59 }
60
61 // Now establish DataBlocks of the appropriate size and number of data codewords
62 var result = new Array(totalBlocks);
63 var numResultBlocks = 0;
64 for (var j = 0; j < ecBlockArray.length; j++)
65 {
66 var ecBlock = ecBlockArray[j];
67 for (var i = 0; i < ecBlock.Count; i++)
68 {
69 var numDataCodewords = ecBlock.DataCodewords;
70 var numBlockCodewords = ecBlocks.ECCodewordsPerBlock + numDataCodewords;
71 result[numResultBlocks++] = new DataBlock(numDataCodewords, new Array(numBlockCodewords));
72 }
73 }
74
75 // All blocks have the same amount of data, except that the last n
76 // (where n may be 0) have 1 more byte. Figure out where these start.
77 var shorterBlocksTotalCodewords = result[0].codewords.length;
78 var longerBlocksStartAt = result.length - 1;
79 while (longerBlocksStartAt >= 0)
80 {
81 var numCodewords = result[longerBlocksStartAt].codewords.length;
82 if (numCodewords == shorterBlocksTotalCodewords)
83 {
84 break;
85 }
86 longerBlocksStartAt--;
87 }
88 longerBlocksStartAt++;
89
90 var shorterBlocksNumDataCodewords = shorterBlocksTotalCodewords - ecBlocks.ECCodewordsPerBlock;
91 // The last elements of result may be 1 element longer;
92 // first fill out as many elements as all of them have
93 var rawCodewordsOffset = 0;
94 for (var i = 0; i < shorterBlocksNumDataCodewords; i++)
95 {
96 for (var j = 0; j < numResultBlocks; j++)
97 {
98 result[j].codewords[i] = rawCodewords[rawCodewordsOffset++];
99 }
100 }
101 // Fill out the last data block in the longer ones
102 for (var j = longerBlocksStartAt; j < numResultBlocks; j++)
103 {
104 result[j].codewords[shorterBlocksNumDataCodewords] = rawCodewords[rawCodewordsOffset++];
105 }
106 // Now add in error correction blocks
107 var max = result[0].codewords.length;
108 for (var i = shorterBlocksNumDataCodewords; i < max; i++)
109 {
110 for (var j = 0; j < numResultBlocks; j++)
111 {
112 var iOffset = j < longerBlocksStartAt?i:i + 1;
113 result[j].codewords[iOffset] = rawCodewords[rawCodewordsOffset++];
114 }
115 }
116 return result;
117 }