Advertisement
H3draut3r

generate_IP_from_string

Dec 8th, 2021
1,277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let result = []; // init
  2. // functions
  3. // if given string is 12 or 4 numbers long, the result is only 1 string
  4. function singleresult(string, length) {
  5.     let convertIncomplete = true;
  6.     let tempResult = [];
  7.     while (convertIncomplete) {
  8.         let tempConcat = string.splice(0, length / 4).join("")
  9.         tempConcat.concat(".");
  10.         tempResult.push(tempConcat);
  11.         if (string.length == 0) {
  12.             convertIncomplete = false
  13.         }
  14.     }
  15.     return tempResult.join(".")
  16. }
  17. // for possible multi-results
  18. function multipleResults(data) {
  19.     let i, j, k;
  20.     i = j = k = 1;
  21.     let tempResult = [];  // save correct arrays here
  22.     let tempCache = []; // for temporary check on result
  23.  
  24.     while (i < 4) {
  25.         var tempData = JSON.parse(JSON.stringify(data));
  26.         if (tempData.length - i > 9) {
  27.             i++
  28.         }
  29.         if (tempData.length - i - j > 6) {
  30.             j++
  31.         }
  32.         if (tempData.length - i - j - k > 3) {
  33.             k++
  34.         }
  35.         else {
  36.             let partString1 = tempData.splice(0, i).join("");
  37.             if (tempData.length >= 3) {
  38.                 let partString2 = tempData.splice(0, j);
  39.                 if (partString2[0] != 0 && tempData.length >= 2) {
  40.                     partString2 = partString2.join("")
  41.                     let partString3 = tempData.splice(0, k);
  42.                     if (partString3[0] != 0 && tempData.length >= 1) {
  43.                         partString3 = partString3.join("");
  44.                         if (tempData[0] != 0) {
  45.                             let partString4 = tempData.join("");
  46.                             if (partString1 >= 0 && partString1 <= 255) {
  47.                                 if (partString2 >= 0 && partString2 <= 255) {
  48.                                     if (partString3 >= 0 && partString3 <= 255) {
  49.                                         if (partString4 >= 0 && partString4 <= 255) {
  50.                                             tempCache.push(partString1, partString2, partString3, partString4); // create an array of that result
  51.                                             tempResult.push(tempCache); // save the current result into a bigger list
  52.                                             tempCache = []
  53.                                         }
  54.                                     }
  55.                                 }
  56.                             }
  57.                         }
  58.                     }
  59.                 }
  60.             }
  61.             // increment group 1-3 from back to front (ending with increment to i==4, which ends the while-loop)
  62.             if (k == 3) {
  63.                 if (j == 3) {
  64.                     if (i == 3) {
  65.                         i = 4
  66.                     }
  67.                     else {
  68.                         i++;
  69.                         j = 1;
  70.                         k = 1
  71.                     }
  72.                 }
  73.                 else {
  74.                     j++
  75.                     k = 1
  76.                 }
  77.             }
  78.             else {
  79.                 k++
  80.             }
  81.         }
  82.     }
  83.     for (var temp of tempResult) {
  84.         result.push(temp.join("."))
  85.     }
  86. }
  87.  
  88. let data = 154137641; // <<<<<< DATA
  89.  
  90.  
  91. let dataArray = Array.from(String(data), Number);
  92.  
  93. if (dataArray.length == 12 || dataArray.length == 4) {
  94.     result.push(singleresult(dataArray, dataArray.length))
  95. }
  96. else {
  97.     multipleResults(dataArray)
  98. }
  99. // run in node, or use Quokka.js on VSCODE
  100. console.log(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement