splice 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env node
  2. /* -*- Mode:js */
  3. /*
  4. * You may redistribute this program and/or modify it under the terms of
  5. * the GNU General Public License as published by the Free Software Foundation,
  6. * either version 3 of the License, or (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. var labelToBits = function (label) {
  17. var out = [];
  18. label.split('.').reverse().forEach(function (sec) {
  19. var n = Number("0x" + sec);
  20. for (var i = 0; i < 16; i++) {
  21. out.unshift((n & 1) ? 1 : 0);
  22. n >>= 1;
  23. }
  24. });
  25. return out;
  26. };
  27. var bitsToChar = function (array) {
  28. var num = 0;
  29. for (var i = 0; i < 4; i++) {
  30. num |= (array.pop() << i);
  31. }
  32. return num.toString(16);
  33. };
  34. var bitsToLabel = function (array) {
  35. var chars = [];
  36. for (var i = 0; i < 16; i++) {
  37. if ((i % 4) === 0) { chars.unshift('.'); }
  38. chars.unshift(bitsToChar(array));
  39. }
  40. chars.pop();
  41. return chars.join('');
  42. };
  43. var randLabel = function () {
  44. var out = [];
  45. for (var i = 0; i < 4; i++) {
  46. var x = Math.random().toString(16);
  47. if (x.length < 6) { i--; continue; }
  48. out.push(x.substring(x.length-4));
  49. }
  50. return out.join('.');
  51. }
  52. var test = function () {
  53. for (var i = 0; i < 1000; i++) {
  54. var x = randLabel();
  55. if (bitsToLabel(labelToBits(x)) !== x) {
  56. throw new Error(x);
  57. }
  58. }
  59. };
  60. test();
  61. var errorArray = function () {
  62. var out = [];
  63. for (var i = 0; i < 64; i++) { out.push(1); }
  64. return out;
  65. };
  66. var splice = function (goHere, viaHere) {
  67. while (viaHere.shift() === 0) ;
  68. goHere.push.apply(goHere, viaHere);
  69. while (goHere.shift() === 0) ;
  70. goHere.unshift(1);
  71. if (goHere.length >= 63) { return errorArray(); }
  72. while (goHere.length < 64) { goHere.unshift(0); }
  73. return goHere;
  74. };
  75. if (process.argv.length > 2) {
  76. var viaHereL = process.argv.pop();
  77. var goHereL = process.argv.pop();
  78. var result = splice(labelToBits(goHereL), labelToBits(viaHereL));
  79. console.log(bitsToLabel(result));
  80. }
  81. /*
  82. var splice = function (goHere, viaHere) {
  83. {
  84. uint64_t log2ViaHere = Bits_log2x64(viaHere);
  85. if (Bits_log2x64(goHere) + log2ViaHere > 59) {
  86. // Too big, can't splice.
  87. return UINT64_MAX;
  88. }
  89. return ((goHere ^ 1) << log2ViaHere) ^ viaHere;
  90. }
  91. */