array.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. local String = require("string");
  2. local Bit = require("lockbox.util.bit");
  3. local Queue = require("lockbox.util.queue");
  4. local XOR = Bit.bxor;
  5. local Array = {};
  6. Array.size = function(array)
  7. return #array;
  8. end
  9. Array.fromString = function(string)
  10. local bytes = {};
  11. local i = 1;
  12. local byte = String.byte(string, i);
  13. while byte ~= nil do
  14. bytes[i] = byte;
  15. i = i + 1;
  16. byte = String.byte(string, i);
  17. end
  18. return bytes;
  19. end
  20. Array.toString = function(bytes)
  21. local chars = {};
  22. local i = 1;
  23. local byte = bytes[i];
  24. while byte ~= nil do
  25. chars[i] = String.char(byte);
  26. i = i + 1;
  27. byte = bytes[i];
  28. end
  29. return table.concat(chars, "");
  30. end
  31. Array.fromStream = function(stream)
  32. local array = {};
  33. local i = 1;
  34. local byte = stream();
  35. while byte ~= nil do
  36. array[i] = byte;
  37. i = i + 1;
  38. byte = stream();
  39. end
  40. return array;
  41. end
  42. Array.readFromQueue = function(queue, size)
  43. local array = {};
  44. for i = 1, size do
  45. array[i] = queue.pop();
  46. end
  47. return array;
  48. end
  49. Array.writeToQueue = function(queue, array)
  50. local size = Array.size(array);
  51. for i = 1, size do
  52. queue.push(array[i]);
  53. end
  54. end
  55. Array.toStream = function(array)
  56. local queue = Queue();
  57. local i = 1;
  58. local byte = array[i];
  59. while byte ~= nil do
  60. queue.push(byte);
  61. i = i + 1;
  62. byte = array[i];
  63. end
  64. return queue.pop;
  65. end
  66. local fromHexTable = {};
  67. for i = 0, 255 do
  68. fromHexTable[String.format("%02X", i)] = i;
  69. fromHexTable[String.format("%02x", i)] = i;
  70. end
  71. Array.fromHex = function(hex)
  72. local array = {};
  73. for i = 1, String.len(hex) / 2 do
  74. local h = String.sub(hex, i * 2 - 1, i * 2);
  75. array[i] = fromHexTable[h];
  76. end
  77. return array;
  78. end
  79. local toHexTable = {};
  80. for i = 0, 255 do
  81. toHexTable[i] = String.format("%02X", i);
  82. end
  83. Array.toHex = function(array)
  84. local hex = {};
  85. local i = 1;
  86. local byte = array[i];
  87. while byte ~= nil do
  88. hex[i] = toHexTable[byte];
  89. i = i + 1;
  90. byte = array[i];
  91. end
  92. return table.concat(hex, "");
  93. end
  94. Array.concat = function(a, b)
  95. local concat = {};
  96. local out = 1;
  97. local i = 1;
  98. local byte = a[i];
  99. while byte ~= nil do
  100. concat[out] = byte;
  101. i = i + 1;
  102. out = out + 1;
  103. byte = a[i];
  104. end
  105. i = 1;
  106. byte = b[i];
  107. while byte ~= nil do
  108. concat[out] = byte;
  109. i = i + 1;
  110. out = out + 1;
  111. byte = b[i];
  112. end
  113. return concat;
  114. end
  115. Array.truncate = function(a, newSize)
  116. local x = {};
  117. for i = 1, newSize do
  118. x[i] = a[i];
  119. end
  120. return x;
  121. end
  122. Array.XOR = function(a, b)
  123. local x = {};
  124. for k, v in pairs(a) do
  125. x[k] = XOR(v, b[k]);
  126. end
  127. return x;
  128. end
  129. Array.substitute = function(input, sbox)
  130. local out = {};
  131. for k, v in pairs(input) do
  132. out[k] = sbox[v];
  133. end
  134. return out;
  135. end
  136. Array.permute = function(input, pbox)
  137. local out = {};
  138. for k, v in pairs(pbox) do
  139. out[k] = input[v];
  140. end
  141. return out;
  142. end
  143. Array.copy = function(input)
  144. local out = {};
  145. for k, v in pairs(input) do
  146. out[k] = v;
  147. end
  148. return out;
  149. end
  150. Array.slice = function(input, start, stop)
  151. local out = {};
  152. for i = start, stop do
  153. out[i - start + 1] = input[i];
  154. end
  155. return out;
  156. end
  157. return Array;