< Summary

Information
Class: Renci.SshNet.Security.Org.BouncyCastle.Utilities.Encoders.HexEncoder
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\BouncyCastle\util\encoders\HexEncoder.cs
Line coverage
49%
Covered lines: 47
Uncovered lines: 48
Coverable lines: 95
Total lines: 174
Line coverage: 49.4%
Branch coverage
35%
Covered branches: 15
Total branches: 42
Branch coverage: 35.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor()100%1100%
InitialiseDecodingTable()100%2100%
Encode(...)0%20%
Ignore(...)50%6100%
Decode(...)0%160%
DecodeString(...)62.5%1670%

File(s)

\home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\BouncyCastle\util\encoders\HexEncoder.cs

#LineLine coverage
 1using System.IO;
 2
 3namespace Renci.SshNet.Security.Org.BouncyCastle.Utilities.Encoders
 4{
 5    internal class HexEncoder
 6    {
 17        protected readonly byte[] encodingTable =
 18        {
 19            (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',
 110            (byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f'
 111        };
 12
 13        /*
 14         * set up the decoding table.
 15         */
 116        protected readonly byte[] decodingTable = new byte[128];
 17
 18        protected void InitialiseDecodingTable()
 119        {
 120            Arrays.Fill(decodingTable, (byte)0xff);
 21
 3422            for (int i = 0; i < encodingTable.Length; i++)
 1623            {
 1624                decodingTable[encodingTable[i]] = (byte)i;
 1625            }
 26
 127            decodingTable['A'] = decodingTable['a'];
 128            decodingTable['B'] = decodingTable['b'];
 129            decodingTable['C'] = decodingTable['c'];
 130            decodingTable['D'] = decodingTable['d'];
 131            decodingTable['E'] = decodingTable['e'];
 132            decodingTable['F'] = decodingTable['f'];
 133        }
 34
 135        public HexEncoder()
 136        {
 137            InitialiseDecodingTable();
 138        }
 39
 40        /**
 41        * encode the input data producing a Hex output stream.
 42        *
 43        * @return the number of bytes produced.
 44        */
 45        public int Encode(
 46            byte[]  data,
 47            int    off,
 48            int    length,
 49            Stream  outStream)
 050        {
 051            for (int i = off; i < (off + length); i++)
 052            {
 053                int v = data[i];
 54
 055                outStream.WriteByte(encodingTable[v >> 4]);
 056                outStream.WriteByte(encodingTable[v & 0xf]);
 057            }
 58
 059            return length * 2;
 060        }
 61
 62        private static bool Ignore(char c)
 189663        {
 189664            return c == '\n' || c =='\r' || c == '\t' || c == ' ';
 189665        }
 66
 67        /**
 68        * decode the Hex encoded byte data writing it to the given output stream,
 69        * whitespace characters will be ignored.
 70        *
 71        * @return the number of bytes produced.
 72        */
 73        public int Decode(
 74            byte[]  data,
 75            int    off,
 76            int    length,
 77            Stream  outStream)
 078        {
 79            byte b1, b2;
 080            int outLen = 0;
 081            int end = off + length;
 82
 083            while (end > off)
 084            {
 085                if (!Ignore((char)data[end - 1]))
 086                {
 087                    break;
 88                }
 89
 090                end--;
 091            }
 92
 093            int i = off;
 094            while (i < end)
 095            {
 096                while (i < end && Ignore((char)data[i]))
 097                {
 098                    i++;
 099                }
 100
 0101                b1 = decodingTable[data[i++]];
 102
 0103                while (i < end && Ignore((char)data[i]))
 0104                {
 0105                    i++;
 0106                }
 107
 0108                b2 = decodingTable[data[i++]];
 109
 0110                if ((b1 | b2) >= 0x80)
 0111                    throw new IOException("invalid characters encountered in Hex data");
 112
 0113                outStream.WriteByte((byte)((b1 << 4) | b2));
 114
 0115                outLen++;
 0116            }
 117
 0118            return outLen;
 0119        }
 120
 121        /**
 122        * decode the Hex encoded string data writing it to the given output stream,
 123        * whitespace characters will be ignored.
 124        *
 125        * @return the number of bytes produced.
 126        */
 127        public int DecodeString(
 128            string  data,
 129            Stream  outStream)
 18130        {
 131            byte    b1, b2;
 18132            int     length = 0;
 133
 18134            int     end = data.Length;
 135
 18136            while (end > 0)
 18137            {
 18138                if (!Ignore(data[end - 1]))
 18139                {
 18140                    break;
 141                }
 142
 0143                end--;
 0144            }
 145
 18146            int i = 0;
 957147            while (i < end)
 939148            {
 939149                while (i < end && Ignore(data[i]))
 0150                {
 0151                    i++;
 0152                }
 153
 939154                b1 = decodingTable[data[i++]];
 155
 939156                while (i < end && Ignore(data[i]))
 0157                {
 0158                    i++;
 0159                }
 160
 939161                b2 = decodingTable[data[i++]];
 162
 939163                if ((b1 | b2) >= 0x80)
 0164                    throw new IOException("invalid characters encountered in Hex data");
 165
 939166                outStream.WriteByte((byte)((b1 << 4) | b2));
 167
 939168                length++;
 939169            }
 170
 18171            return length;
 18172        }
 173    }
 174}