Constant.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. 'use strict';
  16. var Crypto = require('crypto');
  17. var seed = process.env.SOURCE_DATE_EPOCH || Crypto.randomBytes(32).toString('hex');
  18. if (process.env.SOURCE_DATE_EPOCH) { console.log('\n\n\nYES\n\n\n');}
  19. var TABLE = {
  20. '0000': '0',
  21. '0001': '1',
  22. '0010': '2',
  23. '0011': '3',
  24. '0100': '4',
  25. '0101': '5',
  26. '0110': '6',
  27. '0111': '7',
  28. '1000': '8',
  29. '1001': '9',
  30. '1010': 'a',
  31. '1011': 'b',
  32. '1100': 'c',
  33. '1101': 'd',
  34. '1110': 'e',
  35. '1111': 'f'
  36. };
  37. var base2ToHex = function (numStr) {
  38. while ((numStr.length / 4) % 1) { numStr = '0' + numStr; }
  39. var out = [];
  40. for (var i = 0; i < numStr.length; i += 4) {
  41. out.push(TABLE[numStr.substring(i, i+4)]);
  42. }
  43. return out.join('');
  44. };
  45. var testBase2 = function () {
  46. for (var i = 0; i < 10000; i++) {
  47. var num = Math.random() * 4294967296;
  48. var b2 = num.toString(2);
  49. var numB = Number('0x' + base2ToHex(b2));
  50. if (num !== numB) { throw new Error(num + " --> " + b2 + " --> " + numB); }
  51. }
  52. };
  53. var base2 = module.exports.base2 = function (numStr) {
  54. var type = "uint8_t";
  55. if (numStr.length > 8) { type = "uint16_t"; }
  56. if (numStr.length > 16) { type = "uint32_t"; }
  57. if (numStr.length > 32) { type = "uint64_t"; }
  58. if (numStr.length > 64) { throw new Error("cannot have more than 64 bits"); }
  59. return '((' + type + ') 0x' + base2ToHex(numStr) + ((type === 'uint64_t') ? 'ull' : 'ul') + ')';
  60. };
  61. var randomHex = function (bytes, self, fileName) {
  62. var nonce = self.Constant_JS_nonce = (self.Constant_JS_nonce || 0) + 1;
  63. var material = new Crypto.Hash('sha512').update(seed).update(fileName).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 (self, fileName) {
  68. return '((uint64_t) 0x' + randomHex(64 / 8, self, fileName) + 'ull)';
  69. };
  70. var rand32 = module.exports.rand32 = function (self, fileName) {
  71. return '((uint32_t) 0x' + randomHex(32 / 8, self, fileName) + 'ul)';
  72. };
  73. var randHexString = module.exports.randHexString = function (lenStr, self, fileName) {
  74. return '"' + randomHex(lenStr / 2, self, fileName) + '"';
  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. }