IPv6 Compress ahk

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
way1000
Posts: 90
Joined: 20 Jun 2016, 02:19

IPv6 Compress ahk

Post by way1000 » 08 Aug 2022, 02:09

can someone write a IPv6 Compressor script

from

https://dnschecker.org/ipv6-compress.php

User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: IPv6 Compress ahk

Post by Xtra » 08 Aug 2022, 03:29

Example:

Code: Select all

IPv6 := "1050:0000:0000:0000:0005:0600:300c:326b"
IPv6 := RegExReplace(StrReplace(IPv6, "0000:"), ":0+", ":")
MsgBox, 4096, IPv6, % Clipboard := IPv6
ExitApp
1050:5:600:300c:326b

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: IPv6 Compress ahk

Post by boiler » 08 Aug 2022, 05:44

Need to get the double colon in there:

Code: Select all

IPv6 := RegExReplace(RegExReplace(StrReplace(IPv6, "0000"), ":{3,}", "::"), ":0+", ":")
Result:
1050::5:600:300c:326b

Perhaps @teadrinker will show us how it can be done in a single RegEx.

sofista
Posts: 645
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: IPv6 Compress ahk

Post by sofista » 08 Aug 2022, 07:13

Try this:

Code: Select all

IPv6 := "1050:0000:0000:0000:0005:0600:300c:326b"
MsgBox, % RegExReplace(IPv6, "(:0000)+|:0*", ":")

; 1050::5:600:300c:326b
Edited: The purpose of my regex was to match the supplied example, and it does so correctly. However, based on considerations below, it turned out that the proposed example is not fully representative of the standard. There are other proposals that seem to solve the problem.
Last edited by sofista on 09 Aug 2022, 08:25, edited 1 time in total.

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: IPv6 Compress ahk

Post by boiler » 08 Aug 2022, 07:20

Nice. :thumbup:

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: IPv6 Compress ahk

Post by teadrinker » 08 Aug 2022, 08:06

boiler wrote: Nice. :thumbup:
I'm not sure:

Code: Select all

IPv6 := "1050:0000:0000:0000:0005:0000:300c:326b"
MsgBox, % RegExReplace(IPv6, "(:0000)+|:0*", ":")

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: IPv6 Compress ahk

Post by teadrinker » 08 Aug 2022, 08:16

In addition, IPv6 can look like this: ff06:0:0:0:0:0:0:c3, that is compressing like ff06::c3.

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: IPv6 Compress ahk

Post by teadrinker » 08 Aug 2022, 08:30

Perhaps this one is correct:

Code: Select all

RegExReplace(IPv6, "^.*?\K(:0+){2,}(?=:)|:0+(?=[^:])", ":")

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: IPv6 Compress ahk

Post by mikeyww » 08 Aug 2022, 08:36

Yes, matches the Web site.... :crazy: :thumbup:

User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: IPv6 Compress ahk

Post by jNizM » 08 Aug 2022, 08:39

AHK v2 alternative (can be translated in v1 too):

Code: Select all

#Requires AutoHotkey v2.0-beta

MsgBox CompressIpv6("1050:0000:0000:0000:0005:0600:300c:326b")    ; -> 1050::5:600:300c:326b
MsgBox CompressIpv6("ff06:0:0:0:0:0:0:c3")                        ; -> ff06::c3

CompressIpv6(Addr)
{
	static STATUS_SUCCESS := 0
	static AF_INET6       := 23

	IN6_ADDR := Buffer(16, 0)
	if (DllCall("ntdll\RtlIpv6StringToAddressW", "str", Addr, "ptr*", 0, "ptr", IN6_ADDR) = STATUS_SUCCESS)
	{
		Size := VarSetStrCapacity(&AddrString, 94)
		if (DllCall("ws2_32\InetNtopW", "int", AF_INET6, "ptr", IN6_ADDR, "str", AddrString, "uint", Size))
			return AddrString
	}
	return false
}
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: IPv6 Compress ahk

Post by teadrinker » 08 Aug 2022, 08:44

Small adjustment:

Code: Select all

IPv6 := "0:0:0:0:0:1:2:3"
MsgBox, % RegExReplace(IPv6, "^.*?\K((^|:)0+){2,}(?=:)|:0+(?=[^:])", ":")

way1000
Posts: 90
Joined: 20 Jun 2016, 02:19

Re: IPv6 Compress ahk

Post by way1000 » 08 Aug 2022, 15:26

when trying to compress 2​b​0​6​:​0​0​0​0​:​0​0​0​0​:​1​f​2​b​:​d​7​7​f​:​0​0​0​0​:​0​0​0​0​:​8​9​c​e

i get 2​b​0​6​:​0​0​0​0​:​0​0​0​0​:​1​f​2​b​:​d​7​7​f​:​0​0​0​0​:​0​0​0​0​:​8​9​c​e

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: IPv6 Compress ahk

Post by teadrinker » 08 Aug 2022, 16:19

You used an incorrect colon character. Use this one :

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: IPv6 Compress ahk

Post by teadrinker » 08 Aug 2022, 16:56

The rest of the characters you used are also incorrect. You must type the address from an English (US) keyboard.
However, it's better using winapi instead of RegEx in this case. I translated @jNizM's code to v1:

Code: Select all

MsgBox % CompressIpv6("2b06:0000:0000:1f2b:d77f:0000:0000:89ce")

CompressIpv6(Ipv6)
{
   static STATUS_SUCCESS := 0, AF_INET6 := 23
   VarSetCapacity(IN6_ADDR, 16, 0)
   hr := DllCall("ntdll\RtlIpv6StringToAddress", "Str", Ipv6, "PtrP", _, "Ptr", &IN6_ADDR, "UInt")
   if (hr != STATUS_SUCCESS)
      throw "RtlIpv6StringToAddress failed, result: " . Format("{:#x}", hr)
   VarSetCapacity(string, 94)
   DllCall("ws2_32\InetNtop", "Int", AF_INET6, "Ptr", &IN6_ADDR, "WStr", string, "UInt", 47)
   Return string
}

way1000
Posts: 90
Joined: 20 Jun 2016, 02:19

Re: IPv6 Compress ahk

Post by way1000 » 09 Aug 2022, 08:07

teadrinker wrote:
08 Aug 2022, 16:56
The rest of the characters you used are also incorrect. You must type the address from an English (US) keyboard.
However, it's better using winapi instead of RegEx in this case. I translated @jNizM's code to v1:

Code: Select all

MsgBox % CompressIpv6("2b06:0000:0000:1f2b:d77f:0000:0000:89ce")

CompressIpv6(Ipv6)
{
   static STATUS_SUCCESS := 0, AF_INET6 := 23
   VarSetCapacity(IN6_ADDR, 16, 0)
   hr := DllCall("ntdll\RtlIpv6StringToAddress", "Str", Ipv6, "PtrP", _, "Ptr", &IN6_ADDR, "UInt")
   if (hr != STATUS_SUCCESS)
      throw "RtlIpv6StringToAddress failed, result: " . Format("{:#x}", hr)
   VarSetCapacity(string, 94)
   DllCall("ws2_32\InetNtop", "Int", AF_INET6, "Ptr", &IN6_ADDR, "WStr", string, "UInt", 47)
   Return string
}



why does this throw error with 0​0​e​b​:​0​0​0​0​:​0​0​0​0​:​0​0​0​0​:​d​8​c​1​:​0​9​4​6​:​0​2​7​2​:​0​8​7​9

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: IPv6 Compress ahk

Post by teadrinker » 09 Aug 2022, 09:17

way1000 wrote: why does this throw error with 0​0​e​b​:​0​0​0​0​:​0​0​0​0​:​0​0​0​0​:​d​8​c​1​:​0​9​4​6​:​0​2​7​2​:​0​8​7​9
The characters you use have a wrong encoding. This may happen if you are copying text from somewhere. If you type the address from the english keyboard there is no error:

Code: Select all

MsgBox % CompressIpv6("00eb:0000:0000:0000:d8c1:0946:0272:0879")

CompressIpv6(Ipv6)
{
   static STATUS_SUCCESS := 0, AF_INET6 := 23
   VarSetCapacity(IN6_ADDR, 16, 0)
   hr := DllCall("ntdll\RtlIpv6StringToAddress", "Str", Ipv6, "PtrP", _, "Ptr", &IN6_ADDR, "UInt")
   if (hr != STATUS_SUCCESS)
      throw "RtlIpv6StringToAddress failed, result: " . Format("{:#x}", hr)
   VarSetCapacity(string, 94)
   DllCall("ws2_32\InetNtop", "Int", AF_INET6, "Ptr", &IN6_ADDR, "WStr", string, "UInt", 47)
   Return string
}

way1000
Posts: 90
Joined: 20 Jun 2016, 02:19

Re: IPv6 Compress ahk

Post by way1000 » 09 Aug 2022, 12:16

thanks it works now

Post Reply

Return to “Ask for Help (v1)”