public void Decode(byte[] data) { this.Tag = GetMessageType(data); object[] message = BerConverter.Decode("{iV}", data); this.MessageID = (int)message[0]; this.ProtocolOp = message[1]; }
public void Decode_Bytes_ReturnsExpected(string format, byte[] values, object[] expected) { var value = BerConverter.Decode(format, values); _testOutputHelper.WriteLine($"expected: [{string.Join(',', expected)}]"); _testOutputHelper.WriteLine($"actual: [{string.Join(',', value)}]"); Assert.Equal(expected, value); }
public List <DynamicBerConverterField> Decode(byte[] val) { List <DynamicBerConverterField> ret = new List <DynamicBerConverterField>(); object[] decoded = null; int berpos = -1; foreach (int curpos in ConversionRules.Keys) { try { decoded = BerConverter.Decode(ConversionRules[curpos], val); berpos = curpos; break; } catch (Exception ex) { ex.ToDummy(); } } if (berpos != -1) { try { foreach (int pos in FieldNames[berpos].Keys) { DynamicBerConverterField bf = new DynamicBerConverterField(); bf.Name = FieldNames[berpos][pos]; bf.FieldType = FieldTypes[berpos][pos]; bf.Value = Convert.ChangeType(decoded[pos], bf.FieldType); if (bf.FieldType == typeof(string)) { bf.Value = ((string)bf.Value).Replace("\0", String.Empty); } // \0 -> nothing returned due to missing perms? ret.Add(bf); } } catch (Exception ex) { ex.ToDummy(); } } return(ret); }
public static SearchStats Parse(byte[] value) { SearchStats searchStats = null; try { object[] array = BerConverter.Decode("{iiiiiiiiiaia}", value); searchStats = new SearchStats(); searchStats.callTime = (int)array[3]; searchStats.entriesReturned = (int)array[5]; searchStats.entriesVisited = (int)array[7]; searchStats.filter = (string)array[9]; searchStats.index = (string)array[11]; } catch (BerConversionException) { } catch (InvalidCastException) { } catch (DecoderFallbackException) { } if (searchStats != null) { if (!string.IsNullOrEmpty(searchStats.filter)) { SearchStats searchStats2 = searchStats; string text = searchStats.filter; char[] trimChars = new char[1]; searchStats2.filter = text.TrimEnd(trimChars); } if (string.IsNullOrEmpty(searchStats.filter)) { searchStats.filter = "<null>"; } if (!string.IsNullOrEmpty(searchStats.index)) { SearchStats searchStats3 = searchStats; string text2 = searchStats.index; char[] trimChars2 = new char[1]; searchStats3.index = text2.TrimEnd(trimChars2); } if (string.IsNullOrEmpty(searchStats.index)) { searchStats.index = "<null>"; } } return(searchStats); }
public void Encode_Decode_Should_Returns_Expected(string printFormat, string scanFormat, object[] values, string[] platforms = null) { if (platforms != null && !platforms.Any(_ => RuntimeInformation.IsOSPlatform(OSPlatform.Create(_)))) { return; } var encoded = BerConverter.Encode(printFormat, values); _testOutputHelper.WriteLine($"encoded: [{string.Join(',', encoded)}]"); var decoded = BerConverter.Decode(scanFormat, encoded); Assert.Equal(values, decoded); }
static void HandleBerConversionException() { try { Console.WriteLine("\r\nTrying to decode a binary " + "value with an incorrect decode string..."); BerConverter.Decode("{iii}", new byte[] { 0x00, 0xFF, 0x30, 0x84, 0x00, 0x00, 0x00 }); } catch (BerConversionException e) { Console.WriteLine(e.GetType().Name + ": Message:" + e.Message); } }
internal static void TransformControls(DirectoryControl[] controls) { if (controls != null) { for (int i = 0; i < (int)controls.Length; i++) { if (!(controls[i].GetType() != typeof(DirectoryControl)) && controls[i].Type == "1.2.840.113556.1.4.1504") { byte[] value = controls[i].GetValue(); object[] objArray = BerConverter.Decode("{e}", value); int num = (int)objArray[0]; ADAsqResponseControl aDAsqResponseControl = new ADAsqResponseControl(num, controls[i].IsCritical, value); controls[i] = aDAsqResponseControl; } } return; } else { return; } }
internal static void TransformControls(DirectoryControl[] controls) { for (int i = 0; i < controls.Length; i++) { Debug.Assert(controls[i] != null); byte[] value = controls[i].GetValue(); if (controls[i].Type == "1.2.840.113556.1.4.319") { // The control is a PageControl. object[] result = BerConverter.Decode("{iO}", value); Debug.Assert((result != null) && (result.Length == 2)); int size = (int)result[0]; // user expects cookie with length 0 as paged search is done. byte[] cookie = (byte[])result[1] ?? Array.Empty <byte>(); PageResultResponseControl pageControl = new PageResultResponseControl(size, cookie, controls[i].IsCritical, controls[i].GetValue()); controls[i] = pageControl; } else if (controls[i].Type == "1.2.840.113556.1.4.1504") { // The control is an AsqControl. object[] o = BerConverter.Decode("{e}", value); Debug.Assert((o != null) && (o.Length == 1)); int result = (int)o[0]; AsqResponseControl asq = new AsqResponseControl(result, controls[i].IsCritical, controls[i].GetValue()); controls[i] = asq; } else if (controls[i].Type == "1.2.840.113556.1.4.841") { // The control is a DirSyncControl. object[] o = BerConverter.Decode("{iiO}", value); Debug.Assert(o != null && o.Length == 3); int moreData = (int)o[0]; int count = (int)o[1]; byte[] dirsyncCookie = (byte[])o[2]; DirSyncResponseControl dirsync = new DirSyncResponseControl(dirsyncCookie, (moreData == 0 ? false : true), count, controls[i].IsCritical, controls[i].GetValue()); controls[i] = dirsync; } else if (controls[i].Type == "1.2.840.113556.1.4.474") { // The control is a SortControl. int result = 0; string attribute = null; object[] o = BerConverter.TryDecode("{ea}", value, out bool decodeSucceeded); // decode might fail as AD for example never returns attribute name, we don't want to unnecessarily throw and catch exception if (decodeSucceeded) { Debug.Assert(o != null && o.Length == 2); result = (int)o[0]; attribute = (string)o[1]; } else { // decoding might fail as attribute is optional o = BerConverter.Decode("{e}", value); Debug.Assert(o != null && o.Length == 1); result = (int)o[0]; } SortResponseControl sort = new SortResponseControl((ResultCode)result, attribute, controls[i].IsCritical, controls[i].GetValue()); controls[i] = sort; } else if (controls[i].Type == "2.16.840.1.113730.3.4.10") { // The control is a VlvResponseControl. int position; int count; int result; byte[] context = null; object[] o = BerConverter.TryDecode("{iieO}", value, out bool decodeSucceeded); if (decodeSucceeded) { Debug.Assert(o != null && o.Length == 4); position = (int)o[0]; count = (int)o[1]; result = (int)o[2]; context = (byte[])o[3]; } else { o = BerConverter.Decode("{iie}", value); Debug.Assert(o != null && o.Length == 3); position = (int)o[0]; count = (int)o[1]; result = (int)o[2]; } VlvResponseControl vlv = new VlvResponseControl(position, count, context, (ResultCode)result, controls[i].IsCritical, controls[i].GetValue()); controls[i] = vlv; } } }
public void Decode_Invalid_ThrowsBerConversionException(string format, byte[] values) { Assert.Throws <BerConversionException>(() => BerConverter.Decode(format, values)); }
public void UnknownFormat_ThrowsArgumentException(string format, byte[] values) { AssertExtensions.Throws <ArgumentException>(null, () => BerConverter.Decode(format, values)); }
public void Decode_NullFormat_ThrowsArgumentNullException() { AssertExtensions.Throws <ArgumentNullException>("format", () => BerConverter.Decode(null, new byte[0])); }
public void Decode_Bytes_ReturnsExpected(string format, byte[] values, object[] expected) { object value = BerConverter.Decode(format, values); Assert.Equal(expected, value); }
static void DoBerEncoding() { try { bool boolValue = true; int intValue = 5; byte[] binaryValue = new byte[] { 0x00, 0x01, 0x02, 0x03 }; string strValue = "abcdef"; string[] strValues = { "abc", "def", "xyzw" }; byte[][] binaryValues = { new byte[] { 0x00, 0x01, 0x02, 0x03 }, new byte[] { 0x04, 0x05, 0x06, 0x07 }, new byte[] { 0x08, 0x09,0x0A }, new byte[] { 0x0B, 0x0C } }; Console.WriteLine("\r\nBer encoding objects..."); byte[] berEncodedValue = BerConverter.Encode("{bios{v}{V}}", new object[] { boolValue, intValue, binaryValue, strValue, strValues, binaryValues }); Console.WriteLine("\r\nBer decoding the byte array..."); object[] decodedObjects = BerConverter.Decode("{biOavV}", berEncodedValue); Console.WriteLine("\r\nThe decoded objects are:"); foreach (object o in decodedObjects) { if (o is byte[]) { Console.WriteLine(ByteArrayToStringInHex((byte[])o)); } else if (o is byte[][]) { byte[][] byteArrays = (byte[][])o; for (int i = 0; i < byteArrays.Length; i++) { Console.Write(ByteArrayToStringInHex(byteArrays[i]) + " - "); } Console.WriteLine(); } else if (o is string[]) { string[] strArray = (string[])o; for (int i = 0; i < strArray.Length; i++) { Console.Write(strArray[i] + " - "); } Console.WriteLine(); } else { Console.WriteLine(o); } } } catch (BerConversionException e) { Console.WriteLine("BerConversionException:" + e.Message); } catch (ArgumentException e) { Console.WriteLine("ArgumentException:" + e.Message); } }
public object[] Decode2(byte[] Data) { return(BerConverter.Decode("{B}", Data)); }
public object[] Decode(byte[] Data) { return(BerConverter.Decode("{OiiiiiOO}", Data)); }