From 754ad610489603f80ea38bbd2cf18b28f0d8bf1d Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Fri, 20 Apr 2018 20:11:26 -0400 Subject: Import XULRunner and make it produce the generic stub --- application/xulrunner/tools/redit/Makefile.in | 11 ++ application/xulrunner/tools/redit/moz.build | 15 +++ application/xulrunner/tools/redit/redit.cpp | 187 ++++++++++++++++++++++++++ 3 files changed, 213 insertions(+) create mode 100644 application/xulrunner/tools/redit/Makefile.in create mode 100644 application/xulrunner/tools/redit/moz.build create mode 100644 application/xulrunner/tools/redit/redit.cpp (limited to 'application/xulrunner/tools') diff --git a/application/xulrunner/tools/redit/Makefile.in b/application/xulrunner/tools/redit/Makefile.in new file mode 100644 index 000000000..31c253be8 --- /dev/null +++ b/application/xulrunner/tools/redit/Makefile.in @@ -0,0 +1,11 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this file, +# You can obtain one at http://mozilla.org/MPL/2.0/. */ + +ifeq ($(OS_ARCH),WINNT) + +SDK_BINARY = \ + $(PROGRAM) \ + $(NULL) + +endif diff --git a/application/xulrunner/tools/redit/moz.build b/application/xulrunner/tools/redit/moz.build new file mode 100644 index 000000000..812a56578 --- /dev/null +++ b/application/xulrunner/tools/redit/moz.build @@ -0,0 +1,15 @@ +# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +if CONFIG['OS_ARCH'] == 'WINNT': + GeckoProgram('redit') + SOURCES += [ + 'redit.cpp', + ] + for var in ('WIN32_LEAN_AND_MEAN', 'UNICODE', '_UNICODE'): + DEFINES[var] = True + if CONFIG['GNU_CC']: + WIN32_EXE_LDFLAGS += ['-municode'] diff --git a/application/xulrunner/tools/redit/redit.cpp b/application/xulrunner/tools/redit/redit.cpp new file mode 100644 index 000000000..8a96358b2 --- /dev/null +++ b/application/xulrunner/tools/redit/redit.cpp @@ -0,0 +1,187 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ + +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// System headers (alphabetical) +#include +#include +#include +#include +#include +#include +#include + +// Mozilla headers (alphabetical) +#include "mozilla/FileUtils.h" // ScopedClose +#include "mozilla/UniquePtrExtensions.h" + +/* +Icon files are made up of: + +IconHeader +IconDirEntry1 +IconDirEntry2 +... +IconDirEntryN +IconData1 +IconData2 +... +IconDataN + +Each IconData must be added as a new RT_ICON resource to the exe. Then +an RT_GROUP_ICON resource must be added that contains an equivalent +header: + +IconHeader +IconResEntry1 +IconResEntry2 +... +IconResEntryN +*/ + +#pragma pack(push, 2) +typedef struct +{ + WORD Reserved; + WORD ResourceType; + WORD ImageCount; +} IconHeader; + +typedef struct +{ + BYTE Width; + BYTE Height; + BYTE Colors; + BYTE Reserved; + WORD Planes; + WORD BitsPerPixel; + DWORD ImageSize; + DWORD ImageOffset; +} IconDirEntry; + +typedef struct +{ + BYTE Width; + BYTE Height; + BYTE Colors; + BYTE Reserved; + WORD Planes; + WORD BitsPerPixel; + DWORD ImageSize; + WORD ResourceID; // This field is the one difference to above +} IconResEntry; +#pragma pack(pop) + +namespace { + /** + * ScopedResourceUpdate is a RAII wrapper for Windows resource updating + * + * Instances |EndUpdateResourceW()| their handles when they go out of scope. + * They pass |TRUE| as the second argument to |EndUpdateResourceW()|, which + * causes the resource update to be aborted (changes are discarded). + */ + struct ScopedResourceUpdateTraits + { + typedef HANDLE type; + static type empty() { return nullptr; } + static void release(type handle) { + if(nullptr != handle) { + EndUpdateResourceW(handle, TRUE); // Discard changes + } + } + }; + + typedef mozilla::Scoped ScopedResourceUpdate; +}; + +#ifdef __MINGW32__ +extern "C" +#endif +int +wmain(int argc, wchar_t** argv) +{ + if (argc != 3) { + printf("Usage: redit \n"); + return 1; + } + + mozilla::ScopedClose file; + if (0 != _wsopen_s(&file.rwget(), + argv[2], + _O_BINARY | _O_RDONLY, + _SH_DENYWR, + _S_IREAD) + || (-1 == file)) { + fprintf(stderr, "Unable to open icon file.\n"); + return 1; + } + + // Load all the data from the icon file + long filesize = _filelength(file); + auto data = MakeUniqueFallible(filesize); + if(!data) { + fprintf(stderr, "Failed to allocate memory for icon file.\n"); + return 1; + } + _read(file, data.get(), filesize); + + IconHeader* header = reinterpret_cast(data.get()); + + // Open the target library for updating + ScopedResourceUpdate updateRes(BeginUpdateResourceW(argv[1], FALSE)); + if (nullptr == updateRes) { + fprintf(stderr, "Unable to open library for modification.\n"); + return 1; + } + + // Allocate the group resource entry + long groupSize = sizeof(IconHeader) + + header->ImageCount * sizeof(IconResEntry); + auto group = MakeUniqueFallible(groupSize); + if(!group) { + fprintf(stderr, "Failed to allocate memory for new images.\n"); + return 1; + } + memcpy(group.get(), data.get(), sizeof(IconHeader)); + + IconDirEntry* sourceIcon = + reinterpret_cast(data.get() + + sizeof(IconHeader)); + IconResEntry* targetIcon = + reinterpret_cast(group.get() + + sizeof(IconHeader)); + + for (int id = 1; id <= header->ImageCount; id++) { + // Add the individual icon + if (!UpdateResourceW(updateRes, RT_ICON, MAKEINTRESOURCE(id), + MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), + data + sourceIcon->ImageOffset, + sourceIcon->ImageSize)) { + fprintf(stderr, "Unable to update resource (RT_ICON).\n"); + return 1; + } + // Copy the data for this icon + // (note that the structs have different sizes) + memcpy(targetIcon, sourceIcon, sizeof(IconResEntry)); + targetIcon->ResourceID = id; + sourceIcon++; + targetIcon++; + } + + if (!UpdateResourceW(updateRes, RT_GROUP_ICON, L"MAINICON", + MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), + group, groupSize)) { + fprintf(stderr, "Unable to update resource (RT_GROUP_ICON).\n"); + return 1; + } + + // Save the modifications + if(!EndUpdateResourceW(updateRes.forget(), FALSE)) { + fprintf(stderr, "Unable to write changes to library.\n"); + return 1; + } + + return 0; +} -- cgit v1.2.3