| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Globalization; |
| | | 6 | | using System.Text; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.Security |
| | | 9 | | { |
| | | 10 | | internal sealed class SoapHexBinary |
| | | 11 | | { |
| | 0 | 12 | | private readonly StringBuilder _sb = new StringBuilder(100); |
| | | 13 | | |
| | 0 | 14 | | public SoapHexBinary() |
| | | 15 | | { |
| | 0 | 16 | | } |
| | | 17 | | |
| | 0 | 18 | | public SoapHexBinary(byte[] value) |
| | | 19 | | { |
| | 0 | 20 | | Value = value; |
| | 0 | 21 | | } |
| | | 22 | | |
| | 0 | 23 | | public byte[] Value { get; set; } |
| | | 24 | | |
| | | 25 | | public override string ToString() |
| | | 26 | | { |
| | 0 | 27 | | _sb.Length = 0; |
| | 0 | 28 | | for (int i = 0; i < Value.Length; i++) |
| | | 29 | | { |
| | 0 | 30 | | string s = Value[i].ToString("X", CultureInfo.InvariantCulture); |
| | 0 | 31 | | if (s.Length == 1) |
| | | 32 | | { |
| | 0 | 33 | | _sb.Append('0'); |
| | | 34 | | } |
| | | 35 | | |
| | 0 | 36 | | _sb.Append(s); |
| | | 37 | | } |
| | 0 | 38 | | return _sb.ToString(); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | public static SoapHexBinary Parse(string value) |
| | | 42 | | { |
| | 0 | 43 | | return new SoapHexBinary(ToByteArray(FilterBin64(value))); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | private static byte[] ToByteArray(string value) |
| | | 47 | | { |
| | 0 | 48 | | char[] cA = value.ToCharArray(); |
| | 0 | 49 | | if (cA.Length % 2 != 0) |
| | | 50 | | { |
| | 0 | 51 | | throw new FormatException(SR.Format("Remoting_SOAPInteropxsdInvalid", "xsd:hexBinary", value)); |
| | | 52 | | } |
| | 0 | 53 | | byte[] bA = new byte[cA.Length / 2]; |
| | 0 | 54 | | for (int i = 0; i < cA.Length / 2; i++) |
| | | 55 | | { |
| | 0 | 56 | | bA[i] = (byte)(ToByte(cA[i * 2], value) * 16 + ToByte(cA[i * 2 + 1], value)); |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | return bA; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | private static byte ToByte(char c, string value) |
| | | 63 | | { |
| | 0 | 64 | | string s = c.ToString(); |
| | | 65 | | byte b; |
| | | 66 | | try |
| | | 67 | | { |
| | 0 | 68 | | s = c.ToString(); |
| | 0 | 69 | | b = byte.Parse(s, NumberStyles.HexNumber, CultureInfo.InvariantCulture); |
| | 0 | 70 | | } |
| | 0 | 71 | | catch (Exception) |
| | | 72 | | { |
| | 0 | 73 | | throw new FormatException(SR.Format("Remoting_SOAPInteropxsdInvalid", "xsd:hexBinary", value)); |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | return b; |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | internal static string FilterBin64(string value) |
| | | 80 | | { |
| | 0 | 81 | | StringBuilder sb = new StringBuilder(); |
| | 0 | 82 | | for (int i = 0; i < value.Length; i++) |
| | | 83 | | { |
| | 0 | 84 | | if (!(value[i] == ' ' || value[i] == '\n' || value[i] == '\r')) |
| | | 85 | | { |
| | 0 | 86 | | sb.Append(value[i]); |
| | | 87 | | } |
| | | 88 | | } |
| | 0 | 89 | | return sb.ToString(); |
| | | 90 | | } |
| | | 91 | | } |
| | | 92 | | } |