| 1 | // GENERATED FILE. DO NOT EDIT.
|
|---|
| 2 | var ipCodec = (function(exports) {
|
|---|
| 3 | "use strict";
|
|---|
| 4 |
|
|---|
| 5 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 6 | value: true
|
|---|
| 7 | });
|
|---|
| 8 | exports.decode = decode;
|
|---|
| 9 | exports.encode = encode;
|
|---|
| 10 | exports.familyOf = familyOf;
|
|---|
| 11 | exports.name = void 0;
|
|---|
| 12 | exports.sizeOf = sizeOf;
|
|---|
| 13 | exports.v6 = exports.v4 = void 0;
|
|---|
| 14 | const v4Regex = /^(\d{1,3}\.){3,3}\d{1,3}$/;
|
|---|
| 15 | const v4Size = 4;
|
|---|
| 16 | const v6Regex = /^(::)?(((\d{1,3}\.){3}(\d{1,3}){1})?([0-9a-f]){0,4}:{0,2}){1,8}(::)?$/i;
|
|---|
| 17 | const v6Size = 16;
|
|---|
| 18 | const v4 = {
|
|---|
| 19 | name: 'v4',
|
|---|
| 20 | size: v4Size,
|
|---|
| 21 | isFormat: ip => v4Regex.test(ip),
|
|---|
| 22 |
|
|---|
| 23 | encode(ip, buff, offset) {
|
|---|
| 24 | offset = ~~offset;
|
|---|
| 25 | buff = buff || new Uint8Array(offset + v4Size);
|
|---|
| 26 | const max = ip.length;
|
|---|
| 27 | let n = 0;
|
|---|
| 28 |
|
|---|
| 29 | for (let i = 0; i < max;) {
|
|---|
| 30 | const c = ip.charCodeAt(i++);
|
|---|
| 31 |
|
|---|
| 32 | if (c === 46) {
|
|---|
| 33 | // "."
|
|---|
| 34 | buff[offset++] = n;
|
|---|
| 35 | n = 0;
|
|---|
| 36 | } else {
|
|---|
| 37 | n = n * 10 + (c - 48);
|
|---|
| 38 | }
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | buff[offset] = n;
|
|---|
| 42 | return buff;
|
|---|
| 43 | },
|
|---|
| 44 |
|
|---|
| 45 | decode(buff, offset) {
|
|---|
| 46 | offset = ~~offset;
|
|---|
| 47 | return `${buff[offset++]}.${buff[offset++]}.${buff[offset++]}.${buff[offset]}`;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | };
|
|---|
| 51 | exports.v4 = v4;
|
|---|
| 52 | const v6 = {
|
|---|
| 53 | name: 'v6',
|
|---|
| 54 | size: v6Size,
|
|---|
| 55 | isFormat: ip => ip.length > 0 && v6Regex.test(ip),
|
|---|
| 56 |
|
|---|
| 57 | encode(ip, buff, offset) {
|
|---|
| 58 | offset = ~~offset;
|
|---|
| 59 | let end = offset + v6Size;
|
|---|
| 60 | let fill = -1;
|
|---|
| 61 | let hexN = 0;
|
|---|
| 62 | let decN = 0;
|
|---|
| 63 | let prevColon = true;
|
|---|
| 64 | let useDec = false;
|
|---|
| 65 | buff = buff || new Uint8Array(offset + v6Size); // Note: This algorithm needs to check if the offset
|
|---|
| 66 | // could exceed the buffer boundaries as it supports
|
|---|
| 67 | // non-standard compliant encodings that may go beyond
|
|---|
| 68 | // the boundary limits. if (offset < end) checks should
|
|---|
| 69 | // not be necessary...
|
|---|
| 70 |
|
|---|
| 71 | for (let i = 0; i < ip.length; i++) {
|
|---|
| 72 | let c = ip.charCodeAt(i);
|
|---|
| 73 |
|
|---|
| 74 | if (c === 58) {
|
|---|
| 75 | // :
|
|---|
| 76 | if (prevColon) {
|
|---|
| 77 | if (fill !== -1) {
|
|---|
| 78 | // Not Standard! (standard doesn't allow multiple ::)
|
|---|
| 79 | // We need to treat
|
|---|
| 80 | if (offset < end) buff[offset] = 0;
|
|---|
| 81 | if (offset < end - 1) buff[offset + 1] = 0;
|
|---|
| 82 | offset += 2;
|
|---|
| 83 | } else if (offset < end) {
|
|---|
| 84 | // :: in the middle
|
|---|
| 85 | fill = offset;
|
|---|
| 86 | }
|
|---|
| 87 | } else {
|
|---|
| 88 | // : ends the previous number
|
|---|
| 89 | if (useDec === true) {
|
|---|
| 90 | // Non-standard! (ipv4 should be at end only)
|
|---|
| 91 | // A ipv4 address should not be found anywhere else but at
|
|---|
| 92 | // the end. This codec also support putting characters
|
|---|
| 93 | // after the ipv4 address..
|
|---|
| 94 | if (offset < end) buff[offset] = decN;
|
|---|
| 95 | offset++;
|
|---|
| 96 | } else {
|
|---|
| 97 | if (offset < end) buff[offset] = hexN >> 8;
|
|---|
| 98 | if (offset < end - 1) buff[offset + 1] = hexN & 0xff;
|
|---|
| 99 | offset += 2;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | hexN = 0;
|
|---|
| 103 | decN = 0;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | prevColon = true;
|
|---|
| 107 | useDec = false;
|
|---|
| 108 | } else if (c === 46) {
|
|---|
| 109 | // . indicates IPV4 notation
|
|---|
| 110 | if (offset < end) buff[offset] = decN;
|
|---|
| 111 | offset++;
|
|---|
| 112 | decN = 0;
|
|---|
| 113 | hexN = 0;
|
|---|
| 114 | prevColon = false;
|
|---|
| 115 | useDec = true;
|
|---|
| 116 | } else {
|
|---|
| 117 | prevColon = false;
|
|---|
| 118 |
|
|---|
| 119 | if (c >= 97) {
|
|---|
| 120 | c -= 87; // a-f ... 97~102 -87 => 10~15
|
|---|
| 121 | } else if (c >= 65) {
|
|---|
| 122 | c -= 55; // A-F ... 65~70 -55 => 10~15
|
|---|
| 123 | } else {
|
|---|
| 124 | c -= 48; // 0-9 ... starting from charCode 48
|
|---|
| 125 |
|
|---|
| 126 | decN = decN * 10 + c;
|
|---|
| 127 | } // We don't know yet if its a dec or hex number
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 | hexN = (hexN << 4) + c;
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | if (prevColon === false) {
|
|---|
| 135 | // Commiting last number
|
|---|
| 136 | if (useDec === true) {
|
|---|
| 137 | if (offset < end) buff[offset] = decN;
|
|---|
| 138 | offset++;
|
|---|
| 139 | } else {
|
|---|
| 140 | if (offset < end) buff[offset] = hexN >> 8;
|
|---|
| 141 | if (offset < end - 1) buff[offset + 1] = hexN & 0xff;
|
|---|
| 142 | offset += 2;
|
|---|
| 143 | }
|
|---|
| 144 | } else if (fill === 0) {
|
|---|
| 145 | // Not Standard! (standard doesn't allow multiple ::)
|
|---|
| 146 | // This means that a : was found at the start AND end which means the
|
|---|
| 147 | // end needs to be treated as 0 entry...
|
|---|
| 148 | if (offset < end) buff[offset] = 0;
|
|---|
| 149 | if (offset < end - 1) buff[offset + 1] = 0;
|
|---|
| 150 | offset += 2;
|
|---|
| 151 | } else if (fill !== -1) {
|
|---|
| 152 | // Non-standard! (standard doens't allow multiple ::)
|
|---|
| 153 | // Here we find that there has been a :: somewhere in the middle
|
|---|
| 154 | // and the end. To treat the end with priority we need to move all
|
|---|
| 155 | // written data two bytes to the right.
|
|---|
| 156 | offset += 2;
|
|---|
| 157 |
|
|---|
| 158 | for (let i = Math.min(offset - 1, end - 1); i >= fill + 2; i--) {
|
|---|
| 159 | buff[i] = buff[i - 2];
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | buff[fill] = 0;
|
|---|
| 163 | buff[fill + 1] = 0;
|
|---|
| 164 | fill = offset;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | if (fill !== offset && fill !== -1) {
|
|---|
| 168 | // Move the written numbers to the end while filling the everything
|
|---|
| 169 | // "fill" to the bytes with zeros.
|
|---|
| 170 | if (offset > end - 2) {
|
|---|
| 171 | // Non Standard support, when the cursor exceeds bounds.
|
|---|
| 172 | offset = end - 2;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | while (end > fill) {
|
|---|
| 176 | buff[--end] = offset < end && offset > fill ? buff[--offset] : 0;
|
|---|
| 177 | }
|
|---|
| 178 | } else {
|
|---|
| 179 | // Fill the rest with zeros
|
|---|
| 180 | while (offset < end) {
|
|---|
| 181 | buff[offset++] = 0;
|
|---|
| 182 | }
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | return buff;
|
|---|
| 186 | },
|
|---|
| 187 |
|
|---|
| 188 | decode(buff, offset) {
|
|---|
| 189 | offset = ~~offset;
|
|---|
| 190 | let result = '';
|
|---|
| 191 |
|
|---|
| 192 | for (let i = 0; i < v6Size; i += 2) {
|
|---|
| 193 | if (i !== 0) {
|
|---|
| 194 | result += ':';
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | result += (buff[offset + i] << 8 | buff[offset + i + 1]).toString(16);
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | return result.replace(/(^|:)0(:0)*:0(:|$)/, '$1::$3').replace(/:{3,4}/, '::');
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | };
|
|---|
| 204 | exports.v6 = v6;
|
|---|
| 205 | const name = 'ip';
|
|---|
| 206 | exports.name = name;
|
|---|
| 207 |
|
|---|
| 208 | function sizeOf(ip) {
|
|---|
| 209 | if (v4.isFormat(ip)) return v4.size;
|
|---|
| 210 | if (v6.isFormat(ip)) return v6.size;
|
|---|
| 211 | throw Error(`Invalid ip address: ${ip}`);
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | function familyOf(string) {
|
|---|
| 215 | return sizeOf(string) === v4.size ? 1 : 2;
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | function encode(ip, buff, offset) {
|
|---|
| 219 | offset = ~~offset;
|
|---|
| 220 | const size = sizeOf(ip);
|
|---|
| 221 |
|
|---|
| 222 | if (typeof buff === 'function') {
|
|---|
| 223 | buff = buff(offset + size);
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | if (size === v4.size) {
|
|---|
| 227 | return v4.encode(ip, buff, offset);
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | return v6.encode(ip, buff, offset);
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | function decode(buff, offset, length) {
|
|---|
| 234 | offset = ~~offset;
|
|---|
| 235 | length = length || buff.length - offset;
|
|---|
| 236 |
|
|---|
| 237 | if (length === v4.size) {
|
|---|
| 238 | return v4.decode(buff, offset, length);
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | if (length === v6.size) {
|
|---|
| 242 | return v6.decode(buff, offset, length);
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | throw Error(`Invalid buffer size needs to be ${v4.size} for v4 or ${v6.size} for v6.`);
|
|---|
| 246 | }
|
|---|
| 247 | return "default" in exports ? exports.default : exports;
|
|---|
| 248 | })({});
|
|---|
| 249 | if (typeof define === 'function' && define.amd) define([], function() { return ipCodec; });
|
|---|
| 250 | else if (typeof module === 'object' && typeof exports==='object') module.exports = ipCodec;
|
|---|