Coder Social home page Coder Social logo

This is a nice resource! about pskracker HOT 1 OPEN

kimocoder avatar kimocoder commented on September 25, 2024
This is a nice resource!

from pskracker.

Comments (1)

kimocoder avatar kimocoder commented on September 25, 2024

And this is what Telenor (Scandinavia) uses to validate and generate. OEM on their routers is... Zyxel

function isHexaDigit(digit) {

var hexVals = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"A", "B", "C", "D", "E", "F", "a", "b", "c", "d", "e", "f");
var len = hexVals.length;
var i = 0;
var ret = false;

for ( i = 0; i < len; i++ )
if ( digit == hexVals[i] ) break;

if ( i < len )
ret = true;

return ret;
}

function isValidKey(val, size) {
var ret = false;
var len = val.length;
var dbSize = size * 2;

if ( len == size )
ret = true;
else if ( len == dbSize ) {
for ( i = 0; i < dbSize; i++ )
if ( isHexaDigit(val.charAt(i)) == false )
break;
if ( i == dbSize )
ret = true;
} else
ret = false;

return ret;
}

function isValidHexKey(val, size) {
var ret = false;
if (val.length == size) {
for ( i = 0; i < val.length; i++ ) {
if ( isHexaDigit(val.charAt(i)) == false ) {
break;
}
}
if ( i == val.length ) {
ret = true;
}
}

return ret;
}

function isNameUnsafe(compareChar) {
var unsafeString = ""<>%\^[]`+$,='#&@.: \t";

if ( unsafeString.indexOf(compareChar) == -1 && compareChar.charCodeAt(0) > 32
&& compareChar.charCodeAt(0) < 123 )
return false; // found no unsafe chars, return false
else
return true;
}

// Check if a name valid
function isValidName(name) {
var i = 0;

for ( i = 0; i < name.length; i++ ) {
if ( isNameUnsafe(name.charAt(i)) == true )
return false;
}

return true;
}

// same as is isNameUnsafe but allow spaces
function isCharUnsafe(compareChar) {
var unsafeString = ""<>%\^[]`+$,='#&@.:\t";

if ( unsafeString.indexOf(compareChar) == -1 && compareChar.charCodeAt(0) >= 32
&& compareChar.charCodeAt(0) < 123 )
return false; // found no unsafe chars, return false
else
return true;
}

function isValidNameWSpace(name) {
var i = 0;

for ( i = 0; i < name.length; i++ ) {
if ( isCharUnsafe(name.charAt(i)) == true )
return false;
}

return true;
}

function isSameSubNet(lan1Ip, lan1Mask, lan2Ip, lan2Mask) {

var count = 0;

lan1a = lan1Ip.split('.');
lan1m = lan1Mask.split('.');
lan2a = lan2Ip.split('.');
lan2m = lan2Mask.split('.');

for (i = 0; i < 4; i++) {
l1a_n = parseInt(lan1a[i]);
l1m_n = parseInt(lan1m[i]);
l2a_n = parseInt(lan2a[i]);
l2m_n = parseInt(lan2m[i]);
if ((l1a_n & l1m_n) == (l2a_n & l2m_n))
count++;
}
if (count == 4)
return true;
else
return false;
}

function isValidIpAddress(address) {

ipParts = address.split('/');
if (ipParts.length > 2) return false;
if (ipParts.length == 2) {
num = parseInt(ipParts[1]);
if (num <= 0 || num > 32)
return false;
}

if (ipParts[0] == '0.0.0.0' ||
ipParts[0] == '255.255.255.255' )
return false;

addrParts = ipParts[0].split('.');
if ( addrParts.length != 4 ) return false;
for (i = 0; i < 4; i++) {
if (isNaN(addrParts[i]) || addrParts[i] =="")
return false;
num = parseInt(addrParts[i]);
if ( num < 0 || num > 255 )
return false;
}
return true;
}

function substr_count (haystack, needle, offset, length)
{
var pos = 0, cnt = 0;

haystack += '';
needle += '';
if (isNaN(offset)) {offset = 0;}
if (isNaN(length)) {length = 0;}
offset--;

while ((offset = haystack.indexOf(needle, offset+1)) != -1){
    if (length > 0 && (offset+needle.length) > length){
        return false;
    } else{
        cnt++;
    }
}

return cnt;

}

function test_ipv6(ip)
{
// Test for empty address
if (ip.length<3)
{
return ip == "::";
}

// Check if part is in IPv4 format
if (ip.indexOf('.')>0)
{
lastcolon = ip.lastIndexOf(':');

    if (!(lastcolon && isValidIpAddress(ip.substr(lastcolon + 1))))
        return false;

    // replace IPv4 part with dummy
    ip = ip.substr(0, lastcolon) + ':0:0';

}

// Check uncompressed
if (ip.indexOf('::')<0)
{
var match = ip.match(/^(?:[a-f0-9]{1,4}:){7}[a-f0-9]{1,4}$/i);
return match != null;
}

// Check colon-count for compressed format
if (substr_count(ip, ':'))
{
var match = ip.match(/^(?::|(?:[a-f0-9]{1,4}:)+):(?:(?:[a-f0-9]{1,4}:)*[a-f0-9]{1,4})?$/i);
return match != null;
}

// Not a valid IPv6 address
return false;
}

function isValidIpAddress6(address) {
ipParts = address.split('/');
if (ipParts.length > 2) return false;
if (ipParts.length == 2) {
num = parseInt(ipParts[1]);
if (num <= 0 || num > 128)
return false;
}

return test_ipv6(ipParts[0]);
}

function isValidPrefixLength(prefixLen) {
var num;

num = parseInt(prefixLen);
if (num <= 0 || num > 128)
return false;
return true;
}

function areSamePrefix(addr1, addr2) {
var i, j;
var a = [0, 0, 0, 0, 0, 0, 0, 0];
var b = [0, 0, 0, 0, 0, 0, 0, 0];

addr1Parts = addr1.split(':');
if (addr1Parts.length < 3 || addr1Parts.length > 8)
return false;
addr2Parts = addr2.split(':');
if (addr2Parts.length < 3 || addr2Parts.length > 8)
return false;
j = 0;
for (i = 0; i < addr1Parts.length; i++) {
if ( addr1Parts[i] == "" ) {
if ((i != 0) && (i+1 != addr1Parts.length)) {
j = j + (8 - addr1Parts.length + 1);
}
else {
j++;
}
}
else {
a[j] = parseInt(addr1Parts[i], 16);
j++;
}
}
j = 0;
for (i = 0; i < addr2Parts.length; i++) {
if ( addr2Parts[i] == "" ) {
if ((i != 0) && (i+1 != addr2Parts.length)) {
j = j + (8 - addr2Parts.length + 1);
}
else {
j++;
}
}
else {
b[j] = parseInt(addr2Parts[i], 16);
j++;
}
}
//only compare 64 bit prefix
for (i = 0; i < 4; i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}

function getLeftMostZeroBitPos(num) {
var i = 0;
var numArr = [128, 64, 32, 16, 8, 4, 2, 1];

for ( i = 0; i < numArr.length; i++ )
if ( (num & numArr[i]) == 0 )
return i;

return numArr.length;
}

function getRightMostOneBitPos(num) {
var i = 0;
var numArr = [1, 2, 4, 8, 16, 32, 64, 128];

for ( i = 0; i < numArr.length; i++ )
if ( ((num & numArr[i]) >> i) == 1 )
return (numArr.length - i - 1);

return -1;
}

function isValidSubnetMask(mask) {
var i = 0, num = 0;
var zeroBitPos = 0, oneBitPos = 0;
var zeroBitExisted = false;

if ( mask == '0.0.0.0' )
return false;

maskParts = mask.split('.');
if ( maskParts.length != 4 ) return false;

for (i = 0; i < 4; i++) {
if ( isNaN(maskParts[i]) == true )
return false;
num = parseInt(maskParts[i]);
if ( num < 0 || num > 255 )
return false;
if ( zeroBitExisted == true && num != 0 )
return false;
zeroBitPos = getLeftMostZeroBitPos(num);
oneBitPos = getRightMostOneBitPos(num);
if ( zeroBitPos < oneBitPos )
return false;
if ( zeroBitPos < 8 )
zeroBitExisted = true;
}

return true;
}

function isValidPort(port) {
var fromport = 0;
var toport = 100;

portrange = port.split(':');
if ( portrange.length < 1 || portrange.length > 2 ) {
return false;
}
if ( isNaN(portrange[0]) )
return false;
fromport = parseInt(portrange[0]);

if ( portrange.length > 1 ) {
if ( isNaN(portrange[1]) )
return false;
toport = parseInt(portrange[1]);
if ( toport <= fromport )
return false;
}

if ( fromport < 1 || fromport > 65535 || toport < 1 || toport > 65535 )
return false;

return true;
}

function isValidNatPort(port) {
var fromport = 0;
var toport = 100;

portrange = port.split('-');
if ( portrange.length < 1 || portrange.length > 2 ) {
return false;
}
if ( isNaN(portrange[0]) )
return false;
fromport = parseInt(portrange[0]);

if ( portrange.length > 1 ) {
if ( isNaN(portrange[1]) )
return false;
toport = parseInt(portrange[1]);
if ( toport <= fromport )
return false;
}

if ( fromport < 1 || fromport > 65535 || toport < 1 || toport > 65535 )
return false;

return true;
}

function isValidMacAddress(address) {
var c = '';
var num = 0;
var i = 0, j = 0;
var zeros = 0;

addrParts = address.split(':');
if ( addrParts.length != 6 ) return false;

for (i = 0; i < 6; i++) {
if ( addrParts[i] == '' )
return false;
for ( j = 0; j < addrParts[i].length; j++ ) {
c = addrParts[i].toLowerCase().charAt(j);
if ( (c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'f') )
continue;
else
return false;
}

  num = parseInt(addrParts[i], 16);
  if ( num == NaN || num < 0 || num > 255 )
     return false;
  if ( num == 0 )
     zeros++;

}
if (zeros == 6)
return false;

return true;
}

function isValidMacMask(mask) {
var c = '';
var num = 0;
var i = 0, j = 0;
var zeros = 0;
var zeroBitPos = 0, oneBitPos = 0;
var zeroBitExisted = false;

maskParts = mask.split(':');
if ( maskParts.length != 6 ) return false;

for (i = 0; i < 6; i++) {
if ( maskParts[i] == '' )
return false;
for ( j = 0; j < maskParts[i].length; j++ ) {
c = maskParts[i].toLowerCase().charAt(j);
if ( (c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'f') )
continue;
else
return false;
}

  num = parseInt(maskParts[i], 16);
  if ( num == NaN || num < 0 || num > 255 )
     return false;
  if ( zeroBitExisted == true && num != 0 )
     return false;
  if ( num == 0 )
     zeros++;
  zeroBitPos = getLeftMostZeroBitPos(num);
  oneBitPos = getRightMostOneBitPos(num);
  if ( zeroBitPos < oneBitPos )
     return false;
  if ( zeroBitPos < 8 )
     zeroBitExisted = true;

}
if (zeros == 6)
return false;

return true;
}

var hexVals = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"A", "B", "C", "D", "E", "F");
var unsafeString = ""<>%\^[]`+$,'#&";
// deleted these chars from the include list ";", "/", "?", ":", "@", "=", "&" and #
// so that we could analyze actual URLs

function isUnsafe(compareChar)
// this function checks to see if a char is URL unsafe.
// Returns bool result. True = unsafe, False = safe
{
if ( unsafeString.indexOf(compareChar) == -1 && compareChar.charCodeAt(0) > 32
&& compareChar.charCodeAt(0) < 123 )
return false; // found no unsafe chars, return false
else
return true;
}

function decToHex(num, radix)
// part of the hex-ifying functionality
{
var hexString = "";
while ( num >= radix ) {
temp = num % radix;
num = Math.floor(num / radix);
hexString += hexVals[temp];
}
hexString += hexVals[num];
return reversal(hexString);
}

function reversal(s)
// part of the hex-ifying functionality
{
var len = s.length;
var trans = "";
for (i = 0; i < len; i++)
trans = trans + s.substring(len-i-1, len-i);
s = trans;
return s;
}

function convert(val)
// this converts a given char to url hex form
{
return "%" + decToHex(val.charCodeAt(0), 16);
}

function encodeUrl(val)
{
var len = val.length;
var i = 0;
var newStr = "";
var original = val;

for ( i = 0; i < len; i++ ) {
if ( val.substring(i,i+1).charCodeAt(0) < 255 ) {
// hack to eliminate the rest of unicode from this
if (isUnsafe(val.substring(i,i+1)) == false)
newStr = newStr + val.substring(i,i+1);
else
newStr = newStr + convert(val.substring(i,i+1));
} else {
// woopsie! restore.
alert ("Found a non-ISO-8859-1 character at position: " + (i+1) + ",\nPlease eliminate before continuing.");
newStr = original;
// short-circuit the loop and exit
i = len;
}
}

return newStr;
}

var markStrChars = ""'";

// Checks to see if a char is used to mark begining and ending of string.
// Returns bool result. True = special, False = not special
function isMarkStrChar(compareChar)
{
if ( markStrChars.indexOf(compareChar) == -1 )
return false; // found no marked string chars, return false
else
return true;
}

// use backslash in front one of the escape codes to process
// marked string characters.
// Returns new process string
function processMarkStrChars(str) {
var i = 0;
var retStr = '';

for ( i = 0; i < str.length; i++ ) {
if ( isMarkStrChar(str.charAt(i)) == true )
retStr += '\';
retStr += str.charAt(i);
}

return retStr;
}

// Web page manipulation functions

function showhide(element, sh)
{
var status;
if (sh == 1) {
status = "block";
}
else {
status = "none"
}

if (document.getElementById)
{
// standard
document.getElementById(element).style.display = status;
}
else if (document.all)
{
// old IE
document.all[element].style.display = status;
}
else if (document.layers)
{
// Netscape 4
document.layers[element].display = status;
}
}

// Load / submit functions

function getSelect(item)
{
var idx;
if (item.options.length > 0) {
idx = item.selectedIndex;
return item.options[idx].value;
}
else {
return '';
}
}

function setSelect(item, value)
{
for (i=0; i<item.options.length; i++) {
if (item.options[i].value == value) {
item.selectedIndex = i;
break;
}
}
}

function setCheck(item, value)
{
if ( value == '1' ) {
item.checked = true;
} else {
item.checked = false;
}
}

function setInnerHtml(item,value) {
document.getElementById(item).innerHTML=value;
}

function setDisable(item, value)
{
if ( value == 1 || value == '1' ) {
item.disabled = true;
} else {
item.disabled = false;
}
}

function submitText(item)
{
return '&' + item.name + '=' + item.value;
}

function submitSelect(item)
{
return '&' + item.name + '=' + getSelect(item);
}

function submitCheck(item)
{
var val;
if (item.checked == true) {
val = 1;
}
else {
val = 0;
}
return '&' + item.name + '=' + val;
}

from pskracker.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.