Advertisement
Guest User

QROC

a guest
Jun 23rd, 2015
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 40.75 KB | None | 0 0
  1. --source code adapted for OpenComputers mod by 1Ridav
  2.  
  3. local qr = {}
  4. local cclxvi = {[0] = {0,0,0,0,0,0,0,0}, {1,0,0,0,0,0,0,0}, {0,1,0,0,0,0,0,0}, {1,1,0,0,0,0,0,0},
  5. {0,0,1,0,0,0,0,0}, {1,0,1,0,0,0,0,0}, {0,1,1,0,0,0,0,0}, {1,1,1,0,0,0,0,0},
  6. {0,0,0,1,0,0,0,0}, {1,0,0,1,0,0,0,0}, {0,1,0,1,0,0,0,0}, {1,1,0,1,0,0,0,0},
  7. {0,0,1,1,0,0,0,0}, {1,0,1,1,0,0,0,0}, {0,1,1,1,0,0,0,0}, {1,1,1,1,0,0,0,0},
  8. {0,0,0,0,1,0,0,0}, {1,0,0,0,1,0,0,0}, {0,1,0,0,1,0,0,0}, {1,1,0,0,1,0,0,0},
  9. {0,0,1,0,1,0,0,0}, {1,0,1,0,1,0,0,0}, {0,1,1,0,1,0,0,0}, {1,1,1,0,1,0,0,0},
  10. {0,0,0,1,1,0,0,0}, {1,0,0,1,1,0,0,0}, {0,1,0,1,1,0,0,0}, {1,1,0,1,1,0,0,0},
  11. {0,0,1,1,1,0,0,0}, {1,0,1,1,1,0,0,0}, {0,1,1,1,1,0,0,0}, {1,1,1,1,1,0,0,0},
  12. {0,0,0,0,0,1,0,0}, {1,0,0,0,0,1,0,0}, {0,1,0,0,0,1,0,0}, {1,1,0,0,0,1,0,0},
  13. {0,0,1,0,0,1,0,0}, {1,0,1,0,0,1,0,0}, {0,1,1,0,0,1,0,0}, {1,1,1,0,0,1,0,0},
  14. {0,0,0,1,0,1,0,0}, {1,0,0,1,0,1,0,0}, {0,1,0,1,0,1,0,0}, {1,1,0,1,0,1,0,0},
  15. {0,0,1,1,0,1,0,0}, {1,0,1,1,0,1,0,0}, {0,1,1,1,0,1,0,0}, {1,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{1,1,0,0,1,0,1,1},
  57. {0,0,1,0,1,0,1,1}, {1,0,1,0,1,0,1,1}, {0,1,1,0,1,0,1,1}, {1,1,1,0,1,0,1,1},
  58. {0,0,0,1,1,0,1,1}, {1,0,0,1,1,0,1,1}, {0,1,0,1,1,0,1,1}, {1,1,0,1,1,0,1,1},
  59. {0,0,1,1,1,0,1,1}, {1,0,1,1,1,0,1,1}, {0,1,1,1,1,0,1,1}, {1,1,1,1,1,0,1,1},
  60. {0,0,0,0,0,1,1,1}, {1,0,0,0,0,1,1,1}, {0,1,0,0,0,1,1,1}, {1,1,0,0,0,1,1,1},
  61. {0,0,1,0,0,1,1,1}, {1,0,1,0,0,1,1,1}, {0,1,1,0,0,1,1,1}, {1,1,1,0,0,1,1,1},
  62. {0,0,0,1,0,1,1,1}, {1,0,0,1,0,1,1,1}, {0,1,0,1,0,1,1,1}, {1,1,0,1,0,1,1,1},
  63. {0,0,1,1,0,1,1,1}, {1,0,1,1,0,1,1,1}, {0,1,1,1,0,1,1,1}, {1,1,1,1,0,1,1,1},
  64. {0,0,0,0,1,1,1,1}, {1,0,0,0,1,1,1,1}, {0,1,0,0,1,1,1,1}, {1,1,0,0,1,1,1,1},
  65. {0,0,1,0,1,1,1,1}, {1,0,1,0,1,1,1,1}, {0,1,1,0,1,1,1,1}, {1,1,1,0,1,1,1,1},
  66. {0,0,0,1,1,1,1,1}, {1,0,0,1,1,1,1,1}, {0,1,0,1,1,1,1,1}, {1,1,0,1,1,1,1,1},
  67. {0,0,1,1,1,1,1,1}, {1,0,1,1,1,1,1,1}, {0,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1}}
  68.  
  69. local function tbl_to_number(tbl)
  70.     local n = #tbl
  71.     local rslt = 0
  72.     local power = 1
  73.     for i = 1, n do
  74.         rslt = rslt + tbl[i]*power
  75.         power = power*2
  76.     end
  77.     return rslt
  78. end
  79.  
  80. local function bit_xor(m, n)
  81.     local tbl_m = cclxvi[m]
  82.     local tbl_n = cclxvi[n]
  83.     local tbl = {}
  84.     for i = 1, 8 do
  85.         if(tbl_m[i] ~= tbl_n[i]) then
  86.             tbl[i] = 1
  87.         else
  88.             tbl[i] = 0
  89.         end
  90.     end
  91.     return tbl_to_number(tbl)
  92. end
  93.  
  94. local function binary(x,digits)
  95.   local s=string.format("%o",x)
  96.   local a={["0"]="000",["1"]="001", ["2"]="010",["3"]="011",
  97.            ["4"]="100",["5"]="101", ["6"]="110",["7"]="111"}
  98.   s=string.gsub(s,"(.)",function (d) return a[d] end)
  99.   -- remove leading 0s
  100.   s = string.gsub(s,"^0*(.*)$","%1")
  101.   local fmtstring = string.format("%%%ds",digits)
  102.   local ret = string.format(fmtstring,s)
  103.   return string.gsub(ret," ","0")
  104. end
  105.  
  106. local function fill_matrix_position(matrix,bitstring,x,y)
  107.     if bitstring == "1" then
  108.         matrix[x][y] = 2
  109.     else
  110.         matrix[x][y] = -2
  111.     end
  112. end
  113.  
  114. local function get_mode( str )
  115.     local mode
  116.     if string.match(str,"^[0-9]+$") then
  117.         return 1
  118.     elseif string.match(str,"^[0-9A-Z $%%*./:+-]+$") then
  119.         return 2
  120.     else
  121.         return 4
  122.     end
  123.     assert(false,"never reached")
  124.     return nil
  125. end
  126.  
  127. local capacity = {
  128.   {  19,   16,   13,    9},{  34,   28,   22,   16},{  55,   44,   34,   26},{  80,   64,   48,   36},
  129.   { 108,   86,   62,   46},{ 136,  108,   76,   60},{ 156,  124,   88,   66},{ 194,  154,  110,   86},
  130.   { 232,  182,  132,  100},{ 274,  216,  154,  122},{ 324,  254,  180,  140},{ 370,  290,  206,  158},
  131.   { 428,  334,  244,  180},{ 461,  365,  261,  197},{ 523,  415,  295,  223},{ 589,  453,  325,  253},
  132.   { 647,  507,  367,  283},{ 721,  563,  397,  313},{ 795,  627,  445,  341},{ 861,  669,  485,  385},
  133.   { 932,  714,  512,  406},{1006,  782,  568,  442},{1094,  860,  614,  464},{1174,  914,  664,  514},
  134.   {1276, 1000,  718,  538},{1370, 1062,  754,  596},{1468, 1128,  808,  628},{1531, 1193,  871,  661},
  135.   {1631, 1267,  911,  701},{1735, 1373,  985,  745},{1843, 1455, 1033,  793},{1955, 1541, 1115,  845},
  136.   {2071, 1631, 1171,  901},{2191, 1725, 1231,  961},{2306, 1812, 1286,  986},{2434, 1914, 1354, 1054},
  137.   {2566, 1992, 1426, 1096},{2702, 2102, 1502, 1142},{2812, 2216, 1582, 1222},{2956, 2334, 1666, 1276}}
  138.  
  139. local function get_version_eclevel(len,mode,requested_ec_level)
  140.     local local_mode = mode
  141.     if mode == 4 then
  142.         local_mode = 3
  143.     elseif mode == 8 then
  144.         local_mode = 4
  145.     end
  146.     assert( local_mode <= 4 )
  147.  
  148.     local bytes, bits, digits, modebits, c
  149.     local tab = { {10,9,8,8},{12,11,16,10},{14,13,16,12} }
  150.     local minversion = 40
  151.     local maxec_level = 1
  152.     for ec_level=1,4 do
  153.         if requested_ec_level == nil or ec_level >= requested_ec_level then
  154.             for version=1,#capacity do
  155.                 bits = capacity[version][ec_level] * 8
  156.                 bits = bits - 4 -- the mode indicator
  157.                 if version < 10 then
  158.                     digits = tab[1][local_mode]
  159.                 elseif version < 27 then
  160.                     digits = tab[2][local_mode]
  161.                 elseif version <= 40 then
  162.                     digits = tab[3][local_mode]
  163.                 end
  164.                 modebits = bits - digits
  165.                 if local_mode == 1 then -- numeric
  166.                     c = math.floor(modebits * 3 / 10)
  167.                 elseif local_mode == 2 then -- alphanumeric
  168.                     c = math.floor(modebits * 2 / 11)
  169.                 elseif local_mode == 3 then -- binary
  170.                     c = math.floor(modebits * 1 / 8)
  171.                 else
  172.                     c = math.floor(modebits * 1 / 13)
  173.                 end
  174.                 if c >= len then
  175.                     if version <= minversion then
  176.                         minversion = version
  177.                         maxec_level = ec_level
  178.                     end
  179.                     break
  180.                 end
  181.             end
  182.         end
  183.     end
  184.     return minversion, maxec_level
  185. end
  186.  
  187. local function get_length(str,version,mode)
  188.     local i = mode
  189.     if mode == 4 then
  190.         i = 3
  191.     elseif mode == 8 then
  192.         i = 4
  193.     end
  194.     assert( i <= 4 )
  195.     local tab = { {10,9,8,8},{12,11,16,10},{14,13,16,12} }
  196.     local digits
  197.     if version < 10 then
  198.         digits = tab[1][i]
  199.     elseif version < 27 then
  200.         digits = tab[2][i]
  201.     elseif version <= 40 then
  202.         digits = tab[3][i]
  203.     else
  204.         assert(false, "get_length, version > 40 not supported")
  205.     end
  206.     local len = binary(#str,digits)
  207.     return len
  208. end
  209.  
  210. local function get_version_eclevel_mode_bistringlength(str,requested_ec_level,mode)
  211.     local local_mode
  212.     if mode then
  213.         assert(false,"not implemented")
  214.         -- check if the mode is OK for the string
  215.         local_mode = mode
  216.     else
  217.         local_mode = get_mode(str)
  218.     end
  219.     local version, ec_level
  220.     version, ec_level = get_version_eclevel(#str,local_mode,requested_ec_level)
  221.     local length_string = get_length(str,version,local_mode)
  222.     return version,ec_level,binary(local_mode,4),local_mode,length_string
  223. end
  224.  
  225. local asciitbl = {
  226.         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  -- 0x01-0x0f
  227.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  -- 0x10-0x1f
  228.     36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43,  -- 0x20-0x2f
  229.      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 44, -1, -1, -1, -1, -1,  -- 0x30-0x3f
  230.     -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,  -- 0x40-0x4f
  231.     25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1,  -- 0x50-0x5f
  232.   }
  233.  
  234. local function encode_string_numeric(str)
  235.     local bitstring = ""
  236.     local int
  237.     string.gsub(str,"..?.?",function(a)
  238.         int = tonumber(a)
  239.         if #a == 3 then
  240.             bitstring = bitstring .. binary(int,10)
  241.         elseif #a == 2 then
  242.             bitstring = bitstring .. binary(int,7)
  243.         else
  244.             bitstring = bitstring .. binary(int,4)
  245.         end
  246.     end)
  247.     return bitstring
  248. end
  249.  
  250. local function encode_string_ascii(str)
  251.     local bitstring = ""
  252.     local int
  253.     local b1, b2
  254.     string.gsub(str,"..?",function(a)
  255.         if #a == 2 then
  256.             b1 = asciitbl[string.byte(string.sub(a,1,1))]
  257.             b2 = asciitbl[string.byte(string.sub(a,2,2))]
  258.             int = b1 * 45 + b2
  259.             bitstring = bitstring .. binary(int,11)
  260.         else
  261.             int = asciitbl[string.byte(a)]
  262.             bitstring = bitstring .. binary(int,6)
  263.         end
  264.       end)
  265.     return bitstring
  266. end
  267.  
  268. local function encode_string_binary(str)
  269.     local ret = {}
  270.     string.gsub(str,".",function(x)
  271.         ret[#ret + 1] = binary(string.byte(x),8)
  272.     end)
  273.     return table.concat(ret)
  274. end
  275.  
  276. local function encode_data(str,mode)
  277.     if mode == 1 then
  278.         return encode_string_numeric(str)
  279.     elseif mode == 2 then
  280.         return encode_string_ascii(str)
  281.     elseif mode == 4 then
  282.         return encode_string_binary(str)
  283.     else
  284.         assert(false,"not implemented yet")
  285.     end
  286. end
  287.  
  288. local function add_pad_data(version,ec_level,data)
  289.     local count_to_pad, missing_digits
  290.     local cpty = capacity[version][ec_level] * 8
  291.     count_to_pad = math.min(4,cpty - #data)
  292.     if count_to_pad > 0 then
  293.         data = data .. string.rep("0",count_to_pad)
  294.     end
  295.     if math.fmod(#data,8) ~= 0 then
  296.         missing_digits = 8 - math.fmod(#data,8)
  297.         data = data .. string.rep("0",missing_digits)
  298.     end
  299.     assert(math.fmod(#data,8) == 0)
  300.     -- add "11101100" and "00010001" until enough data
  301.     while #data < cpty do
  302.         data = data .. "11101100"
  303.         if #data < cpty then
  304.             data = data .. "00010001"
  305.         end
  306.     end
  307.     return data
  308. end
  309.  
  310. local alpha_int = {
  311.     [0] = 0,
  312.       2,   4,   8,  16,  32,  64, 128,  29,  58, 116, 232, 205, 135,  19,  38,  76,
  313.     152,  45,  90, 180, 117, 234, 201, 143,   3,   6,  12,  24,  48,  96, 192, 157,
  314.      39,  78, 156,  37,  74, 148,  53, 106, 212, 181, 119, 238, 193, 159,  35,  70,
  315.     140,   5,  10,  20,  40,  80, 160,  93, 186, 105, 210, 185, 111, 222, 161,  95,
  316.     190,  97, 194, 153,  47,  94, 188, 101, 202, 137,  15,  30,  60, 120, 240, 253,
  317.     231, 211, 187, 107, 214, 177, 127, 254, 225, 223, 163,  91, 182, 113, 226, 217,
  318.     175,  67, 134,  17,  34,  68, 136,  13,  26,  52, 104, 208, 189, 103, 206, 129,
  319.      31,  62, 124, 248, 237, 199, 147,  59, 118, 236, 197, 151,  51, 102, 204, 133,
  320.      23,  46,  92, 184, 109, 218, 169,  79, 158,  33,  66, 132,  21,  42,  84, 168,
  321.      77, 154,  41,  82, 164,  85, 170,  73, 146,  57, 114, 228, 213, 183, 115, 230,
  322.     209, 191,  99, 198, 145,  63, 126, 252, 229, 215, 179, 123, 246, 241, 255, 227,
  323.     219, 171,  75, 150,  49,  98, 196, 149,  55, 110, 220, 165,  87, 174,  65, 130,
  324.      25,  50, 100, 200, 141,   7,  14,  28,  56, 112, 224, 221, 167,  83, 166,  81,
  325.     162,  89, 178, 121, 242, 249, 239, 195, 155,  43,  86, 172,  69, 138,   9,  18,
  326.      36,  72, 144,  61, 122, 244, 245, 247, 243, 251, 235, 203, 139,  11,  22,  44,
  327.      88, 176, 125, 250, 233, 207, 131,  27,  54, 108, 216, 173,  71, 142,   1
  328. }
  329.  
  330. local int_alpha = {
  331.     [0] = 0,
  332.     255,   1,  25,   2,  50,  26, 198,   3, 223,  51, 238,  27, 104, 199,  75,   4,
  333.     100, 224,  14,  52, 141, 239, 129,  28, 193, 105, 248, 200,   8,  76, 113,   5,
  334.     138, 101,  47, 225,  36,  15,  33,  53, 147, 142, 218, 240,  18, 130,  69,  29,
  335.     181, 194, 125, 106,  39, 249, 185, 201, 154,   9, 120,  77, 228, 114, 166,   6,
  336.     191, 139,  98, 102, 221,  48, 253, 226, 152,  37, 179,  16, 145,  34, 136,  54,
  337.     208, 148, 206, 143, 150, 219, 189, 241, 210,  19,  92, 131,  56,  70,  64,  30,
  338.      66, 182, 163, 195,  72, 126, 110, 107,  58,  40,  84, 250, 133, 186,  61, 202,
  339.      94, 155, 159,  10,  21, 121,  43,  78, 212, 229, 172, 115, 243, 167,  87,   7,
  340.     112, 192, 247, 140, 128,  99,  13, 103,  74, 222, 237,  49, 197, 254,  24, 227,
  341.     165, 153, 119,  38, 184, 180, 124,  17,  68, 146, 217,  35,  32, 137,  46,  55,
  342.      63, 209,  91, 149, 188, 207, 205, 144, 135, 151, 178, 220, 252, 190,  97, 242,
  343.      86, 211, 171,  20,  42,  93, 158, 132,  60,  57,  83,  71, 109,  65, 162,  31,
  344.      45,  67, 216, 183, 123, 164, 118, 196,  23,  73, 236, 127,  12, 111, 246, 108,
  345.     161,  59,  82,  41, 157,  85, 170, 251,  96, 134, 177, 187, 204,  62,  90, 203,
  346.      89,  95, 176, 156, 169, 160,  81,  11, 245,  22, 235, 122, 117,  44, 215,  79,
  347.     174, 213, 233, 230, 231, 173, 232, 116, 214, 244, 234, 168,  80,  88, 175
  348. }
  349.  
  350. local generator_polynomial = {
  351.      [7] = { 21, 102, 238, 149, 146, 229,  87,   0},
  352.     [10] = { 45,  32,  94,  64,  70, 118,  61,  46,  67, 251,   0 },
  353.     [13] = { 78, 140, 206, 218, 130, 104, 106, 100,  86, 100, 176, 152,  74,   0 },
  354.     [15] = {105,  99,   5, 124, 140, 237,  58,  58,  51,  37, 202,  91,  61, 183,   8,   0},
  355.     [16] = {120, 225, 194, 182, 169, 147, 191,  91,   3,  76, 161, 102, 109, 107, 104, 120,   0},
  356.     [17] = {136, 163, 243,  39, 150,  99,  24, 147, 214, 206, 123, 239,  43,  78, 206, 139,  43,   0},
  357.     [18] = {153,  96,  98,   5, 179, 252, 148, 152, 187,  79, 170, 118,  97, 184,  94, 158, 234, 215,   0},
  358.     [20] = {190, 188, 212, 212, 164, 156, 239,  83, 225, 221, 180, 202, 187,  26, 163,  61,  50,  79,  60,  17,   0},
  359.     [22] = {231, 165, 105, 160, 134, 219,  80,  98, 172,   8,  74, 200,  53, 221, 109,  14, 230,  93, 242, 247, 171, 210,   0},
  360.     [24] = { 21, 227,  96,  87, 232, 117,   0, 111, 218, 228, 226, 192, 152, 169, 180, 159, 126, 251, 117, 211,  48, 135, 121, 229,   0},
  361.     [26] = { 70, 218, 145, 153, 227,  48, 102,  13, 142, 245,  21, 161,  53, 165,  28, 111, 201, 145,  17, 118, 182, 103,   2, 158, 125, 173,   0},
  362.     [28] = {123,   9,  37, 242, 119, 212, 195,  42,  87, 245,  43,  21, 201, 232,  27, 205, 147, 195, 190, 110, 180, 108, 234, 224, 104, 200, 223, 168,   0},
  363.     [30] = {180, 192,  40, 238, 216, 251,  37, 156, 130, 224, 193, 226, 173,  42, 125, 222,  96, 239,  86, 110,  48,  50, 182, 179,  31, 216, 152, 145, 173, 41, 0}}
  364.  
  365. local function convert_bitstring_to_bytes(data)
  366.     local msg = {}
  367.     local tab = string.gsub(data,"(........)",function(x)
  368.         msg[#msg+1] = tonumber(x,2)
  369.         end)
  370.     return msg
  371. end
  372.  
  373. function get_generator_polynominal_adjusted(num_ec_codewords,highest_exponent)
  374.     local gp_alpha = {[0]=0}
  375.     for i=0,highest_exponent - num_ec_codewords - 1 do
  376.         gp_alpha[i] = 0
  377.     end
  378.     local gp = generator_polynomial[num_ec_codewords]
  379.     for i=1,num_ec_codewords + 1 do
  380.         gp_alpha[highest_exponent - num_ec_codewords + i - 1] = gp[i]
  381.     end
  382.     return gp_alpha
  383. end
  384.  
  385. local function convert_to_alpha( tab )
  386.     local new_tab = {}
  387.     for i=0,#tab do
  388.         new_tab[i] = int_alpha[tab[i]]
  389.     end
  390.     return new_tab
  391. end
  392.  
  393. local function convert_to_int(tab,len_message)
  394.     local new_tab = {}
  395.     for i=0,#tab do
  396.         new_tab[i] = alpha_int[tab[i]]
  397.     end
  398.     return new_tab
  399. end
  400.  
  401. local function calculate_error_correction(data,num_ec_codewords)
  402.     local mp
  403.     if type(data)=="string" then
  404.         mp = convert_bitstring_to_bytes(data)
  405.     elseif type(data)=="table" then
  406.         mp = data
  407.     else
  408.         assert(false,"Unknown type for data: %s",type(data))
  409.     end
  410.     local len_message = #mp
  411.  
  412.     local highest_exponent = len_message + num_ec_codewords - 1
  413.     local gp_alpha,tmp
  414.     local he
  415.     local gp_int = {}
  416.     local mp_int,mp_alpha = {},{}
  417.     for i=1,len_message do
  418.         mp_int[highest_exponent - i + 1] = mp[i]
  419.     end
  420.     for i=1,highest_exponent - len_message do
  421.         mp_int[i] = 0
  422.     end
  423.     mp_int[0] = 0
  424.  
  425.     mp_alpha = convert_to_alpha(mp_int)
  426.  
  427.     while highest_exponent >= num_ec_codewords do
  428.         gp_alpha = get_generator_polynominal_adjusted(num_ec_codewords,highest_exponent)
  429.         local exp = mp_alpha[highest_exponent]
  430.         for i=highest_exponent,highest_exponent - num_ec_codewords,-1 do
  431.             if gp_alpha[i] + exp > 255 then
  432.                 gp_alpha[i] = math.fmod(gp_alpha[i] + exp,255)
  433.             else
  434.                 gp_alpha[i] = gp_alpha[i] + exp
  435.             end
  436.         end
  437.         for i=highest_exponent - num_ec_codewords - 1,0,-1 do
  438.             gp_alpha[i] = 0
  439.         end
  440.  
  441.         gp_int = convert_to_int(gp_alpha)
  442.         mp_int = convert_to_int(mp_alpha)
  443.  
  444.  
  445.         tmp = {}
  446.         for i=highest_exponent,0,-1 do
  447.             tmp[i] = bit_xor(gp_int[i],mp_int[i])
  448.         end
  449.         he = highest_exponent
  450.         for i=he,0,-1 do
  451.             if i < num_ec_codewords then break end
  452.             if tmp[i] == 0 then
  453.                 tmp[i] = nil
  454.                 highest_exponent = highest_exponent - 1
  455.             else
  456.                 break
  457.             end
  458.         end
  459.         mp_int = tmp
  460.         mp_alpha = convert_to_alpha(mp_int)
  461.     end
  462.     local ret = {}
  463.  
  464.     for i=#mp_int,0,-1 do
  465.         ret[#ret + 1] = mp_int[i]
  466.     end
  467.     return ret
  468. end
  469.  
  470. local ecblocks = {
  471.   {{  1,{ 26, 19, 2}                 },   {  1,{26,16, 4}},                  {  1,{26,13, 6}},                  {  1, {26, 9, 8}               }},
  472.   {{  1,{ 44, 34, 4}                 },   {  1,{44,28, 8}},                  {  1,{44,22,11}},                  {  1, {44,16,14}               }},
  473.   {{  1,{ 70, 55, 7}                 },   {  1,{70,44,13}},                  {  2,{35,17, 9}},                  {  2, {35,13,11}               }},
  474.   {{  1,{100, 80,10}                 },   {  2,{50,32, 9}},                  {  2,{50,24,13}},                  {  4, {25, 9, 8}               }},
  475.   {{  1,{134,108,13}                 },   {  2,{67,43,12}},                  {  2,{33,15, 9},  2,{34,16, 9}},   {  2, {33,11,11},  2,{34,12,11}}},
  476.   {{  2,{ 86, 68, 9}                 },   {  4,{43,27, 8}},                  {  4,{43,19,12}},                  {  4, {43,15,14}               }},
  477.   {{  2,{ 98, 78,10}                 },   {  4,{49,31, 9}},                  {  2,{32,14, 9},  4,{33,15, 9}},   {  4, {39,13,13},  1,{40,14,13}}},
  478.   {{  2,{121, 97,12}                 },   {  2,{60,38,11},  2,{61,39,11}},   {  4,{40,18,11},  2,{41,19,11}},   {  4, {40,14,13},  2,{41,15,13}}},
  479.   {{  2,{146,116,15}                 },   {  3,{58,36,11},  2,{59,37,11}},   {  4,{36,16,10},  4,{37,17,10}},   {  4, {36,12,12},  4,{37,13,12}}},
  480.   {{  2,{ 86, 68, 9},  2,{ 87, 69, 9}},   {  4,{69,43,13},  1,{70,44,13}},   {  6,{43,19,12},  2,{44,20,12}},   {  6, {43,15,14},  2,{44,16,14}}},
  481.   {{  4,{101, 81,10}                 },   {  1,{80,50,15},  4,{81,51,15}},   {  4,{50,22,14},  4,{51,23,14}},   {  3, {36,12,12},  8,{37,13,12}}},
  482.   {{  2,{116, 92,12},  2,{117, 93,12}},   {  6,{58,36,11},  2,{59,37,11}},   {  4,{46,20,13},  6,{47,21,13}},   {  7, {42,14,14},  4,{43,15,14}}},
  483.   {{  4,{133,107,13}                 },   {  8,{59,37,11},  1,{60,38,11}},   {  8,{44,20,12},  4,{45,21,12}},   { 12, {33,11,11},  4,{34,12,11}}},
  484.   {{  3,{145,115,15},  1,{146,116,15}},   {  4,{64,40,12},  5,{65,41,12}},   { 11,{36,16,10},  5,{37,17,10}},   { 11, {36,12,12},  5,{37,13,12}}},
  485.   {{  5,{109, 87,11},  1,{110, 88,11}},   {  5,{65,41,12},  5,{66,42,12}},   {  5,{54,24,15},  7,{55,25,15}},   { 11, {36,12,12},  7,{37,13,12}}},
  486.   {{  5,{122, 98,12},  1,{123, 99,12}},   {  7,{73,45,14},  3,{74,46,14}},   { 15,{43,19,12},  2,{44,20,12}},   {  3, {45,15,15}, 13,{46,16,15}}},
  487.   {{  1,{135,107,14},  5,{136,108,14}},   { 10,{74,46,14},  1,{75,47,14}},   {  1,{50,22,14}, 15,{51,23,14}},   {  2, {42,14,14}, 17,{43,15,14}}},
  488.   {{  5,{150,120,15},  1,{151,121,15}},   {  9,{69,43,13},  4,{70,44,13}},   { 17,{50,22,14},  1,{51,23,14}},   {  2, {42,14,14}, 19,{43,15,14}}},
  489.   {{  3,{141,113,14},  4,{142,114,14}},   {  3,{70,44,13}, 11,{71,45,13}},   { 17,{47,21,13},  4,{48,22,13}},   {  9, {39,13,13}, 16,{40,14,13}}},
  490.   {{  3,{135,107,14},  5,{136,108,14}},   {  3,{67,41,13}, 13,{68,42,13}},   { 15,{54,24,15},  5,{55,25,15}},   { 15, {43,15,14}, 10,{44,16,14}}},
  491.   {{  4,{144,116,14},  4,{145,117,14}},   { 17,{68,42,13}},                  { 17,{50,22,14},  6,{51,23,14}},   { 19, {46,16,15},  6,{47,17,15}}},
  492.   {{  2,{139,111,14},  7,{140,112,14}},   { 17,{74,46,14}},                  {  7,{54,24,15}, 16,{55,25,15}},   { 34, {37,13,12}               }},
  493.   {{  4,{151,121,15},  5,{152,122,15}},   {  4,{75,47,14}, 14,{76,48,14}},   { 11,{54,24,15}, 14,{55,25,15}},   { 16, {45,15,15}, 14,{46,16,15}}},
  494.   {{  6,{147,117,15},  4,{148,118,15}},   {  6,{73,45,14}, 14,{74,46,14}},   { 11,{54,24,15}, 16,{55,25,15}},   { 30, {46,16,15},  2,{47,17,15}}},
  495.   {{  8,{132,106,13},  4,{133,107,13}},   {  8,{75,47,14}, 13,{76,48,14}},   {  7,{54,24,15}, 22,{55,25,15}},   { 22, {45,15,15}, 13,{46,16,15}}},
  496.   {{ 10,{142,114,14},  2,{143,115,14}},   { 19,{74,46,14},  4,{75,47,14}},   { 28,{50,22,14},  6,{51,23,14}},   { 33, {46,16,15},  4,{47,17,15}}},
  497.   {{  8,{152,122,15},  4,{153,123,15}},   { 22,{73,45,14},  3,{74,46,14}},   {  8,{53,23,15}, 26,{54,24,15}},   { 12, {45,15,15}, 28,{46,16,15}}},
  498.   {{  3,{147,117,15}, 10,{148,118,15}},   {  3,{73,45,14}, 23,{74,46,14}},   {  4,{54,24,15}, 31,{55,25,15}},   { 11, {45,15,15}, 31,{46,16,15}}},
  499.   {{  7,{146,116,15},  7,{147,117,15}},   { 21,{73,45,14},  7,{74,46,14}},   {  1,{53,23,15}, 37,{54,24,15}},   { 19, {45,15,15}, 26,{46,16,15}}},
  500.   {{  5,{145,115,15}, 10,{146,116,15}},   { 19,{75,47,14}, 10,{76,48,14}},   { 15,{54,24,15}, 25,{55,25,15}},   { 23, {45,15,15}, 25,{46,16,15}}},
  501.   {{ 13,{145,115,15},  3,{146,116,15}},   {  2,{74,46,14}, 29,{75,47,14}},   { 42,{54,24,15},  1,{55,25,15}},   { 23, {45,15,15}, 28,{46,16,15}}},
  502.   {{ 17,{145,115,15}                 },   { 10,{74,46,14}, 23,{75,47,14}},   { 10,{54,24,15}, 35,{55,25,15}},   { 19, {45,15,15}, 35,{46,16,15}}},
  503.   {{ 17,{145,115,15},  1,{146,116,15}},   { 14,{74,46,14}, 21,{75,47,14}},   { 29,{54,24,15}, 19,{55,25,15}},   { 11, {45,15,15}, 46,{46,16,15}}},
  504.   {{ 13,{145,115,15},  6,{146,116,15}},   { 14,{74,46,14}, 23,{75,47,14}},   { 44,{54,24,15},  7,{55,25,15}},   { 59, {46,16,15},  1,{47,17,15}}},
  505.   {{ 12,{151,121,15},  7,{152,122,15}},   { 12,{75,47,14}, 26,{76,48,14}},   { 39,{54,24,15}, 14,{55,25,15}},   { 22, {45,15,15}, 41,{46,16,15}}},
  506.   {{  6,{151,121,15}, 14,{152,122,15}},   {  6,{75,47,14}, 34,{76,48,14}},   { 46,{54,24,15}, 10,{55,25,15}},   {  2, {45,15,15}, 64,{46,16,15}}},
  507.   {{ 17,{152,122,15},  4,{153,123,15}},   { 29,{74,46,14}, 14,{75,47,14}},   { 49,{54,24,15}, 10,{55,25,15}},   { 24, {45,15,15}, 46,{46,16,15}}},
  508.   {{  4,{152,122,15}, 18,{153,123,15}},   { 13,{74,46,14}, 32,{75,47,14}},   { 48,{54,24,15}, 14,{55,25,15}},   { 42, {45,15,15}, 32,{46,16,15}}},
  509.   {{ 20,{147,117,15},  4,{148,118,15}},   { 40,{75,47,14},  7,{76,48,14}},   { 43,{54,24,15}, 22,{55,25,15}},   { 10, {45,15,15}, 67,{46,16,15}}},
  510.   {{ 19,{148,118,15},  6,{149,119,15}},   { 18,{75,47,14}, 31,{76,48,14}},   { 34,{54,24,15}, 34,{55,25,15}},   { 20, {45,15,15}, 61,{46,16,15}}}
  511. }
  512.  
  513. local remainder = {0, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0}
  514.  
  515. local function arrange_codewords_and_calculate_ec( version,ec_level,data )
  516.     if type(data)=="table" then
  517.         local tmp = ""
  518.         for i=1,#data do
  519.             tmp = tmp .. binary(data[i],8)
  520.         end
  521.         data = tmp
  522.     end
  523.     local blocks = ecblocks[version][ec_level]
  524.     local size_datablock_bytes, size_ecblock_bytes
  525.     local datablocks = {}
  526.     local ecblocks = {}
  527.     local count = 1
  528.     local pos = 0
  529.     local cpty_ec_bits = 0
  530.     for i=1,#blocks/2 do
  531.         for j=1,blocks[2*i - 1] do
  532.             size_datablock_bytes = blocks[2*i][2]
  533.             size_ecblock_bytes   = blocks[2*i][1] - blocks[2*i][2]
  534.             cpty_ec_bits = cpty_ec_bits + size_ecblock_bytes * 8
  535.             datablocks[#datablocks + 1] = string.sub(data, pos * 8 + 1,( pos + size_datablock_bytes)*8)
  536.             tmp_tab = calculate_error_correction(datablocks[#datablocks],size_ecblock_bytes)
  537.             tmp_str = ""
  538.             for x=1,#tmp_tab do
  539.                 tmp_str = tmp_str .. binary(tmp_tab[x],8)
  540.             end
  541.             ecblocks[#ecblocks + 1] = tmp_str
  542.             pos = pos + size_datablock_bytes
  543.             count = count + 1
  544.         end
  545.     end
  546.     local arranged_data = ""
  547.     pos = 1
  548.     repeat
  549.         for i=1,#datablocks do
  550.             if pos < #datablocks[i] then
  551.                 arranged_data = arranged_data .. string.sub(datablocks[i],pos, pos + 7)
  552.             end
  553.         end
  554.         pos = pos + 8
  555.     until #arranged_data == #data
  556.     -- ec
  557.     local arranged_ec = ""
  558.     pos = 1
  559.     repeat
  560.         for i=1,#ecblocks do
  561.             if pos < #ecblocks[i] then
  562.                 arranged_ec = arranged_ec .. string.sub(ecblocks[i],pos, pos + 7)
  563.             end
  564.         end
  565.         pos = pos + 8
  566.     until #arranged_ec == cpty_ec_bits
  567.     return arranged_data .. arranged_ec
  568. end
  569.  
  570. local function add_position_detection_patterns(tab_x)
  571.     local size = #tab_x
  572.     for i=1,8 do
  573.         for j=1,8 do
  574.             tab_x[i][j] = -2
  575.             tab_x[size - 8 + i][j] = -2
  576.             tab_x[i][size - 8 + j] = -2
  577.         end
  578.     end
  579.     for i=1,7 do
  580.         -- top left
  581.         tab_x[1][i]=2
  582.         tab_x[7][i]=2
  583.         tab_x[i][1]=2
  584.         tab_x[i][7]=2
  585.  
  586.         -- top right
  587.         tab_x[size][i]=2
  588.         tab_x[size - 6][i]=2
  589.         tab_x[size - i + 1][1]=2
  590.         tab_x[size - i + 1][7]=2
  591.  
  592.         -- bottom left
  593.         tab_x[1][size - i + 1]=2
  594.         tab_x[7][size - i + 1]=2
  595.         tab_x[i][size - 6]=2
  596.         tab_x[i][size]=2
  597.     end
  598.     -- draw the detection pattern (inner)
  599.     for i=1,3 do
  600.         for j=1,3 do
  601.             -- top left
  602.             tab_x[2+j][i+2]=2
  603.             -- top right
  604.             tab_x[size - j - 1][i+2]=2
  605.             -- bottom left
  606.             tab_x[2 + j][size - i - 1]=2
  607.         end
  608.     end
  609. end
  610.  
  611. local function add_timing_pattern(tab_x)
  612.     local line,col
  613.     line = 7
  614.     col = 9
  615.     for i=col,#tab_x - 8 do
  616.         if math.fmod(i,2) == 1 then
  617.             tab_x[i][line] = 2
  618.         else
  619.             tab_x[i][line] = -2
  620.         end
  621.     end
  622.     for i=col,#tab_x - 8 do
  623.         if math.fmod(i,2) == 1 then
  624.             tab_x[line][i] = 2
  625.         else
  626.             tab_x[line][i] = -2
  627.         end
  628.     end
  629. end
  630.  
  631. local alignment_pattern = {
  632.   {},{6,18},{6,22},{6,26},{6,30},{6,34}, -- 1-6
  633.   {6,22,38},{6,24,42},{6,26,46},{6,28,50},{6,30,54},{6,32,58},{6,34,62}, -- 7-13
  634.   {6,26,46,66},{6,26,48,70},{6,26,50,74},{6,30,54,78},{6,30,56,82},{6,30,58,86},{6,34,62,90}, -- 14-20
  635.   {6,28,50,72,94},{6,26,50,74,98},{6,30,54,78,102},{6,28,54,80,106},{6,32,58,84,110},{6,30,58,86,114},{6,34,62,90,118}, -- 21-27
  636.   {6,26,50,74,98 ,122},{6,30,54,78,102,126},{6,26,52,78,104,130},{6,30,56,82,108,134},{6,34,60,86,112,138},{6,30,58,86,114,142},{6,34,62,90,118,146}, -- 28-34
  637.   {6,30,54,78,102,126,150}, {6,24,50,76,102,128,154},{6,28,54,80,106,132,158},{6,32,58,84,110,136,162},{6,26,54,82,110,138,166},{6,30,58,86,114,142,170} -- 35 - 40
  638. }
  639.  
  640. local function add_alignment_pattern( tab_x )
  641.     local version = (#tab_x - 17) / 4
  642.     local ap = alignment_pattern[version]
  643.     local pos_x, pos_y
  644.     for x=1,#ap do
  645.         for y=1,#ap do
  646.             -- we must not put an alignment pattern on top of the positioning pattern
  647.             if not (x == 1 and y == 1 or x == #ap and y == 1 or x == 1 and y == #ap ) then
  648.                 pos_x = ap[x] + 1
  649.                 pos_y = ap[y] + 1
  650.                 tab_x[pos_x][pos_y] = 2
  651.                 tab_x[pos_x+1][pos_y] = -2
  652.                 tab_x[pos_x-1][pos_y] = -2
  653.                 tab_x[pos_x+2][pos_y] =  2
  654.                 tab_x[pos_x-2][pos_y] =  2
  655.                 tab_x[pos_x  ][pos_y - 2] = 2
  656.                 tab_x[pos_x+1][pos_y - 2] = 2
  657.                 tab_x[pos_x-1][pos_y - 2] = 2
  658.                 tab_x[pos_x+2][pos_y - 2] = 2
  659.                 tab_x[pos_x-2][pos_y - 2] = 2
  660.                 tab_x[pos_x  ][pos_y + 2] = 2
  661.                 tab_x[pos_x+1][pos_y + 2] = 2
  662.                 tab_x[pos_x-1][pos_y + 2] = 2
  663.                 tab_x[pos_x+2][pos_y + 2] = 2
  664.                 tab_x[pos_x-2][pos_y + 2] = 2
  665.  
  666.                 tab_x[pos_x  ][pos_y - 1] = -2
  667.                 tab_x[pos_x+1][pos_y - 1] = -2
  668.                 tab_x[pos_x-1][pos_y - 1] = -2
  669.                 tab_x[pos_x+2][pos_y - 1] =  2
  670.                 tab_x[pos_x-2][pos_y - 1] =  2
  671.                 tab_x[pos_x  ][pos_y + 1] = -2
  672.                 tab_x[pos_x+1][pos_y + 1] = -2
  673.                 tab_x[pos_x-1][pos_y + 1] = -2
  674.                 tab_x[pos_x+2][pos_y + 1] =  2
  675.                 tab_x[pos_x-2][pos_y + 1] =  2
  676.             end
  677.         end
  678.     end
  679. end
  680.  
  681. local typeinfo = {
  682.     { [-1]= "111111111111111", [0] = "111011111000100", "111001011110011", "111110110101010", "111100010011101", "110011000101111", "110001100011000", "110110001000001", "110100101110110" },
  683.     { [-1]= "111111111111111", [0] = "101010000010010", "101000100100101", "101111001111100", "101101101001011", "100010111111001", "100000011001110", "100111110010111", "100101010100000" },
  684.     { [-1]= "111111111111111", [0] = "011010101011111", "011000001101000", "011111100110001", "011101000000110", "010010010110100", "010000110000011", "010111011011010", "010101111101101" },
  685.     { [-1]= "111111111111111", [0] = "001011010001001", "001001110111110", "001110011100111", "001100111010000", "000011101100010", "000001001010101", "000110100001100", "000100000111011" }
  686. }
  687.  
  688. local function add_typeinfo_to_matrix( matrix,ec_level,mask )
  689.     local ec_mask_type = typeinfo[ec_level][mask]
  690.  
  691.     local bit
  692.     -- vertical from bottom to top
  693.     for i=1,7 do
  694.         bit = string.sub(ec_mask_type,i,i)
  695.         fill_matrix_position(matrix, bit, 9, #matrix - i + 1)
  696.     end
  697.     for i=8,9 do
  698.         bit = string.sub(ec_mask_type,i,i)
  699.         fill_matrix_position(matrix,bit,9,17-i)
  700.     end
  701.     for i=10,15 do
  702.         bit = string.sub(ec_mask_type,i,i)
  703.         fill_matrix_position(matrix,bit,9,16 - i)
  704.     end
  705.     -- horizontal, left to right
  706.     for i=1,6 do
  707.         bit = string.sub(ec_mask_type,i,i)
  708.         fill_matrix_position(matrix,bit,i,9)
  709.     end
  710.     bit = string.sub(ec_mask_type,7,7)
  711.     fill_matrix_position(matrix,bit,8,9)
  712.     for i=8,15 do
  713.         bit = string.sub(ec_mask_type,i,i)
  714.         fill_matrix_position(matrix,bit,#matrix - 15 + i,9)
  715.     end
  716. end
  717.  
  718. local version_information = {"001010010011111000", "000111101101000100", "100110010101100100","011001011001010100",
  719.   "011011111101110100", "001000110111001100", "111000100001101100", "010110000011011100", "000101001001111100",
  720.   "000111101101000010", "010111010001100010", "111010000101010010", "001001100101110010", "011001011001001010",
  721.   "011000001011101010", "100100110001011010", "000110111111111010", "001000110111000110", "000100001111100110",
  722.   "110101011111010110", "000001110001110110", "010110000011001110", "001111110011101110", "101011101011011110",
  723.   "000000101001111110", "101010111001000001", "000001111011100001", "010111010001010001", "011111001111110001",
  724.   "110100001101001001", "001110100001101001", "001001100101011001", "010000010101111001", "100101100011000101" }
  725.  
  726. local function add_version_information(matrix,version)
  727.     if version < 7 then return end
  728.     local size = #matrix
  729.     local bitstring = version_information[version - 6]
  730.     local x,y, bit
  731.     local start_x, start_y
  732.     -- first top right
  733.     start_x = #matrix - 10
  734.     start_y = 1
  735.     for i=1,#bitstring do
  736.         bit = string.sub(bitstring,i,i)
  737.         x = start_x + math.fmod(i - 1,3)
  738.         y = start_y + math.floor( (i - 1) / 3 )
  739.         fill_matrix_position(matrix,bit,x,y)
  740.     end
  741.  
  742.     start_x = 1
  743.     start_y = #matrix - 10
  744.     for i=1,#bitstring do
  745.         bit = string.sub(bitstring,i,i)
  746.         x = start_x + math.floor( (i - 1) / 3 )
  747.         y = start_y + math.fmod(i - 1,3)
  748.         fill_matrix_position(matrix,bit,x,y)
  749.     end
  750. end
  751.  
  752. local function prepare_matrix_with_mask( version,ec_level, mask )
  753.     local size
  754.     local tab_x = {}
  755.  
  756.     size = version * 4 + 17
  757.     for i=1,size do
  758.         tab_x[i]={}
  759.         for j=1,size do
  760.             tab_x[i][j] = 0
  761.         end
  762.     end
  763.     add_position_detection_patterns(tab_x)
  764.     add_timing_pattern(tab_x)
  765.     add_version_information(tab_x,version)
  766.  
  767.     -- black pixel above lower left position detection pattern
  768.     tab_x[9][size - 7] = 2
  769.     add_alignment_pattern(tab_x)
  770.     add_typeinfo_to_matrix(tab_x,ec_level, mask)
  771.     return tab_x
  772. end
  773.  
  774. local function get_pixel_with_mask( mask, x,y,value )
  775.     x = x - 1
  776.     y = y - 1
  777.     local invert = false
  778.     -- test purpose only:
  779.     if mask == -1 then
  780.         -- ignore, no masking applied
  781.     elseif mask == 0 then
  782.         if math.fmod(x + y,2) == 0 then invert = true end
  783.     elseif mask == 1 then
  784.         if math.fmod(y,2) == 0 then invert = true end
  785.     elseif mask == 2 then
  786.         if math.fmod(x,3) == 0 then invert = true end
  787.     elseif mask == 3 then
  788.         if math.fmod(x + y,3) == 0 then invert = true end
  789.     elseif mask == 4 then
  790.         if math.fmod(math.floor(y / 2) + math.floor(x / 3),2) == 0 then invert = true end
  791.     elseif mask == 5 then
  792.         if math.fmod(x * y,2) + math.fmod(x * y,3) == 0 then invert = true end
  793.     elseif mask == 6 then
  794.         if math.fmod(math.fmod(x * y,2) + math.fmod(x * y,3),2) == 0 then invert = true end
  795.     elseif mask == 7 then
  796.         if math.fmod(math.fmod(x * y,3) + math.fmod(x + y,2),2) == 0 then invert = true end
  797.     else
  798.         assert(false,"This can't happen (mask must be <= 7)")
  799.     end
  800.     if invert then
  801.         -- value = 1? -> -1, value = 0? -> 1
  802.         return 1 - 2 * tonumber(value)
  803.     else
  804.         -- value = 1? -> 1, value = 0? -> -1
  805.         return -1 + 2*tonumber(value)
  806.     end
  807. end
  808.  
  809. function get_next_free_positions(matrix,x,y,dir,byte)
  810.     local ret = {}
  811.     local count = 1
  812.     local mode = "right"
  813.     while count <= #byte do
  814.         if mode == "right" and matrix[x][y] == 0 then
  815.             ret[#ret + 1] = {x,y}
  816.             mode = "left"
  817.             count = count + 1
  818.         elseif mode == "left" and matrix[x-1][y] == 0 then
  819.             ret[#ret + 1] = {x-1,y}
  820.             mode = "right"
  821.             count = count + 1
  822.             if dir == "up" then
  823.                 y = y - 1
  824.             else
  825.                 y = y + 1
  826.             end
  827.         elseif mode == "right" and matrix[x-1][y] == 0 then
  828.             ret[#ret + 1] = {x-1,y}
  829.             count = count + 1
  830.             if dir == "up" then
  831.                 y = y - 1
  832.             else
  833.                 y = y + 1
  834.             end
  835.         else
  836.             if dir == "up" then
  837.                 y = y - 1
  838.             else
  839.                 y = y + 1
  840.             end
  841.         end
  842.         if y < 1 or y > #matrix then
  843.             x = x - 2
  844.             -- don't overwrite the timing pattern
  845.             if x == 7 then x = 6 end
  846.             if dir == "up" then
  847.                 dir = "down"
  848.                 y = 1
  849.             else
  850.                 dir = "up"
  851.                 y = #matrix
  852.             end
  853.         end
  854.     end
  855.     return ret,x,y,dir
  856. end
  857.  
  858. local function add_data_to_matrix(matrix,data,mask)
  859.     size = #matrix
  860.     local x,y,positions
  861.     local _x,_y,m
  862.     local dir = "up"
  863.     local byte_number = 0
  864.     x,y = size,size
  865.     string.gsub(data,".?.?.?.?.?.?.?.?",function ( byte )
  866.         byte_number = byte_number + 1
  867.         positions,x,y,dir = get_next_free_positions(matrix,x,y,dir,byte,mask)
  868.         for i=1,#byte do
  869.             _x = positions[i][1]
  870.             _y = positions[i][2]
  871.             m = get_pixel_with_mask(mask,_x,_y,string.sub(byte,i,i))
  872.             if debugging then
  873.                 matrix[_x][_y] = m * (i + 10)
  874.             else
  875.                 matrix[_x][_y] = m
  876.             end
  877.         end
  878.     end)
  879. end
  880.  
  881. local function calculate_penalty(matrix)
  882.     local penalty1, penalty2, penalty3, penalty4 = 0,0,0,0
  883.     local size = #matrix
  884.     -- this is for penalty 4
  885.     local number_of_dark_cells = 0
  886.  
  887.     -- 1: Adjacent modules in row/column in same color
  888.     -- --------------------------------------------
  889.     -- No. of modules = (5+i)  -> 3 + i
  890.     local last_bit_blank -- < 0:  blank, > 0: black
  891.     local is_blank
  892.     local number_of_consecutive_bits
  893.     -- first: vertical
  894.     for x=1,size do
  895.         number_of_consecutive_bits = 0
  896.         last_bit_blank = nil
  897.         for y = 1,size do
  898.             if matrix[x][y] > 0 then
  899.                 -- small optimization: this is for penalty 4
  900.                 number_of_dark_cells = number_of_dark_cells + 1
  901.                 is_blank = false
  902.             else
  903.                 is_blank = true
  904.             end
  905.             is_blank = matrix[x][y] < 0
  906.             if last_bit_blank == is_blank then
  907.                 number_of_consecutive_bits = number_of_consecutive_bits + 1
  908.             else
  909.                 if number_of_consecutive_bits >= 5 then
  910.                     penalty1 = penalty1 + number_of_consecutive_bits - 2
  911.                 end
  912.                 number_of_consecutive_bits = 1
  913.             end
  914.             last_bit_blank = is_blank
  915.         end
  916.         if number_of_consecutive_bits >= 5 then
  917.             penalty1 = penalty1 + number_of_consecutive_bits - 2
  918.         end
  919.     end
  920.     -- now horizontal
  921.     for y=1,size do
  922.         number_of_consecutive_bits = 0
  923.         last_bit_blank = nil
  924.         for x = 1,size do
  925.             is_blank = matrix[x][y] < 0
  926.             if last_bit_blank == is_blank then
  927.                 number_of_consecutive_bits = number_of_consecutive_bits + 1
  928.             else
  929.                 if number_of_consecutive_bits >= 5 then
  930.                     penalty1 = penalty1 + number_of_consecutive_bits - 2
  931.                 end
  932.                 number_of_consecutive_bits = 1
  933.             end
  934.             last_bit_blank = is_blank
  935.         end
  936.         if number_of_consecutive_bits >= 5 then
  937.             penalty1 = penalty1 + number_of_consecutive_bits - 2
  938.         end
  939.     end
  940.     for x=1,size do
  941.         for y=1,size do
  942.             if (y < size - 1) and ( x < size - 1) and ( (matrix[x][y] < 0 and matrix[x+1][y] < 0 and matrix[x][y+1] < 0 and matrix[x+1][y+1] < 0) or (matrix[x][y] > 0 and matrix[x+1][y] > 0 and matrix[x][y+1] > 0 and matrix[x+1][y+1] > 0) ) then
  943.                 penalty2 = penalty2 + 3
  944.             end
  945.            
  946.             if (y + 6 < size and
  947.                 matrix[x][y] > 0 and
  948.                 matrix[x][y +  1] < 0 and
  949.                 matrix[x][y +  2] > 0 and
  950.                 matrix[x][y +  3] > 0 and
  951.                 matrix[x][y +  4] > 0 and
  952.                 matrix[x][y +  5] < 0 and
  953.                 matrix[x][y +  6] > 0 and
  954.                 ((y + 10 < size and
  955.                     matrix[x][y +  7] < 0 and
  956.                     matrix[x][y +  8] < 0 and
  957.                     matrix[x][y +  9] < 0 and
  958.                     matrix[x][y + 10] < 0) or
  959.                  (y - 4 >= 1 and
  960.                     matrix[x][y -  1] < 0 and
  961.                     matrix[x][y -  2] < 0 and
  962.                     matrix[x][y -  3] < 0 and
  963.                     matrix[x][y -  4] < 0))) then penalty3 = penalty3 + 40 end
  964.             if (x + 6 <= size and
  965.                 matrix[x][y] > 0 and
  966.                 matrix[x +  1][y] < 0 and
  967.                 matrix[x +  2][y] > 0 and
  968.                 matrix[x +  3][y] > 0 and
  969.                 matrix[x +  4][y] > 0 and
  970.                 matrix[x +  5][y] < 0 and
  971.                 matrix[x +  6][y] > 0 and
  972.                 ((x + 10 <= size and
  973.                     matrix[x +  7][y] < 0 and
  974.                     matrix[x +  8][y] < 0 and
  975.                     matrix[x +  9][y] < 0 and
  976.                     matrix[x + 10][y] < 0) or
  977.                  (x - 4 >= 1 and
  978.                     matrix[x -  1][y] < 0 and
  979.                     matrix[x -  2][y] < 0 and
  980.                     matrix[x -  3][y] < 0 and
  981.                     matrix[x -  4][y] < 0))) then penalty3 = penalty3 + 40 end
  982.         end
  983.     end
  984.     local dark_ratio = number_of_dark_cells / ( size * size )
  985.     penalty4 = math.floor(math.abs(dark_ratio * 100 - 50)) * 2
  986.     return penalty1 + penalty2 + penalty3 + penalty4
  987. end
  988.  
  989. local function get_matrix_and_penalty(version,ec_level,data,mask)
  990.     local tab = prepare_matrix_with_mask(version,ec_level,mask)
  991.     add_data_to_matrix(tab,data,mask)
  992.     local penalty = calculate_penalty(tab)
  993.     return tab, penalty
  994. end
  995.  
  996. local function get_matrix_with_lowest_penalty(version,ec_level,data)
  997.     local tab, penalty
  998.     local tab_min_penalty, min_penalty
  999.  
  1000.     tab_min_penalty, min_penalty = get_matrix_and_penalty(version,ec_level,data,0)
  1001.     for i=1,7 do
  1002.         tab, penalty = get_matrix_and_penalty(version,ec_level,data,i)
  1003.         if penalty < min_penalty then
  1004.             tab_min_penalty = tab
  1005.             min_penalty = penalty
  1006.         end
  1007.     end
  1008.     return tab_min_penalty
  1009. end
  1010.  
  1011. local function qrcode( str, ec_level, mode )
  1012.     local arranged_data, version, ec_level, data_raw, mode, len_bitstring
  1013.     version, ec_level, data_raw, mode, len_bitstring = get_version_eclevel_mode_bistringlength(str)
  1014.     data_raw = data_raw .. len_bitstring
  1015.     data_raw = data_raw .. encode_data(str,mode)
  1016.     data_raw = add_pad_data(version,ec_level,data_raw)
  1017.     arranged_data = arrange_codewords_and_calculate_ec(version,ec_level,data_raw)
  1018.     if math.fmod(#arranged_data,8) ~= 0 then
  1019.         return false, string.format("Arranged data %% 8 != 0: data length = %d, mod 8 = %d",#arranged_data, math.fmod(#arranged_data,8))
  1020.     end
  1021.     arranged_data = arranged_data .. string.rep("0",remainder[version])
  1022.     local tab = get_matrix_with_lowest_penalty(version,ec_level,arranged_data)
  1023.     return true, tab
  1024. end
  1025.  
  1026. local function prepareMatrix( tab )
  1027.     local mx = {}
  1028.    
  1029.     for i = 1, #tab + 2 do
  1030.         mx[i] = {}
  1031.     end
  1032.    
  1033.     for i=1,#tab + 2 do
  1034.         mx[i][1] = 0 --left
  1035.         mx[1][i] = 0 --top
  1036.         mx[i][#tab + 2] = 0 --right
  1037.         mx[#tab + 2][i] = 0 --bottom
  1038.     end
  1039.  
  1040.     for y=1,#tab do
  1041.         for x=1,#tab do
  1042.             if tab[x][y] > 0 then
  1043.                 mx[y + 1][x + 1] = 1
  1044.             else
  1045.                 mx[y + 1][x + 1] = 0
  1046.             end
  1047.         end
  1048.     end
  1049.  
  1050.     return mx
  1051. end
  1052.  
  1053. function qr.encode(codeword)
  1054.     local mx
  1055.     local ok, tab_or_message = qrcode(codeword)
  1056.     if not ok then
  1057.         print(tab_or_message)
  1058.     else
  1059.         mx = prepareMatrix(tab_or_message)
  1060.     end
  1061.     return mx
  1062. end
  1063.  
  1064. function qr.printHalf(data)
  1065.     local term = require("term")
  1066.     local tmp
  1067.     for i = 1, #data, 2 do
  1068.         for j = 1, #data do
  1069.             if i+1 > #data then
  1070.                 tmp = nil
  1071.             else
  1072.                 tmp = data[i+1][j]
  1073.             end
  1074.    
  1075.             if data[i][j] == tmp or tmp == nil then
  1076.                 if data[i][j] > 0 then
  1077.                     term.write(" ")
  1078.                 else
  1079.                     term.write("█")
  1080.                 end
  1081.             elseif data[i][j] > tmp then
  1082.                 term.write("▄")
  1083.             else
  1084.                 term.write("▀")
  1085.             end
  1086.         end
  1087.     print()
  1088.     end
  1089. end
  1090.  
  1091.  
  1092. return qr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement