| | | 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.Buffers; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Text; |
| | | 7 | | |
| | | 8 | | namespace System.IO.Compression; |
| | | 9 | | |
| | | 10 | | internal static partial class ZipHelper |
| | | 11 | | { |
| | | 12 | | internal const uint Mask32Bit = 0xFFFFFFFF; |
| | | 13 | | internal const ushort Mask16Bit = 0xFFFF; |
| | | 14 | | |
| | | 15 | | private const int BackwardsSeekingBufferSize = 4096; |
| | | 16 | | |
| | | 17 | | internal const int ValidZipDate_YearMin = 1980; |
| | | 18 | | internal const int ValidZipDate_YearMax = 2107; |
| | | 19 | | |
| | 1 | 20 | | private static readonly DateTime s_invalidDateIndicator = new DateTime(ValidZipDate_YearMin, 1, 1, 0, 0, 0); |
| | | 21 | | |
| | | 22 | | internal static Encoding GetEncoding(string text) |
| | 0 | 23 | | { |
| | 0 | 24 | | if (text.AsSpan().ContainsAnyExceptInRange((char)32, (char)126)) |
| | 0 | 25 | | { |
| | | 26 | | // The Zip Format uses code page 437 when the Unicode bit is not set. This format |
| | | 27 | | // is the same as ASCII for characters 32-126 but differs otherwise. If we can fit |
| | | 28 | | // the string into CP437 then we treat ASCII as acceptable. |
| | 0 | 29 | | return Encoding.UTF8; |
| | | 30 | | } |
| | | 31 | | |
| | 0 | 32 | | return Encoding.ASCII; |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | // will silently return InvalidDateIndicator if the uint is not a valid Dos DateTime |
| | | 36 | | internal static DateTime DosTimeToDateTime(uint dateTime) |
| | 29756 | 37 | | { |
| | 29756 | 38 | | if (dateTime == 0) |
| | 4448 | 39 | | { |
| | 4448 | 40 | | return s_invalidDateIndicator; |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | // DosTime format 32 bits |
| | | 44 | | // Year: 7 bits, 0 is ValidZipDate_YearMin, unsigned (ValidZipDate_YearMin = 1980) |
| | | 45 | | // Month: 4 bits |
| | | 46 | | // Day: 5 bits |
| | | 47 | | // Hour: 5 |
| | | 48 | | // Minute: 6 bits |
| | | 49 | | // Second: 5 bits |
| | | 50 | | |
| | | 51 | | // do the bit shift as unsigned because the fields are unsigned, but |
| | | 52 | | // we can safely convert to int, because they won't be too big |
| | 25308 | 53 | | int year = (int)(ValidZipDate_YearMin + (dateTime >> 25)); |
| | 25308 | 54 | | int month = (int)((dateTime >> 21) & 0xF); |
| | 25308 | 55 | | int day = (int)((dateTime >> 16) & 0x1F); |
| | 25308 | 56 | | int hour = (int)((dateTime >> 11) & 0x1F); |
| | 25308 | 57 | | int minute = (int)((dateTime >> 5) & 0x3F); |
| | 25308 | 58 | | int second = (int)((dateTime & 0x001F) * 2); // only 5 bits for second, so we only have a granularity of 2 sec. |
| | | 59 | | |
| | | 60 | | try |
| | 25308 | 61 | | { |
| | 25308 | 62 | | return new DateTime(year, month, day, hour, minute, second, 0); |
| | | 63 | | } |
| | 17636 | 64 | | catch (ArgumentOutOfRangeException) |
| | 17636 | 65 | | { |
| | 17636 | 66 | | return s_invalidDateIndicator; |
| | | 67 | | } |
| | 0 | 68 | | catch (ArgumentException) |
| | 0 | 69 | | { |
| | 0 | 70 | | return s_invalidDateIndicator; |
| | | 71 | | } |
| | 29756 | 72 | | } |
| | | 73 | | |
| | | 74 | | // assume date time has passed IsConvertibleToDosTime |
| | | 75 | | internal static uint DateTimeToDosTime(DateTime dateTime) |
| | 0 | 76 | | { |
| | | 77 | | // DateTime must be Convertible to DosTime: |
| | 0 | 78 | | Debug.Assert(ValidZipDate_YearMin <= dateTime.Year && dateTime.Year <= ValidZipDate_YearMax); |
| | | 79 | | |
| | 0 | 80 | | int ret = ((dateTime.Year - ValidZipDate_YearMin) & 0x7F); |
| | 0 | 81 | | ret = (ret << 4) + dateTime.Month; |
| | 0 | 82 | | ret = (ret << 5) + dateTime.Day; |
| | 0 | 83 | | ret = (ret << 5) + dateTime.Hour; |
| | 0 | 84 | | ret = (ret << 6) + dateTime.Minute; |
| | 0 | 85 | | ret = (ret << 5) + (dateTime.Second / 2); // only 5 bits for second, so we only have a granularity of 2 sec. |
| | 0 | 86 | | return (uint)ret; |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | // Assumes all bytes of signatureToFind are non zero, looks backwards from current position in stream, |
| | | 90 | | // assumes maxBytesToRead is positive, ensures to not read beyond the provided max number of bytes, |
| | | 91 | | // if the signature is found then returns true and positions stream at first byte of signature |
| | | 92 | | // if the signature is not found, returns false |
| | | 93 | | internal static bool SeekBackwardsToSignature(Stream stream, ReadOnlySpan<byte> signatureToFind, int maxBytesToRead) |
| | 3015 | 94 | | { |
| | 3015 | 95 | | Debug.Assert(signatureToFind.Length != 0); |
| | 3015 | 96 | | Debug.Assert(maxBytesToRead > 0); |
| | | 97 | | |
| | | 98 | | // This method reads blocks of BackwardsSeekingBufferSize bytes, searching each block for signatureToFind. |
| | | 99 | | // A simple LastIndexOf(signatureToFind) doesn't account for cases where signatureToFind is split, starting in |
| | | 100 | | // one block and ending in another. |
| | | 101 | | // To account for this, we read blocks of BackwardsSeekingBufferSize bytes, but seek backwards by |
| | | 102 | | // [BackwardsSeekingBufferSize - signatureToFind.Length] bytes. This guarantees that signatureToFind will not be |
| | | 103 | | // split between two consecutive blocks, at the cost of reading [signatureToFind.Length] duplicate bytes in each |
| | 3015 | 104 | | int bufferPointer = 0; |
| | 3015 | 105 | | byte[] buffer = ArrayPool<byte>.Shared.Rent(BackwardsSeekingBufferSize); |
| | 3015 | 106 | | Span<byte> bufferSpan = buffer.AsSpan(0, BackwardsSeekingBufferSize); |
| | | 107 | | |
| | | 108 | | try |
| | 3015 | 109 | | { |
| | 3015 | 110 | | bool outOfBytes = false; |
| | 3015 | 111 | | bool signatureFound = false; |
| | | 112 | | |
| | 3015 | 113 | | int totalBytesRead = 0; |
| | | 114 | | |
| | 4212 | 115 | | while (!signatureFound && !outOfBytes && totalBytesRead < maxBytesToRead) |
| | 3015 | 116 | | { |
| | 3015 | 117 | | int overlap = totalBytesRead == 0 ? 0 : signatureToFind.Length; |
| | | 118 | | |
| | 3015 | 119 | | if (maxBytesToRead - totalBytesRead + overlap < bufferSpan.Length) |
| | 1280 | 120 | | { |
| | | 121 | | // If we have less than a full buffer left to read, we adjust the buffer size. |
| | 1280 | 122 | | bufferSpan = bufferSpan.Slice(0, maxBytesToRead - totalBytesRead + overlap); |
| | 1280 | 123 | | } |
| | | 124 | | |
| | 3015 | 125 | | int bytesRead = SeekBackwardsAndRead(stream, bufferSpan, overlap); |
| | | 126 | | |
| | 3015 | 127 | | outOfBytes = bytesRead < bufferSpan.Length; |
| | 3015 | 128 | | if (bytesRead < bufferSpan.Length) |
| | 1734 | 129 | | { |
| | 1734 | 130 | | bufferSpan = bufferSpan.Slice(0, bytesRead); |
| | 1734 | 131 | | } |
| | | 132 | | |
| | 3015 | 133 | | bufferPointer = bufferSpan.LastIndexOf(signatureToFind); |
| | 3015 | 134 | | Debug.Assert(bufferPointer < bufferSpan.Length); |
| | | 135 | | |
| | 3015 | 136 | | totalBytesRead += bytesRead - overlap; |
| | | 137 | | |
| | 3015 | 138 | | if (bufferPointer != -1) |
| | 1818 | 139 | | { |
| | 1818 | 140 | | signatureFound = true; |
| | 1818 | 141 | | break; |
| | | 142 | | } |
| | 1197 | 143 | | } |
| | | 144 | | |
| | 3015 | 145 | | if (!signatureFound) |
| | 1197 | 146 | | { |
| | 1197 | 147 | | return false; |
| | | 148 | | } |
| | | 149 | | else |
| | 1818 | 150 | | { |
| | 1818 | 151 | | stream.Seek(bufferPointer, SeekOrigin.Current); |
| | 1818 | 152 | | return true; |
| | | 153 | | } |
| | | 154 | | } |
| | | 155 | | finally |
| | 3015 | 156 | | { |
| | 3015 | 157 | | ArrayPool<byte>.Shared.Return(buffer); |
| | 3015 | 158 | | } |
| | 3015 | 159 | | } |
| | | 160 | | |
| | | 161 | | // Returns the number of bytes actually read. |
| | | 162 | | // Allows successive buffers to overlap by a number of bytes. This handles cases where |
| | | 163 | | // the value being searched for straddles buffers (i.e. where the first buffer ends with the |
| | | 164 | | // first X bytes being searched for, and the second buffer begins with the remaining bytes.) |
| | | 165 | | private static int SeekBackwardsAndRead(Stream stream, Span<byte> buffer, int overlap) |
| | 3015 | 166 | | { |
| | | 167 | | int bytesRead; |
| | | 168 | | |
| | 3015 | 169 | | if (stream.Position >= buffer.Length) |
| | 1280 | 170 | | { |
| | 1280 | 171 | | Debug.Assert(overlap <= buffer.Length); |
| | 1280 | 172 | | stream.Seek(-(buffer.Length - overlap), SeekOrigin.Current); |
| | 1280 | 173 | | bytesRead = stream.ReadAtLeast(buffer, buffer.Length, throwOnEndOfStream: true); |
| | 1280 | 174 | | stream.Seek(-buffer.Length, SeekOrigin.Current); |
| | 1280 | 175 | | } |
| | | 176 | | else |
| | 1735 | 177 | | { |
| | 1735 | 178 | | int bytesToRead = (int)stream.Position; |
| | 1735 | 179 | | stream.Seek(0, SeekOrigin.Begin); |
| | 1735 | 180 | | bytesRead = stream.ReadAtLeast(buffer, bytesToRead, throwOnEndOfStream: true); |
| | 1735 | 181 | | stream.Seek(0, SeekOrigin.Begin); |
| | 1735 | 182 | | } |
| | | 183 | | |
| | 3015 | 184 | | return bytesRead; |
| | 3015 | 185 | | } |
| | | 186 | | // Converts the specified string into bytes using the optional specified encoding. |
| | | 187 | | // If the encoding null, then the encoding is calculated from the string itself. |
| | | 188 | | // If maxBytes is greater than zero, the returned string will be truncated to a total |
| | | 189 | | // number of characters whose bytes do not add up to more than that number. |
| | | 190 | | internal static byte[] GetEncodedTruncatedBytesFromString(string? text, Encoding? encoding, int maxBytes, out bool i |
| | 0 | 191 | | { |
| | 0 | 192 | | Debug.Assert(maxBytes >= 0, "maxBytes must be non-negative"); |
| | | 193 | | |
| | 0 | 194 | | if (string.IsNullOrEmpty(text)) |
| | 0 | 195 | | { |
| | 0 | 196 | | isUTF8 = false; |
| | 0 | 197 | | return Array.Empty<byte>(); |
| | | 198 | | } |
| | | 199 | | |
| | 0 | 200 | | encoding ??= GetEncoding(text); |
| | 0 | 201 | | isUTF8 = encoding.CodePage == 65001; |
| | | 202 | | |
| | 0 | 203 | | if (maxBytes == 0) |
| | 0 | 204 | | { |
| | 0 | 205 | | return encoding.GetBytes(text); |
| | | 206 | | } |
| | | 207 | | |
| | 0 | 208 | | byte[] bytes = encoding.GetBytes(text); |
| | | 209 | | |
| | 0 | 210 | | if (maxBytes < bytes.Length) |
| | 0 | 211 | | { |
| | 0 | 212 | | if (isUTF8) |
| | 0 | 213 | | { |
| | 0 | 214 | | while ((bytes[maxBytes] & 0xC0) == 0x80) |
| | 0 | 215 | | { |
| | 0 | 216 | | maxBytes--; |
| | 0 | 217 | | } |
| | 0 | 218 | | } |
| | | 219 | | |
| | 0 | 220 | | bytes = bytes[0..maxBytes]; |
| | 0 | 221 | | } |
| | | 222 | | |
| | 0 | 223 | | return bytes; |
| | 0 | 224 | | } |
| | | 225 | | } |