< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Text.BinHexEncoding
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Text/BinHexEncoding.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 98
Coverable lines: 98
Total lines: 173
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 66
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.cctor()100%110%
GetMaxByteCount(...)0%440%
GetByteCount(...)100%110%
GetBytes(...)0%32320%
GetMaxCharCount(...)0%440%
GetCharCount(...)100%110%
GetChars(...)0%26260%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Text/BinHexEncoding.cs

#LineLine coverage
 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
 4using System;
 5using System.Globalization;
 6using System.Text;
 7
 8using CoreWCF.Runtime;
 9
 10namespace CoreWCF.Text
 11{
 12internal class BinHexEncoding : Encoding
 13    {
 014        private static byte[] s_char2val = new byte[128]
 015        {
 016                /*    0-15 */ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
 017                /*   16-31 */ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
 018                /*   32-47 */ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
 019                /*   48-63 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
 020                /*   64-79 */ 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
 021                /*   80-95 */ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
 022                /*  96-111 */ 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
 023                /* 112-127 */ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
 024        };
 025        private static string s_val2char = "0123456789ABCDEF";
 26
 27#if DEBUG
 28        static BinHexEncoding()
 29        {
 30            for (char ch = '0'; ch <= '9'; ch++)
 31            {
 32                Fx.Assert(s_char2val[ch] == ch - '0', "");
 33            }
 34
 35            for (char ch = 'A'; ch <= 'F'; ch++)
 36            {
 37                Fx.Assert(s_char2val[ch] == ch - 'A' + 10, "");
 38            }
 39
 40            for (char ch = 'a'; ch <= 'f'; ch++)
 41            {
 42                Fx.Assert(s_char2val[ch] == ch - 'a' + 10, "");
 43            }
 44        }
 45#endif
 46
 47        public override int GetMaxByteCount(int charCount)
 48        {
 049            if (charCount < 0)
 050                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(charCou
 051            if ((charCount % 2) != 0)
 052                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(SR.Format(SR.XmlInvalidBin
 053            return charCount / 2;
 54        }
 55
 56        public override int GetByteCount(char[] chars, int index, int count)
 57        {
 058            return GetMaxByteCount(count);
 59        }
 60
 61        public override unsafe int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
 62        {
 063            if (chars == null)
 064                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(chars)));
 065            if (charIndex < 0)
 066                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(charInd
 067            if (charIndex > chars.Length)
 068                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(charInd
 069            if (charCount < 0)
 070                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(charCou
 071            if (charCount > chars.Length - charIndex)
 072                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(charCou
 073            if (bytes == null)
 074                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(bytes)));
 075            if (byteIndex < 0)
 076                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(byteInd
 077            if (byteIndex > bytes.Length)
 078                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(byteInd
 079            int byteCount = GetByteCount(chars, charIndex, charCount);
 080            if (byteCount < 0 || byteCount > bytes.Length - byteIndex)
 081                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.XmlArrayTooSmall, nam
 082            if (charCount > 0)
 83            {
 084                fixed (byte* _char2val = s_char2val)
 085                {
 086                    fixed (byte* _bytes = &bytes[byteIndex])
 087                    {
 088                        fixed (char* _chars = &chars[charIndex])
 89                        {
 090                            char* pch = _chars;
 091                            char* pchMax = _chars + charCount;
 092                            byte* pb = _bytes;
 093                            while (pch < pchMax)
 94                            {
 95                                Fx.Assert(pch + 2 <= pchMax, "");
 096                                char pch0 = pch[0];
 097                                char pch1 = pch[1];
 098                                if ((pch0 | pch1) >= 128)
 099                                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(SR.For
 0100                                byte d1 = _char2val[pch0];
 0101                                byte d2 = _char2val[pch1];
 0102                                if ((d1 | d2) == 0xFF)
 0103                                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(SR.For
 0104                                pb[0] = (byte)((d1 << 4) + d2);
 0105                                pch += 2;
 0106                                pb++;
 107                            }
 108                        }
 109                    }
 110                }
 111            }
 0112            return byteCount;
 113        }
 114
 115        public override int GetMaxCharCount(int byteCount)
 116        {
 0117            if (byteCount < 0 || byteCount > int.MaxValue / 2)
 0118                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(byteCou
 0119            return byteCount * 2;
 120        }
 121
 122        public override int GetCharCount(byte[] bytes, int index, int count)
 123        {
 0124            return GetMaxCharCount(count);
 125        }
 126
 127        public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
 128        {
 0129            if (bytes == null)
 0130                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(bytes)));
 0131            if (byteIndex < 0)
 0132                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(byteInd
 0133            if (byteIndex > bytes.Length)
 0134                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(byteInd
 0135            if (byteCount < 0)
 0136                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(byteCou
 0137            if (byteCount > bytes.Length - byteIndex)
 0138                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(byteCou
 0139            int charCount = GetCharCount(bytes, byteIndex, byteCount);
 0140            if (chars == null)
 0141                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(chars)));
 0142            if (charIndex < 0)
 0143                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(charInd
 0144            if (charIndex > chars.Length)
 0145                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(charInd
 0146            if (charCount < 0 || charCount > chars.Length - charIndex)
 0147                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.XmlArrayTooSmall, "ch
 0148            if (byteCount > 0)
 0149            {
 0150                fixed (char* _val2char = s_val2char)
 0151                {
 0152                    fixed (byte* _bytes = &bytes[byteIndex])
 0153                    {
 0154                        fixed (char* _chars = &chars[charIndex])
 155                        {
 0156                            char* pch = _chars;
 0157                            byte* pb = _bytes;
 0158                            byte* pbMax = _bytes + byteCount;
 0159                            while (pb < pbMax)
 160                            {
 0161                                pch[0] = _val2char[pb[0] >> 4];
 0162                                pch[1] = _val2char[pb[0] & 0x0F];
 0163                                pb++;
 0164                                pch += 2;
 165                            }
 166                        }
 167                    }
 168                }
 169            }
 0170            return charCount;
 171        }
 172    }
 173}