1
0

Constant.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. var Crypto = require('crypto');
  16. var seed = process.env.SOURCE_DATE_EPOCH || Crypto.randomBytes(32).toString('hex');
  17. if (process.env.SOURCE_DATE_EPOCH) { console.log('\n\n\nYES\n\n\n');}
  18. var TABLE = {
  19. '0000': '0',
  20. '0001': '1',
  21. '0010': '2',
  22. '0011': '3',
  23. '0100': '4',
  24. '0101': '5',
  25. '0110': '6',
  26. '0111': '7',
  27. '1000': '8',
  28. '1001': '9',
  29. '1010': 'a',
  30. '1011': 'b',
  31. '1100': 'c',
  32. '1101': 'd',
  33. '1110': 'e',
  34. '1111': 'f'
  35. };
  36. var base2ToHex = function (numStr) {
  37. while ((numStr.length / 4) % 1) { numStr = '0' + numStr; }
  38. var out = [];
  39. for (var i = 0; i < numStr.length; i += 4) {
  40. out.push(TABLE[numStr.substring(i, i+4)]);
  41. }
  42. return out.join('');
  43. };
  44. var testBase2 = function () {
  45. for (var i = 0; i < 10000; i++) {
  46. var num = Math.random() * 4294967296;
  47. var b2 = num.toString(2);
  48. var numB = Number('0x' + base2ToHex(b2));
  49. if (num !== numB) { throw new Error(num + " --> " + b2 + " --> " + numB); }
  50. }
  51. };
  52. var base2 = module.exports.base2 = function (numStr) {
  53. var type = "uint8_t";
  54. if (numStr.length > 8) { type = "uint16_t"; }
  55. if (numStr.length > 16) { type = "uint32_t"; }
  56. if (numStr.length > 32) { type = "uint64_t"; }
  57. if (numStr.length > 64) { throw new Error("cannot have more than 64 bits"); }
  58. return '((' + type + ') 0x' + base2ToHex(numStr) + ((type === 'uint64_t') ? 'ull' : 'ul') + ')';
  59. };
  60. var randomHex = function (bytes, file) {
  61. if (!file) { throw new Error('file unspecified'); }
  62. var nonce = file.Constant_JS_nonce = (file.Constant_JS_nonce || 0) + 1;
  63. var material = new Crypto.Hash('sha512').update(seed).update(file.name).update(String(nonce)).digest();
  64. if (bytes > 64) { throw new Error("meh, randomHex of over 64 bytes is unimplemented"); }
  65. return material.slice(0, bytes).toString('hex');
  66. };
  67. var rand64 = module.exports.rand64 = function (file) {
  68. return '((uint64_t) 0x' + randomHex(64 / 8, file) + 'ull)';
  69. };
  70. var rand32 = module.exports.rand32 = function (file) {
  71. return '((uint32_t) 0x' + randomHex(32 / 8, file) + 'ul)';
  72. };
  73. var randHexString = module.exports.randHexString = function (lenStr, file) {
  74. return '"' + randomHex(lenStr / 2, file) + '"';
  75. };
  76. var log2 = module.exports.log2 = function (val) {
  77. var x = 1;
  78. for (var i = 0; i < 31; i++) {
  79. if (x === val) {
  80. if ((1 << i) !== val) { throw new Error(); }
  81. return i;
  82. }
  83. x = x + x;
  84. }
  85. throw new Error("not an even power of 2");
  86. };
  87. var stringForHex = module.exports.stringForHex = function (hex) {
  88. var out = [];
  89. for (var i = 0; i < hex.length; i+=2) {
  90. out.push(hex[i] + hex[i+1]);
  91. }
  92. if (out.length < 2) { throw new Error(); }
  93. return '"\\x' + out.join('\\x') + '"';
  94. };
  95. if (!module.parent) {
  96. console.log("testing " + __filename);
  97. testBase2();
  98. }