Example #1
0
        private static int DecodingMultiByteArrayHelper(BerSafeHandle berElement, char fmt, out byte[][] result)
        {
            int rc;
            var ptrResult = IntPtr.Zero;

            result = null;

            try
            {
                rc = LdapNative.Instance.ber_scanf_ptr(berElement, new string(fmt, 1), ref ptrResult);

                if (rc != -1 && ptrResult != IntPtr.Zero)
                {
                    result = MarshalUtils.GetPointerArray(ptrResult)
                             .Select(MarshalUtils.GetBytes)
                             .Select(_ => _.ToArray())
                             .ToArray();
                }
            }
            finally
            {
                if (ptrResult != IntPtr.Zero)
                {
                    LdapNative.Instance.ber_memfree(ptrResult);
                }
            }

            return(rc);
        }
        private LdapSearchResultReference GetLdapReference(SafeHandle ld, IntPtr msg)
        {
            var ctrls = IntPtr.Zero;

            try
            {
                var referencePtr = IntPtr.Zero;
                var rc           = Native.ldap_parse_reference(ld, msg, ref referencePtr, ref ctrls, 0);
                Native.ThrowIfError(ld, rc, nameof(Native.ldap_parse_reference));
                var arr  = MarshalUtils.GetPointerArray(referencePtr);
                var uris = arr.Select(_ => new Uri(Encoder.Instance.PtrToString(_))).ToArray();
                if (uris.Any())
                {
                    return(new LdapSearchResultReference(uris, null));
                }
            }
            finally
            {
                if (ctrls != IntPtr.Zero)
                {
                    Native.ldap_controls_free(ctrls);
                }
            }

            return(null);
        }
 private static void FreeAttributes(IntPtr attributes)
 {
     foreach (var tempPtr in MarshalUtils.GetPointerArray(attributes))
     {
         Marshal.FreeHGlobal(tempPtr);
     }
     Marshal.FreeHGlobal(attributes);
 }
Example #4
0
        private static void FreeControls(IntPtr controlArray)
        {
            foreach (var ptr in MarshalUtils.GetPointerArray(controlArray))
            {
                var ctrl = Marshal.PtrToStructure <Native.Native.LdapControl>(ptr);
                FreeManagedControl(ctrl);
            }

            MarshalUtils.FreeIntPtrArray(controlArray);
        }
Example #5
0
        public void MarshalUtils_StringArrayToPtr_Returns_Ptr_To_StringArray(params string[] data)
        {
            var actual = Marshal.AllocHGlobal(IntPtr.Size * (data.Length + 1));

            MarshalUtils.StringArrayToPtr(data, actual);
            Marshal.WriteIntPtr(actual, IntPtr.Size * (data.Length), IntPtr.Zero);

            var actualData = MarshalUtils.GetPointerArray(actual)
                             .Select(Encoder.Instance.PtrToString)
                             .ToList();

            Marshal.FreeHGlobal(actual);

            Assert.Equal(data, actualData);
        }
Example #6
0
        private static int EncodingBerValMultiByteArrayHelper(BerSafeHandle berElement, char fmt, byte[][] value)
        {
            var berValArray = IntPtr.Zero;

            Native.Native.SafeBerval[] managedBerVal = null;
            int rc;

            try
            {
                if (value != null)
                {
                    berValArray = MarshalUtils.AllocHGlobalIntPtrArray(value.Length + 1);
                    var structSize = Marshal.SizeOf(typeof(Native.Native.SafeBerval));
                    managedBerVal = new Native.Native.SafeBerval[value.Length];

                    for (var i = 0; i < value.Length; i++)
                    {
                        var byteArray = value[i];

                        // construct the managed berval
                        managedBerVal[i] = new Native.Native.SafeBerval();

                        if (byteArray == null)
                        {
                            managedBerVal[i].bv_len = 0;
                            managedBerVal[i].bv_val = IntPtr.Zero;
                        }
                        else
                        {
                            managedBerVal[i].bv_len = byteArray.Length;
                            managedBerVal[i].bv_val = Marshal.AllocHGlobal(byteArray.Length);
                            if (managedBerVal[i].bv_val != IntPtr.Zero)
                            {
                                Marshal.Copy(byteArray, 0, managedBerVal[i].bv_val, byteArray.Length);
                            }
                        }

                        // allocate memory for the unmanaged structure
                        var valPtr = Marshal.AllocHGlobal(structSize);
                        Marshal.StructureToPtr(managedBerVal[i], valPtr, false);

                        Marshal.WriteIntPtr(berValArray, IntPtr.Size * i, valPtr);
                    }

                    Marshal.WriteIntPtr(berValArray, IntPtr.Size * value.Length, IntPtr.Zero);
                }

                rc = LdapNative.Instance.ber_printf_berarray(berElement, new string(fmt, 1), berValArray);

                GC.KeepAlive(managedBerVal);
            }
            finally
            {
                if (berValArray != IntPtr.Zero)
                {
                    foreach (var ptr in MarshalUtils.GetPointerArray(berValArray))
                    {
                        Marshal.FreeHGlobal(ptr);
                    }
                    Marshal.FreeHGlobal(berValArray);
                }
            }

            return(rc);
        }