index.cjs 7.0 KB

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