X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-sharp%2FUtility.cs;h=a1d0a669ed66608069eafe0297f9533e2a0ad06e;hb=6589afffbb5f484ebc3aa5a7a533eaf0940ac202;hp=f8b4ffd8982857d55b65f603f839efb666cfa6fd;hpb=94d2753047cff1e9223e42736884e4d51348b45a;p=catta diff --git a/avahi-sharp/Utility.cs b/avahi-sharp/Utility.cs index f8b4ffd..a1d0a66 100644 --- a/avahi-sharp/Utility.cs +++ b/avahi-sharp/Utility.cs @@ -1,5 +1,3 @@ -/* $Id$ */ - /*** This file is part of avahi. @@ -24,7 +22,9 @@ using System.Net; using System.Text; using System.Runtime.InteropServices; using Mono.Unix; +using Mono.Unix.Native; +using Stdlib = Mono.Unix.Native.Stdlib; namespace Avahi { @@ -43,7 +43,7 @@ namespace Avahi { if (ptr == IntPtr.Zero) return null; - + int len = strlen (ptr); byte[] bytes = new byte[len]; Marshal.Copy (ptr, bytes, 0, len); @@ -54,18 +54,26 @@ namespace Avahi { if (ptr == IntPtr.Zero) return null; - + string ret = PtrToString (ptr); Free (ptr); return ret; } - public static IntPtr StringToPtr (string str) + public static byte[] StringToBytes (string str) + { + if (str == null) + return null; + + return Encoding.UTF8.GetBytes (str + "\0"); // lame. + } + + private static IntPtr StringToPtr (string str) { if (str == null) return IntPtr.Zero; - byte[] bytes = Encoding.UTF8.GetBytes (str); + byte[] bytes = Utility.StringToBytes (str); IntPtr buf = Stdlib.malloc ((uint) bytes.Length + 1); Marshal.Copy (bytes, 0, buf, bytes.Length); Marshal.WriteByte (buf, bytes.Length, 0); @@ -90,7 +98,7 @@ namespace Avahi public static IPAddress PtrToAddress (IntPtr ptr) { IPAddress address = null; - + if (ptr != IntPtr.Zero) { IntPtr buf = Stdlib.malloc (256); IntPtr addrPtr = avahi_address_snprint (buf, 256, ptr);