Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Mtom message encoding #4687

Merged
merged 2 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ internal static string CreateDefaultSourceString(object source)
throw new ArgumentNullException("source");
}

return String.Format(CultureInfo.CurrentCulture, "{0}/{1}", source.GetType().ToString(), source.GetHashCode());
return string.Format(CultureInfo.CurrentCulture, "{0}/{1}", source.GetType().ToString(), source.GetHashCode());
}

protected static void AddExceptionToTraceString(XmlWriter xml, Exception exception)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Globalization;
using System.Runtime;
using System.ServiceModel;

namespace System.Text
{
internal class BinHexEncoding : Encoding
{
private static byte[] s_char2val = new byte[128]
{
/* 0-15 */ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
/* 16-31 */ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
/* 32-47 */ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
/* 48-63 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
/* 64-79 */ 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
/* 80-95 */ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
/* 96-111 */ 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
/* 112-127 */ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
};
private static string s_val2char = "0123456789ABCDEF";

#if DEBUG
static BinHexEncoding()
{
for (char ch = '0'; ch <= '9'; ch++)
{
Fx.Assert(s_char2val[ch] == ch - '0', "");
}

for (char ch = 'A'; ch <= 'F'; ch++)
{
Fx.Assert(s_char2val[ch] == ch - 'A' + 10, "");
}

for (char ch = 'a'; ch <= 'f'; ch++)
{
Fx.Assert(s_char2val[ch] == ch - 'a' + 10, "");
}
}
#endif

public override int GetMaxByteCount(int charCount)
{
if (charCount < 0)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(charCount), SR.ValueMustBeNonNegative));
if ((charCount % 2) != 0)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(SR.Format(SR.XmlInvalidBinHexLength, charCount.ToString(NumberFormatInfo.CurrentInfo))));
return charCount / 2;
}

public override int GetByteCount(char[] chars, int index, int count)
{
return GetMaxByteCount(count);
}

public override unsafe int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
{
if (chars == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("chars"));
if (charIndex < 0)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(charIndex), SR.ValueMustBeNonNegative));
if (charIndex > chars.Length)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(charIndex), SR.Format(SR.OffsetExceedsBufferSize, chars.Length)));
if (charCount < 0)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(charCount), SR.ValueMustBeNonNegative));
if (charCount > chars.Length - charIndex)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(charCount), SR.Format(SR.SizeExceedsRemainingBufferSpace, chars.Length - charIndex)));
if (bytes == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(bytes)));
if (byteIndex < 0)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(byteIndex), SR.ValueMustBeNonNegative));
if (byteIndex > bytes.Length)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(byteIndex), SR.Format(SR.OffsetExceedsBufferSize, bytes.Length)));
int byteCount = GetByteCount(chars, charIndex, charCount);
if (byteCount < 0 || byteCount > bytes.Length - byteIndex)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.XmlArrayTooSmall, nameof(bytes)));
if (charCount > 0)
{
fixed (byte* _char2val = s_char2val)
{
fixed (byte* _bytes = &bytes[byteIndex])
{
fixed (char* _chars = &chars[charIndex])
{
char* pch = _chars;
char* pchMax = _chars + charCount;
byte* pb = _bytes;
while (pch < pchMax)
{
Fx.Assert(pch + 2 <= pchMax, "");
char pch0 = pch[0];
char pch1 = pch[1];
if ((pch0 | pch1) >= 128)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(SR.Format(SR.XmlInvalidBinHexSequence, new string(pch, 0, 2), charIndex + (int)(pch - _chars))));
byte d1 = _char2val[pch0];
byte d2 = _char2val[pch1];
if ((d1 | d2) == 0xFF)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(SR.Format(SR.XmlInvalidBinHexSequence, new string(pch, 0, 2), charIndex + (int)(pch - _chars))));
pb[0] = (byte)((d1 << 4) + d2);
pch += 2;
pb++;
}
}
}
}
}
return byteCount;
}

public override int GetMaxCharCount(int byteCount)
{
if (byteCount < 0 || byteCount > int.MaxValue / 2)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(byteCount), SR.Format(SR.ValueMustBeInRange, 0, int.MaxValue / 2)));
return byteCount * 2;
}

public override int GetCharCount(byte[] bytes, int index, int count)
{
return GetMaxCharCount(count);
}

public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
{
if (bytes == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("bytes"));
if (byteIndex < 0)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(byteIndex), SR.ValueMustBeNonNegative));
if (byteIndex > bytes.Length)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(byteIndex), SR.Format(SR.OffsetExceedsBufferSize, bytes.Length)));
if (byteCount < 0)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(byteCount), SR.ValueMustBeNonNegative));
if (byteCount > bytes.Length - byteIndex)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(byteCount), SR.Format(SR.SizeExceedsRemainingBufferSpace, bytes.Length - byteIndex)));
int charCount = GetCharCount(bytes, byteIndex, byteCount);
if (chars == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("chars"));
if (charIndex < 0)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(charIndex), SR.ValueMustBeNonNegative));
if (charIndex > chars.Length)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(charIndex), SR.Format(SR.OffsetExceedsBufferSize, chars.Length)));
if (charCount < 0 || charCount > chars.Length - charIndex)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.XmlArrayTooSmall, "chars"));
if (byteCount > 0)
{
fixed (char* _val2char = s_val2char)
{
fixed (byte* _bytes = &bytes[byteIndex])
{
fixed (char* _chars = &chars[charIndex])
{
char* pch = _chars;
byte* pb = _bytes;
byte* pbMax = _bytes + byteCount;
while (pb < pbMax)
{
pch[0] = _val2char[pb[0] >> 4];
pch[1] = _val2char[pb[0] & 0x0F];
pb++;
pch += 2;
}
}
}
}
}
return charCount;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.Xml
{
internal static class XmlConverter
mconnew marked this conversation as resolved.
Show resolved Hide resolved
{
public static bool IsWhitespace(char ch)
{
return (ch <= ' ' && (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n'));
}

public static bool IsWhitespace(string s)
{
for (int i = 0; i < s.Length; i++)
{
if (!IsWhitespace(s[i]))
return false;
}

return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Globalization;
using System.ServiceModel;

namespace System.Xml
{
internal static class XmlExceptionHelper
{
static void ThrowXmlException(XmlDictionaryReader reader, string res, string arg1)
{
ThrowXmlException(reader, res, arg1, null);
}

static void ThrowXmlException(XmlDictionaryReader reader, string res, string arg1, string arg2)
{
ThrowXmlException(reader, res, arg1, arg2, null);
}

static void ThrowXmlException(XmlDictionaryReader reader, string res, string arg1, string arg2, string arg3)
{
string s = SR.Format(res, arg1, arg2, arg3);
IXmlLineInfo lineInfo = reader as IXmlLineInfo;
if (lineInfo != null && lineInfo.HasLineInfo())
{
s += " " + SR.Format(SR.XmlLineInfo, lineInfo.LineNumber, lineInfo.LinePosition);
}

throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(s));
}

static public void ThrowMaxStringContentLengthExceeded(XmlDictionaryReader reader, int maxStringContentLength)
{
ThrowXmlException(reader, SR.XmlMaxStringContentLengthExceeded, maxStringContentLength.ToString(NumberFormatInfo.CurrentInfo));
}
}
}
Loading