forked from whitequark/ipaddr.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipaddr.min.js
More file actions
1 lines (1 loc) · 9.48 KB
/
ipaddr.min.js
File metadata and controls
1 lines (1 loc) · 9.48 KB
1
!function(r){!function(r){"use strict";function t(r,t){if(r.indexOf("::")!==r.lastIndexOf("::"))return null;var n,e,i=0,o=-1,s=(r.match(p.zoneIndex)||[])[0];for(s&&(s=s.substring(1),r=r.replace(/%.+$/,""));(o=r.indexOf(":",o+1))>=0;)i++;if("::"===r.substr(0,2)&&i--,"::"===r.substr(-2,2)&&i--,i>t)return null;for(e=t-i,n=":";e--;)n+="0:";return":"===(r=r.replace("::",n))[0]&&(r=r.slice(1)),":"===r[r.length-1]&&(r=r.slice(0,-1)),t=function(){var t,n=r.split(":"),e=[];for(t=0;t<n.length;t++)e.push(parseInt(n[t],16));return e}(),{parts:t,zoneId:s}}function n(r,t,n,e){if(r.length!==t.length)throw new Error("ipaddr: cannot match CIDR for objects with different lengths");for(var i,o=0;e>0;){if((i=n-e)<0&&(i=0),r[o]>>i!=t[o]>>i)return!1;e-=n,o+=1}return!0}function e(r){return"0"===r[0]&&"x"!==r[1]?parseInt(r,8):parseInt(r)}function i(r,t){for(;r.length<t;)r="0"+r;return r}var o="(0?\\d+|0x[a-f0-9]+)",s={fourOctet:new RegExp("^"+o+"\\."+o+"\\."+o+"\\."+o+"$","i"),longValue:new RegExp("^"+o+"$","i")},a="(?:[0-9a-f]+::?)+",p={zoneIndex:new RegExp("%[0-9a-z]{1,}","i"),native:new RegExp("^(::)?("+a+")?([0-9a-f]+)?(::)?(%[0-9a-z]{1,})?$","i"),transitional:new RegExp("^((?:"+a+")|(?:::)(?:"+a+")?)"+o+"\\."+o+"\\."+o+"\\."+o+"(%[0-9a-z]{1,})?$","i")},u={};u.subnetMatch=function(r,t,n){var e,i,o,s;void 0!==n&&null!==n||(n="unicast");for(i in t)if(t.hasOwnProperty(i))for(!(o=t[i])[0]||o[0]instanceof Array||(o=[o]),e=0;e<o.length;e++)if(s=o[e],r.kind()===s[0].kind()&&r.match.apply(r,s))return i;return n},u.IPv4=function(){function r(r){if(4!==r.length)throw new Error("ipaddr: ipv4 octet count should be 4");var t,n;for(t=0;t<r.length;t++)if(!(0<=(n=r[t])&&n<=255))throw new Error("ipaddr: ipv4 octet should fit in 8 bits");this.octets=r}return r.prototype.kind=function(){return"ipv4"},r.prototype.toString=function(){return this.octets.join(".")},r.prototype.toNormalizedString=function(){return this.toString()},r.prototype.toByteArray=function(){return this.octets.slice(0)},r.prototype.match=function(r,t){var e;if(void 0===t&&(r=(e=r)[0],t=e[1]),"ipv4"!==r.kind())throw new Error("ipaddr: cannot match ipv4 address with non-ipv4 one");return n(this.octets,r.octets,8,t)},r.prototype.SpecialRanges={unspecified:[[new r([0,0,0,0]),8]],broadcast:[[new r([255,255,255,255]),32]],multicast:[[new r([224,0,0,0]),4]],linkLocal:[[new r([169,254,0,0]),16]],loopback:[[new r([127,0,0,0]),8]],carrierGradeNat:[[new r([100,64,0,0]),10]],private:[[new r([10,0,0,0]),8],[new r([172,16,0,0]),12],[new r([192,168,0,0]),16]],reserved:[[new r([192,0,0,0]),24],[new r([192,0,2,0]),24],[new r([192,88,99,0]),24],[new r([198,51,100,0]),24],[new r([203,0,113,0]),24],[new r([240,0,0,0]),4]]},r.prototype.range=function(){return u.subnetMatch(this,this.SpecialRanges)},r.prototype.toIPv4MappedAddress=function(){return u.IPv6.parse("::ffff:"+this.toString())},r.prototype.prefixLengthFromSubnetMask=function(){var r,t,n,e=0,i=!1,o={0:8,128:7,192:6,224:5,240:4,248:3,252:2,254:1,255:0};for(r=3;r>=0;r-=1){if(!((t=this.octets[r])in o))return null;if(n=o[t],i&&0!==n)return null;8!==n&&(i=!0),e+=n}return 32-e},r}(),u.IPv4.parser=function(r){var t,n,i;if(t=r.match(s.fourOctet))return function(){var r,i=t.slice(1,6),o=[];for(r=0;r<i.length;r++)n=i[r],o.push(e(n));return o}();if(t=r.match(s.longValue)){if((i=e(t[1]))>4294967295||i<0)throw new Error("ipaddr: address outside defined range");return function(){var r,t=[];for(r=0;r<=24;r+=8)t.push(i>>r&255);return t}().reverse()}return null},u.IPv6=function(){function r(r,t){var n,e;if(16===r.length)for(this.parts=[],n=0;n<=14;n+=2)this.parts.push(r[n]<<8|r[n+1]);else{if(8!==r.length)throw new Error("ipaddr: ipv6 part count should be 8 or 16");this.parts=r}for(n=0;n<this.parts.length;n++)if(!(0<=(e=this.parts[n])&&e<=65535))throw new Error("ipaddr: ipv6 part should fit in 16 bits");t&&(this.zoneId=t)}return r.prototype.kind=function(){return"ipv6"},r.prototype.toString=function(){return this.toNormalizedString().replace(/((^|:)(0(:|$))+)/,"::")},r.prototype.toRFC5952String=function(){for(var r,t=/((^|:)(0(:|$)){2,})/g,n=this.toNormalizedString(),e=0,i=-1;r=t.exec(n);)r[0].length>i&&(e=r.index,i=r[0].length);return i<0?n:n.substring(0,e)+"::"+n.substring(e+i)},r.prototype.toByteArray=function(){var r,t,n,e;for(r=[],e=this.parts,t=0;t<e.length;t++)n=e[t],r.push(n>>8),r.push(255&n);return r},r.prototype.toNormalizedString=function(){var r=function(){var r,t=[];for(r=0;r<this.parts.length;r++)t.push(this.parts[r].toString(16));return t}.call(this).join(":"),t="";return this.zoneId&&(t="%"+this.zoneId),r+t},r.prototype.toFixedLengthString=function(){var r=function(){var r,t=[];for(r=0;r<this.parts.length;r++)t.push(i(this.parts[r].toString(16),4));return t}.call(this).join(":"),t="";return this.zoneId&&(t="%"+this.zoneId),r+t},r.prototype.match=function(r,t){var e;if(void 0===t&&(r=(e=r)[0],t=e[1]),"ipv6"!==r.kind())throw new Error("ipaddr: cannot match ipv6 address with non-ipv6 one");return n(this.parts,r.parts,16,t)},r.prototype.SpecialRanges={unspecified:[new r([0,0,0,0,0,0,0,0]),128],linkLocal:[new r([65152,0,0,0,0,0,0,0]),10],multicast:[new r([65280,0,0,0,0,0,0,0]),8],loopback:[new r([0,0,0,0,0,0,0,1]),128],uniqueLocal:[new r([64512,0,0,0,0,0,0,0]),7],ipv4Mapped:[new r([0,0,0,0,0,65535,0,0]),96],rfc6145:[new r([0,0,0,0,65535,0,0,0]),96],rfc6052:[new r([100,65435,0,0,0,0,0,0]),96],"6to4":[new r([8194,0,0,0,0,0,0,0]),16],teredo:[new r([8193,0,0,0,0,0,0,0]),32],reserved:[[new r([8193,3512,0,0,0,0,0,0]),32]]},r.prototype.range=function(){return u.subnetMatch(this,this.SpecialRanges)},r.prototype.isIPv4MappedAddress=function(){return"ipv4Mapped"===this.range()},r.prototype.toIPv4Address=function(){if(!this.isIPv4MappedAddress())throw new Error("ipaddr: trying to convert a generic ipv6 address to ipv4");var r=this.parts.slice(-2),t=r[0],n=r[1];return new u.IPv4([t>>8,255&t,n>>8,255&n])},r.prototype.prefixLengthFromSubnetMask=function(){var r,t,n,e=0,i=!1,o={0:16,32768:15,49152:14,57344:13,61440:12,63488:11,64512:10,65024:9,65280:8,65408:7,65472:6,65504:5,65520:4,65528:3,65532:2,65534:1,65535:0};for(r=7;r>=0;r-=1){if(!((t=this.parts[r])in o))return null;if(n=o[t],i&&0!==n)return null;16!==n&&(i=!0),e+=n}return 128-e},r}(),u.IPv6.parser=function(r){var n,e,i,o,s,a;if(p.native.test(r))return t(r,8);if((i=r.match(p.transitional))&&(a=i[6]||"",(n=t(i[1].slice(0,-1)+a,6)).parts)){for(s=[parseInt(i[2]),parseInt(i[3]),parseInt(i[4]),parseInt(i[5])],e=0;e<s.length;e++)if(!(0<=(o=s[e])&&o<=255))return null;return n.parts.push(s[0]<<8|s[1]),n.parts.push(s[2]<<8|s[3]),{parts:n.parts,zoneId:n.zoneId}}return null},u.IPv4.isIPv4=function(r){return null!==this.parser(r)},u.IPv6.isIPv6=function(r){return null!==this.parser(r)},u.IPv4.isValid=function(r){try{return new this(this.parser(r)),!0}catch(r){return!1}},u.IPv4.isValidFourPartDecimal=function(r){return!(!u.IPv4.isValid(r)||!r.match(/^(0|[1-9]\d*)(\.(0|[1-9]\d*)){3}$/))},u.IPv6.isValid=function(r){var t;if("string"==typeof r&&-1===r.indexOf(":"))return!1;try{return t=this.parser(r),new this(t.parts,t.zoneId),!0}catch(r){return!1}},u.IPv4.parse=function(r){var t=this.parser(r);if(null===t)throw new Error("ipaddr: string is not formatted like ip address");return new this(t)},u.IPv6.parse=function(r){var t=this.parser(r);if(null===t.parts)throw new Error("ipaddr: string is not formatted like ip address");return new this(t.parts,t.zoneId)},u.IPv4.parseCIDR=function(r){var t,n,e;if((n=r.match(/^(.+)\/(\d+)$/))&&(t=parseInt(n[2]))>=0&&t<=32)return e=[this.parse(n[1]),t],Object.defineProperty(e,"toString",{value:function(){return this.join("/")}}),e;throw new Error("ipaddr: string is not formatted like an IPv4 CIDR range")},u.IPv4.subnetMaskFromPrefixLength=function(r){if((r=parseInt(r))<0||r>32)throw new Error("ipaddr: invalid IPv4 prefix length");for(var t=[0,0,0,0],n=0,e=Math.floor(r/8);n<e;)t[n]=255,n++;return e<4&&(t[e]=Math.pow(2,r%8)-1<<8-r%8),new this(t)},u.IPv4.broadcastAddressFromCIDR=function(r){var t,n,e,i,o;try{for(e=(t=this.parseCIDR(r))[0].toByteArray(),o=this.subnetMaskFromPrefixLength(t[1]).toByteArray(),i=[],n=0;n<4;)i.push(parseInt(e[n],10)|255^parseInt(o[n],10)),n++;return new this(i)}catch(r){throw new Error("ipaddr: the address does not have IPv4 CIDR format")}},u.IPv4.networkAddressFromCIDR=function(r){var t,n,e,i,o;try{for(e=(t=this.parseCIDR(r))[0].toByteArray(),o=this.subnetMaskFromPrefixLength(t[1]).toByteArray(),i=[],n=0;n<4;)i.push(parseInt(e[n],10)&parseInt(o[n],10)),n++;return new this(i)}catch(r){throw new Error("ipaddr: the address does not have IPv4 CIDR format")}},u.IPv6.parseCIDR=function(r){var t,n,e;if((n=r.match(/^(.+)\/(\d+)$/))&&(t=parseInt(n[2]))>=0&&t<=128)return e=[this.parse(n[1]),t],Object.defineProperty(e,"toString",{value:function(){return this.join("/")}}),e;throw new Error("ipaddr: string is not formatted like an IPv6 CIDR range")},u.isValid=function(r){return u.IPv6.isValid(r)||u.IPv4.isValid(r)},u.parse=function(r){if(u.IPv6.isValid(r))return u.IPv6.parse(r);if(u.IPv4.isValid(r))return u.IPv4.parse(r);throw new Error("ipaddr: the address has neither IPv6 nor IPv4 format")},u.parseCIDR=function(r){try{return u.IPv6.parseCIDR(r)}catch(t){try{return u.IPv4.parseCIDR(r)}catch(r){throw new Error("ipaddr: the address has neither IPv6 nor IPv4 CIDR format")}}},u.fromByteArray=function(r){var t=r.length;if(4===t)return new u.IPv4(r);if(16===t)return new u.IPv6(r);throw new Error("ipaddr: the binary input is neither an IPv6 nor IPv4 address")},u.process=function(r){var t=this.parse(r);return"ipv6"===t.kind()&&t.isIPv4MappedAddress()?t.toIPv4Address():t},"undefined"!=typeof module&&module.exports?module.exports=u:r.ipaddr=u}(this)}("undefined"==typeof window?window={}:window);