< Summary

Information
Class: Renci.SshNet.Security.Chaos.NaCl.Internal.ByteIntegerConverter
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\Chaos.NaCl\Internal\ByteIntegerConverter.cs
Line coverage
39%
Covered lines: 39
Uncovered lines: 59
Coverable lines: 98
Total lines: 416
Line coverage: 39.7%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

\home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\Chaos.NaCl\Internal\ByteIntegerConverter.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3
 4namespace Renci.SshNet.Security.Chaos.NaCl.Internal
 5{
 6    // Loops? Arrays? Never heard of that stuff
 7    // Library avoids unnecessary heap allocations and unsafe code
 8    // so this ugly code becomes necessary :(
 9    internal static class ByteIntegerConverter
 10    {
 11        #region Individual
 12
 13        internal static UInt32 LoadLittleEndian32(byte[] buf, int offset)
 014        {
 015            return
 016                (UInt32)(buf[offset + 0])
 017            | (((UInt32)(buf[offset + 1])) << 8)
 018            | (((UInt32)(buf[offset + 2])) << 16)
 019            | (((UInt32)(buf[offset + 3])) << 24);
 020        }
 21
 22        internal static void StoreLittleEndian32(byte[] buf, int offset, UInt32 value)
 023        {
 024            buf[offset + 0] = unchecked((byte)value);
 025            buf[offset + 1] = unchecked((byte)(value >> 8));
 026            buf[offset + 2] = unchecked((byte)(value >> 16));
 027            buf[offset + 3] = unchecked((byte)(value >> 24));
 028        }
 29
 30        internal static UInt64 LoadBigEndian64(byte[] buf, int offset)
 24031        {
 24032            return
 24033                (UInt64)(buf[offset + 7])
 24034                | (((UInt64)(buf[offset + 6])) << 8)
 24035                | (((UInt64)(buf[offset + 5])) << 16)
 24036                | (((UInt64)(buf[offset + 4])) << 24)
 24037                | (((UInt64)(buf[offset + 3])) << 32)
 24038                | (((UInt64)(buf[offset + 2])) << 40)
 24039                | (((UInt64)(buf[offset + 1])) << 48)
 24040                | (((UInt64)(buf[offset + 0])) << 56);
 24041        }
 42
 43        internal static void StoreBigEndian64(byte[] buf, int offset, UInt64 value)
 10444        {
 10445            buf[offset + 7] = unchecked((byte)value);
 10446            buf[offset + 6] = unchecked((byte)(value >> 8));
 10447            buf[offset + 5] = unchecked((byte)(value >> 16));
 10448            buf[offset + 4] = unchecked((byte)(value >> 24));
 10449            buf[offset + 3] = unchecked((byte)(value >> 32));
 10450            buf[offset + 2] = unchecked((byte)(value >> 40));
 10451            buf[offset + 1] = unchecked((byte)(value >> 48));
 10452            buf[offset + 0] = unchecked((byte)(value >> 56));
 10453        }
 54
 55        /*internal static void XorLittleEndian32(byte[] buf, int offset, UInt32 value)
 56        {
 57            buf[offset + 0] ^= (byte)value;
 58            buf[offset + 1] ^= (byte)(value >> 8);
 59            buf[offset + 2] ^= (byte)(value >> 16);
 60            buf[offset + 3] ^= (byte)(value >> 24);
 61        }*/
 62
 63        /*internal static void XorLittleEndian32(byte[] output, int outputOffset, byte[] input, int inputOffset, UInt32 
 64        {
 65            output[outputOffset + 0] = (byte)(input[inputOffset + 0] ^ value);
 66            output[outputOffset + 1] = (byte)(input[inputOffset + 1] ^ (value >> 8));
 67            output[outputOffset + 2] = (byte)(input[inputOffset + 2] ^ (value >> 16));
 68            output[outputOffset + 3] = (byte)(input[inputOffset + 3] ^ (value >> 24));
 69        }*/
 70
 71        #endregion
 72
 73        #region Array8
 74
 75        internal static void Array8LoadLittleEndian32(out Array8<UInt32> output, byte[] input, int inputOffset)
 076        {
 077            output.x0 = LoadLittleEndian32(input, inputOffset + 0);
 078            output.x1 = LoadLittleEndian32(input, inputOffset + 4);
 079            output.x2 = LoadLittleEndian32(input, inputOffset + 8);
 080            output.x3 = LoadLittleEndian32(input, inputOffset + 12);
 081            output.x4 = LoadLittleEndian32(input, inputOffset + 16);
 082            output.x5 = LoadLittleEndian32(input, inputOffset + 20);
 083            output.x6 = LoadLittleEndian32(input, inputOffset + 24);
 084            output.x7 = LoadLittleEndian32(input, inputOffset + 28);
 085        }
 86
 87        /*        internal static void Array8LoadLittleEndian32(out Array8<uint> output, byte[] input, int inputOffset, 
 88                {
 89        #if DEBUG
 90                    if (inputLength <= 0)
 91                        throw new ArgumentException();
 92        #endif
 93                    int inputEnd = inputOffset + inputLength;
 94                    UInt32 highestInt;
 95                    switch (inputLength & 3)
 96                    {
 97                        case 1:
 98                            highestInt = input[inputEnd - 1];
 99                            break;
 100                        case 2:
 101                            highestInt = (uint)(
 102                                (input[inputEnd - 1] << 8) |
 103                                (input[inputEnd - 2]));
 104                            break;
 105                        case 3:
 106                            highestInt = (uint)(
 107                                (input[inputEnd - 1] << 16) |
 108                                (input[inputEnd - 2] << 8) |
 109                                (input[inputEnd - 3]));
 110                            break;
 111                        case 0:
 112                            highestInt = (uint)(
 113                                (input[inputEnd - 1] << 24) |
 114                                (input[inputEnd - 2] << 16) |
 115                                (input[inputEnd - 3] << 8) |
 116                                (input[inputEnd - 4]));
 117                            break;
 118                        default:
 119                            throw new InvalidOperationException();
 120                    }
 121                    switch ((inputLength - 1) >> 2)
 122                    {
 123                        case 7:
 124                            output.x7 = highestInt;
 125                            output.x6 = LoadLittleEndian32(input, inputOffset + 6 * 4);
 126                            output.x5 = LoadLittleEndian32(input, inputOffset + 5 * 4);
 127                            output.x4 = LoadLittleEndian32(input, inputOffset + 4 * 4);
 128                            output.x3 = LoadLittleEndian32(input, inputOffset + 3 * 4);
 129                            output.x2 = LoadLittleEndian32(input, inputOffset + 2 * 4);
 130                            output.x1 = LoadLittleEndian32(input, inputOffset + 1 * 4);
 131                            output.x0 = LoadLittleEndian32(input, inputOffset + 0 * 4);
 132                            return;
 133                        case 6:
 134                            output.x7 = 0;
 135                            output.x6 = highestInt;
 136                            output.x5 = LoadLittleEndian32(input, inputOffset + 5 * 4);
 137                            output.x4 = LoadLittleEndian32(input, inputOffset + 4 * 4);
 138                            output.x3 = LoadLittleEndian32(input, inputOffset + 3 * 4);
 139                            output.x2 = LoadLittleEndian32(input, inputOffset + 2 * 4);
 140                            output.x1 = LoadLittleEndian32(input, inputOffset + 1 * 4);
 141                            output.x0 = LoadLittleEndian32(input, inputOffset + 0 * 4);
 142                            return;
 143                        case 5:
 144                            output.x7 = 0;
 145                            output.x6 = 0;
 146                            output.x5 = highestInt;
 147                            output.x4 = LoadLittleEndian32(input, inputOffset + 4 * 4);
 148                            output.x3 = LoadLittleEndian32(input, inputOffset + 3 * 4);
 149                            output.x2 = LoadLittleEndian32(input, inputOffset + 2 * 4);
 150                            output.x1 = LoadLittleEndian32(input, inputOffset + 1 * 4);
 151                            output.x0 = LoadLittleEndian32(input, inputOffset + 0 * 4);
 152                            return;
 153                        case 4:
 154                            output.x7 = 0;
 155                            output.x6 = 0;
 156                            output.x5 = 0;
 157                            output.x4 = highestInt;
 158                            output.x3 = LoadLittleEndian32(input, inputOffset + 3 * 4);
 159                            output.x2 = LoadLittleEndian32(input, inputOffset + 2 * 4);
 160                            output.x1 = LoadLittleEndian32(input, inputOffset + 1 * 4);
 161                            output.x0 = LoadLittleEndian32(input, inputOffset + 0 * 4);
 162                            return;
 163                        case 3:
 164                            output.x7 = 0;
 165                            output.x6 = 0;
 166                            output.x5 = 0;
 167                            output.x4 = 0;
 168                            output.x3 = highestInt;
 169                            output.x2 = LoadLittleEndian32(input, inputOffset + 2 * 4);
 170                            output.x1 = LoadLittleEndian32(input, inputOffset + 1 * 4);
 171                            output.x0 = LoadLittleEndian32(input, inputOffset + 0 * 4);
 172                            return;
 173                        case 2:
 174                            output.x7 = 0;
 175                            output.x6 = 0;
 176                            output.x5 = 0;
 177                            output.x4 = 0;
 178                            output.x3 = 0;
 179                            output.x2 = highestInt;
 180                            output.x1 = LoadLittleEndian32(input, inputOffset + 1 * 4);
 181                            output.x0 = LoadLittleEndian32(input, inputOffset + 0 * 4);
 182                            return;
 183                        case 1:
 184                            output.x7 = 0;
 185                            output.x6 = 0;
 186                            output.x5 = 0;
 187                            output.x4 = 0;
 188                            output.x3 = 0;
 189                            output.x2 = 0;
 190                            output.x1 = highestInt;
 191                            output.x0 = LoadLittleEndian32(input, inputOffset + 0 * 4);
 192                            return;
 193                        case 0:
 194                            output.x7 = 0;
 195                            output.x6 = 0;
 196                            output.x5 = 0;
 197                            output.x4 = 0;
 198                            output.x3 = 0;
 199                            output.x2 = 0;
 200                            output.x1 = 0;
 201                            output.x0 = highestInt;
 202                            return;
 203                        default:
 204                            throw new InvalidOperationException();
 205                    }
 206                }*/
 207
 208        /*internal static void Array8XorLittleEndian(byte[] output, int outputOffset, byte[] input, int inputOffset, ref
 209        {
 210#if DEBUG
 211            InternalAssert(length > 0);
 212#endif
 213            int outputEnd = outputOffset + length;
 214            UInt32 highestInt;
 215            switch ((length - 1) >> 2)
 216            {
 217                case 7:
 218                    highestInt = keyStream.x7;
 219                    XorLittleEndian32(output, outputOffset + 6 * 4, input, inputOffset + 6 * 4, keyStream.x6);
 220                    XorLittleEndian32(output, outputOffset + 5 * 4, input, inputOffset + 6 * 4, keyStream.x5);
 221                    XorLittleEndian32(output, outputOffset + 4 * 4, input, inputOffset + 6 * 4, keyStream.x4);
 222                    XorLittleEndian32(output, outputOffset + 3 * 4, input, inputOffset + 6 * 4, keyStream.x3);
 223                    XorLittleEndian32(output, outputOffset + 2 * 4, input, inputOffset + 6 * 4, keyStream.x2);
 224                    XorLittleEndian32(output, outputOffset + 1 * 4, input, inputOffset + 6 * 4, keyStream.x1);
 225                    XorLittleEndian32(output, outputOffset + 0 * 4, input, inputOffset + 6 * 4, keyStream.x0);
 226                    break;
 227                case 6:
 228                    highestInt = keyStream.x6;
 229                    XorLittleEndian32(output, outputOffset + 5 * 4, input, inputOffset + 6 * 4, keyStream.x5);
 230                    XorLittleEndian32(output, outputOffset + 4 * 4, input, inputOffset + 6 * 4, keyStream.x4);
 231                    XorLittleEndian32(output, outputOffset + 3 * 4, input, inputOffset + 6 * 4, keyStream.x3);
 232                    XorLittleEndian32(output, outputOffset + 2 * 4, input, inputOffset + 6 * 4, keyStream.x2);
 233                    XorLittleEndian32(output, outputOffset + 1 * 4, input, inputOffset + 6 * 4, keyStream.x1);
 234                    XorLittleEndian32(output, outputOffset + 0 * 4, input, inputOffset + 6 * 4, keyStream.x0);
 235                    break;
 236                case 5:
 237                    highestInt = keyStream.x5;
 238                    XorLittleEndian32(output, outputOffset + 4 * 4, input, inputOffset + 6 * 4, keyStream.x4);
 239                    XorLittleEndian32(output, outputOffset + 3 * 4, input, inputOffset + 6 * 4, keyStream.x3);
 240                    XorLittleEndian32(output, outputOffset + 2 * 4, input, inputOffset + 6 * 4, keyStream.x2);
 241                    XorLittleEndian32(output, outputOffset + 1 * 4, input, inputOffset + 6 * 4, keyStream.x1);
 242                    XorLittleEndian32(output, outputOffset + 0 * 4, input, inputOffset + 6 * 4, keyStream.x0);
 243                    break;
 244                case 4:
 245                    highestInt = keyStream.x4;
 246                    XorLittleEndian32(output, outputOffset + 3 * 4, input, inputOffset + 6 * 4, keyStream.x3);
 247                    XorLittleEndian32(output, outputOffset + 2 * 4, input, inputOffset + 6 * 4, keyStream.x2);
 248                    XorLittleEndian32(output, outputOffset + 1 * 4, input, inputOffset + 6 * 4, keyStream.x1);
 249                    XorLittleEndian32(output, outputOffset + 0 * 4, input, inputOffset + 6 * 4, keyStream.x0);
 250                    break;
 251                case 3:
 252                    highestInt = keyStream.x3;
 253                    XorLittleEndian32(output, outputOffset + 2 * 4, input, inputOffset + 6 * 4, keyStream.x2);
 254                    XorLittleEndian32(output, outputOffset + 1 * 4, input, inputOffset + 6 * 4, keyStream.x1);
 255                    XorLittleEndian32(output, outputOffset + 0 * 4, input, inputOffset + 6 * 4, keyStream.x0);
 256                    break;
 257                case 2:
 258                    highestInt = keyStream.x2;
 259                    XorLittleEndian32(output, outputOffset + 1 * 4, input, inputOffset + 6 * 4, keyStream.x1);
 260                    XorLittleEndian32(output, outputOffset + 0 * 4, input, inputOffset + 6 * 4, keyStream.x0);
 261                    break;
 262                case 1:
 263                    highestInt = keyStream.x1;
 264                    XorLittleEndian32(output, outputOffset + 0 * 4, input, inputOffset + 6 * 4, keyStream.x0);
 265                    break;
 266                case 0:
 267                    highestInt = keyStream.x0;
 268                    break;
 269                default:
 270                    throw new InvalidOperationException();
 271            }
 272            switch (length & 3)
 273            {
 274                case 1:
 275                    output[outputEnd - 1] ^= (byte)highestInt;
 276                    break;
 277                case 2:
 278                    output[outputEnd - 1] ^= (byte)(highestInt >> 8);
 279                    output[outputEnd - 2] ^= (byte)highestInt;
 280                    break;
 281                case 3:
 282                    output[outputEnd - 1] ^= (byte)(highestInt >> 16);
 283                    output[outputEnd - 2] ^= (byte)(highestInt >> 8);
 284                    output[outputEnd - 3] ^= (byte)highestInt;
 285                    break;
 286                case 0:
 287                    output[outputEnd - 1] ^= (byte)(highestInt >> 24);
 288                    output[outputEnd - 2] ^= (byte)(highestInt >> 16);
 289                    output[outputEnd - 3] ^= (byte)(highestInt >> 8);
 290                    output[outputEnd - 4] ^= (byte)highestInt;
 291                    break;
 292                default:
 293                    throw new InvalidOperationException();
 294            }
 295        }*/
 296
 297        /*internal static void Array8StoreLittleEndian32(byte[] output, int outputOffset, ref Array8<uint> input)
 298        {
 299            StoreLittleEndian32(output, outputOffset + 0, input.x0);
 300            StoreLittleEndian32(output, outputOffset + 4, input.x1);
 301            StoreLittleEndian32(output, outputOffset + 8, input.x2);
 302            StoreLittleEndian32(output, outputOffset + 12, input.x3);
 303            StoreLittleEndian32(output, outputOffset + 16, input.x4);
 304            StoreLittleEndian32(output, outputOffset + 20, input.x5);
 305            StoreLittleEndian32(output, outputOffset + 24, input.x6);
 306            StoreLittleEndian32(output, outputOffset + 28, input.x7);
 307        }*/
 308        #endregion
 309
 310        internal static void Array16LoadBigEndian64(out Array16<UInt64> output, byte[] input, int inputOffset)
 15311        {
 15312            output.x0 = LoadBigEndian64(input, inputOffset + 0);
 15313            output.x1 = LoadBigEndian64(input, inputOffset + 8);
 15314            output.x2 = LoadBigEndian64(input, inputOffset + 16);
 15315            output.x3 = LoadBigEndian64(input, inputOffset + 24);
 15316            output.x4 = LoadBigEndian64(input, inputOffset + 32);
 15317            output.x5 = LoadBigEndian64(input, inputOffset + 40);
 15318            output.x6 = LoadBigEndian64(input, inputOffset + 48);
 15319            output.x7 = LoadBigEndian64(input, inputOffset + 56);
 15320            output.x8 = LoadBigEndian64(input, inputOffset + 64);
 15321            output.x9 = LoadBigEndian64(input, inputOffset + 72);
 15322            output.x10 = LoadBigEndian64(input, inputOffset + 80);
 15323            output.x11 = LoadBigEndian64(input, inputOffset + 88);
 15324            output.x12 = LoadBigEndian64(input, inputOffset + 96);
 15325            output.x13 = LoadBigEndian64(input, inputOffset + 104);
 15326            output.x14 = LoadBigEndian64(input, inputOffset + 112);
 15327            output.x15 = LoadBigEndian64(input, inputOffset + 120);
 15328        }
 329
 330        // ToDo: Only used in tests. Remove?
 331        internal static void Array16LoadLittleEndian32(out Array16<UInt32> output, byte[] input, int inputOffset)
 0332        {
 0333            output.x0 = LoadLittleEndian32(input, inputOffset + 0);
 0334            output.x1 = LoadLittleEndian32(input, inputOffset + 4);
 0335            output.x2 = LoadLittleEndian32(input, inputOffset + 8);
 0336            output.x3 = LoadLittleEndian32(input, inputOffset + 12);
 0337            output.x4 = LoadLittleEndian32(input, inputOffset + 16);
 0338            output.x5 = LoadLittleEndian32(input, inputOffset + 20);
 0339            output.x6 = LoadLittleEndian32(input, inputOffset + 24);
 0340            output.x7 = LoadLittleEndian32(input, inputOffset + 28);
 0341            output.x8 = LoadLittleEndian32(input, inputOffset + 32);
 0342            output.x9 = LoadLittleEndian32(input, inputOffset + 36);
 0343            output.x10 = LoadLittleEndian32(input, inputOffset + 40);
 0344            output.x11 = LoadLittleEndian32(input, inputOffset + 44);
 0345            output.x12 = LoadLittleEndian32(input, inputOffset + 48);
 0346            output.x13 = LoadLittleEndian32(input, inputOffset + 52);
 0347            output.x14 = LoadLittleEndian32(input, inputOffset + 56);
 0348            output.x15 = LoadLittleEndian32(input, inputOffset + 60);
 0349        }
 350
 351        /*internal static void Array16LoadLittleEndian32(out Array16<UInt32> output, byte[] input, int inputOffset, int 
 352        {
 353            Array8<UInt32> temp;
 354            if (inputLength > 32)
 355            {
 356                output.x0 = LoadLittleEndian32(input, inputOffset + 0);
 357                output.x1 = LoadLittleEndian32(input, inputOffset + 4);
 358                output.x2 = LoadLittleEndian32(input, inputOffset + 8);
 359                output.x3 = LoadLittleEndian32(input, inputOffset + 12);
 360                output.x4 = LoadLittleEndian32(input, inputOffset + 16);
 361                output.x5 = LoadLittleEndian32(input, inputOffset + 20);
 362                output.x6 = LoadLittleEndian32(input, inputOffset + 24);
 363                output.x7 = LoadLittleEndian32(input, inputOffset + 28);
 364                Array8LoadLittleEndian32(out temp, input, inputOffset + 32, inputLength - 32);
 365                output.x8 = temp.x0;
 366                output.x9 = temp.x1;
 367                output.x10 = temp.x2;
 368                output.x11 = temp.x3;
 369                output.x12 = temp.x4;
 370                output.x13 = temp.x5;
 371                output.x14 = temp.x6;
 372                output.x15 = temp.x7;
 373            }
 374            else
 375            {
 376                Array8LoadLittleEndian32(out temp, input, inputOffset, inputLength);
 377                output.x0 = temp.x0;
 378                output.x1 = temp.x1;
 379                output.x2 = temp.x2;
 380                output.x3 = temp.x3;
 381                output.x4 = temp.x4;
 382                output.x5 = temp.x5;
 383                output.x6 = temp.x6;
 384                output.x7 = temp.x7;
 385                output.x8 = 0;
 386                output.x9 = 0;
 387                output.x10 = 0;
 388                output.x11 = 0;
 389                output.x12 = 0;
 390                output.x13 = 0;
 391                output.x14 = 0;
 392                output.x15 = 0;
 393            }
 394        }*/
 395
 396        internal static void Array16StoreLittleEndian32(byte[] output, int outputOffset, ref Array16<UInt32> input)
 0397        {
 0398            StoreLittleEndian32(output, outputOffset + 0, input.x0);
 0399            StoreLittleEndian32(output, outputOffset + 4, input.x1);
 0400            StoreLittleEndian32(output, outputOffset + 8, input.x2);
 0401            StoreLittleEndian32(output, outputOffset + 12, input.x3);
 0402            StoreLittleEndian32(output, outputOffset + 16, input.x4);
 0403            StoreLittleEndian32(output, outputOffset + 20, input.x5);
 0404            StoreLittleEndian32(output, outputOffset + 24, input.x6);
 0405            StoreLittleEndian32(output, outputOffset + 28, input.x7);
 0406            StoreLittleEndian32(output, outputOffset + 32, input.x8);
 0407            StoreLittleEndian32(output, outputOffset + 36, input.x9);
 0408            StoreLittleEndian32(output, outputOffset + 40, input.x10);
 0409            StoreLittleEndian32(output, outputOffset + 44, input.x11);
 0410            StoreLittleEndian32(output, outputOffset + 48, input.x12);
 0411            StoreLittleEndian32(output, outputOffset + 52, input.x13);
 0412            StoreLittleEndian32(output, outputOffset + 56, input.x14);
 0413            StoreLittleEndian32(output, outputOffset + 60, input.x15);
 0414        }
 415    }
 416}