internal static object[] TryDecode(string format, byte[] value, out bool decodeSucceeded) { Utility.CheckOSVersion(); if (format == null) { throw new ArgumentNullException("format"); } UTF8Encoding encoding = new UTF8Encoding(false, true); berval berval = new berval(); ArrayList list = new ArrayList(); BerSafeHandle berElement = null; object[] objArray = null; decodeSucceeded = false; if (value == null) { berval.bv_len = 0; berval.bv_val = IntPtr.Zero; } else { berval.bv_len = value.Length; berval.bv_val = Marshal.AllocHGlobal(value.Length); Marshal.Copy(value, 0, berval.bv_val, value.Length); } try { berElement = new BerSafeHandle(berval); } finally { if (berval.bv_val != IntPtr.Zero) { Marshal.FreeHGlobal(berval.bv_val); } } int error = 0; for (int i = 0; i < format.Length; i++) { char c = format[i]; switch (c) { case '{': case '}': case '[': case ']': case 'n': case 'x': error = Wldap32.ber_scanf(berElement, new string(c, 1)); if (error == 0) { } break; case 'i': case 'e': case 'b': { int num3 = 0; error = Wldap32.ber_scanf_int(berElement, new string(c, 1), ref num3); if (error == 0) { if (c == 'b') { bool flag = false; if (num3 == 0) { flag = false; } else { flag = true; } list.Add(flag); } else { list.Add(num3); } } break; } case 'a': { byte[] bytes = DecodingByteArrayHelper(berElement, 'O', ref error); if (error == 0) { string str = null; if (bytes != null) { str = encoding.GetString(bytes); } list.Add(str); } break; } case 'O': { byte[] buffer2 = DecodingByteArrayHelper(berElement, c, ref error); if (error == 0) { list.Add(buffer2); } break; } case 'B': { IntPtr zero = IntPtr.Zero; int length = 0; error = Wldap32.ber_scanf_bitstring(berElement, "B", ref zero, ref length); if (error == 0) { byte[] destination = null; if (zero != IntPtr.Zero) { destination = new byte[length]; Marshal.Copy(zero, destination, 0, length); } list.Add(destination); } break; } case 'v': { byte[][] bufferArray = null; string[] strArray = null; bufferArray = DecodingMultiByteArrayHelper(berElement, 'V', ref error); if (error == 0) { if (bufferArray != null) { strArray = new string[bufferArray.Length]; for (int k = 0; k < bufferArray.Length; k++) { if (bufferArray[k] == null) { strArray[k] = null; } else { strArray[k] = encoding.GetString(bufferArray[k]); } } } list.Add(strArray); } break; } default: { if (c != 'V') { throw new ArgumentException(Res.GetString("BerConverterUndefineChar")); } byte[][] bufferArray2 = null; bufferArray2 = DecodingMultiByteArrayHelper(berElement, c, ref error); if (error == 0) { list.Add(bufferArray2); } break; } } if (error != 0) { return(objArray); } } objArray = new object[list.Count]; for (int j = 0; j < list.Count; j++) { objArray[j] = list[j]; } decodeSucceeded = true; return(objArray); }
internal static object[] TryDecode(string format, byte[] value, out bool decodeSucceeded) { if (format == null) { throw new ArgumentNullException(nameof(format)); } Debug.WriteLine("Begin decoding\n"); UTF8Encoding utf8Encoder = new UTF8Encoding(false, true); berval berValue = new berval(); ArrayList resultList = new ArrayList(); BerSafeHandle berElement = null; object[] decodeResult = null; decodeSucceeded = false; if (value == null) { berValue.bv_len = 0; berValue.bv_val = IntPtr.Zero; } else { berValue.bv_len = value.Length; berValue.bv_val = Marshal.AllocHGlobal(value.Length); Marshal.Copy(value, 0, berValue.bv_val, value.Length); } try { berElement = new BerSafeHandle(berValue); } finally { if (berValue.bv_val != IntPtr.Zero) { Marshal.FreeHGlobal(berValue.bv_val); } } int error = 0; for (int formatCount = 0; formatCount < format.Length; formatCount++) { char fmt = format[formatCount]; if (fmt == '{' || fmt == '}' || fmt == '[' || fmt == ']' || fmt == 'n' || fmt == 'x') { error = Wldap32.ber_scanf(berElement, new string(fmt, 1)); if (error != 0) { Debug.WriteLine("ber_scanf for {, }, [, ], n or x failed"); } } else if (fmt == 'i' || fmt == 'e' || fmt == 'b') { int result = 0; error = Wldap32.ber_scanf_int(berElement, new string(fmt, 1), ref result); if (error == 0) { if (fmt == 'b') { // should return a bool bool boolResult = false; if (result == 0) { boolResult = false; } else { boolResult = true; } resultList.Add(boolResult); } else { resultList.Add(result); } } else { Debug.WriteLine("ber_scanf for format character 'i', 'e' or 'b' failed"); } } else if (fmt == 'a') { // return a string byte[] byteArray = DecodingByteArrayHelper(berElement, 'O', ref error); if (error == 0) { string s = null; if (byteArray != null) { s = utf8Encoder.GetString(byteArray); } resultList.Add(s); } } else if (fmt == 'O') { // return berval byte[] byteArray = DecodingByteArrayHelper(berElement, fmt, ref error); if (error == 0) { // add result to the list resultList.Add(byteArray); } } else if (fmt == 'B') { // return a bitstring and its length IntPtr ptrResult = IntPtr.Zero; int length = 0; error = Wldap32.ber_scanf_bitstring(berElement, "B", ref ptrResult, ref length); if (error == 0) { byte[] byteArray = null; if (ptrResult != IntPtr.Zero) { byteArray = new byte[length]; Marshal.Copy(ptrResult, byteArray, 0, length); } resultList.Add(byteArray); } else { Debug.WriteLine("ber_scanf for format character 'B' failed"); } // no need to free memory as wldap32 returns the original pointer instead of a duplicating memory pointer that // needs to be freed } else if (fmt == 'v') { //null terminate strings byte[][] byteArrayresult = null; string[] stringArray = null; byteArrayresult = DecodingMultiByteArrayHelper(berElement, 'V', ref error); if (error == 0) { if (byteArrayresult != null) { stringArray = new string[byteArrayresult.Length]; for (int i = 0; i < byteArrayresult.Length; i++) { if (byteArrayresult[i] == null) { stringArray[i] = null; } else { stringArray[i] = utf8Encoder.GetString(byteArrayresult[i]); } } } resultList.Add(stringArray); } } else if (fmt == 'V') { byte[][] result = null; result = DecodingMultiByteArrayHelper(berElement, fmt, ref error); if (error == 0) { resultList.Add(result); } } else { Debug.WriteLine("Format string contains undefined character\n"); throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, SR.BerConverterUndefineChar)); } if (error != 0) { // decode failed, just return return(decodeResult); } } decodeResult = new object[resultList.Count]; for (int count = 0; count < resultList.Count; count++) { decodeResult[count] = resultList[count]; } decodeSucceeded = true; return(decodeResult); }
internal static object[] TryDecode(string format, byte[] value, out bool decodeSucceeded) { Utility.CheckOSVersion(); if (format != null) { UTF8Encoding uTF8Encoding = new UTF8Encoding(false, true); berval _berval = new berval(); ArrayList arrayLists = new ArrayList(); BerSafeHandle berSafeHandle = null; object[] item = null; decodeSucceeded = false; if (value != null) { _berval.bv_len = (int)value.Length; _berval.bv_val = Marshal.AllocHGlobal((int)value.Length); Marshal.Copy(value, 0, _berval.bv_val, (int)value.Length); } else { _berval.bv_len = 0; _berval.bv_val = (IntPtr)0; } try { berSafeHandle = new BerSafeHandle(_berval); } finally { if (_berval.bv_val != (IntPtr)0) { Marshal.FreeHGlobal(_berval.bv_val); } } int num = 0; int num1 = 0; while (num1 < format.Length) { char chr = format[num1]; if (chr == '{' || chr == '}' || chr == '[' || chr == ']' || chr == 'n' || chr == 'x') { num = Wldap32.ber_scanf(berSafeHandle, new string(chr, 1)); if (num != 0) { } } else { if (chr == 'i' || chr == 'e' || chr == 'b') { int num2 = 0; num = Wldap32.ber_scanf_int(berSafeHandle, new string(chr, 1), ref num2); if (num == 0) { if (chr != 'b') { arrayLists.Add(num2); } else { bool flag = false; if (num2 != 0) { flag = true; } else { flag = false; } arrayLists.Add(flag); } } } else { if (chr != 'a') { if (chr != 'O') { if (chr != 'B') { if (chr != 'v') { if (chr != 'V') { throw new ArgumentException(Res.GetString("BerConverterUndefineChar")); } else { byte[][] numArray = BerConverter.DecodingMultiByteArrayHelper(berSafeHandle, chr, ref num); if (num == 0) { arrayLists.Add(numArray); } } } else { string[] str = null; byte[][] numArray1 = BerConverter.DecodingMultiByteArrayHelper(berSafeHandle, 'V', ref num); if (num == 0) { if (numArray1 != null) { str = new string[(int)numArray1.Length]; for (int i = 0; i < (int)numArray1.Length; i++) { if (numArray1[i] != null) { str[i] = uTF8Encoding.GetString(numArray1[i]); } else { str[i] = null; } } } arrayLists.Add(str); } } } else { IntPtr intPtr = (IntPtr)0; int num3 = 0; num = Wldap32.ber_scanf_bitstring(berSafeHandle, "B", ref intPtr, ref num3); if (num == 0) { byte[] numArray2 = null; if (intPtr != (IntPtr)0) { numArray2 = new byte[num3]; Marshal.Copy(intPtr, numArray2, 0, num3); } arrayLists.Add(numArray2); } } } else { byte[] numArray3 = BerConverter.DecodingByteArrayHelper(berSafeHandle, chr, ref num); if (num == 0) { arrayLists.Add(numArray3); } } } else { byte[] numArray4 = BerConverter.DecodingByteArrayHelper(berSafeHandle, 'O', ref num); if (num == 0) { string str1 = null; if (numArray4 != null) { str1 = uTF8Encoding.GetString(numArray4); } arrayLists.Add(str1); } } } } if (num == 0) { num1++; } else { return(item); } } item = new object[arrayLists.Count]; for (int j = 0; j < arrayLists.Count; j++) { item[j] = arrayLists[j]; } decodeSucceeded = true; return(item); } else { throw new ArgumentNullException("format"); } }