Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/actions/spelling/allow.txt
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ NNN
NOCASE
NOCLOSEPROCESS
nodiscard
nologo
NOMINMAX
nonexistentsetting
norestart
Expand Down Expand Up @@ -383,6 +384,7 @@ testsettingname
TEXTFORMAT
TEXTINCLUDE
threadpool
tlog
TOCTOU
toolsets
tpl
Expand Down
3 changes: 3 additions & 0 deletions .github/actions/spelling/patterns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ El proyecto .* diferentes
# schema regex
"pattern": .*$

# Log viewer parsing and highlighting regular expressions
\b(?:LOG_LINE_RE|KW_RE) = /[^*\r\n].*/[gi]*(?=;|$)

# doc/.../manifest.md
^ShortDescription: Le nouveau.*$

Expand Down
7 changes: 7 additions & 0 deletions doc/release-notes/1.31/compress-embedded-json-schemas.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## New Features

### Smaller native binaries

Embedded JSON schemas are minified and compressed to reduce the size of the
Windows Package Manager and its utility binaries without changing schema
validation or the published schema files.
5 changes: 5 additions & 0 deletions src/AppInstallerCLI.sln
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Management.Deploy
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "targets", "targets", "{A0B4F808-B190-41C4-97CB-C8EA1932F84F}"
ProjectSection(SolutionItems) = preProject
targets\CompressedResources.cs = targets\CompressedResources.cs
targets\CompressedResources.md = targets\CompressedResources.md
targets\CompressedResources.targets = targets\CompressedResources.targets
targets\EmbeddedCsWinRT.targets = targets\EmbeddedCsWinRT.targets
targets\Generate-CompressedResources.ps1 = targets\Generate-CompressedResources.ps1
targets\ReferenceEmbeddedCsWinRTProject.targets = targets\ReferenceEmbeddedCsWinRTProject.targets
targets\Test-CompressedResources.ps1 = targets\Test-CompressedResources.ps1
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PureLib", "PureLib\PureLib.vcxitems", "{A33223D2-550B-4D99-A53D-488B1F68683E}"
Expand Down
8 changes: 8 additions & 0 deletions src/AppInstallerCLITests/AppInstallerCLITests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<WindowsTargetPlatformVersion>10.0.26100.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformMinVersion>10.0.17763.0</WindowsTargetPlatformMinVersion>
<ProjectName>AppInstallerCLITests</ProjectName>
<CompressedResourceTestOriginals>true</CompressedResourceTestOriginals>
<CppWinRTParameters>
</CppWinRTParameters>
</PropertyGroup>
Expand Down Expand Up @@ -63,6 +64,13 @@
<Import Project="..\WinGetSchemas\WinGetSchemas.vcxitems" Label="Shared" />
<Import Project="..\CertificateResources\CertificateResources.vcxitems" Label="Shared" />
</ImportGroup>
<ItemGroup>
<CustomBuild Include="CompressedResources.rc">
<CompressionPreprocess>None</CompressionPreprocess>
</CustomBuild>
<CompressedResourceInput Include="TestData\notepad.ico;TestData\CompressedResourceInvalidJson.txt" />
<None Include="TestData\CompressedResourceInvalidJson.txt" />
</ItemGroup>
<ImportGroup Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,12 @@
<None Include="packages.config" />
<None Include="Run-TestsInPackage.ps1" />
</ItemGroup>
<ItemGroup>
<CustomBuild Include="CompressedResources.rc" />
<None Include="TestData\CompressedResourceInvalidJson.txt">
<Filter>TestData</Filter>
</None>
</ItemGroup>
<ItemGroup>
<CopyFileToFolders Include="TestData\TestRoot.cer">
<Filter>TestData</Filter>
Expand Down
9 changes: 9 additions & 0 deletions src/AppInstallerCLITests/CompressedResources.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

BINARY COMPRESSED_RESOURCE_TEST "TestData\\notepad.ico"
INVALID_JSON COMPRESSED_RESOURCE_TEST "TestData\\CompressedResourceInvalidJson.txt"

CORRUPT COMPRESSED_RESOURCE_TEST BEGIN
0x0000, 0x0000
END
59 changes: 59 additions & 0 deletions src/AppInstallerCLITests/Resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,29 @@
#include <ChannelStreams.h>
#include <ExecutionReporter.h>
#include <winget/Resources.h>
#include <winget/JsonSchemaValidation.h>
#include <ManifestSchema.h>
#include <PackagesSchema.h>

using namespace std::string_view_literals;
using namespace AppInstaller::Utility;
using namespace AppInstaller::Utility::literals;
using namespace AppInstaller::CLI;

namespace
{
BOOL CALLBACK CollectSchemaResourceIds(HMODULE, LPCWSTR, LPWSTR name, LONG_PTR context)
{
if (!IS_INTRESOURCE(name))
{
return FALSE;
}

reinterpret_cast<std::vector<WORD>*>(context)->push_back(static_cast<WORD>(reinterpret_cast<ULONG_PTR>(name)));
return TRUE;
}
}

#define WINGET_TEST_OUTPUT_STREAM(_expected_, _input_) \
do { \
std::istringstream iInput; \
Expand All @@ -31,6 +48,48 @@ TEST_CASE("Resources_StringId", "[resources]")
);
}

TEST_CASE("Resources_CompressedBinary", "[resources]")
{
auto expected = AppInstaller::Resource::GetResourceAsBytes(L"BINARY", L"ORIGINAL_COMPRESSED_RESOURCE_TEST");
auto actual = AppInstaller::Resource::GetResourceAsDecompressedBytes(L"BINARY", L"COMPRESSED_RESOURCE_TEST");
REQUIRE(actual == std::vector<uint8_t>(expected.first, expected.first + expected.second));
REQUIRE(std::find(actual.begin(), actual.end(), uint8_t{ 0 }) != actual.end());

auto raw = AppInstaller::Resource::GetResourceAsBytes(L"BINARY", L"COMPRESSED_RESOURCE_TEST");
REQUIRE(actual != std::vector<uint8_t>(raw.first, raw.first + raw.second));
}

TEST_CASE("Resources_CompressedSchemaParity", "[resources]")
{
const std::pair<int, PCWSTR> types[] = {
{ MANIFESTSCHEMA_RESOURCE_TYPE, L"ORIGINAL_MANIFESTSCHEMA_RESOURCE_TYPE" },
{ PACKAGESSCHEMA_RESOURCE_TYPE, L"ORIGINAL_PACKAGESSCHEMA_RESOURCE_TYPE" },
};

for (const auto& [type, originalType] : types)
{
std::vector<WORD> ids;
REQUIRE(EnumResourceNamesW(GetModuleHandleW(nullptr), MAKEINTRESOURCE(type), CollectSchemaResourceIds, reinterpret_cast<LONG_PTR>(&ids)));
REQUIRE(!ids.empty());
for (auto id : ids)
{
CAPTURE(type, id);
auto source = AppInstaller::Resource::GetResourceAsString(MAKEINTRESOURCE(id), originalType);
auto actual = AppInstaller::JsonSchema::LoadResourceAsSchemaDoc(MAKEINTRESOURCE(id), MAKEINTRESOURCE(type));
REQUIRE(actual == AppInstaller::JsonSchema::LoadSchemaDoc(source));
auto bytes = AppInstaller::Resource::GetResourceAsDecompressedBytes(id, type);
REQUIRE(AppInstaller::JsonSchema::LoadSchemaDoc({ reinterpret_cast<const char*>(bytes.data()), bytes.size() }) == actual);
}
}
}

TEST_CASE("Resources_CompressedFailures", "[resources]")
{
REQUIRE_THROWS_HR(AppInstaller::Resource::GetResourceAsDecompressedBytes(L"CORRUPT", L"COMPRESSED_RESOURCE_TEST"), HRESULT_FROM_WIN32(ERROR_BAD_COMPRESSION_BUFFER));
REQUIRE_THROWS_HR(AppInstaller::Resource::GetResourceAsDecompressedBytes(L"MISSING", L"COMPRESSED_RESOURCE_TEST"), HRESULT_FROM_WIN32(ERROR_RESOURCE_NAME_NOT_FOUND));
REQUIRE_THROWS_HR(AppInstaller::JsonSchema::LoadResourceAsSchemaDoc(L"INVALID_JSON", L"COMPRESSED_RESOURCE_TEST"), E_UNEXPECTED);
}

TEST_CASE("Resources_StringIdWithPlaceholders_LocIndString", "[resources]")
{
WINGET_TEST_OUTPUT_STREAM(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is not JSON.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "winget/ManifestCommon.h"
#include "winget/ManifestSchemaValidation.h"
#include "winget/ManifestYamlParser.h"
#include "winget/Resources.h"

#include <ManifestSchema.h>

Expand Down Expand Up @@ -409,8 +408,7 @@ namespace AppInstaller::Manifest::YamlParser
THROW_HR(HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED));
}

std::string_view schemaStr = Resource::GetResourceAsString(idx, MANIFESTSCHEMA_RESOURCE_TYPE);
return JsonSchema::LoadSchemaDoc(schemaStr);
return JsonSchema::LoadResourceAsSchemaDoc(MAKEINTRESOURCE(idx), MAKEINTRESOURCE(MANIFESTSCHEMA_RESOURCE_TYPE));
}

std::vector<ValidationError> ValidateAgainstSchema(const std::vector<YamlManifestInfo>& manifestList, const ManifestVer& manifestVersion)
Expand Down
12 changes: 9 additions & 3 deletions src/AppInstallerSharedLib/Compression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,25 @@ namespace AppInstaller::Compression
}

std::vector<uint8_t> Decompressor::Decompress(const std::vector<uint8_t>& data)
{
return Decompress(data.data(), data.size());
}

std::vector<uint8_t> Decompressor::Decompress(const uint8_t* data, size_t size)
{
std::vector<uint8_t> result;

if (!data.empty())
if (size)
{
THROW_HR_IF_NULL(E_INVALIDARG, data);
SIZE_T decompressedBufferSize = 0;
THROW_HR_IF(E_UNEXPECTED, ::Decompress(m_decompressor.get(), data.data(), data.size(), nullptr, 0, &decompressedBufferSize));
THROW_HR_IF(E_UNEXPECTED, ::Decompress(m_decompressor.get(), data, size, nullptr, 0, &decompressedBufferSize));
THROW_LAST_ERROR_IF(GetLastError() != ERROR_INSUFFICIENT_BUFFER);

result.resize(decompressedBufferSize);

SIZE_T decompressedDataSize = 0;
THROW_IF_WIN32_BOOL_FALSE(::Decompress(m_decompressor.get(), data.data(), data.size(), &result[0], result.size(), &decompressedDataSize));
THROW_IF_WIN32_BOOL_FALSE(::Decompress(m_decompressor.get(), data, size, result.data(), result.size(), &decompressedDataSize));

result.resize(decompressedDataSize);
}
Expand Down
3 changes: 2 additions & 1 deletion src/AppInstallerSharedLib/JsonSchemaValidation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ namespace AppInstaller::JsonSchema

Json::Value LoadResourceAsSchemaDoc(PCWSTR resourceName, PCWSTR resourceType)
{
return LoadSchemaDoc(Resource::GetResourceAsString(resourceName, resourceType));
auto data = Resource::GetResourceAsDecompressedBytes(resourceName, resourceType);
return LoadSchemaDoc({ reinterpret_cast<const char*>(data.data()), data.size() });
}

void PopulateSchema(const Json::Value& schemaJson, valijson::Schema& schema)
Expand Down
1 change: 1 addition & 0 deletions src/AppInstallerSharedLib/Public/winget/Compression.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace AppInstaller::Compression

// Decompresses the given data.
std::vector<uint8_t> Decompress(const std::vector<uint8_t>& data);
std::vector<uint8_t> Decompress(const uint8_t* data, size_t size);

// Resets the decompressor.
void Reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
#pragma once
#include <json/json.h>

namespace valijson
{
class Schema;
class ValidationResults;
}

namespace AppInstaller::JsonSchema
{
// Load schema as parsed json doc
Json::Value LoadSchemaDoc(std::string_view schemaStr);

// Load an embedded resource from binary and return as Json::Value
// Load an MSZIP-compressed embedded resource and return as Json::Value.
Json::Value LoadResourceAsSchemaDoc(PCWSTR resourceName, PCWSTR resourceType);

// Populate a valijson Schema object from a json value
Expand Down
5 changes: 5 additions & 0 deletions src/AppInstallerSharedLib/Public/winget/Resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <winrt/Windows.ApplicationModel.Resources.h>

#include <string>
#include <cstdint>
#include <optional>
#include <vector>
#include "AppInstallerStrings.h"
Expand Down Expand Up @@ -87,6 +88,10 @@ namespace AppInstaller
std::pair<const BYTE*, size_t> GetResourceAsBytes(int resourceName, int resourceType);
std::pair<const BYTE*, size_t> GetResourceAsBytes(PCWSTR resourceName, PCWSTR resourceType);

// Decompress an MSZIP buffer-mode resource into owned storage.
std::vector<uint8_t> GetResourceAsDecompressedBytes(int resourceName, int resourceType);
std::vector<uint8_t> GetResourceAsDecompressedBytes(PCWSTR resourceName, PCWSTR resourceType);

// A localized string
struct LocString : public Utility::LocIndString
{
Expand Down
12 changes: 12 additions & 0 deletions src/AppInstallerSharedLib/Resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.
#include "pch.h"
#include "winget/Resources.h"
#include "winget/Compression.h"
#include "Public/AppInstallerLogging.h"
#include "Public/AppInstallerStrings.h"
#include "Public/AppInstallerErrors.h"
Expand Down Expand Up @@ -61,6 +62,17 @@ namespace AppInstaller
return std::make_pair(reinterpret_cast<BYTE*>(resourceData.first), resourceData.second);
}

std::vector<uint8_t> GetResourceAsDecompressedBytes(int resourceName, int resourceType)
{
return GetResourceAsDecompressedBytes(MAKEINTRESOURCE(resourceName), MAKEINTRESOURCE(resourceType));
}

std::vector<uint8_t> GetResourceAsDecompressedBytes(PCWSTR resourceName, PCWSTR resourceType)
{
auto [data, size] = GetResourceAsBytes(resourceName, resourceType);
return Compression::Decompressor{ COMPRESS_ALGORITHM_MSZIP }.Decompress(data, size);
}

// Utility class to load resources
struct Loader
{
Expand Down
2 changes: 2 additions & 0 deletions src/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<!-- Consume containing solution build targets if present. -->
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.targets', '$(MSBuildThisFileDirectory)../'))" Condition="'' != $([MSBuild]::GetPathOfFileAbove('Directory.Build.targets', '$(MSBuildThisFileDirectory)../'))" />

<Import Project="$(MSBuildThisFileDirectory)targets\CompressedResources.targets" Condition="'$(MSBuildProjectExtension)' == '.vcxproj'" />

<!-- Incremental link time code generation writes .iobj and .ipdb files that can be hundreds of megabytes
per binary. They only pay off across repeated local links, so they are pure disk cost on build agents.
This must be in Directory.Build.targets rather than Directory.Build.props, as WholeProgramOptimization
Expand Down
5 changes: 4 additions & 1 deletion src/ManifestSchema/ManifestSchema.vcxitems
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
<ClInclude Include="$(MSBuildThisFileDirectory)resource.h" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="$(MSBuildThisFileDirectory)ManifestSchema.rc" />
<CustomBuild Include="$(MSBuildThisFileDirectory)ManifestSchema.rc">
<CompressionPreprocess>Json</CompressionPreprocess>
</CustomBuild>
<CompressedResourceInput Include="$(MSBuildThisFileDirectory)..\..\schemas\JSON\manifests\**\*.json" />
</ItemGroup>
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)..\..\schemas\JSON\manifests\latest\manifest.defaultLocale.latest.json" />
Expand Down
2 changes: 1 addition & 1 deletion src/ManifestSchema/ManifestSchema.vcxitems.filters
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<ClInclude Include="$(MSBuildThisFileDirectory)resource.h" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="$(MSBuildThisFileDirectory)ManifestSchema.rc" />
<CustomBuild Include="$(MSBuildThisFileDirectory)ManifestSchema.rc" />
</ItemGroup>
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)..\..\schemas\JSON\manifests\latest\manifest.defaultLocale.latest.json">
Expand Down
5 changes: 4 additions & 1 deletion src/WinGetSchemas/WinGetSchemas.vcxitems
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<ClInclude Include="$(MSBuildThisFileDirectory)resource.h" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="$(MSBuildThisFileDirectory)WinGetSchemas.rc" />
<CustomBuild Include="$(MSBuildThisFileDirectory)WinGetSchemas.rc">
<CompressionPreprocess>Json</CompressionPreprocess>
</CustomBuild>
<CompressedResourceInput Include="$(MSBuildThisFileDirectory)..\..\schemas\JSON\packages\*.json" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/WinGetSchemas/WinGetSchemas.vcxitems.filters
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
<ClInclude Include="$(MSBuildThisFileDirectory)PackagesSchema.h" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="$(MSBuildThisFileDirectory)WinGetSchemas.rc" />
<CustomBuild Include="$(MSBuildThisFileDirectory)WinGetSchemas.rc" />
</ItemGroup>
</Project>
Loading
Loading