2   var expandIPv6, ipaddr, ipv4Part, ipv4Regexes, ipv6Part, ipv6Regexes, matchCIDR, root;
 
   8   if ((typeof module !== "undefined" && module !== null) && module.exports) {
 
   9     module.exports = ipaddr;
 
  11     root['ipaddr'] = ipaddr;
 
  14   matchCIDR = function(first, second, partSize, cidrBits) {
 
  16     if (first.length !== second.length) {
 
  17       throw new Error("ipaddr: cannot match CIDR for objects with different lengths");
 
  20     while (cidrBits > 0) {
 
  21       shift = partSize - cidrBits;
 
  25       if (first[part] >> shift !== second[part] >> shift) {
 
  34   ipaddr.subnetMatch = function(address, rangeList, defaultName) {
 
  35     var rangeName, rangeSubnets, subnet, _i, _len;
 
  36     if (defaultName == null) {
 
  37       defaultName = 'unicast';
 
  39     for (rangeName in rangeList) {
 
  40       rangeSubnets = rangeList[rangeName];
 
  41       if (toString.call(rangeSubnets[0]) !== '[object Array]') {
 
  42         rangeSubnets = [rangeSubnets];
 
  44       for (_i = 0, _len = rangeSubnets.length; _i < _len; _i++) {
 
  45         subnet = rangeSubnets[_i];
 
  46         if (address.match.apply(address, subnet)) {
 
  54   ipaddr.IPv4 = (function() {
 
  55     function IPv4(octets) {
 
  57       if (octets.length !== 4) {
 
  58         throw new Error("ipaddr: ipv4 octet count should be 4");
 
  60       for (_i = 0, _len = octets.length; _i < _len; _i++) {
 
  62         if (!((0 <= octet && octet <= 255))) {
 
  63           throw new Error("ipaddr: ipv4 octet is a byte");
 
  69     IPv4.prototype.kind = function() {
 
  73     IPv4.prototype.toString = function() {
 
  74       return this.octets.join(".");
 
  77     IPv4.prototype.toByteArray = function() {
 
  78       return this.octets.slice(0);
 
  81     IPv4.prototype.match = function(other, cidrRange) {
 
  82       if (other.kind() !== 'ipv4') {
 
  83         throw new Error("ipaddr: cannot match ipv4 address with non-ipv4 one");
 
  85       return matchCIDR(this.octets, other.octets, 8, cidrRange);
 
  88     IPv4.prototype.SpecialRanges = {
 
  89       unspecified: [[new IPv4([0, 0, 0, 0]), 8]],
 
  90       broadcast: [[new IPv4([255, 255, 255, 255]), 32]],
 
  91       multicast: [[new IPv4([224, 0, 0, 0]), 4]],
 
  92       linkLocal: [[new IPv4([169, 254, 0, 0]), 16]],
 
  93       loopback: [[new IPv4([127, 0, 0, 0]), 8]],
 
  94       "private": [[new IPv4([10, 0, 0, 0]), 8], [new IPv4([172, 16, 0, 0]), 12], [new IPv4([192, 168, 0, 0]), 16]],
 
  95       reserved: [[new IPv4([192, 0, 0, 0]), 24], [new IPv4([192, 0, 2, 0]), 24], [new IPv4([192, 88, 99, 0]), 24], [new IPv4([198, 51, 100, 0]), 24], [new IPv4([203, 0, 113, 0]), 24], [new IPv4([240, 0, 0, 0]), 4]]
 
  98     IPv4.prototype.range = function() {
 
  99       return ipaddr.subnetMatch(this, this.SpecialRanges);
 
 102     IPv4.prototype.toIPv4MappedAddress = function() {
 
 103       return ipaddr.IPv6.parse("::ffff:" + (this.toString()));
 
 110   ipv4Part = "(0?\\d+|0x[a-f0-9]+)";
 
 113     fourOctet: new RegExp("^" + ipv4Part + "\\." + ipv4Part + "\\." + ipv4Part + "\\." + ipv4Part + "$", 'i'),
 
 114     longValue: new RegExp("^" + ipv4Part + "$", 'i')
 
 117   ipaddr.IPv4.parser = function(string) {
 
 118     var match, parseIntAuto, part, shift, value;
 
 119     parseIntAuto = function(string) {
 
 120       if (string[0] === "0" && string[1] !== "x") {
 
 121         return parseInt(string, 8);
 
 123         return parseInt(string);
 
 126     if (match = string.match(ipv4Regexes.fourOctet)) {
 
 128         var _i, _len, _ref, _results;
 
 129         _ref = match.slice(1, 6);
 
 131         for (_i = 0, _len = _ref.length; _i < _len; _i++) {
 
 133           _results.push(parseIntAuto(part));
 
 137     } else if (match = string.match(ipv4Regexes.longValue)) {
 
 138       value = parseIntAuto(match[1]);
 
 139       if (value > 0xffffffff || value < 0) {
 
 140         throw new Error("ipaddr: address outside defined range");
 
 142       return ((function() {
 
 145         for (shift = _i = 0; _i <= 24; shift = _i += 8) {
 
 146           _results.push((value >> shift) & 0xff);
 
 155   ipaddr.IPv6 = (function() {
 
 156     function IPv6(parts) {
 
 158       if (parts.length !== 8) {
 
 159         throw new Error("ipaddr: ipv6 part count should be 8");
 
 161       for (_i = 0, _len = parts.length; _i < _len; _i++) {
 
 163         if (!((0 <= part && part <= 0xffff))) {
 
 164           throw new Error("ipaddr: ipv6 part should fit to two octets");
 
 170     IPv6.prototype.kind = function() {
 
 174     IPv6.prototype.toString = function() {
 
 175       var compactStringParts, part, pushPart, state, stringParts, _i, _len;
 
 176       stringParts = (function() {
 
 177         var _i, _len, _ref, _results;
 
 180         for (_i = 0, _len = _ref.length; _i < _len; _i++) {
 
 182           _results.push(part.toString(16));
 
 186       compactStringParts = [];
 
 187       pushPart = function(part) {
 
 188         return compactStringParts.push(part);
 
 191       for (_i = 0, _len = stringParts.length; _i < _len; _i++) {
 
 192         part = stringParts[_i];
 
 224       return compactStringParts.join(":");
 
 227     IPv6.prototype.toByteArray = function() {
 
 228       var bytes, part, _i, _len, _ref;
 
 231       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
 
 233         bytes.push(part >> 8);
 
 234         bytes.push(part & 0xff);
 
 239     IPv6.prototype.toNormalizedString = function() {
 
 241       return ((function() {
 
 242         var _i, _len, _ref, _results;
 
 245         for (_i = 0, _len = _ref.length; _i < _len; _i++) {
 
 247           _results.push(part.toString(16));
 
 250       }).call(this)).join(":");
 
 253     IPv6.prototype.match = function(other, cidrRange) {
 
 254       if (other.kind() !== 'ipv6') {
 
 255         throw new Error("ipaddr: cannot match ipv6 address with non-ipv6 one");
 
 257       return matchCIDR(this.parts, other.parts, 16, cidrRange);
 
 260     IPv6.prototype.SpecialRanges = {
 
 261       unspecified: [new IPv6([0, 0, 0, 0, 0, 0, 0, 0]), 128],
 
 262       linkLocal: [new IPv6([0xfe80, 0, 0, 0, 0, 0, 0, 0]), 10],
 
 263       multicast: [new IPv6([0xff00, 0, 0, 0, 0, 0, 0, 0]), 8],
 
 264       loopback: [new IPv6([0, 0, 0, 0, 0, 0, 0, 1]), 128],
 
 265       uniqueLocal: [new IPv6([0xfc00, 0, 0, 0, 0, 0, 0, 0]), 7],
 
 266       ipv4Mapped: [new IPv6([0, 0, 0, 0, 0, 0xffff, 0, 0]), 96],
 
 267       rfc6145: [new IPv6([0, 0, 0, 0, 0xffff, 0, 0, 0]), 96],
 
 268       rfc6052: [new IPv6([0x64, 0xff9b, 0, 0, 0, 0, 0, 0]), 96],
 
 269       '6to4': [new IPv6([0x2002, 0, 0, 0, 0, 0, 0, 0]), 16],
 
 270       teredo: [new IPv6([0x2001, 0, 0, 0, 0, 0, 0, 0]), 32],
 
 271       reserved: [[new IPv6([0x2001, 0xdb8, 0, 0, 0, 0, 0, 0]), 32]]
 
 274     IPv6.prototype.range = function() {
 
 275       return ipaddr.subnetMatch(this, this.SpecialRanges);
 
 278     IPv6.prototype.isIPv4MappedAddress = function() {
 
 279       return this.range() === 'ipv4Mapped';
 
 282     IPv6.prototype.toIPv4Address = function() {
 
 284       if (!this.isIPv4MappedAddress()) {
 
 285         throw new Error("ipaddr: trying to convert a generic ipv6 address to ipv4");
 
 287       _ref = this.parts.slice(-2), high = _ref[0], low = _ref[1];
 
 288       return new ipaddr.IPv4([high >> 8, high & 0xff, low >> 8, low & 0xff]);
 
 295   ipv6Part = "(?:[0-9a-f]+::?)+";
 
 298     "native": new RegExp("^(::)?(" + ipv6Part + ")?([0-9a-f]+)?(::)?$", 'i'),
 
 299     transitional: new RegExp(("^((?:" + ipv6Part + ")|(?:::)(?:" + ipv6Part + ")?)") + ("" + ipv4Part + "\\." + ipv4Part + "\\." + ipv4Part + "\\." + ipv4Part + "$"), 'i')
 
 302   expandIPv6 = function(string, parts) {
 
 303     var colonCount, lastColon, part, replacement, replacementCount;
 
 304     if (string.indexOf('::') !== string.lastIndexOf('::')) {
 
 309     while ((lastColon = string.indexOf(':', lastColon + 1)) >= 0) {
 
 312     if (string[0] === ':') {
 
 315     if (string[string.length - 1] === ':') {
 
 318     replacementCount = parts - colonCount;
 
 320     while (replacementCount--) {
 
 323     string = string.replace('::', replacement);
 
 324     if (string[0] === ':') {
 
 325       string = string.slice(1);
 
 327     if (string[string.length - 1] === ':') {
 
 328       string = string.slice(0, -1);
 
 331       var _i, _len, _ref, _results;
 
 332       _ref = string.split(":");
 
 334       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
 
 336         _results.push(parseInt(part, 16));
 
 342   ipaddr.IPv6.parser = function(string) {
 
 344     if (string.match(ipv6Regexes['native'])) {
 
 345       return expandIPv6(string, 8);
 
 346     } else if (match = string.match(ipv6Regexes['transitional'])) {
 
 347       parts = expandIPv6(match[1].slice(0, -1), 6);
 
 349         parts.push(parseInt(match[2]) << 8 | parseInt(match[3]));
 
 350         parts.push(parseInt(match[4]) << 8 | parseInt(match[5]));
 
 357   ipaddr.IPv4.isIPv4 = ipaddr.IPv6.isIPv6 = function(string) {
 
 358     return this.parser(string) !== null;
 
 361   ipaddr.IPv4.isValid = ipaddr.IPv6.isValid = function(string) {
 
 364       new this(this.parser(string));
 
 372   ipaddr.IPv4.parse = ipaddr.IPv6.parse = function(string) {
 
 374     parts = this.parser(string);
 
 375     if (parts === null) {
 
 376       throw new Error("ipaddr: string is not formatted like ip address");
 
 378     return new this(parts);
 
 381   ipaddr.isValid = function(string) {
 
 382     return ipaddr.IPv6.isValid(string) || ipaddr.IPv4.isValid(string);
 
 385   ipaddr.parse = function(string) {
 
 386     if (ipaddr.IPv6.isValid(string)) {
 
 387       return ipaddr.IPv6.parse(string);
 
 388     } else if (ipaddr.IPv4.isValid(string)) {
 
 389       return ipaddr.IPv4.parse(string);
 
 391       throw new Error("ipaddr: the address has neither IPv6 nor IPv4 format");
 
 395   ipaddr.process = function(string) {
 
 397     addr = this.parse(string);
 
 398     if (addr.kind() === 'ipv6' && addr.isIPv4MappedAddress()) {
 
 399       return addr.toIPv4Address();