-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathPackageParser.cs
1014 lines (879 loc) · 46.5 KB
/
PackageParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
namespace Microsoft.WingetCreateCore
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;
using Microsoft.Deployment.WindowsInstaller.Linq;
using Microsoft.Msix.Utils;
using Microsoft.Msix.Utils.AppxPackaging;
using Microsoft.Msix.Utils.AppxPackagingInterop;
using Microsoft.WingetCreateCore.Common;
using Microsoft.WingetCreateCore.Common.Exceptions;
using Microsoft.WingetCreateCore.Models;
using Microsoft.WingetCreateCore.Models.DefaultLocale;
using Microsoft.WingetCreateCore.Models.Installer;
using Microsoft.WingetCreateCore.Models.Version;
using Newtonsoft.Json;
using Vestris.ResourceLib;
/// <summary>
/// Provides functionality for a parsing and extracting relevant metadata from a given package.
/// </summary>
public static class PackageParser
{
/// <summary>
/// The default path where downloaded installers are stored.
/// </summary>
public static readonly string DefaultInstallerDownloadPath = Path.Combine(Path.GetTempPath(), Constants.ProgramName);
private const string InvalidCharacters = "©|®";
private static readonly string[] KnownInstallerResourceNames = new[]
{
"inno",
"nullsoft",
};
private static HttpClient httpClient = new HttpClient();
private enum MachineType
{
X86 = 0x014c,
X64 = 0x8664,
Arm = 0x01c0,
Armv7 = 0x01c4,
Arm64 = 0xaa64,
}
private enum CompatibilitySet
{
None,
Exe,
Msi,
Msix,
}
/// <summary>
/// Gets or sets the path in the %TEMP% directory where installers are downloaded to.
/// </summary>
public static string InstallerDownloadPath { get; set; } = DefaultInstallerDownloadPath;
/// <summary>
/// Sets the HttpMessageHandler used for the static HttpClient.
/// </summary>
/// <param name="httpMessageHandler">Optional HttpMessageHandler to override default HttpClient behavior.</param>
public static void SetHttpMessageHandler(HttpMessageHandler httpMessageHandler)
{
httpClient.Dispose();
httpClient = httpMessageHandler != null ? new HttpClient(httpMessageHandler) : new HttpClient();
}
/// <summary>
/// Parses packages for available metadata including Version, Publisher, Name, Descripion, License, etc.
/// </summary>
/// <param name="installerMetadataList">List of <see cref="InstallerMetadata"/>.</param>
/// <param name="manifests">Wrapper object for manifest object models.</param>
public static void ParsePackages(List<InstallerMetadata> installerMetadataList, Manifests manifests)
{
manifests.VersionManifest = new VersionManifest();
manifests.InstallerManifest = new InstallerManifest();
manifests.DefaultLocaleManifest = new DefaultLocaleManifest();
List<string> parseFailedInstallerUrls = new List<string>();
foreach (var installerMetadata in installerMetadataList)
{
if (!ParsePackage(installerMetadata, manifests))
{
parseFailedInstallerUrls.Add(installerMetadata.InstallerUrl);
}
}
if (parseFailedInstallerUrls.Any())
{
throw new ParsePackageException(parseFailedInstallerUrls);
}
}
/// <summary>
/// Download file at specified URL to temp directory, unless it's already present.
/// </summary>
/// <param name="url">The URL of the file to be downloaded.</param>
/// <param name="maxDownloadSize">The maximum file size in bytes to download.</param>
/// <returns>Path of downloaded, or previously downloaded, file.</returns>
public static async Task<string> DownloadFileAsync(string url, long? maxDownloadSize = null)
{
var response = await httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
int redirectCount = 0;
while (response.StatusCode == System.Net.HttpStatusCode.Redirect && redirectCount < 2)
{
var redirectUri = response.Headers.Location;
response = await httpClient.GetAsync(redirectUri, HttpCompletionOption.ResponseHeadersRead);
redirectCount++;
}
if (!response.IsSuccessStatusCode)
{
string message = await response.Content.ReadAsStringAsync();
throw new HttpRequestException(message, null, response.StatusCode);
}
long? downloadSize = response.Content.Headers.ContentLength;
if (downloadSize > maxDownloadSize)
{
throw new DownloadSizeExceededException(maxDownloadSize.Value);
}
string urlFile = Path.GetFileName(url.Split('?').Last());
string contentDispositionFile = response.Content.Headers.ContentDisposition?.FileName?.Trim('"');
string requestUrlFileName = Path.GetFileName(response.RequestMessage?.RequestUri?.ToString());
if (!Directory.Exists(InstallerDownloadPath))
{
Directory.CreateDirectory(InstallerDownloadPath);
}
// If no relevant filename can be obtained for the installer download, use a temporary filename as last option.
string targetFileName = contentDispositionFile.NullIfEmpty() ?? urlFile.NullIfEmpty() ?? requestUrlFileName.NullIfEmpty() ?? Path.GetTempFileName();
string targetFile = GetNumericFilename(Path.Combine(InstallerDownloadPath, targetFileName));
using var targetFileStream = File.OpenWrite(targetFile);
var contentStream = await response.Content.ReadAsStreamAsync();
/*
* There seems to be a difference between the test environments and a users environment
* On the users environment, the stream cannot seek and will always have position 0 when
* the response content is read into it. In the unit test environment, the stream can
* seek and will not reset the position when the response content is read in. This logic
* should always ensure that the position is reset to 0 regardless of the environment.
*/
if (contentStream.CanSeek)
{
contentStream.Position = 0;
}
await contentStream.CopyToAsync(targetFileStream);
return targetFile;
}
/// <summary>
/// When creating a file, inserts a number into the desired filename when other files
/// with the same name exist in the target directory.
/// </summary>
/// <param name="desiredPath">The path where the new file would be created.</param>
/// <returns>The path where the new file should be created.</returns>
public static string GetNumericFilename(string desiredPath)
{
string fileName = Path.GetFileNameWithoutExtension(desiredPath);
string fileExt = Path.GetExtension(desiredPath);
string fileDir = Path.GetDirectoryName(desiredPath);
DirectoryInfo dir = new DirectoryInfo(fileDir);
FileInfo[] existingFiles = dir.GetFiles(Path.ChangeExtension(fileName + "*", fileExt));
return existingFiles.Length == 0 ? Path.Combine(fileDir, fileName + fileExt) : Path.Combine(fileDir, fileName + " (" + existingFiles.Length.ToString() + ")" + fileExt);
}
/// <summary>
/// Computes the SHA256 hash of a file given its file path.
/// </summary>
/// <param name="path">Path to file to be hashed.</param>
/// <returns>The computed SHA256 hash string.</returns>
public static string GetFileHash(string path)
{
using Stream stream = File.OpenRead(path);
using var hasher = SHA256.Create();
return BitConverter.ToString(hasher.ComputeHash(stream)).Remove("-");
}
/// <summary>
/// Update InstallerManifest's Installer nodes using the provided list of InstallerMetadata objects.
/// </summary>
/// <param name="installerMetadataList">List of <see cref="InstallerMetadata"/>.</param>
/// <param name="installerManifest"><see cref="InstallerManifest"/> to update.</param>
public static void UpdateInstallerNodesAsync(List<InstallerMetadata> installerMetadataList, InstallerManifest installerManifest)
{
var existingInstallers = new List<Installer>(installerManifest.Installers);
List<Installer> unmatchedInstallers = new List<Installer>();
List<Installer> multipleMatchedInstallers = new List<Installer>();
List<string> parseFailedInstallerUrls = new List<string>();
foreach (var installerMetadata in installerMetadataList)
{
if (!ParsePackageAndGenerateInstallerNodes(installerMetadata, null))
{
parseFailedInstallerUrls.Add(installerMetadata.InstallerUrl);
}
}
int numOfNewInstallers = installerMetadataList.Sum(x => x.NewInstallers.Count);
// We only allow updating manifests with the same package count
if (numOfNewInstallers != existingInstallers.Count)
{
throw new InvalidOperationException();
}
if (parseFailedInstallerUrls.Any())
{
throw new ParsePackageException(parseFailedInstallerUrls);
}
Dictionary<Installer, Installer> installerMatchDict = new Dictionary<Installer, Installer>();
// Update previous installers with parsed data from downloaded packages
foreach (var installerUpdate in installerMetadataList)
{
foreach (var newInstaller in installerUpdate.NewInstallers)
{
// if the installerUpdate does not have a binary or url architecture specified, then just use what is specified in the installer.
Installer existingInstallerMatch = FindInstallerMatch(
newInstaller,
existingInstallers,
installerManifest,
installerUpdate,
ref unmatchedInstallers,
ref multipleMatchedInstallers);
// If a match is found, add match to dictionary and remove for list of existingInstallers
if (existingInstallerMatch != null)
{
installerMatchDict.Add(existingInstallerMatch, newInstaller);
existingInstallers.Remove(existingInstallerMatch);
}
else
{
continue;
}
}
}
if (unmatchedInstallers.Any() || multipleMatchedInstallers.Any())
{
bool isArchitectureOverride = installerMetadataList.Any(x => x.OverrideArchitecture.HasValue);
throw new InstallerMatchException(multipleMatchedInstallers, unmatchedInstallers, isArchitectureOverride);
}
else
{
foreach (var existingInstaller in installerMatchDict.Keys)
{
UpdateInstallerMetadata(existingInstaller, installerMatchDict[existingInstaller]);
}
}
}
/// <summary>
/// Parses the package for relevant metadata and and updates the metadata of the provided installer node.
/// </summary>
/// <param name="installer">Installer node.</param>
/// <param name="path">Path to package file.</param>
/// <param name="url">Installer url.</param>
/// <returns>Boolean indicating whether the package parse was successful.</returns>
public static bool ParsePackageAndUpdateInstallerNode(Installer installer, string path, string url)
{
List<Installer> newInstallers = new List<Installer>();
bool parseResult = ParseExeInstallerType(path, installer, newInstallers) ||
ParseMsix(path, installer, null, newInstallers) ||
ParseMsi(path, installer, null, newInstallers);
if (!parseResult || !newInstallers.Any())
{
return false;
}
Installer newInstaller = newInstallers.First();
if (newInstallers.Count > 1)
{
// For multiple installers in an AppxBundle, use the existing architecture to avoid matching conflicts.
newInstaller.Architecture = installer.Architecture;
}
else
{
// For a single installer, detect the architecture. If no architecture is detected, default to architecture from existing manifest.
newInstaller.Architecture = GetArchFromUrl(url) ?? GetMachineType(path)?.ToString().ToEnumOrDefault<Architecture>() ?? installer.Architecture;
}
newInstaller.InstallerUrl = url;
newInstaller.InstallerSha256 = GetFileHash(path);
UpdateInstallerMetadata(installer, newInstallers.First());
return true;
}
/// <summary>
/// Creates a new Installer object that is a copy of the provided Installer.
/// </summary>
/// <param name="installer">Installer object to be cloned.</param>
/// <returns>A new cloned Installer object.</returns>
public static Installer CloneInstaller(Installer installer)
{
string json = JsonConvert.SerializeObject(installer);
return JsonConvert.DeserializeObject<Installer>(json);
}
/// <summary>
/// Finds an existing installer that matches the new installer by checking the installerType and the following:
/// 1. Matching based on architecture specified as an override if present.
/// 2. Matching based on architecture detected from URL string if present.
/// 3. If no singular match is found based on architecture, use scope to narrow down the match results if a scope override is present.
/// </summary>
/// <param name="newInstaller">New installer to be matched.</param>
/// <param name="existingInstallers">List of existing installers to be matched.</param>
/// <param name="installerManifest">Installer manifest.</param>
/// <param name="installerMetadata">Helper class for storing an installer's metadata information.</param>
/// <param name="unmatchedInstallers">List of unmatched installers.</param>
/// <param name="multipleMatchedInstallers">List of installers with multiple matches..</param>
/// <returns>The installer match from the list of existing installers.</returns>
private static Installer FindInstallerMatch(
Installer newInstaller,
List<Installer> existingInstallers,
InstallerManifest installerManifest,
InstallerMetadata installerMetadata,
ref List<Installer> unmatchedInstallers,
ref List<Installer> multipleMatchedInstallers)
{
// If we can find an exact match by comparing the installerType and the override architecture, then return match.
// Otherwise, continue and try matching based on arch detected from url and binary detection, as the user could be trying overwrite with a new architecture.
var installerTypeMatches = existingInstallers.Where(
i => (i.InstallerType ?? installerManifest.InstallerType) == newInstaller.InstallerType);
// If there are no exact installerType matches, check if there is a compatible installerType that can be matched.
if (!installerTypeMatches.Any())
{
installerTypeMatches = existingInstallers.Where(
i => IsCompatibleInstallerType(i.InstallerType ?? installerManifest.InstallerType, newInstaller.InstallerType));
}
// Match installers using the installer architecture with the following priority: OverrideArchitecture > UrlArchitecture > BinaryArchitecture.
IEnumerable<Installer> architectureMatches;
if (installerMetadata.OverrideArchitecture.HasValue)
{
architectureMatches = installerTypeMatches.Where(i => i.Architecture == installerMetadata.OverrideArchitecture);
}
else
{
if (installerMetadata.UrlArchitecture.HasValue)
{
architectureMatches = installerTypeMatches.Where(i => i.Architecture == installerMetadata.UrlArchitecture);
}
else
{
var binaryArchitecture = installerMetadata.BinaryArchitecture ?? newInstaller.Architecture;
architectureMatches = installerTypeMatches.Where(i => i.Architecture == binaryArchitecture);
}
}
int architectureMatchesCount = architectureMatches.Count();
if (architectureMatchesCount == 1)
{
return architectureMatches.Single();
}
else if (architectureMatchesCount == 0)
{
unmatchedInstallers.Add(newInstaller);
}
else
{
// If there are multiple architecture matches, use scope to further narrow down the matches (if present).
IEnumerable<Installer> scopeMatches;
if (installerMetadata.OverrideScope.HasValue)
{
scopeMatches = architectureMatches.Where(i => i.Scope == installerMetadata.OverrideScope);
}
else
{
scopeMatches = architectureMatches;
}
int scopeMatchesCount = scopeMatches.Count();
if (scopeMatchesCount == 1)
{
return scopeMatches.Single();
}
else if (scopeMatchesCount == 0)
{
unmatchedInstallers.Add(newInstaller);
}
else
{
multipleMatchedInstallers.Add(newInstaller);
}
}
return null;
}
/// <summary>
/// Updates the metadata from an existing installer node with the metadata from a new installer node.
/// </summary>
/// <param name="existingInstaller">Existing installer node.</param>
/// <param name="newInstaller">New installer node.</param>
private static void UpdateInstallerMetadata(Installer existingInstaller, Installer newInstaller)
{
existingInstaller.Architecture = newInstaller.Architecture;
existingInstaller.InstallerUrl = newInstaller.InstallerUrl;
existingInstaller.InstallerSha256 = newInstaller.InstallerSha256;
existingInstaller.SignatureSha256 = newInstaller.SignatureSha256;
// If the newInstaller field value is null, we default to using the existingInstaller field value.
existingInstaller.ProductCode = newInstaller.ProductCode ?? existingInstaller.ProductCode;
existingInstaller.MinimumOSVersion = newInstaller.MinimumOSVersion ?? existingInstaller.MinimumOSVersion;
existingInstaller.PackageFamilyName = newInstaller.PackageFamilyName ?? existingInstaller.PackageFamilyName;
existingInstaller.NestedInstallerFiles = newInstaller.NestedInstallerFiles ?? existingInstaller.NestedInstallerFiles;
existingInstaller.Platform = newInstaller.Platform ?? existingInstaller.Platform;
}
/// <summary>
/// Parses a package for available metadata including Version, Publisher, Name, Descripion, License, etc.
/// </summary>
/// <param name="installerMetadata">Helper class for storing an installer's metadata information.</param>
/// <param name="manifests">Wrapper object for manifest object models.</param>
/// <returns>True if package was successfully parsed and metadata extracted, false otherwise.</returns>
private static bool ParsePackage(InstallerMetadata installerMetadata, Manifests manifests)
{
VersionManifest versionManifest = manifests.VersionManifest;
InstallerManifest installerManifest = manifests.InstallerManifest;
DefaultLocaleManifest defaultLocaleManifest = manifests.DefaultLocaleManifest;
var versionInfo = FileVersionInfo.GetVersionInfo(installerMetadata.PackageFile);
defaultLocaleManifest.PackageVersion ??= versionInfo.FileVersion?.Trim() ?? versionInfo.ProductVersion?.Trim();
defaultLocaleManifest.Publisher ??= versionInfo.CompanyName?.Trim();
defaultLocaleManifest.PackageName ??= versionInfo.ProductName?.Trim();
defaultLocaleManifest.ShortDescription ??= versionInfo.FileDescription?.Trim();
defaultLocaleManifest.Copyright ??= versionInfo.LegalCopyright?.Trim();
if (ParsePackageAndGenerateInstallerNodes(installerMetadata, manifests))
{
// Add range of new installers generates from parsing the package.
manifests.InstallerManifest.Installers.AddRange(installerMetadata.NewInstallers);
if (!string.IsNullOrEmpty(defaultLocaleManifest.PackageVersion))
{
versionManifest.PackageVersion = installerManifest.PackageVersion = RemoveInvalidCharsFromString(defaultLocaleManifest.PackageVersion);
}
string packageIdPublisher = defaultLocaleManifest.Publisher?.Remove(" ").Trim('.') ?? $"<{nameof(defaultLocaleManifest.Publisher)}>";
string packageIdName = defaultLocaleManifest.PackageName?.Remove(" ").Trim('.') ?? $"<{nameof(defaultLocaleManifest.PackageName)}>";
versionManifest.PackageIdentifier ??= $"{RemoveInvalidCharsFromString(packageIdPublisher)}.{RemoveInvalidCharsFromString(packageIdName)}";
installerManifest.PackageIdentifier = defaultLocaleManifest.PackageIdentifier = versionManifest.PackageIdentifier;
return true;
}
else
{
return false;
}
}
/// <summary>
/// Parses a package for relevant metadata and generates a new installer node for each installer file (MSIX can have multiple installers).
/// </summary>
/// <param name="installerMetadata">Helper class for storing an installer's metadata information.</param>
/// <param name="manifests">Manifests object model.</param>
/// <returns>Boolean value indicating whether the function was successful or not.</returns>
private static bool ParsePackageAndGenerateInstallerNodes(InstallerMetadata installerMetadata, Manifests manifests)
{
var url = installerMetadata.InstallerUrl;
var packageFile = installerMetadata.PackageFile;
var newInstallers = installerMetadata.NewInstallers;
var baseInstaller = new Installer();
baseInstaller.InstallerUrl = url;
baseInstaller.InstallerSha256 = GetFileHash(packageFile);
List<string> installerPaths = new List<string>();
if (installerMetadata.IsZipFile)
{
baseInstaller.InstallerType = InstallerType.Zip;
baseInstaller.NestedInstallerFiles = new List<NestedInstallerFile>();
List<string> relativeFilePaths = installerMetadata.NestedInstallerFiles.Select(i => i.RelativeFilePath).Distinct().ToList();
foreach (NestedInstallerFile nestedInstallerFile in installerMetadata.NestedInstallerFiles)
{
// Skip adding duplicate NestedInstallerFile object.
if (baseInstaller.NestedInstallerFiles.Any(i =>
i.RelativeFilePath == nestedInstallerFile.RelativeFilePath &&
i.PortableCommandAlias == nestedInstallerFile.PortableCommandAlias))
{
continue;
}
baseInstaller.NestedInstallerFiles.Add(new NestedInstallerFile
{
RelativeFilePath = nestedInstallerFile.RelativeFilePath,
PortableCommandAlias = nestedInstallerFile.PortableCommandAlias,
});
}
// Number of installer paths should be equal to the distinct relative file paths.
foreach (var relativeFilePath in relativeFilePaths)
{
installerPaths.Add(Path.Combine(installerMetadata.ExtractedDirectory, relativeFilePath));
}
}
else
{
installerPaths.Add(packageFile);
}
Architecture? nestedArchitecture = null;
bool parseMsixResult = false;
// There will only be multiple installer paths if there are multiple nested portable installers in an zip archive.
foreach (string path in installerPaths)
{
bool parseResult = ParseExeInstallerType(path, baseInstaller, newInstallers) ||
(parseMsixResult = ParseMsix(path, baseInstaller, manifests, newInstallers)) ||
ParseMsi(path, baseInstaller, manifests, newInstallers);
if (!parseResult)
{
return false;
}
// Check if the detected architectures are the same for all nested portables (exe).
if (nestedArchitecture.HasValue && nestedArchitecture != baseInstaller.Architecture)
{
installerMetadata.MultipleNestedInstallerArchitectures = true;
}
nestedArchitecture = baseInstaller.Architecture;
}
// Only capture architecture if installer is non-msix as the architecture for msix installers is deterministic
if (!parseMsixResult)
{
var urlArchitecture = installerMetadata.UrlArchitecture = GetArchFromUrl(baseInstaller.InstallerUrl);
installerMetadata.UrlArchitecture = GetArchFromUrl(baseInstaller.InstallerUrl);
installerMetadata.BinaryArchitecture = baseInstaller.Architecture;
var overrideArch = installerMetadata.OverrideArchitecture;
if (overrideArch.HasValue)
{
baseInstaller.Architecture = overrideArch.Value;
}
else if (urlArchitecture.HasValue)
{
baseInstaller.Architecture = urlArchitecture.Value;
}
}
return true;
}
/// <summary>
/// Performs a regex match to determine the installer architecture based on the url string.
/// </summary>
/// <param name="url">Installer url string.</param>
/// <returns>Installer architecture enum.</returns>
private static Architecture? GetArchFromUrl(string url)
{
List<Architecture> archMatches = new List<Architecture>();
// Arm must only be checked if arm64 check fails, otherwise it'll match for arm64 too
if (Regex.Match(url, "arm64|aarch64", RegexOptions.IgnoreCase).Success)
{
archMatches.Add(Architecture.Arm64);
}
else if (Regex.Match(url, @"\barm\b", RegexOptions.IgnoreCase).Success)
{
archMatches.Add(Architecture.Arm);
}
if (Regex.Match(url, "x64|win64|_64|64-bit", RegexOptions.IgnoreCase).Success)
{
archMatches.Add(Architecture.X64);
}
if (Regex.Match(url, "x86|win32|ia32|_86|32-bit", RegexOptions.IgnoreCase).Success)
{
archMatches.Add(Architecture.X86);
}
return archMatches.Count == 1 ? archMatches.Single() : null;
}
/// <summary>
/// Computes the SHA256 hash value for the specified byte array.
/// </summary>
/// <param name="buffer">The input to compute the hash code for.</param>
/// <returns>The computed SHA256 hash string.</returns>
private static string HashBytes(byte[] buffer)
{
using var hasher = SHA256.Create();
return BitConverter.ToString(hasher.ComputeHash(buffer)).Remove("-");
}
private static string HashAppxFile(IAppxFile signatureFile)
{
var signatureBytes = StreamUtils.ReadStreamToByteArray(signatureFile.GetStream());
return HashBytes(signatureBytes);
}
private static MachineType? GetMachineType(string binary)
{
using (FileStream stream = File.OpenRead(binary))
using (BinaryReader bw = new BinaryReader(stream))
{
const ushort executableMagicNumber = 0x5a4d;
const int peMagicNumber = 0x00004550; // "PE\0\0"
stream.Seek(0, SeekOrigin.Begin);
int magicNumber = bw.ReadUInt16();
bool isExecutable = magicNumber == executableMagicNumber;
if (isExecutable)
{
stream.Seek(60, SeekOrigin.Begin);
int headerOffset = bw.ReadInt32();
stream.Seek(headerOffset, SeekOrigin.Begin);
int signature = bw.ReadInt32();
bool isPortableExecutable = signature == peMagicNumber;
if (isPortableExecutable)
{
MachineType machineType = (MachineType)bw.ReadUInt16();
return GetCompatibleMachineType(machineType);
}
}
}
return null;
}
private static MachineType GetCompatibleMachineType(MachineType type)
{
switch (type)
{
case MachineType.Armv7:
return MachineType.Arm;
default:
return type;
}
}
/// <summary>
/// Checks if the provided installerTypes are compatible.
/// </summary>
/// <param name="type1">First InstallerType.</param>
/// <param name="type2">Second InstallerType.</param>
/// <returns>A boolean value indicating whether the installerTypes are compatible.</returns>
private static bool IsCompatibleInstallerType(InstallerType? type1, InstallerType? type2)
{
if (!type1.HasValue || !type2.HasValue)
{
return false;
}
InstallerType installerType1 = type1.Value;
InstallerType installerType2 = type2.Value;
if (installerType1 == installerType2)
{
return true;
}
CompatibilitySet set1 = GetCompatibilitySet(installerType1);
CompatibilitySet set2 = GetCompatibilitySet(installerType2);
if (set1 == CompatibilitySet.None || set2 == CompatibilitySet.None)
{
return false;
}
return set1 == set2;
}
private static CompatibilitySet GetCompatibilitySet(InstallerType type)
{
switch (type)
{
case InstallerType.Inno:
case InstallerType.Nullsoft:
case InstallerType.Exe:
case InstallerType.Burn:
// Portable is included as a compatible installer type since
// they are detected as 'exe' installers. This is to ensure
// updating a portable manifest is supported.
case InstallerType.Portable:
return CompatibilitySet.Exe;
case InstallerType.Wix:
case InstallerType.Msi:
return CompatibilitySet.Msi;
case InstallerType.Msix:
case InstallerType.Appx:
return CompatibilitySet.Msix;
default:
return CompatibilitySet.None;
}
}
private static bool ParseExeInstallerType(string path, Installer baseInstaller, List<Installer> newInstallers)
{
try
{
ManifestResource rc = new ManifestResource();
InstallerType? installerTypeEnum;
try
{
rc.LoadFrom(path);
string installerType = rc.Manifest.DocumentElement
.GetElementsByTagName("description")
.Cast<XmlNode>()
.FirstOrDefault()?
.InnerText?
.Split(' ').First()
.ToLowerInvariant();
if (installerType.EqualsIC("wix"))
{
// See https://github.com/microsoft/winget-create/issues/26, a Burn installer is an exe-installer produced by the WiX toolset.
installerTypeEnum = InstallerType.Burn;
}
else if (KnownInstallerResourceNames.Contains(installerType))
{
// If it's a known exe installer type, set as appropriately
installerTypeEnum = installerType.ToEnumOrDefault<InstallerType>();
}
else
{
installerTypeEnum = (baseInstaller.InstallerType == InstallerType.Portable ||
baseInstaller.NestedInstallerType == NestedInstallerType.Portable) ?
InstallerType.Portable : InstallerType.Exe;
}
}
catch (Win32Exception err)
{
if ((err.Message == "The specified resource type cannot be found in the image file."
&& err.NativeErrorCode == 1813) ||
(err.Message == "The specified image file did not contain a resource section."
&& err.NativeErrorCode == 1812))
{
installerTypeEnum = (baseInstaller.InstallerType == InstallerType.Portable ||
baseInstaller.NestedInstallerType == NestedInstallerType.Portable) ?
InstallerType.Portable : InstallerType.Exe;
}
else
{
return false;
}
}
SetInstallerType(baseInstaller, installerTypeEnum.Value);
baseInstaller.Architecture = GetMachineType(path)?.ToString().ToEnumOrDefault<Architecture>() ?? Architecture.Neutral;
newInstallers.Add(baseInstaller);
return true;
}
catch (Win32Exception)
{
return false;
}
}
/// <summary>
/// Checks if a MSI Installer database was generated by WiX, based on common characteristics.
/// </summary>
/// <param name="installer">A MSI Installer database.</param>
/// <returns>A boolean.</returns>
private static bool IsWix(QDatabase installer)
{
return
installer.Tables.AsEnumerable().Any(table => table.Name.ToLower().Contains("wix")) ||
installer.Properties.AsEnumerable().Any(property => property.Property.ToLower().Contains("wix") || property.Value.ToLower().Contains("wix")) ||
installer.SummaryInfo.CreatingApp.ToLower().Contains("wix") ||
installer.SummaryInfo.CreatingApp.ToLower().Contains("windows installer xml");
}
private static bool ParseMsi(string path, Installer baseInstaller, Manifests manifests, List<Installer> newInstallers)
{
DefaultLocaleManifest defaultLocaleManifest = manifests?.DefaultLocaleManifest;
try
{
using (var database = new QDatabase(path, Deployment.WindowsInstaller.DatabaseOpenMode.ReadOnly))
{
InstallerType installerType = IsWix(database)
? InstallerType.Wix
: InstallerType.Msi;
SetInstallerType(baseInstaller, installerType);
var properties = database.Properties.ToList();
if (defaultLocaleManifest != null)
{
defaultLocaleManifest.PackageVersion ??= properties.FirstOrDefault(p => p.Property == "ProductVersion")?.Value;
defaultLocaleManifest.PackageName ??= properties.FirstOrDefault(p => p.Property == "ProductName")?.Value;
defaultLocaleManifest.Publisher ??= properties.FirstOrDefault(p => p.Property == "Manufacturer")?.Value;
}
baseInstaller.ProductCode = properties.FirstOrDefault(p => p.Property == "ProductCode")?.Value;
string archString = database.SummaryInfo.Template.Split(';').First();
archString = archString.EqualsIC("Intel") ? "x86" :
archString.EqualsIC("Intel64") ? "x64" :
archString.EqualsIC("Arm64") ? "Arm64" :
archString.EqualsIC("Arm") ? "Arm" : archString;
baseInstaller.Architecture = archString.ToEnumOrDefault<Architecture>() ?? Architecture.Neutral;
if (baseInstaller.InstallerLocale == null)
{
string languageString = properties.FirstOrDefault(p => p.Property == "ProductLanguage")?.Value;
if (int.TryParse(languageString, out int lcid))
{
try
{
baseInstaller.InstallerLocale = new CultureInfo(lcid).Name;
}
catch (Exception ex) when (ex is ArgumentOutOfRangeException || ex is CultureNotFoundException)
{
// If the lcid value is invalid, do nothing.
}
}
}
}
newInstallers?.Add(baseInstaller);
return true;
}
catch (Deployment.WindowsInstaller.InstallerException)
{
// Binary wasn't an MSI, skip
return false;
}
}
private static bool ParseMsix(string path, Installer baseInstaller, Manifests manifests, List<Installer> newInstallers)
{
InstallerManifest installerManifest = manifests?.InstallerManifest;
DefaultLocaleManifest defaultLocaleManifest = manifests?.DefaultLocaleManifest;
AppxMetadata metadata = GetAppxMetadataAndSetInstallerProperties(path, installerManifest, baseInstaller, newInstallers);
if (metadata == null)
{
// Binary wasn't an MSIX, skip
return false;
}
if (defaultLocaleManifest != null)
{
defaultLocaleManifest.PackageVersion ??= metadata.Version?.ToString();
defaultLocaleManifest.PackageName ??= metadata.DisplayName;
defaultLocaleManifest.Publisher ??= metadata.PublisherDisplayName;
defaultLocaleManifest.ShortDescription ??= GetApplicationProperty(metadata, "Description");
}
return true;
}
private static string GetApplicationProperty(AppxMetadata appxMetadata, string propertyName)
{
IAppxManifestApplicationsEnumerator enumerator = appxMetadata.AppxReader.GetManifest().GetApplications();
while (enumerator.GetHasCurrent())
{
IAppxManifestApplication application = enumerator.GetCurrent();
try
{
application.GetStringValue(propertyName, out string value);
return value;
}
catch (ArgumentException)
{
// Property not found on this node, continue
}
enumerator.MoveNext();
}
return null;
}
private static void SetInstallerPropertiesFromAppxMetadata(AppxMetadata appxMetadata, Installer installer, InstallerManifest installerManifest)
{
installer.Architecture = appxMetadata.Architecture.ToEnumOrDefault<Architecture>() ?? Architecture.Neutral;
installer.MinimumOSVersion = SetInstallerStringPropertyIfNeeded(installerManifest?.MinimumOSVersion, appxMetadata.MinOSVersion?.ToString());
installer.PackageFamilyName = SetInstallerStringPropertyIfNeeded(installerManifest?.PackageFamilyName, appxMetadata.PackageFamilyName);
// We have to fixup the Platform string first, and then remove anything that fails to parse.
var platformValues = appxMetadata.TargetDeviceFamiliesMinVersions.Keys
.Select(k => k.Replace('.', '_').ToEnumOrDefault<Platform>())
.Where(p => p != null)
.Select(p => p.Value)
.ToList();
installer.Platform = SetInstallerListPropertyIfNeeded(installerManifest?.Platform, platformValues);
}
private static string SetInstallerStringPropertyIfNeeded(string rootProperty, string valueToSet)
{
return valueToSet == rootProperty ? null : valueToSet;
}
private static List<T> SetInstallerListPropertyIfNeeded<T>(List<T> rootProperty, List<T> valueToSet)
{
return rootProperty != null && new HashSet<T>(rootProperty).SetEquals(valueToSet) ? null : valueToSet;
}
private static AppxMetadata GetAppxMetadataAndSetInstallerProperties(string path, InstallerManifest installerManifest, Installer baseInstaller, List<Installer> installers)
{
try
{
var appxMetadatas = new List<AppxMetadata>();
string signatureSha256;
try
{
// Check if package is an MsixBundle
var bundle = new AppxBundleMetadata(path);
IAppxFile signatureFile = bundle.AppxBundleReader.GetFootprintFile(APPX_BUNDLE_FOOTPRINT_FILE_TYPE.APPX_BUNDLE_FOOTPRINT_FILE_TYPE_SIGNATURE);
signatureSha256 = HashAppxFile(signatureFile);
// Only create installer nodes for non-resource packages
foreach (var childPackage in bundle.ChildAppxPackages.Where(p => p.PackageType == PackageType.Application))
{
// Ignore stub packages.
if (childPackage.RelativeFilePath.StartsWith("AppxMetadata\\Stub", StringComparison.OrdinalIgnoreCase))
{
continue;
}
var appxFile = bundle.AppxBundleReader.GetPayloadPackage(childPackage.RelativeFilePath);
appxMetadatas.Add(new AppxMetadata(appxFile.GetStream()));
}
}
catch (System.Runtime.InteropServices.COMException)
{
// Check if package is an Msix
var appxMetadata = new AppxMetadata(path);
appxMetadatas.Add(appxMetadata);
IAppxFile signatureFile = appxMetadata.AppxReader.GetFootprintFile(APPX_FOOTPRINT_FILE_TYPE.APPX_FOOTPRINT_FILE_TYPE_SIGNATURE);
signatureSha256 = HashAppxFile(signatureFile);
}
baseInstaller.SignatureSha256 = signatureSha256;
SetInstallerType(baseInstaller, InstallerType.Msix);
// Add installer nodes for MSIX installers
foreach (var appxMetadata in appxMetadatas)
{
var msixInstaller = CloneInstaller(baseInstaller);
installers.Add(msixInstaller);
SetInstallerPropertiesFromAppxMetadata(appxMetadata, msixInstaller, installerManifest);
}
return appxMetadatas.First();
}
catch (System.Runtime.InteropServices.COMException)
{
// Binary wasn't an MSIX
return null;
}
}
private static void SetInstallerType(Installer baseInstaller, InstallerType installerType)
{
if (baseInstaller.InstallerType.IsArchiveType())
{