about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-07-07 14:12:40 +0200
committerbptato <nincsnevem662@gmail.com>2023-07-07 14:13:19 +0200
commita84193b0af85cc1e4c298d66a6fd951e0e46b480 (patch)
tree87ba336c353eaa3ce6bf75b43422b02082403b03
parentb2514e94bac6f7b3462649834ad36287b0b29ab2 (diff)
downloadchawan-a84193b0af85cc1e4c298d66a6fd951e0e46b480.tar.gz
Fixes in ipv6 parser and serializer
-rw-r--r--src/types/url.nim21
-rw-r--r--src/utils/twtstr.nim23
2 files changed, 30 insertions, 14 deletions
diff --git a/src/types/url.nim b/src/types/url.nim
index 727d7b84..bfdc3eaa 100644
--- a/src/types/url.nim
+++ b/src/types/url.nim
@@ -148,13 +148,17 @@ func parseIpv6(input: string): Option[array[8, uint16]] =
         #TODO validation error
         return failure
       break
-    elif has and c == ':':
-      inc pointer
-      if not has:
+    elif has:
+      if c == ':':
+        inc pointer
+        if not has:
+          #TODO validation error
+          return failure
+      else:
         #TODO validation error
         return failure
-      address[pieceindex] = value
-      inc pieceindex
+    address[pieceindex] = value
+    inc pieceindex
   if compress != -1:
     var swaps = pieceindex - compress
     pieceindex = 7
@@ -574,7 +578,6 @@ proc basicParseUrl*(input: string, base = none(URL), url: URL = URL(),
         if buffer != "":
           let i = parseInt32(buffer)
           if i.isNone or i.get notin 0..65535:
-            eprint "validation error???", i
             #TODO validation error
             return none(Url)
           let port = cast[uint16](i.get).some
@@ -798,11 +801,7 @@ func serializeip(ipv6: array[8, uint16]): string =
         result &= ':'
       ignore0 = true
       continue
-    const HexChars = "0123456789abcdef"
-    var x = ipv6[i]
-    while x != 0:
-      result &= HexChars[x mod 0x10]
-      x = x div 0x10
+    result &= toHexLower(ipv6[i])
     if i != high(ipv6):
       result &= ':'
 
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim
index 77d86998..a32cc8da 100644
--- a/src/utils/twtstr.nim
+++ b/src/utils/twtstr.nim
@@ -172,11 +172,28 @@ func isAscii*(s: string): bool =
       return false
   return true
 
-const HexChars = "0123456789ABCDEF"
+const HexCharsUpper = "0123456789ABCDEF"
+const HexCharsLower = "0123456789abcdef"
 func toHex*(c: char): string =
   result = newString(2)
-  result[0] = HexChars[(uint8(c) shr 4)]
-  result[1] = HexChars[(uint8(c) and 0xF)]
+  result[0] = HexCharsUpper[(uint8(c) shr 4)]
+  result[1] = HexCharsLower[(uint8(c) and 0xF)]
+
+func toHexLower*(u: uint16): string =
+  var x = u
+  let len = if (u and 0xF000) != 0:
+    4
+  elif (u and 0x0F00) != 0:
+    3
+  elif (u and 0xF0) != 0:
+    2
+  else:
+    1
+  var s = newString(len)
+  for i in countdown(len - 1, 0):
+    s[i] = HexCharsLower[x and 0xF]
+    x = x shr 4
+  return s
 
 func toHex*(i: uint8): string =
   return toHex(cast[char](i))