|
| 1 | +using System; |
| 2 | +using System.Collections.Concurrent; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Text.RegularExpressions; |
| 6 | +using UnshieldSharp.Archive; |
| 7 | + |
| 8 | +namespace BurnOutSharp.FileType |
| 9 | +{ |
| 10 | + internal class InstallShieldArchiveV3 : IScannable |
| 11 | + { |
| 12 | + /// <inheritdoc/> |
| 13 | + public bool ShouldScan(byte[] magic) |
| 14 | + { |
| 15 | + if (magic.StartsWith(new byte?[] { 0x13, 0x5D, 0x65, 0x8C })) |
| 16 | + return true; |
| 17 | + |
| 18 | + return false; |
| 19 | + } |
| 20 | + |
| 21 | + /// <inheritdoc/> |
| 22 | + public ConcurrentDictionary<string, ConcurrentQueue<string>> Scan(Scanner scanner, string file) |
| 23 | + { |
| 24 | + if (!File.Exists(file)) |
| 25 | + return null; |
| 26 | + |
| 27 | + using (var fs = File.OpenRead(file)) |
| 28 | + { |
| 29 | + return Scan(scanner, fs, file); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + // TODO: Add stream opening support |
| 34 | + /// <inheritdoc/> |
| 35 | + public ConcurrentDictionary<string, ConcurrentQueue<string>> Scan(Scanner scanner, Stream stream, string file) |
| 36 | + { |
| 37 | + // Get the name of the first cabinet file or header |
| 38 | + string directory = Path.GetDirectoryName(file); |
| 39 | + string noExtension = Path.GetFileNameWithoutExtension(file); |
| 40 | + string filenamePattern = Path.Combine(directory, noExtension); |
| 41 | + filenamePattern = new Regex(@"\d+$").Replace(filenamePattern, string.Empty); |
| 42 | + |
| 43 | + bool cabinetHeaderExists = File.Exists(Path.Combine(directory, filenamePattern + "1.hdr")); |
| 44 | + bool shouldScanCabinet = cabinetHeaderExists |
| 45 | + ? file.Equals(Path.Combine(directory, filenamePattern + "1.hdr"), StringComparison.OrdinalIgnoreCase) |
| 46 | + : file.Equals(Path.Combine(directory, filenamePattern + "1.cab"), StringComparison.OrdinalIgnoreCase); |
| 47 | + |
| 48 | + // If we have the first file |
| 49 | + if (shouldScanCabinet) |
| 50 | + { |
| 51 | + // If the cab file itself fails |
| 52 | + try |
| 53 | + { |
| 54 | + string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); |
| 55 | + Directory.CreateDirectory(tempPath); |
| 56 | + |
| 57 | + UnshieldSharp.Archive.InstallShieldArchiveV3 archive = new UnshieldSharp.Archive.InstallShieldArchiveV3(file); |
| 58 | + foreach (CompressedFile cfile in archive.Files.Select(kvp => kvp.Value)) |
| 59 | + { |
| 60 | + // If an individual entry fails |
| 61 | + try |
| 62 | + { |
| 63 | + string tempFile = Path.Combine(tempPath, cfile.FullPath); |
| 64 | + if (!Directory.Exists(Path.GetDirectoryName(tempFile))) |
| 65 | + Directory.CreateDirectory(Path.GetDirectoryName(tempFile)); |
| 66 | + |
| 67 | + (byte[] fileContents, string error) = archive.Extract(cfile.FullPath); |
| 68 | + if (!string.IsNullOrWhiteSpace(error)) |
| 69 | + continue; |
| 70 | + |
| 71 | + using (FileStream fs = File.OpenWrite(tempFile)) |
| 72 | + { |
| 73 | + fs.Write(fileContents, 0, fileContents.Length); |
| 74 | + } |
| 75 | + } |
| 76 | + catch { } |
| 77 | + } |
| 78 | + |
| 79 | + // Collect and format all found protections |
| 80 | + var protections = scanner.GetProtections(tempPath); |
| 81 | + |
| 82 | + // If temp directory cleanup fails |
| 83 | + try |
| 84 | + { |
| 85 | + Directory.Delete(tempPath, true); |
| 86 | + } |
| 87 | + catch { } |
| 88 | + |
| 89 | + // Remove temporary path references |
| 90 | + Utilities.StripFromKeys(protections, tempPath); |
| 91 | + |
| 92 | + return protections; |
| 93 | + } |
| 94 | + catch { } |
| 95 | + } |
| 96 | + |
| 97 | + return null; |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments