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 << 65 "', type='" << typeStr << "')").str(); 66 } 67 68 HRSRC Resource::findResource() const { 69 LPCTSTR id = namePtr; 70 // string resources are stored in blocks (stringtables) 71 // id of the resource is (stringId / 16 + 1) 72 if (typePtr == RT_STRING) { 73 id = MAKEINTRESOURCE(UINT(size_t(id) / 16 + 1)); 74 } 75 return FindResource(instance, id, typePtr); 76 } 77 78 LPVOID Resource::getPtr(DWORD &size) const 79 { 80 // LoadString returns the same result if value is zero-length or 81 // if if the value does not exists, 82 // so wee need to ensure the stringtable exists 83 HRSRC resInfo = findResource(); 84 if (resInfo == NULL) { 85 JP_THROW(SysError(getErrMsg("cannot find resource"), FindResource)); 86 } 87 88 HGLOBAL res = LoadResource(instance, resInfo); 89 if (res == NULL) { 90 JP_THROW(SysError(getErrMsg("cannot load resource"), LoadResource)); 91 } 92 93 LPVOID ptr = LockResource(res); 94 if (res == NULL) { 95 JP_THROW(SysError(getErrMsg("cannot lock resource"), LockResource)); 96 } 97 98 if (typePtr == RT_STRING) { 99 // string resources are stored in stringtables and 100 // need special handling 101 // The simplest way (while we don't need handle resource locale) 102 // is LoadString 103 // But this adds dependency on user32.dll, 104 // so implement custom string extraction 105 106 // number in the block (namePtr is an integer) 107 size_t num = size_t(namePtr) & 0xf; 108 LPWSTR strPtr = (LPWSTR)ptr; 109 for (size_t i = 0; i < num; i++) { 110 // 1st symbol contains string length 111 strPtr += DWORD(*strPtr) + 1; 112 } 113 // *strPtr contains string length, string value starts at strPtr+1 114 size = DWORD(*strPtr) * sizeof(wchar_t); 115 ptr = strPtr+1; 116 } else { 117 size = SizeofResource(instance, resInfo); 118 } 119 120 return ptr; 121 } 122 123 bool Resource::available() const { 124 return NULL != findResource(); 125 } 126 127 unsigned Resource::size() const { 128 DWORD size = 0; 129 getPtr(size); 130 return size; 131 } 132 133 LPCVOID Resource::rawData() const { 134 DWORD size = 0; 135 return getPtr(size); 136 } 137 138 void Resource::saveToFile(const std::wstring &filePath) const { 139 DWORD size = 0; 140 const char *resPtr = (const char *)getPtr(size); 141 142 FileUtils::FileWriter(filePath).write(resPtr, size).finalize(); 143 } 144 145 Resource::ByteArray Resource::binary() const { 146 DWORD size = 0; 147 LPBYTE resPtr = (LPBYTE)getPtr(size); 148 return ByteArray(resPtr, resPtr+size); 149 }