index.mjs 5.9 KB

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