|
| 1 | +using System.IO; |
| 2 | +using System.Runtime.CompilerServices; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using CT = System.Threading.CancellationToken; |
| 5 | + |
| 6 | +// ReSharper disable MemberCanBePrivate.Global |
| 7 | +// ReSharper disable InconsistentNaming |
| 8 | + |
| 9 | +namespace ICSharpCode.SharpZipLib.Core |
| 10 | +{ |
| 11 | + internal static class ByteOrderStreamExtensions |
| 12 | + { |
| 13 | + internal static byte[] SwappedBytes(ushort value) => new[] {(byte)value, (byte)(value >> 8)}; |
| 14 | + internal static byte[] SwappedBytes(short value) => new[] {(byte)value, (byte)(value >> 8)}; |
| 15 | + internal static byte[] SwappedBytes(uint value) => new[] {(byte)value, (byte)(value >> 8), (byte)(value >> 16), (byte)(value >> 24)}; |
| 16 | + internal static byte[] SwappedBytes(int value) => new[] {(byte)value, (byte)(value >> 8), (byte)(value >> 16), (byte)(value >> 24)}; |
| 17 | + |
| 18 | + internal static byte[] SwappedBytes(long value) => new[] { |
| 19 | + (byte)value, (byte)(value >> 8), (byte)(value >> 16), (byte)(value >> 24), |
| 20 | + (byte)(value >> 32), (byte)(value >> 40), (byte)(value >> 48), (byte)(value >> 56) |
| 21 | + }; |
| 22 | + |
| 23 | + internal static byte[] SwappedBytes(ulong value) => new[] { |
| 24 | + (byte)value, (byte)(value >> 8), (byte)(value >> 16), (byte)(value >> 24), |
| 25 | + (byte)(value >> 32), (byte)(value >> 40), (byte)(value >> 48), (byte)(value >> 56) |
| 26 | + }; |
| 27 | + |
| 28 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 29 | + internal static long SwappedS64(byte[] bytes) => ( |
| 30 | + (long)bytes[0] << 0 | (long)bytes[1] << 8 | (long)bytes[2] << 16 | (long)bytes[3] << 24 | |
| 31 | + (long)bytes[4] << 32 | (long)bytes[5] << 40 | (long)bytes[6] << 48 | (long)bytes[7] << 56); |
| 32 | + |
| 33 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 34 | + internal static ulong SwappedU64(byte[] bytes) => ( |
| 35 | + (ulong)bytes[0] << 0 | (ulong)bytes[1] << 8 | (ulong)bytes[2] << 16 | (ulong)bytes[3] << 24 | |
| 36 | + (ulong)bytes[4] << 32 | (ulong)bytes[5] << 40 | (ulong)bytes[6] << 48 | (ulong)bytes[7] << 56); |
| 37 | + |
| 38 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 39 | + internal static int SwappedS32(byte[] bytes) => bytes[0] | bytes[1] << 8 | bytes[2] << 16 | bytes[3] << 24; |
| 40 | + |
| 41 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 42 | + internal static uint SwappedU32(byte[] bytes) => (uint) SwappedS32(bytes); |
| 43 | + |
| 44 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 45 | + internal static short SwappedS16(byte[] bytes) => (short)(bytes[0] | bytes[1] << 8); |
| 46 | + |
| 47 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 48 | + internal static ushort SwappedU16(byte[] bytes) => (ushort) SwappedS16(bytes); |
| 49 | + |
| 50 | + internal static byte[] ReadBytes(this Stream stream, int count) |
| 51 | + { |
| 52 | + var bytes = new byte[count]; |
| 53 | + var remaining = count; |
| 54 | + while (remaining > 0) |
| 55 | + { |
| 56 | + var bytesRead = stream.Read(bytes, count - remaining, remaining); |
| 57 | + if (bytesRead < 1) throw new EndOfStreamException(); |
| 58 | + remaining -= bytesRead; |
| 59 | + } |
| 60 | + |
| 61 | + return bytes; |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> Read an unsigned short in little endian byte order. </summary> |
| 65 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 66 | + public static int ReadLEShort(this Stream stream) => SwappedS16(ReadBytes(stream, 2)); |
| 67 | + |
| 68 | + /// <summary> Read an int in little endian byte order. </summary> |
| 69 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 70 | + public static int ReadLEInt(this Stream stream) => SwappedS32(ReadBytes(stream, 4)); |
| 71 | + |
| 72 | + /// <summary> Read a long in little endian byte order. </summary> |
| 73 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 74 | + public static long ReadLELong(this Stream stream) => SwappedS64(ReadBytes(stream, 8)); |
| 75 | + |
| 76 | + /// <summary> Write an unsigned short in little endian byte order. </summary> |
| 77 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 78 | + public static void WriteLEShort(this Stream stream, int value) => stream.Write(SwappedBytes(value), 0, 2); |
| 79 | + |
| 80 | + /// <inheritdoc cref="WriteLEShort"/> |
| 81 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 82 | + public static async Task WriteLEShortAsync(this Stream stream, int value, CT ct) |
| 83 | + => await stream.WriteAsync(SwappedBytes(value), 0, 2, ct); |
| 84 | + |
| 85 | + /// <summary> Write a ushort in little endian byte order. </summary> |
| 86 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 87 | + public static void WriteLEUshort(this Stream stream, ushort value) => stream.Write(SwappedBytes(value), 0, 2); |
| 88 | + |
| 89 | + /// <inheritdoc cref="WriteLEUshort"/> |
| 90 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 91 | + public static async Task WriteLEUshortAsync(this Stream stream, ushort value, CT ct) |
| 92 | + => await stream.WriteAsync(SwappedBytes(value), 0, 2, ct); |
| 93 | + |
| 94 | + /// <summary> Write an int in little endian byte order. </summary> |
| 95 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 96 | + public static void WriteLEInt(this Stream stream, int value) => stream.Write(SwappedBytes(value), 0, 4); |
| 97 | + |
| 98 | + /// <inheritdoc cref="WriteLEInt"/> |
| 99 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 100 | + public static async Task WriteLEIntAsync(this Stream stream, int value, CT ct) |
| 101 | + => await stream.WriteAsync(SwappedBytes(value), 0, 4, ct); |
| 102 | + |
| 103 | + /// <summary> Write a uint in little endian byte order. </summary> |
| 104 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 105 | + public static void WriteLEUint(this Stream stream, uint value) => stream.Write(SwappedBytes(value), 0, 4); |
| 106 | + |
| 107 | + /// <inheritdoc cref="WriteLEUint"/> |
| 108 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 109 | + public static async Task WriteLEUintAsync(this Stream stream, uint value, CT ct) |
| 110 | + => await stream.WriteAsync(SwappedBytes(value), 0, 4, ct); |
| 111 | + |
| 112 | + /// <summary> Write a long in little endian byte order. </summary> |
| 113 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 114 | + public static void WriteLELong(this Stream stream, long value) => stream.Write(SwappedBytes(value), 0, 8); |
| 115 | + |
| 116 | + /// <inheritdoc cref="WriteLELong"/> |
| 117 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 118 | + public static async Task WriteLELongAsync(this Stream stream, long value, CT ct) |
| 119 | + => await stream.WriteAsync(SwappedBytes(value), 0, 8, ct); |
| 120 | + |
| 121 | + /// <summary> Write a ulong in little endian byte order. </summary> |
| 122 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 123 | + public static void WriteLEUlong(this Stream stream, ulong value) => stream.Write(SwappedBytes(value), 0, 8); |
| 124 | + |
| 125 | + /// <inheritdoc cref="WriteLEUlong"/> |
| 126 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 127 | + public static async Task WriteLEUlongAsync(this Stream stream, ulong value, CT ct) |
| 128 | + => await stream.WriteAsync(SwappedBytes(value), 0, 8, ct); |
| 129 | + } |
| 130 | +} |
0 commit comments