mirror of
https://github.com/QL-Win/QuickLook.git
synced 2025-09-18 22:52:37 +00:00
Restruct AppViewer codes
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// This file is part of QuickLook program.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace QuickLook.Plugin.AppViewer.PackageParsers.Apk;
|
||||
|
||||
public class ApkInfo
|
||||
{
|
||||
public string VersionName { get; set; }
|
||||
|
||||
public string VersionCode { get; set; }
|
||||
|
||||
public string TargetSdkVersion { get; set; }
|
||||
|
||||
public List<string> Permissions { get; set; } = [];
|
||||
|
||||
public string PackageName { get; set; }
|
||||
|
||||
public string MinSdkVersion { get; set; }
|
||||
|
||||
public string Icon { get; set; }
|
||||
|
||||
public Dictionary<string, string> Icons { get; set; } = [];
|
||||
|
||||
public byte[] Logo { get; set; }
|
||||
|
||||
public string Label { get; set; }
|
||||
|
||||
public Dictionary<string, string> Labels { get; set; } = [];
|
||||
|
||||
public bool HasIcon
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Icons.Count <= 0)
|
||||
{
|
||||
return !string.IsNullOrEmpty(Icon);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public List<string> Locales { get; set; } = [];
|
||||
|
||||
public List<string> Densities { get; set; } = [];
|
||||
|
||||
public string LaunchableActivity { get; set; }
|
||||
}
|
@@ -0,0 +1,57 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// This file is part of QuickLook program.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
using ICSharpCode.SharpZipLib.Zip;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace QuickLook.Plugin.AppViewer.PackageParsers.Apk;
|
||||
|
||||
public static class ApkParser
|
||||
{
|
||||
public static ApkInfo Parse(string path)
|
||||
{
|
||||
using var zip = new ZipFile(path);
|
||||
|
||||
var apkReader = new ApkReader.ApkReader();
|
||||
ApkReader.ApkInfo baseInfo = apkReader.Read(path);
|
||||
ApkInfo info = new()
|
||||
{
|
||||
VersionName = baseInfo.VersionName,
|
||||
VersionCode = baseInfo.VersionCode,
|
||||
TargetSdkVersion = baseInfo.TargetSdkVersion,
|
||||
Permissions = baseInfo.Permissions,
|
||||
PackageName = baseInfo.PackageName,
|
||||
MinSdkVersion = baseInfo.MinSdkVersion,
|
||||
Icon = baseInfo.Icon,
|
||||
Icons = baseInfo.Icons,
|
||||
Label = baseInfo.Label,
|
||||
Labels = baseInfo.Labels,
|
||||
Locales = baseInfo.Locales,
|
||||
Densities = baseInfo.Densities,
|
||||
LaunchableActivity = baseInfo.LaunchableActivity,
|
||||
};
|
||||
|
||||
if (baseInfo.HasIcon)
|
||||
{
|
||||
ZipEntry entry = zip.GetEntry(baseInfo.Icons.Values.LastOrDefault());
|
||||
using var s = new BinaryReader(zip.GetInputStream(entry));
|
||||
info.Logo = s.ReadBytes((int)entry.Size);
|
||||
}
|
||||
return info;
|
||||
}
|
||||
}
|
@@ -0,0 +1,136 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// This file is part of QuickLook program.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
using ICSharpCode.SharpZipLib.Zip;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Xml;
|
||||
|
||||
namespace QuickLook.Plugin.AppViewer.PackageParsers.Appx;
|
||||
|
||||
public class AppxBundleReader : IDisposable
|
||||
{
|
||||
private ZipFile zip;
|
||||
private AppxReader appxReader = null;
|
||||
|
||||
private string name;
|
||||
private string publisher;
|
||||
private string version;
|
||||
|
||||
public string Name => name ?? appxReader?.Name;
|
||||
public string Publisher => publisher ?? appxReader?.Publisher;
|
||||
public string Version => version ?? appxReader?.Version;
|
||||
public string DisplayName => appxReader?.DisplayName;
|
||||
public string PublisherDisplayName => appxReader?.PublisherDisplayName;
|
||||
public string Description => appxReader?.Description;
|
||||
public string Logo => appxReader?.Logo;
|
||||
public string[] Capabilities => appxReader?.Capabilities;
|
||||
public Bitmap Icon => appxReader?.Icon;
|
||||
|
||||
public AppxBundleReader(Stream stream)
|
||||
{
|
||||
Open(stream);
|
||||
}
|
||||
|
||||
public AppxBundleReader(string path)
|
||||
{
|
||||
Open(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
appxReader?.Dispose();
|
||||
appxReader = null;
|
||||
zip?.Close();
|
||||
zip = null;
|
||||
}
|
||||
|
||||
private void Open(Stream stream)
|
||||
{
|
||||
zip = new ZipFile(stream);
|
||||
ZipEntry entry = zip.GetEntry("AppxMetadata/AppxBundleManifest.xml")
|
||||
?? throw new InvalidDataException("AppxMetadata/AppxBundleManifest.xml not found");
|
||||
|
||||
XmlDocument xml = new() { XmlResolver = null };
|
||||
xml.Load(zip.GetInputStream(entry));
|
||||
|
||||
XmlElement bundleNode = xml.DocumentElement;
|
||||
|
||||
// Identity
|
||||
{
|
||||
XmlElement identityNode = bundleNode["Identity"];
|
||||
|
||||
if (identityNode != null)
|
||||
{
|
||||
name = identityNode.Attributes["Name"]?.Value;
|
||||
version = identityNode.Attributes["Version"]?.Value;
|
||||
publisher = identityNode.Attributes["Publisher"]?.Value;
|
||||
|
||||
Match m = Regex.Match(Publisher, @"CN=([^,]*),?");
|
||||
if (m.Success) publisher = m.Groups[1].Value;
|
||||
}
|
||||
}
|
||||
|
||||
// Packages
|
||||
{
|
||||
XmlElement packagesNode = bundleNode["Packages"];
|
||||
|
||||
if (packagesNode != null)
|
||||
{
|
||||
string arch = RuntimeInformation.OSArchitecture == Architecture.Arm64
|
||||
? "arm64"
|
||||
: Environment.Is64BitProcess ? "x64" : "x86";
|
||||
|
||||
var packages = packagesNode.ChildNodes.Select(package =>
|
||||
{
|
||||
return new
|
||||
{
|
||||
Type = package.Attributes["Type"]?.Value,
|
||||
Architecture = package.Attributes["Architecture"]?.Value,
|
||||
FileName = package.Attributes["FileName"]?.Value,
|
||||
Version = package.Attributes["Version"]?.Value
|
||||
};
|
||||
});
|
||||
|
||||
var package = packages
|
||||
.Where(p => p.Type == "application" && p.Architecture == arch)
|
||||
.FirstOrDefault() ?? packages.FirstOrDefault();
|
||||
|
||||
if (package != null)
|
||||
{
|
||||
appxReader = new AppxReader(zip.GetInputStream(zip.GetEntry(package.FileName)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
file static class LinqExtension
|
||||
{
|
||||
public static IEnumerable<dynamic> Select(this XmlNodeList nodes, Func<XmlNode, dynamic> selector)
|
||||
{
|
||||
foreach (XmlNode node in nodes)
|
||||
{
|
||||
yield return selector(node);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,33 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// This file is part of QuickLook program.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
using System.Drawing;
|
||||
|
||||
namespace QuickLook.Plugin.AppViewer.PackageParsers.Appx;
|
||||
|
||||
public class AppxInfo
|
||||
{
|
||||
public string ProductName { get; set; }
|
||||
|
||||
public string ProductVersion { get; set; }
|
||||
|
||||
public string Publisher { get; set; }
|
||||
|
||||
public Bitmap Logo { get; set; }
|
||||
|
||||
public string[] Capabilities { get; set; }
|
||||
}
|
@@ -0,0 +1,59 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// This file is part of QuickLook program.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
using System.IO;
|
||||
|
||||
namespace QuickLook.Plugin.AppViewer.PackageParsers.Appx;
|
||||
|
||||
public static class AppxParser
|
||||
{
|
||||
public static AppxInfo Parse(string path)
|
||||
{
|
||||
bool isBundle = Path.GetExtension(path).ToLower() switch
|
||||
{
|
||||
".msixbundle" or ".appxbundle" => true,
|
||||
_ => false,
|
||||
};
|
||||
|
||||
if (isBundle)
|
||||
{
|
||||
AppxBundleReader appxReader = new(path);
|
||||
|
||||
return new AppxInfo
|
||||
{
|
||||
ProductName = appxReader.DisplayName,
|
||||
ProductVersion = appxReader.Version,
|
||||
Publisher = appxReader.Publisher,
|
||||
Logo = appxReader.Icon,
|
||||
Capabilities = appxReader.Capabilities,
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
AppxReader appxReader = new(path);
|
||||
|
||||
return new AppxInfo
|
||||
{
|
||||
ProductName = appxReader.DisplayName,
|
||||
ProductVersion = appxReader.Version,
|
||||
Publisher = appxReader.Publisher,
|
||||
Logo = appxReader.Icon,
|
||||
Capabilities = appxReader.Capabilities,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,155 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// This file is part of QuickLook program.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
using ICSharpCode.SharpZipLib.Zip;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Xml;
|
||||
|
||||
namespace QuickLook.Plugin.AppViewer.PackageParsers.Appx;
|
||||
|
||||
public class AppxReader : IDisposable
|
||||
{
|
||||
private ZipFile zip;
|
||||
|
||||
public string Name { get; set; }
|
||||
public string Publisher { get; set; }
|
||||
public string Version { get; set; }
|
||||
public string DisplayName { get; set; }
|
||||
public string PublisherDisplayName { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string Logo { get; set; }
|
||||
public string[] Capabilities { get; set; }
|
||||
|
||||
public Bitmap Icon
|
||||
{
|
||||
get
|
||||
{
|
||||
if (string.IsNullOrEmpty(Logo)) return null;
|
||||
|
||||
string extension = Path.GetExtension(Logo);
|
||||
string name = Logo.Substring(0, Logo.Length - extension.Length);
|
||||
|
||||
ZipEntry logoEntry = null;
|
||||
int logoScale = 0;
|
||||
|
||||
foreach (ZipEntry entry in zip)
|
||||
{
|
||||
Match m = Regex.Match(entry.Name, @$"{name}(\.scale\-(\d+))?{extension}");
|
||||
|
||||
if (m.Success)
|
||||
{
|
||||
if (int.TryParse(m.Groups[2].Value, out int currentScale))
|
||||
{
|
||||
if (currentScale > logoScale)
|
||||
{
|
||||
logoEntry = entry;
|
||||
logoScale = currentScale;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (logoEntry != null)
|
||||
{
|
||||
return new Bitmap(zip.GetInputStream(logoEntry));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public AppxReader(Stream stream)
|
||||
{
|
||||
zip = new ZipFile(stream);
|
||||
Open();
|
||||
}
|
||||
|
||||
public AppxReader(string path)
|
||||
{
|
||||
zip = new ZipFile(path);
|
||||
Open();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
zip?.Close();
|
||||
zip = null;
|
||||
}
|
||||
|
||||
private void Open()
|
||||
{
|
||||
ZipEntry entry = zip.GetEntry("AppxManifest.xml")
|
||||
?? throw new InvalidDataException("AppxManifest.xml not found");
|
||||
|
||||
XmlDocument xml = new() { XmlResolver = null };
|
||||
xml.Load(zip.GetInputStream(entry));
|
||||
|
||||
XmlElement packageNode = xml.DocumentElement;
|
||||
|
||||
// Identity
|
||||
{
|
||||
XmlElement identityNode = packageNode["Identity"];
|
||||
|
||||
if (identityNode != null)
|
||||
{
|
||||
Name = identityNode.Attributes["Name"]?.Value;
|
||||
Version = identityNode.Attributes["Version"]?.Value;
|
||||
Publisher = identityNode.Attributes["Publisher"]?.Value;
|
||||
|
||||
Match m = Regex.Match(Publisher, @"CN=([^,]*),?");
|
||||
if (m.Success) Publisher = m.Groups[1].Value;
|
||||
}
|
||||
}
|
||||
|
||||
// Properties
|
||||
{
|
||||
XmlElement propertiesNode = packageNode["Properties"];
|
||||
|
||||
if (propertiesNode != null)
|
||||
{
|
||||
DisplayName = propertiesNode["DisplayName"]?.FirstChild?.Value;
|
||||
PublisherDisplayName = propertiesNode["PublisherDisplayName"]?.FirstChild?.Value;
|
||||
Description = propertiesNode["Description"]?.FirstChild?.Value;
|
||||
Logo = propertiesNode["Logo"]?.FirstChild?.Value.Replace(@"\", "/");
|
||||
}
|
||||
}
|
||||
|
||||
// Capabilities
|
||||
{
|
||||
XmlElement capabilitiesNode = packageNode["Capabilities"];
|
||||
|
||||
if (capabilitiesNode != null)
|
||||
{
|
||||
Capabilities = [.. capabilitiesNode.ChildNodes.Select(capability => capability.Attributes["Name"]?.Value)];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
file static class LinqExtension
|
||||
{
|
||||
public static IEnumerable<dynamic> Select(this XmlNodeList nodes, Func<XmlNode, dynamic> selector)
|
||||
{
|
||||
foreach (XmlNode node in nodes)
|
||||
{
|
||||
yield return selector(node);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,65 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace QuickLook.Plugin.AppViewer.PackageParsers.Deb;
|
||||
|
||||
public static class ArReader
|
||||
{
|
||||
public static ArEntry[] Read(string filePath)
|
||||
{
|
||||
var entries = new List<ArEntry>();
|
||||
|
||||
using var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
|
||||
using var br = new BinaryReader(fs);
|
||||
|
||||
// Check ar magic
|
||||
var magic = br.ReadBytes(8);
|
||||
if (Encoding.ASCII.GetString(magic) != "!<arch>\n")
|
||||
throw new InvalidDataException("Not a valid ar file");
|
||||
|
||||
while (fs.Position < fs.Length)
|
||||
{
|
||||
var header = br.ReadBytes(60);
|
||||
if (header.Length < 60)
|
||||
break;
|
||||
|
||||
string name = Encoding.ASCII.GetString(header, 0, 16).Trim();
|
||||
string sizeStr = Encoding.ASCII.GetString(header, 48, 10).Trim();
|
||||
string fmag = Encoding.ASCII.GetString(header, 58, 2);
|
||||
if (fmag != "`\n")
|
||||
throw new InvalidDataException("Invalid header end");
|
||||
|
||||
long size = long.Parse(sizeStr);
|
||||
|
||||
byte[] data = br.ReadBytes((int)size);
|
||||
|
||||
// Completion alignment: ar file is evenly aligned
|
||||
if (size % 2 != 0)
|
||||
br.ReadByte();
|
||||
|
||||
entries.Add(new ArEntry
|
||||
{
|
||||
FileName = name,
|
||||
FileSize = size,
|
||||
Data = data
|
||||
});
|
||||
}
|
||||
|
||||
return [.. entries];
|
||||
}
|
||||
}
|
||||
|
||||
public class ArEntry
|
||||
{
|
||||
public string FileName { get; set; } = string.Empty;
|
||||
|
||||
public long FileSize { get; set; }
|
||||
|
||||
public byte[] Data { get; set; } = [];
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{FileName} ({FileSize})";
|
||||
}
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// This file is part of QuickLook program.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace QuickLook.Plugin.AppViewer.PackageParsers.Deb;
|
||||
|
||||
public class DebInfo
|
||||
{
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// This file is part of QuickLook program.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace QuickLook.Plugin.AppViewer.PackageParsers.Deb;
|
||||
|
||||
public static class DebParser
|
||||
{
|
||||
public static DebInfo Parse(string path)
|
||||
{
|
||||
ArEntry[] ar = ArReader.Read(path);
|
||||
return new();
|
||||
}
|
||||
}
|
@@ -0,0 +1,55 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// This file is part of QuickLook program.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace QuickLook.Plugin.AppViewer.PackageParsers.Hap;
|
||||
|
||||
public sealed class HapInfo
|
||||
{
|
||||
public string Label { get; set; }
|
||||
|
||||
public string Icon { get; set; }
|
||||
|
||||
public byte[] Logo { get; set; }
|
||||
|
||||
public byte[] AppIconForeground { get; set; }
|
||||
|
||||
public byte[] AppIconBackground { get; set; }
|
||||
|
||||
public bool HasIcon { get; set; } = false;
|
||||
|
||||
public bool HasLayeredIcon { get; set; } = false;
|
||||
|
||||
public string VersionName { get; set; }
|
||||
|
||||
public string VersionCode { get; set; }
|
||||
|
||||
public string CompileSdkType { get; set; }
|
||||
|
||||
public string CompileSdkVersion { get; set; }
|
||||
|
||||
public string MinAPIVersion { get; set; }
|
||||
|
||||
public string TargetAPIVersion { get; set; }
|
||||
|
||||
public string BundleName { get; set; }
|
||||
|
||||
public bool Debug { get; set; }
|
||||
|
||||
public string[] RequestPermissions { get; set; } = [];
|
||||
|
||||
public string[] DeviceTypes { get; set; } = [];
|
||||
}
|
@@ -0,0 +1,152 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// This file is part of QuickLook program.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
using ICSharpCode.SharpZipLib.Zip;
|
||||
using Newtonsoft.Json;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace QuickLook.Plugin.AppViewer.PackageParsers.Hap;
|
||||
|
||||
internal static class HapParser
|
||||
{
|
||||
public static HapInfo Parse(string path)
|
||||
{
|
||||
using var zip = new ZipFile(path);
|
||||
ZipEntry entry = zip.GetEntry("module.json");
|
||||
|
||||
if (entry != null)
|
||||
{
|
||||
using var stream = zip.GetInputStream(entry);
|
||||
using var reader = new StreamReader(stream, Encoding.UTF8);
|
||||
string content = reader.ReadToEnd();
|
||||
var dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(content);
|
||||
|
||||
if (dict == null) return null;
|
||||
|
||||
var info = new HapInfo();
|
||||
|
||||
if (dict.ContainsKey("app"))
|
||||
{
|
||||
dynamic app = dict["app"];
|
||||
|
||||
if (app.ContainsKey("label")) info.Label = app["label"].ToString();
|
||||
if (app.ContainsKey("icon")) info.Icon = app["icon"].ToString();
|
||||
if (app.ContainsKey("versionName")) info.VersionName = app["versionName"].ToString();
|
||||
if (app.ContainsKey("versionCode")) info.VersionCode = app["versionCode"].ToString();
|
||||
if (app.ContainsKey("compileSdkType")) info.CompileSdkType = app["compileSdkType"].ToString();
|
||||
if (app.ContainsKey("compileSdkVersion")) info.CompileSdkVersion = app["compileSdkVersion"].ToString();
|
||||
if (app.ContainsKey("minAPIVersion")) info.MinAPIVersion = app["minAPIVersion"].ToString();
|
||||
if (app.ContainsKey("targetAPIVersion")) info.TargetAPIVersion = app["targetAPIVersion"].ToString();
|
||||
if (app.ContainsKey("bundleName")) info.BundleName = app["bundleName"].ToString();
|
||||
if (app.ContainsKey("debug")) info.Debug = (bool)app["debug"];
|
||||
}
|
||||
|
||||
{
|
||||
if (info.Icon == "$media:layered_image")
|
||||
{
|
||||
ZipEntry foreground = zip.GetEntry("resources/base/media/foreground.png");
|
||||
ZipEntry background = zip.GetEntry("resources/base/media/background.png");
|
||||
|
||||
if (foreground != null && background != null)
|
||||
{
|
||||
{
|
||||
using var s = new BinaryReader(zip.GetInputStream(foreground));
|
||||
info.AppIconForeground = s.ReadBytes((int)foreground.Size);
|
||||
}
|
||||
{
|
||||
using var s = new BinaryReader(zip.GetInputStream(background));
|
||||
info.AppIconBackground = s.ReadBytes((int)background.Size);
|
||||
}
|
||||
|
||||
info.HasLayeredIcon = true;
|
||||
info.HasIcon = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
info.HasLayeredIcon = false;
|
||||
info.HasIcon = false;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
ZipEntry appIcon = zip.GetEntry("resources/base/media/app_icon.png");
|
||||
|
||||
if (appIcon != null)
|
||||
{
|
||||
using var s = new BinaryReader(zip.GetInputStream(appIcon));
|
||||
info.Logo = s.ReadBytes((int)appIcon.Size);
|
||||
|
||||
info.HasLayeredIcon = false;
|
||||
info.HasIcon = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!info.HasIcon)
|
||||
{
|
||||
ZipEntry icon = zip.GetEntry("resources/base/media/icon.png");
|
||||
|
||||
if (icon != null)
|
||||
{
|
||||
using var s = new BinaryReader(zip.GetInputStream(icon));
|
||||
info.Logo = s.ReadBytes((int)icon.Size);
|
||||
|
||||
info.HasLayeredIcon = false;
|
||||
info.HasIcon = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (dict.ContainsKey("module"))
|
||||
{
|
||||
dynamic module = dict["module"];
|
||||
|
||||
{
|
||||
List<string> requestPermissions = [];
|
||||
|
||||
if (module.ContainsKey("requestPermissions"))
|
||||
{
|
||||
foreach (dynamic requestPermission in module["requestPermissions"])
|
||||
{
|
||||
if (requestPermission.ContainsKey("name"))
|
||||
{
|
||||
requestPermissions.Add(requestPermission["name"].ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
info.RequestPermissions = [.. requestPermissions];
|
||||
}
|
||||
{
|
||||
List<string> deviceTypes = [];
|
||||
|
||||
if (module.ContainsKey("deviceTypes"))
|
||||
{
|
||||
foreach (dynamic deviceType in module["deviceTypes"])
|
||||
{
|
||||
deviceTypes.Add(deviceType.ToString());
|
||||
}
|
||||
}
|
||||
info.DeviceTypes = [.. deviceTypes];
|
||||
}
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// This file is part of QuickLook program.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
using System.Linq;
|
||||
|
||||
namespace QuickLook.Plugin.AppViewer.PackageParsers.Ipa;
|
||||
|
||||
public class IpaInfo
|
||||
{
|
||||
public string DisplayName { get; set; }
|
||||
|
||||
public string VersionName { get; set; }
|
||||
|
||||
public string VersionCode { get; set; }
|
||||
|
||||
public string Identifier { get; set; }
|
||||
|
||||
public string MinimumOSVersion { get; set; }
|
||||
|
||||
public string PlatformVersion { get; set; }
|
||||
|
||||
public string DeviceFamily { get; set; }
|
||||
|
||||
public string[] Permissions { get; set; } = [];
|
||||
|
||||
public byte[] Logo { get; set; }
|
||||
|
||||
public bool HasIcon => Logo?.Any() ?? false;
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// This file is part of QuickLook program.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
using System.Linq;
|
||||
|
||||
namespace QuickLook.Plugin.AppViewer.PackageParsers.Ipa;
|
||||
|
||||
public static class IpaParser
|
||||
{
|
||||
public static IpaInfo Parse(string path)
|
||||
{
|
||||
IpaReader reader = new(path);
|
||||
|
||||
return new IpaInfo()
|
||||
{
|
||||
DisplayName = reader.DisplayName,
|
||||
VersionName = reader.ShortVersionString,
|
||||
VersionCode = reader.Version,
|
||||
Identifier = reader.Identifier,
|
||||
DeviceFamily = reader.DeviceFamily,
|
||||
MinimumOSVersion = reader.MinimumOSVersion,
|
||||
PlatformVersion = reader.PlatformVersion,
|
||||
Permissions = [.. reader.InfoPlistDict.Keys.Where(key => key.StartsWith("NS") && key.EndsWith("UsageDescription"))],
|
||||
Logo = reader.Icon,
|
||||
};
|
||||
}
|
||||
}
|
@@ -0,0 +1,187 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// This file is part of QuickLook program.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
using ICSharpCode.SharpZipLib.Zip;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace QuickLook.Plugin.AppViewer.PackageParsers.Ipa;
|
||||
|
||||
public class IpaReader
|
||||
{
|
||||
private ZipFile zip;
|
||||
private string appRoot;
|
||||
|
||||
public Dictionary<string, object> InfoPlistDict { get; set; }
|
||||
|
||||
public Dictionary<string, object> ItunesMetadataDic { get; set; }
|
||||
|
||||
public string DisplayName { get; set; }
|
||||
|
||||
public string ShortVersionString { get; set; }
|
||||
|
||||
public string Version { get; set; }
|
||||
|
||||
public string Identifier { get; set; }
|
||||
|
||||
public byte[] Icon { get; set; }
|
||||
|
||||
public string IconName { get; set; }
|
||||
|
||||
public string DeviceFamily { get; set; }
|
||||
|
||||
public string MinimumOSVersion { get; set; }
|
||||
|
||||
public string PlatformVersion { get; set; }
|
||||
|
||||
public IpaReader(string path)
|
||||
{
|
||||
Open(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read));
|
||||
}
|
||||
|
||||
public IpaReader(Stream stream)
|
||||
{
|
||||
Open(stream);
|
||||
}
|
||||
|
||||
protected void Dispose()
|
||||
{
|
||||
zip?.Close();
|
||||
zip = null;
|
||||
}
|
||||
|
||||
private void Open(Stream stream)
|
||||
{
|
||||
zip = new ZipFile(stream);
|
||||
byte[] infoPlistData = null;
|
||||
|
||||
// Info.plist
|
||||
{
|
||||
foreach (ZipEntry entry in zip)
|
||||
{
|
||||
Match m = Regex.Match(entry.Name, @"(Payload/.*\.app/)Info\.plist");
|
||||
|
||||
if (m.Success)
|
||||
{
|
||||
appRoot = m.Groups[1].Value;
|
||||
ZipEntry infoPlist = zip.GetEntry(appRoot);
|
||||
using var s = new BinaryReader(zip.GetInputStream(entry));
|
||||
infoPlistData = s.ReadBytes((int)entry.Size);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (Plist.ReadPlist(infoPlistData) is Dictionary<string, object> dict)
|
||||
{
|
||||
InfoPlistDict = dict;
|
||||
}
|
||||
}
|
||||
{
|
||||
if (InfoPlistDict.TryGetValue("CFBundleDisplayName", out object value) && value is string stringValue)
|
||||
{
|
||||
DisplayName = stringValue;
|
||||
}
|
||||
}
|
||||
{
|
||||
if (InfoPlistDict.TryGetValue("CFBundleShortVersionString", out object value) && value is string stringValue)
|
||||
{
|
||||
ShortVersionString = stringValue;
|
||||
}
|
||||
}
|
||||
{
|
||||
if (InfoPlistDict.TryGetValue("CFBundleVersion", out object value) && value is string stringValue)
|
||||
{
|
||||
Version = stringValue;
|
||||
}
|
||||
}
|
||||
{
|
||||
if (InfoPlistDict.TryGetValue("CFBundleIdentifier", out object value) && value is string stringValue)
|
||||
{
|
||||
Identifier = stringValue;
|
||||
}
|
||||
}
|
||||
{
|
||||
if (InfoPlistDict.TryGetValue("MinimumOSVersion", out object value) && value is string stringValue)
|
||||
{
|
||||
MinimumOSVersion = $"iOS {stringValue}";
|
||||
}
|
||||
}
|
||||
{
|
||||
if (InfoPlistDict.TryGetValue("DTPlatformVersion", out object value) && value is string stringValue)
|
||||
{
|
||||
PlatformVersion = $"iOS {stringValue}";
|
||||
}
|
||||
}
|
||||
{
|
||||
if (InfoPlistDict.TryGetValue("UIDeviceFamily", out object familyNode) && familyNode is IEnumerable<object> list)
|
||||
{
|
||||
DeviceFamily = string.Join(", ",
|
||||
list.Select(deviceId => Convert.ToInt32(deviceId) switch
|
||||
{
|
||||
1 => "iPhone",
|
||||
2 => "iPad",
|
||||
3 => "Apple TV",
|
||||
4 => "Apple Watch",
|
||||
5 => "HomePod",
|
||||
6 => "Mac",
|
||||
7 => "Apple Vision Pro",
|
||||
_ => "Unknown Device"
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
if (InfoPlistDict.TryGetValue("CFBundleIcons", out object iconsNode) && iconsNode is IDictionary<string, object> icons)
|
||||
{
|
||||
if (icons.TryGetValue("CFBundlePrimaryIcon", out object primaryIconsNode) && primaryIconsNode is IDictionary<string, object> primaryIcons)
|
||||
{
|
||||
if (primaryIcons.TryGetValue("CFBundleIconFiles", out object iconFilesNode) && iconFilesNode is IList<object> iconFiles)
|
||||
{
|
||||
IconName = iconFiles.LastOrDefault() as string;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(IconName))
|
||||
{
|
||||
if (InfoPlistDict.TryGetValue("CFBundleIconFiles", out object iconFilesNode) && iconFilesNode is IList<object> iconFiles)
|
||||
{
|
||||
IconName = iconFiles.LastOrDefault() as string;
|
||||
}
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(IconName))
|
||||
{
|
||||
foreach (ZipEntry entry in zip)
|
||||
{
|
||||
if (entry.Name.StartsWith(appRoot))
|
||||
{
|
||||
string fileName = Path.GetFileName(entry.Name);
|
||||
|
||||
if (fileName.StartsWith(IconName))
|
||||
{
|
||||
using var s = new BinaryReader(zip.GetInputStream(entry));
|
||||
Icon = s.ReadBytes((int)entry.Size);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,910 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// This file is part of QuickLook program.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
|
||||
namespace QuickLook.Plugin.AppViewer.PackageParsers.Ipa;
|
||||
|
||||
/// <summary>
|
||||
/// https://github.com/animetrics/PlistCS
|
||||
/// </summary>
|
||||
public static class Plist
|
||||
{
|
||||
private static readonly List<int> offsetTable = [];
|
||||
private static List<byte> objectTable = [];
|
||||
private static int refCount;
|
||||
private static int objRefSize;
|
||||
private static int offsetByteSize;
|
||||
private static long offsetTableOffset;
|
||||
|
||||
public static object ReadPlist(string path)
|
||||
{
|
||||
using var f = new FileStream(path, FileMode.Open, FileAccess.Read);
|
||||
return ReadPlist(f, PlistType.Auto);
|
||||
}
|
||||
|
||||
public static object ReadPlistSource(string source)
|
||||
{
|
||||
return ReadPlist(Encoding.UTF8.GetBytes(source));
|
||||
}
|
||||
|
||||
public static object ReadPlist(byte[] data)
|
||||
{
|
||||
return ReadPlist(new MemoryStream(data), PlistType.Auto);
|
||||
}
|
||||
|
||||
public static PlistType GetPlistType(Stream stream)
|
||||
{
|
||||
byte[] magicHeader = new byte[8];
|
||||
stream.Read(magicHeader, 0, 8);
|
||||
|
||||
if (BitConverter.ToInt64(magicHeader, 0) == 3472403351741427810L)
|
||||
{
|
||||
return PlistType.Binary;
|
||||
}
|
||||
else
|
||||
{
|
||||
return PlistType.Xml;
|
||||
}
|
||||
}
|
||||
|
||||
public static object ReadPlist(Stream stream, PlistType type)
|
||||
{
|
||||
if (type == PlistType.Auto)
|
||||
{
|
||||
type = GetPlistType(stream);
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
}
|
||||
|
||||
if (type == PlistType.Binary)
|
||||
{
|
||||
using var reader = new BinaryReader(stream);
|
||||
byte[] data = reader.ReadBytes((int)reader.BaseStream.Length);
|
||||
return ReadBinary(data);
|
||||
}
|
||||
else
|
||||
{
|
||||
XmlDocument xml = new()
|
||||
{
|
||||
XmlResolver = null,
|
||||
};
|
||||
xml.Load(stream);
|
||||
return ReadXml(xml);
|
||||
}
|
||||
}
|
||||
|
||||
public static void WriteXml(object value, string path)
|
||||
{
|
||||
using var writer = new StreamWriter(path);
|
||||
writer.Write(WriteXml(value));
|
||||
}
|
||||
|
||||
public static void WriteXml(object value, Stream stream)
|
||||
{
|
||||
using var writer = new StreamWriter(stream);
|
||||
writer.Write(WriteXml(value));
|
||||
}
|
||||
|
||||
public static string WriteXml(object value)
|
||||
{
|
||||
using var ms = new MemoryStream();
|
||||
XmlWriterSettings xmlWriterSettings = new()
|
||||
{
|
||||
Encoding = new UTF8Encoding(false),
|
||||
ConformanceLevel = ConformanceLevel.Document,
|
||||
Indent = true,
|
||||
};
|
||||
|
||||
using var xmlWriter = XmlWriter.Create(ms, xmlWriterSettings);
|
||||
xmlWriter.WriteStartDocument();
|
||||
//xmlWriter.WriteComment("DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" " + "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"");
|
||||
xmlWriter.WriteDocType("plist", "-//Apple Computer//DTD PLIST 1.0//EN", "http://www.apple.com/DTDs/PropertyList-1.0.dtd", null);
|
||||
xmlWriter.WriteStartElement("plist");
|
||||
xmlWriter.WriteAttributeString("version", "1.0");
|
||||
Compose(value, xmlWriter);
|
||||
xmlWriter.WriteEndElement();
|
||||
xmlWriter.WriteEndDocument();
|
||||
xmlWriter.Flush();
|
||||
xmlWriter.Close();
|
||||
return Encoding.UTF8.GetString(ms.ToArray());
|
||||
}
|
||||
|
||||
public static void WriteBinary(object value, string path)
|
||||
{
|
||||
using var writer = new BinaryWriter(new FileStream(path, FileMode.Create));
|
||||
writer.Write(WriteBinary(value));
|
||||
}
|
||||
|
||||
public static void WriteBinary(object value, Stream stream)
|
||||
{
|
||||
using var writer = new BinaryWriter(stream);
|
||||
writer.Write(WriteBinary(value));
|
||||
}
|
||||
|
||||
public static byte[] WriteBinary(object value)
|
||||
{
|
||||
offsetTable.Clear();
|
||||
objectTable.Clear();
|
||||
refCount = 0;
|
||||
objRefSize = 0;
|
||||
offsetByteSize = 0;
|
||||
offsetTableOffset = 0;
|
||||
|
||||
//Do not count the root node, subtract by 1
|
||||
int totalRefs = CountObject(value) - 1;
|
||||
|
||||
refCount = totalRefs;
|
||||
|
||||
objRefSize = RegulateNullBytes(BitConverter.GetBytes(refCount)).Length;
|
||||
|
||||
ComposeBinary(value);
|
||||
|
||||
WriteBinaryString("bplist00", false);
|
||||
|
||||
offsetTableOffset = objectTable.Count;
|
||||
|
||||
offsetTable.Add(objectTable.Count - 8);
|
||||
|
||||
offsetByteSize = RegulateNullBytes(BitConverter.GetBytes(offsetTable[offsetTable.Count - 1])).Length;
|
||||
|
||||
List<byte> offsetBytes = [];
|
||||
|
||||
offsetTable.Reverse();
|
||||
|
||||
for (int i = 0; i < offsetTable.Count; i++)
|
||||
{
|
||||
offsetTable[i] = objectTable.Count - offsetTable[i];
|
||||
byte[] buffer = RegulateNullBytes(BitConverter.GetBytes(offsetTable[i]), offsetByteSize);
|
||||
Array.Reverse(buffer);
|
||||
offsetBytes.AddRange(buffer);
|
||||
}
|
||||
|
||||
objectTable.AddRange(offsetBytes);
|
||||
|
||||
objectTable.AddRange(new byte[6]);
|
||||
objectTable.Add(Convert.ToByte(offsetByteSize));
|
||||
objectTable.Add(Convert.ToByte(objRefSize));
|
||||
|
||||
var a = BitConverter.GetBytes((long)totalRefs + 1);
|
||||
Array.Reverse(a);
|
||||
objectTable.AddRange(a);
|
||||
|
||||
objectTable.AddRange(BitConverter.GetBytes((long)0));
|
||||
a = BitConverter.GetBytes(offsetTableOffset);
|
||||
Array.Reverse(a);
|
||||
objectTable.AddRange(a);
|
||||
|
||||
return [.. objectTable];
|
||||
}
|
||||
|
||||
private static object ReadXml(XmlDocument xml)
|
||||
{
|
||||
XmlNode rootNode = xml.DocumentElement.ChildNodes[0];
|
||||
return Parse(rootNode);
|
||||
}
|
||||
|
||||
private static object ReadBinary(byte[] data)
|
||||
{
|
||||
offsetTable.Clear();
|
||||
objectTable.Clear();
|
||||
refCount = 0;
|
||||
objRefSize = 0;
|
||||
offsetByteSize = 0;
|
||||
offsetTableOffset = 0;
|
||||
|
||||
List<byte> bList = [.. data];
|
||||
|
||||
List<byte> trailer = bList.GetRange(bList.Count - 32, 32);
|
||||
|
||||
ParseTrailer(trailer);
|
||||
|
||||
objectTable = bList.GetRange(0, (int)offsetTableOffset);
|
||||
|
||||
List<byte> offsetTableBytes = bList.GetRange((int)offsetTableOffset, bList.Count - (int)offsetTableOffset - 32);
|
||||
|
||||
ParseOffsetTable(offsetTableBytes);
|
||||
|
||||
return ParseBinary(0);
|
||||
}
|
||||
|
||||
private static Dictionary<string, object> ParseDictionary(XmlNode node)
|
||||
{
|
||||
XmlNodeList children = node.ChildNodes;
|
||||
if (children.Count % 2 != 0)
|
||||
{
|
||||
throw new DataMisalignedException("Dictionary elements must have an even number of child nodes");
|
||||
}
|
||||
|
||||
Dictionary<string, object> dict = [];
|
||||
|
||||
for (int i = 0; i < children.Count; i += 2)
|
||||
{
|
||||
XmlNode keynode = children[i];
|
||||
XmlNode valnode = children[i + 1];
|
||||
|
||||
if (keynode.Name != "key")
|
||||
{
|
||||
throw new ApplicationException("expected a key node");
|
||||
}
|
||||
|
||||
object result = Parse(valnode);
|
||||
|
||||
if (result != null)
|
||||
{
|
||||
dict.Add(keynode.InnerText, result);
|
||||
}
|
||||
}
|
||||
|
||||
//dict.Add("$Proxy", node);
|
||||
|
||||
return dict;
|
||||
}
|
||||
|
||||
private static List<object> ParseArray(XmlNode node)
|
||||
{
|
||||
List<object> array = [];
|
||||
|
||||
foreach (XmlNode child in node.ChildNodes)
|
||||
{
|
||||
object result = Parse(child);
|
||||
if (result != null)
|
||||
{
|
||||
array.Add(result);
|
||||
}
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
private static void ComposeArray(List<object> value, XmlWriter writer)
|
||||
{
|
||||
writer.WriteStartElement("array");
|
||||
foreach (object obj in value)
|
||||
{
|
||||
Compose(obj, writer);
|
||||
}
|
||||
writer.WriteEndElement();
|
||||
}
|
||||
|
||||
private static object Parse(XmlNode node)
|
||||
{
|
||||
return node.Name switch
|
||||
{
|
||||
"dict" => ParseDictionary(node),
|
||||
"array" => ParseArray(node),
|
||||
"string" => node.InnerText,
|
||||
"integer" => Convert.ToInt32(node.InnerText, NumberFormatInfo.InvariantInfo), // int result;
|
||||
// int.TryParse(node.InnerText, System.Globalization.NumberFormatInfo.InvariantInfo, out result);
|
||||
"real" => Convert.ToDouble(node.InnerText, NumberFormatInfo.InvariantInfo),
|
||||
"false" => false,
|
||||
"true" => true,
|
||||
"null" => null,
|
||||
"date" => XmlConvert.ToDateTime(node.InnerText, XmlDateTimeSerializationMode.Utc),
|
||||
"data" => Convert.FromBase64String(node.InnerText),
|
||||
_ => throw new ApplicationException(string.Format("Plist Node `{0}' is not supported", node.Name)),
|
||||
};
|
||||
}
|
||||
|
||||
private static void Compose(object value, XmlWriter writer)
|
||||
{
|
||||
if (value == null || value is string)
|
||||
{
|
||||
writer.WriteElementString("string", value as string);
|
||||
}
|
||||
else if (value is int || value is long)
|
||||
{
|
||||
writer.WriteElementString("integer", ((int)value).ToString(NumberFormatInfo.InvariantInfo));
|
||||
}
|
||||
else if (value is Dictionary<string, object> ||
|
||||
value.GetType().ToString().StartsWith("System.Collections.Generic.Dictionary`2[System.String"))
|
||||
{
|
||||
// Convert to Dictionary<string, object>
|
||||
if (value is not Dictionary<string, object> dic)
|
||||
{
|
||||
dic = [];
|
||||
IDictionary idic = (IDictionary)value;
|
||||
foreach (var key in idic.Keys)
|
||||
{
|
||||
dic.Add(key.ToString(), idic[key]);
|
||||
}
|
||||
}
|
||||
WriteDictionaryValues(dic, writer);
|
||||
}
|
||||
else if (value is List<object> list)
|
||||
{
|
||||
ComposeArray(list, writer);
|
||||
}
|
||||
else if (value is byte[] bytes)
|
||||
{
|
||||
writer.WriteElementString("data", Convert.ToBase64String(bytes));
|
||||
}
|
||||
else if (value is float || value is double)
|
||||
{
|
||||
writer.WriteElementString("real", ((double)value).ToString(NumberFormatInfo.InvariantInfo));
|
||||
}
|
||||
else if (value is DateTime time)
|
||||
{
|
||||
string theString = XmlConvert.ToString(time, XmlDateTimeSerializationMode.Utc);
|
||||
writer.WriteElementString("date", theString);//, "yyyy-MM-ddTHH:mm:ssZ"));
|
||||
}
|
||||
else if (value is bool)
|
||||
{
|
||||
writer.WriteElementString(value.ToString().ToLower(), "");
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception(string.Format("Value type '{0}' is unhandled", value.GetType().ToString()));
|
||||
}
|
||||
}
|
||||
|
||||
private static void WriteDictionaryValues(Dictionary<string, object> dictionary, XmlWriter writer)
|
||||
{
|
||||
writer.WriteStartElement("dict");
|
||||
foreach (string key in dictionary.Keys)
|
||||
{
|
||||
object value = dictionary[key];
|
||||
writer.WriteElementString("key", key);
|
||||
Compose(value, writer);
|
||||
}
|
||||
writer.WriteEndElement();
|
||||
}
|
||||
|
||||
private static int CountObject(object value)
|
||||
{
|
||||
int count = 0;
|
||||
switch (value.GetType().ToString())
|
||||
{
|
||||
case "System.Collections.Generic.Dictionary`2[System.String,System.Object]":
|
||||
Dictionary<string, object> dict = (Dictionary<string, object>)value;
|
||||
foreach (string key in dict.Keys)
|
||||
{
|
||||
count += CountObject(dict[key]);
|
||||
}
|
||||
count += dict.Keys.Count;
|
||||
count++;
|
||||
break;
|
||||
|
||||
case "System.Collections.Generic.List`1[System.Object]":
|
||||
List<object> list = (List<object>)value;
|
||||
foreach (object obj in list)
|
||||
{
|
||||
count += CountObject(obj);
|
||||
}
|
||||
count++;
|
||||
break;
|
||||
|
||||
default:
|
||||
count++;
|
||||
break;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
private static byte[] WriteBinaryDictionary(Dictionary<string, object> dictionary)
|
||||
{
|
||||
List<byte> buffer = [];
|
||||
List<byte> header = [];
|
||||
List<int> refs = [];
|
||||
for (int i = dictionary.Count - 1; i >= 0; i--)
|
||||
{
|
||||
var o = new object[dictionary.Count];
|
||||
dictionary.Values.CopyTo(o, 0);
|
||||
ComposeBinary(o[i]);
|
||||
offsetTable.Add(objectTable.Count);
|
||||
refs.Add(refCount);
|
||||
refCount--;
|
||||
}
|
||||
for (int i = dictionary.Count - 1; i >= 0; i--)
|
||||
{
|
||||
var o = new string[dictionary.Count];
|
||||
dictionary.Keys.CopyTo(o, 0);
|
||||
ComposeBinary(o[i]);
|
||||
offsetTable.Add(objectTable.Count);
|
||||
refs.Add(refCount);
|
||||
refCount--;
|
||||
}
|
||||
|
||||
if (dictionary.Count < 15)
|
||||
{
|
||||
header.Add(Convert.ToByte(0xD0 | Convert.ToByte(dictionary.Count)));
|
||||
}
|
||||
else
|
||||
{
|
||||
header.Add(0xD0 | 0xf);
|
||||
header.AddRange(WriteBinaryInteger(dictionary.Count, false));
|
||||
}
|
||||
|
||||
foreach (int val in refs)
|
||||
{
|
||||
byte[] refBuffer = RegulateNullBytes(BitConverter.GetBytes(val), objRefSize);
|
||||
Array.Reverse(refBuffer);
|
||||
buffer.InsertRange(0, refBuffer);
|
||||
}
|
||||
|
||||
buffer.InsertRange(0, header);
|
||||
|
||||
objectTable.InsertRange(0, buffer);
|
||||
|
||||
return [.. buffer];
|
||||
}
|
||||
|
||||
private static byte[] ComposeBinaryArray(List<object> objects)
|
||||
{
|
||||
List<byte> buffer = [];
|
||||
List<byte> header = [];
|
||||
List<int> refs = [];
|
||||
|
||||
for (int i = objects.Count - 1; i >= 0; i--)
|
||||
{
|
||||
ComposeBinary(objects[i]);
|
||||
offsetTable.Add(objectTable.Count);
|
||||
refs.Add(refCount);
|
||||
refCount--;
|
||||
}
|
||||
|
||||
if (objects.Count < 15)
|
||||
{
|
||||
header.Add(Convert.ToByte(0xA0 | Convert.ToByte(objects.Count)));
|
||||
}
|
||||
else
|
||||
{
|
||||
header.Add(0xA0 | 0xf);
|
||||
header.AddRange(WriteBinaryInteger(objects.Count, false));
|
||||
}
|
||||
|
||||
foreach (int val in refs)
|
||||
{
|
||||
byte[] refBuffer = RegulateNullBytes(BitConverter.GetBytes(val), objRefSize);
|
||||
Array.Reverse(refBuffer);
|
||||
buffer.InsertRange(0, refBuffer);
|
||||
}
|
||||
|
||||
buffer.InsertRange(0, header);
|
||||
|
||||
objectTable.InsertRange(0, buffer);
|
||||
|
||||
return [.. buffer];
|
||||
}
|
||||
|
||||
private static byte[] ComposeBinary(object obj)
|
||||
{
|
||||
byte[] value;
|
||||
switch (obj.GetType().ToString())
|
||||
{
|
||||
case "System.Collections.Generic.Dictionary`2[System.String,System.Object]":
|
||||
value = WriteBinaryDictionary((Dictionary<string, object>)obj);
|
||||
return value;
|
||||
|
||||
case "System.Collections.Generic.List`1[System.Object]":
|
||||
value = ComposeBinaryArray((List<object>)obj);
|
||||
return value;
|
||||
|
||||
case "System.Byte[]":
|
||||
value = WriteBinaryByteArray((byte[])obj);
|
||||
return value;
|
||||
|
||||
case "System.Double":
|
||||
value = WriteBinaryDouble((double)obj);
|
||||
return value;
|
||||
|
||||
case "System.Int32":
|
||||
value = WriteBinaryInteger((int)obj, true);
|
||||
return value;
|
||||
|
||||
case "System.String":
|
||||
value = WriteBinaryString((string)obj, true);
|
||||
return value;
|
||||
|
||||
case "System.DateTime":
|
||||
value = WriteBinaryDate((DateTime)obj);
|
||||
return value;
|
||||
|
||||
case "System.Boolean":
|
||||
value = WriteBinaryBool((bool)obj);
|
||||
return value;
|
||||
|
||||
default:
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] WriteBinaryDate(DateTime obj)
|
||||
{
|
||||
List<byte> buffer = [.. RegulateNullBytes(BitConverter.GetBytes(PlistDateConverter.ConvertToAppleTimeStamp(obj)), 8)];
|
||||
buffer.Reverse();
|
||||
buffer.Insert(0, 0x33);
|
||||
objectTable.InsertRange(0, buffer);
|
||||
return [.. buffer];
|
||||
}
|
||||
|
||||
public static byte[] WriteBinaryBool(bool obj)
|
||||
{
|
||||
List<byte> buffer = [.. new byte[1] { obj ? (byte)9 : (byte)8 }];
|
||||
objectTable.InsertRange(0, buffer);
|
||||
return [.. buffer];
|
||||
}
|
||||
|
||||
private static byte[] WriteBinaryInteger(int value, bool write)
|
||||
{
|
||||
List<byte> buffer = [.. BitConverter.GetBytes((long)value)];
|
||||
buffer = [.. RegulateNullBytes([.. buffer])];
|
||||
while (buffer.Count != Math.Pow(2, Math.Log(buffer.Count) / Math.Log(2)))
|
||||
buffer.Add(0);
|
||||
int header = 0x10 | (int)(Math.Log(buffer.Count) / Math.Log(2));
|
||||
|
||||
buffer.Reverse();
|
||||
|
||||
buffer.Insert(0, Convert.ToByte(header));
|
||||
|
||||
if (write)
|
||||
objectTable.InsertRange(0, buffer);
|
||||
|
||||
return [.. buffer];
|
||||
}
|
||||
|
||||
private static byte[] WriteBinaryDouble(double value)
|
||||
{
|
||||
List<byte> buffer = [.. RegulateNullBytes(BitConverter.GetBytes(value), 4)];
|
||||
while (buffer.Count != Math.Pow(2, Math.Log(buffer.Count) / Math.Log(2)))
|
||||
buffer.Add(0);
|
||||
int header = 0x20 | (int)(Math.Log(buffer.Count) / Math.Log(2));
|
||||
|
||||
buffer.Reverse();
|
||||
|
||||
buffer.Insert(0, Convert.ToByte(header));
|
||||
|
||||
objectTable.InsertRange(0, buffer);
|
||||
|
||||
return [.. buffer];
|
||||
}
|
||||
|
||||
private static byte[] WriteBinaryByteArray(byte[] value)
|
||||
{
|
||||
List<byte> buffer = [.. value];
|
||||
List<byte> header = [];
|
||||
if (value.Length < 15)
|
||||
{
|
||||
header.Add(Convert.ToByte(0x40 | Convert.ToByte(value.Length)));
|
||||
}
|
||||
else
|
||||
{
|
||||
header.Add(0x40 | 0xf);
|
||||
header.AddRange(WriteBinaryInteger(buffer.Count, false));
|
||||
}
|
||||
|
||||
buffer.InsertRange(0, header);
|
||||
|
||||
objectTable.InsertRange(0, buffer);
|
||||
|
||||
return [.. buffer];
|
||||
}
|
||||
|
||||
private static byte[] WriteBinaryString(string value, bool head)
|
||||
{
|
||||
List<byte> buffer = [];
|
||||
List<byte> header = [];
|
||||
foreach (char chr in value.ToCharArray())
|
||||
buffer.Add(Convert.ToByte(chr));
|
||||
|
||||
if (head)
|
||||
{
|
||||
if (value.Length < 15)
|
||||
{
|
||||
header.Add(Convert.ToByte(0x50 | Convert.ToByte(value.Length)));
|
||||
}
|
||||
else
|
||||
{
|
||||
header.Add(0x50 | 0xf);
|
||||
header.AddRange(WriteBinaryInteger(buffer.Count, false));
|
||||
}
|
||||
}
|
||||
|
||||
buffer.InsertRange(0, header);
|
||||
|
||||
objectTable.InsertRange(0, buffer);
|
||||
|
||||
return [.. buffer];
|
||||
}
|
||||
|
||||
private static byte[] RegulateNullBytes(byte[] value)
|
||||
{
|
||||
return RegulateNullBytes(value, 1);
|
||||
}
|
||||
|
||||
private static byte[] RegulateNullBytes(byte[] value, int minBytes)
|
||||
{
|
||||
Array.Reverse(value);
|
||||
List<byte> bytes = [.. value];
|
||||
for (int i = 0; i < bytes.Count; i++)
|
||||
{
|
||||
if (bytes[i] == 0 && bytes.Count > minBytes)
|
||||
{
|
||||
bytes.Remove(bytes[i]);
|
||||
i--;
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
if (bytes.Count < minBytes)
|
||||
{
|
||||
int dist = minBytes - bytes.Count;
|
||||
for (int i = 0; i < dist; i++)
|
||||
bytes.Insert(0, 0);
|
||||
}
|
||||
|
||||
value = [.. bytes];
|
||||
Array.Reverse(value);
|
||||
return value;
|
||||
}
|
||||
|
||||
private static void ParseTrailer(List<byte> trailer)
|
||||
{
|
||||
offsetByteSize = BitConverter.ToInt32(RegulateNullBytes([.. trailer.GetRange(6, 1)], 4), 0);
|
||||
objRefSize = BitConverter.ToInt32(RegulateNullBytes([.. trailer.GetRange(7, 1)], 4), 0);
|
||||
byte[] refCountBytes = [.. trailer.GetRange(12, 4)];
|
||||
Array.Reverse(refCountBytes);
|
||||
refCount = BitConverter.ToInt32(refCountBytes, 0);
|
||||
byte[] offsetTableOffsetBytes = [.. trailer.GetRange(24, 8)];
|
||||
Array.Reverse(offsetTableOffsetBytes);
|
||||
offsetTableOffset = BitConverter.ToInt64(offsetTableOffsetBytes, 0);
|
||||
}
|
||||
|
||||
private static void ParseOffsetTable(List<byte> offsetTableBytes)
|
||||
{
|
||||
for (int i = 0; i < offsetTableBytes.Count; i += offsetByteSize)
|
||||
{
|
||||
byte[] buffer = [.. offsetTableBytes.GetRange(i, offsetByteSize)];
|
||||
Array.Reverse(buffer);
|
||||
offsetTable.Add(BitConverter.ToInt32(RegulateNullBytes(buffer, 4), 0));
|
||||
}
|
||||
}
|
||||
|
||||
private static object ParseBinaryDictionary(int objRef)
|
||||
{
|
||||
Dictionary<string, object> buffer = [];
|
||||
List<int> refs = [];
|
||||
int refCount = GetCount(offsetTable[objRef], out _);
|
||||
int refStartPosition;
|
||||
if (refCount < 15)
|
||||
refStartPosition = offsetTable[objRef] + 1;
|
||||
else
|
||||
refStartPosition = offsetTable[objRef] + 2 + RegulateNullBytes(BitConverter.GetBytes(refCount), 1).Length;
|
||||
|
||||
for (int i = refStartPosition; i < refStartPosition + refCount * 2 * objRefSize; i += objRefSize)
|
||||
{
|
||||
byte[] refBuffer = [.. objectTable.GetRange(i, objRefSize)];
|
||||
Array.Reverse(refBuffer);
|
||||
refs.Add(BitConverter.ToInt32(RegulateNullBytes(refBuffer, 4), 0));
|
||||
}
|
||||
|
||||
for (int i = 0; i < refCount; i++)
|
||||
{
|
||||
buffer.Add((string)ParseBinary(refs[i]), ParseBinary(refs[i + refCount]));
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
private static object ParseBinaryArray(int objRef)
|
||||
{
|
||||
List<object> buffer = [];
|
||||
List<int> refs = [];
|
||||
int refCount = GetCount(offsetTable[objRef], out _);
|
||||
int refStartPosition;
|
||||
if (refCount < 15)
|
||||
refStartPosition = offsetTable[objRef] + 1;
|
||||
else
|
||||
// The following integer has a header aswell so we increase the refStartPosition by two to account for that.
|
||||
refStartPosition = offsetTable[objRef] + 2 + RegulateNullBytes(BitConverter.GetBytes(refCount), 1).Length;
|
||||
|
||||
for (int i = refStartPosition; i < refStartPosition + refCount * objRefSize; i += objRefSize)
|
||||
{
|
||||
byte[] refBuffer = [.. objectTable.GetRange(i, objRefSize)];
|
||||
Array.Reverse(refBuffer);
|
||||
refs.Add(BitConverter.ToInt32(RegulateNullBytes(refBuffer, 4), 0));
|
||||
}
|
||||
|
||||
for (int i = 0; i < refCount; i++)
|
||||
{
|
||||
buffer.Add(ParseBinary(refs[i]));
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
private static int GetCount(int bytePosition, out int newBytePosition)
|
||||
{
|
||||
byte headerByte = objectTable[bytePosition];
|
||||
byte headerByteTrail = Convert.ToByte(headerByte & 0xf);
|
||||
int count;
|
||||
if (headerByteTrail < 15)
|
||||
{
|
||||
count = headerByteTrail;
|
||||
newBytePosition = bytePosition + 1;
|
||||
}
|
||||
else
|
||||
count = (int)ParseBinaryInt(bytePosition + 1, out newBytePosition);
|
||||
return count;
|
||||
}
|
||||
|
||||
private static object ParseBinary(int objRef)
|
||||
{
|
||||
byte header = objectTable[offsetTable[objRef]];
|
||||
switch (header & 0xF0)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
// If the byte is
|
||||
// 0 return null
|
||||
// 9 return true
|
||||
// 8 return false
|
||||
return objectTable[offsetTable[objRef]] == 0 ? null : objectTable[offsetTable[objRef]] == 9;
|
||||
}
|
||||
case 0x10:
|
||||
{
|
||||
return ParseBinaryInt(offsetTable[objRef]);
|
||||
}
|
||||
case 0x20:
|
||||
{
|
||||
return ParseBinaryReal(offsetTable[objRef]);
|
||||
}
|
||||
case 0x30:
|
||||
{
|
||||
return ParseBinaryDate(offsetTable[objRef]);
|
||||
}
|
||||
case 0x40:
|
||||
{
|
||||
return ParseBinaryByteArray(offsetTable[objRef]);
|
||||
}
|
||||
case 0x50: // String ASCII
|
||||
{
|
||||
return ParseBinaryAsciiString(offsetTable[objRef]);
|
||||
}
|
||||
case 0x60: // String Unicode
|
||||
{
|
||||
return ParseBinaryUnicodeString(offsetTable[objRef]);
|
||||
}
|
||||
case 0xD0:
|
||||
{
|
||||
return ParseBinaryDictionary(objRef);
|
||||
}
|
||||
case 0xA0:
|
||||
{
|
||||
return ParseBinaryArray(objRef);
|
||||
}
|
||||
}
|
||||
throw new Exception("This type is not supported");
|
||||
}
|
||||
|
||||
public static object ParseBinaryDate(int headerPosition)
|
||||
{
|
||||
byte[] buffer = [.. objectTable.GetRange(headerPosition + 1, 8)];
|
||||
Array.Reverse(buffer);
|
||||
double appleTime = BitConverter.ToDouble(buffer, 0);
|
||||
DateTime result = PlistDateConverter.ConvertFromAppleTimeStamp(appleTime);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static object ParseBinaryInt(int headerPosition)
|
||||
{
|
||||
return ParseBinaryInt(headerPosition, out _);
|
||||
}
|
||||
|
||||
private static object ParseBinaryInt(int headerPosition, out int newHeaderPosition)
|
||||
{
|
||||
byte header = objectTable[headerPosition];
|
||||
int byteCount = (int)Math.Pow(2, header & 0xf);
|
||||
byte[] buffer = [.. objectTable.GetRange(headerPosition + 1, byteCount)];
|
||||
Array.Reverse(buffer);
|
||||
// Add one to account for the header byte
|
||||
newHeaderPosition = headerPosition + byteCount + 1;
|
||||
return BitConverter.ToInt32(RegulateNullBytes(buffer, 4), 0);
|
||||
}
|
||||
|
||||
private static object ParseBinaryReal(int headerPosition)
|
||||
{
|
||||
byte header = objectTable[headerPosition];
|
||||
int byteCount = (int)Math.Pow(2, header & 0xf);
|
||||
byte[] buffer = [.. objectTable.GetRange(headerPosition + 1, byteCount)];
|
||||
Array.Reverse(buffer);
|
||||
|
||||
return BitConverter.ToDouble(RegulateNullBytes(buffer, 8), 0);
|
||||
}
|
||||
|
||||
private static object ParseBinaryAsciiString(int headerPosition)
|
||||
{
|
||||
int charCount = GetCount(headerPosition, out int charStartPosition);
|
||||
var buffer = objectTable.GetRange(charStartPosition, charCount);
|
||||
return buffer.Count > 0 ? Encoding.ASCII.GetString([.. buffer]) : string.Empty;
|
||||
}
|
||||
|
||||
private static object ParseBinaryUnicodeString(int headerPosition)
|
||||
{
|
||||
int charCount = GetCount(headerPosition, out int charStartPosition);
|
||||
charCount *= 2;
|
||||
|
||||
byte[] buffer = new byte[charCount];
|
||||
byte one, two;
|
||||
|
||||
for (int i = 0; i < charCount; i += 2)
|
||||
{
|
||||
one = objectTable.GetRange(charStartPosition + i, 1)[0];
|
||||
two = objectTable.GetRange(charStartPosition + i + 1, 1)[0];
|
||||
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
buffer[i] = two;
|
||||
buffer[i + 1] = one;
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer[i] = one;
|
||||
buffer[i + 1] = two;
|
||||
}
|
||||
}
|
||||
|
||||
return Encoding.Unicode.GetString(buffer);
|
||||
}
|
||||
|
||||
private static object ParseBinaryByteArray(int headerPosition)
|
||||
{
|
||||
int byteCount = GetCount(headerPosition, out int byteStartPosition);
|
||||
return objectTable.GetRange(byteStartPosition, byteCount).ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public enum PlistType
|
||||
{
|
||||
Auto,
|
||||
Binary,
|
||||
Xml,
|
||||
}
|
||||
|
||||
public static class PlistDateConverter
|
||||
{
|
||||
public const long timeDifference = 978307200L;
|
||||
|
||||
public static long GetAppleTime(long unixTime)
|
||||
{
|
||||
return unixTime - timeDifference;
|
||||
}
|
||||
|
||||
public static long GetUnixTime(long appleTime)
|
||||
{
|
||||
return appleTime + timeDifference;
|
||||
}
|
||||
|
||||
public static DateTime ConvertFromAppleTimeStamp(double timestamp)
|
||||
{
|
||||
DateTime origin = new(2001, 1, 1, 0, 0, 0, 0);
|
||||
return origin.AddSeconds(timestamp);
|
||||
}
|
||||
|
||||
public static double ConvertToAppleTimeStamp(DateTime date)
|
||||
{
|
||||
DateTime begin = new(2001, 1, 1, 0, 0, 0, 0);
|
||||
TimeSpan diff = date - begin;
|
||||
return Math.Floor(diff.TotalSeconds);
|
||||
}
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// This file is part of QuickLook program.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace QuickLook.Plugin.AppViewer.PackageParsers.Msi;
|
||||
|
||||
public sealed class MsiInfo
|
||||
{
|
||||
public string ProductVersion { get; set; }
|
||||
|
||||
public string ProductName { get; set; }
|
||||
|
||||
public string Manufacturer { get; set; }
|
||||
|
||||
public string ProductCode { get; set; }
|
||||
|
||||
public string UpgradeCode { get; set; }
|
||||
}
|
@@ -0,0 +1,45 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// This file is part of QuickLook program.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
using WixToolset.Dtf.WindowsInstaller;
|
||||
|
||||
namespace QuickLook.Plugin.AppViewer.PackageParsers.Msi;
|
||||
|
||||
public static class MsiParser
|
||||
{
|
||||
public static MsiInfo Parse(string path)
|
||||
{
|
||||
MsiInfo info = new();
|
||||
using var database = new Database(path, DatabaseOpenMode.ReadOnly);
|
||||
|
||||
info.ProductVersion = database.ReadPropertyString(nameof(MsiInfo.ProductVersion));
|
||||
info.ProductName = database.ReadPropertyString(nameof(MsiInfo.ProductName));
|
||||
info.Manufacturer = database.ReadPropertyString(nameof(MsiInfo.Manufacturer));
|
||||
info.ProductCode = database.ReadPropertyString(nameof(MsiInfo.ProductCode));
|
||||
info.UpgradeCode = database.ReadPropertyString(nameof(MsiInfo.UpgradeCode));
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
private static string ReadPropertyString(this Database database, string propertyName)
|
||||
{
|
||||
using View view = database.OpenView($"SELECT `Value` FROM `Property` WHERE `Property`='{propertyName}'");
|
||||
view.Execute(null);
|
||||
using Record record = view.Fetch();
|
||||
return record?.GetString(1);
|
||||
}
|
||||
}
|
@@ -0,0 +1,111 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// This file is part of QuickLook program.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
|
||||
namespace QuickLook.Plugin.AppViewer.PackageParsers.Wgt;
|
||||
|
||||
public sealed class WgtInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Json path: @platforms
|
||||
/// </summary>
|
||||
public string[] Platforms { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Json path: id
|
||||
/// </summary>
|
||||
public string AppId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Json path: name
|
||||
/// </summary>
|
||||
public string AppName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Json path: plus.locales.xxx.name
|
||||
/// </summary>
|
||||
public Dictionary<string, string> AppNameLocales { get; set; } = [];
|
||||
|
||||
public string AppNameLocale
|
||||
{
|
||||
get
|
||||
{
|
||||
try
|
||||
{
|
||||
CultureInfo culture = CultureInfo.CurrentCulture;
|
||||
|
||||
while (!AppNameLocales.ContainsKey(culture.Name))
|
||||
{
|
||||
if (culture.Parent == CultureInfo.InvariantCulture)
|
||||
{
|
||||
culture = CultureInfo.InvariantCulture;
|
||||
break;
|
||||
}
|
||||
culture = culture.Parent;
|
||||
}
|
||||
|
||||
return AppNameLocales[culture.Name];
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Json path: plus.uni-app.vueVersion
|
||||
/// </summary>
|
||||
public string VueVersion { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Json path: plus.uni-app.compilerVersion
|
||||
/// </summary>
|
||||
public string CompilerVersion { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Json path: versionName.name
|
||||
/// </summary>
|
||||
public string AppVersionName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Json path: versionName.code
|
||||
/// </summary>
|
||||
public string AppVersionCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Json path: description
|
||||
/// </summary>
|
||||
public string AppDescription { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Json path: descriptionLocales
|
||||
/// </summary>
|
||||
public Dictionary<string, string> AppDescriptionLocales { get; set; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// Json path: descriptionLocales
|
||||
/// </summary>
|
||||
public string[] Permissions { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Json path: fallbackLocale
|
||||
/// </summary>
|
||||
public string FallbackLocale { get; set; }
|
||||
}
|
@@ -0,0 +1,130 @@
|
||||
// Copyright © 2017-2025 QL-Win Contributors
|
||||
//
|
||||
// This file is part of QuickLook program.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace QuickLook.Plugin.AppViewer.PackageParsers.Wgt;
|
||||
|
||||
internal static class WgtParser
|
||||
{
|
||||
public static WgtInfo Parse(string path)
|
||||
{
|
||||
using var fileStream = File.OpenRead(path);
|
||||
using var zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Read);
|
||||
var manifestEntry = zipArchive.GetEntry("manifest.json");
|
||||
|
||||
if (manifestEntry != null)
|
||||
{
|
||||
using var stream = manifestEntry.Open();
|
||||
using var reader = new StreamReader(stream, Encoding.UTF8);
|
||||
string content = reader.ReadToEnd();
|
||||
var dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(content);
|
||||
|
||||
if (dict == null) return null;
|
||||
|
||||
var info = new WgtInfo();
|
||||
|
||||
if (dict.ContainsKey("@platforms"))
|
||||
{
|
||||
info.Platforms = ((JArray)dict["@platforms"]).Values().Select(v => v.ToString()).ToArray();
|
||||
}
|
||||
if (dict.ContainsKey("id")) info.AppId = dict["id"].ToString();
|
||||
if (dict.ContainsKey("name")) info.AppName = dict["name"].ToString();
|
||||
if (dict.ContainsKey("version"))
|
||||
{
|
||||
var version = (JObject)dict["version"];
|
||||
|
||||
if (version != null)
|
||||
{
|
||||
if (version.ContainsKey("name")) info.AppVersionName = version["name"].ToString();
|
||||
if (version.ContainsKey("code")) info.AppVersionCode = version["code"].ToString();
|
||||
}
|
||||
}
|
||||
if (dict.ContainsKey("description")) info.AppDescription = dict["description"].ToString();
|
||||
if (dict.ContainsKey("permissions"))
|
||||
{
|
||||
var permissions = (JObject)dict["permissions"];
|
||||
|
||||
if (permissions != null)
|
||||
{
|
||||
List<string> permissionNames = [];
|
||||
foreach (var permission in permissions)
|
||||
{
|
||||
permissionNames.Add(permission.Key);
|
||||
}
|
||||
info.Permissions = [.. permissionNames];
|
||||
}
|
||||
}
|
||||
if (dict.ContainsKey("plus"))
|
||||
{
|
||||
var plus = (JObject)dict["plus"];
|
||||
|
||||
if (plus != null)
|
||||
{
|
||||
if (plus.ContainsKey("locales"))
|
||||
{
|
||||
var locales = plus["locales"];
|
||||
var dictionary = locales.ToObject<Dictionary<string, Dictionary<string, object>>>();
|
||||
|
||||
foreach (var locale in dictionary)
|
||||
{
|
||||
foreach (var kv in locale.Value)
|
||||
{
|
||||
if (kv.Key == "name")
|
||||
{
|
||||
info.AppNameLocales.Add(locale.Key, kv.Value.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (plus.ContainsKey("uni-app"))
|
||||
{
|
||||
var uni_app = plus["uni-app"];
|
||||
var dictionary = uni_app.ToObject<Dictionary<string, object>>();
|
||||
|
||||
if (dictionary.ContainsKey("vueVersion")) info.VueVersion = dictionary["vueVersion"].ToString();
|
||||
if (dictionary.ContainsKey("compilerVersion")) info.CompilerVersion = dictionary["compilerVersion"].ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (dict.ContainsKey("descriptionLocales"))
|
||||
{
|
||||
var descriptionLocales = (JObject)dict["descriptionLocales"];
|
||||
|
||||
if (descriptionLocales != null)
|
||||
{
|
||||
foreach (var descriptionLocale in descriptionLocales)
|
||||
{
|
||||
info.AppDescriptionLocales.Add(descriptionLocale.Key, descriptionLocale.Value.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (dict.ContainsKey("fallbackLocale")) info.FallbackLocale = dict["fallbackLocale"].ToString();
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user