src/jdk.jpackage/windows/native/msiwrapper/Resources.cpp
branchJDK-8200758-branch
changeset 57413 45c74e654794
child 57909 c7de06ed4b54
equal deleted inserted replaced
57412:02034583f4dc 57413:45c74e654794
       
     1 /*
       
     2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 #include "Resources.h"
       
    27 #include "FileUtils.h"
       
    28 #include "WinErrorHandling.h"
       
    29 
       
    30 #include <fstream>
       
    31 
       
    32 
       
    33 Resource::Resource(LPCTSTR name, LPCTSTR type, HINSTANCE module) {
       
    34     init(name, type, module);
       
    35 }
       
    36 
       
    37 Resource::Resource(UINT id, LPCTSTR type, HINSTANCE module) {
       
    38     init(MAKEINTRESOURCE(id), type, module);
       
    39 }
       
    40 
       
    41 void Resource::init(LPCTSTR name, LPCTSTR type, HINSTANCE module) {
       
    42     if (IS_INTRESOURCE(name)) {
       
    43         std::wostringstream printer;
       
    44         printer << L"#" << reinterpret_cast<size_t>(name);
       
    45         nameStr = printer.str();
       
    46         namePtr = name;
       
    47     } else {
       
    48         nameStr = name;
       
    49         namePtr = nameStr.c_str();
       
    50     }
       
    51     if (IS_INTRESOURCE(type)) {
       
    52         std::wostringstream printer;
       
    53         printer << L"#" << reinterpret_cast<size_t>(name);
       
    54         typeStr = printer.str();
       
    55         typePtr = type;
       
    56     } else {
       
    57         typeStr = type;
       
    58         typePtr = typeStr.c_str();
       
    59     }
       
    60     instance = module;
       
    61 }
       
    62 
       
    63 std::string Resource::getErrMsg(const std::string &descr) const {
       
    64     return (tstrings::any() << descr << " (name='" << nameStr << "', type='" << typeStr << "')").str();
       
    65 }
       
    66 
       
    67 HRSRC Resource::findResource() const {
       
    68     LPCTSTR id = namePtr;
       
    69     // string resources are stored in blocks (stringtables)
       
    70     // id of the resource is (stringId / 16 + 1)
       
    71     if (typePtr == RT_STRING) {
       
    72         id = MAKEINTRESOURCE(UINT(size_t(id) / 16 + 1));
       
    73     }
       
    74     return FindResource(instance, id, typePtr);
       
    75 }
       
    76 
       
    77 LPVOID Resource::getPtr(DWORD &size) const
       
    78 {
       
    79     // LoadString returns the same result if value is zero-length or if if the value does not exists,
       
    80     // so wee need to ensure the stringtable exists
       
    81     HRSRC resInfo = findResource();
       
    82     if (resInfo == NULL) {
       
    83         JP_THROW(SysError(getErrMsg("cannot find resource"), FindResource));
       
    84     }
       
    85 
       
    86     HGLOBAL res = LoadResource(instance, resInfo);
       
    87     if (res == NULL) {
       
    88         JP_THROW(SysError(getErrMsg("cannot load resource"), LoadResource));
       
    89     }
       
    90 
       
    91     LPVOID ptr = LockResource(res);
       
    92     if (res == NULL) {
       
    93         JP_THROW(SysError(getErrMsg("cannot lock resource"), LockResource));
       
    94     }
       
    95 
       
    96     if (typePtr == RT_STRING) {
       
    97         // string resources are stored in stringtables and need special handling
       
    98         // The simplest way (while we don't need handle resource locale) is LoadString
       
    99         // But this adds dependency on user32.dll, so implement custom string extraction
       
   100 
       
   101         // number in the block (namePtr is an integer)
       
   102         size_t num = size_t(namePtr) & 0xf;
       
   103         LPWSTR strPtr = (LPWSTR)ptr;
       
   104         for (size_t i = 0; i < num; i++) {
       
   105             // 1st symbol contains string length
       
   106             strPtr += DWORD(*strPtr) + 1;
       
   107         }
       
   108         // *strPtr contains string length, string value starts at strPtr+1
       
   109         size = DWORD(*strPtr) * sizeof(wchar_t);
       
   110         ptr = strPtr+1;
       
   111     } else {
       
   112         size = SizeofResource(instance, resInfo);
       
   113     }
       
   114 
       
   115     return ptr;
       
   116 }
       
   117 
       
   118 bool Resource::available() const {
       
   119     return NULL != findResource();
       
   120 }
       
   121 
       
   122 unsigned Resource::size() const {
       
   123     DWORD size = 0;
       
   124     getPtr(size);
       
   125     return size;
       
   126 }
       
   127 
       
   128 LPCVOID Resource::rawData() const {
       
   129     DWORD size = 0;
       
   130     return getPtr(size);
       
   131 }
       
   132 
       
   133 void Resource::saveToFile(const std::wstring &filePath) const {
       
   134     DWORD size = 0;
       
   135     const char *resPtr = (const char *)getPtr(size);
       
   136 
       
   137     FileUtils::FileWriter(filePath).write(resPtr, size).finalize();
       
   138 }
       
   139 
       
   140 Resource::ByteArray Resource::binary() const {
       
   141     DWORD size = 0;
       
   142     LPBYTE resPtr = (LPBYTE)getPtr(size);
       
   143     return ByteArray(resPtr, resPtr+size);
       
   144 }