Debian Codegraph @

Source/ResourceVersionInfo.cpp

raw
1 /*
2 * ResourceVersionInfo.cpp: implementation of the CResourceVersionInfo class.
3 *
4 * This file is a part of NSIS.
5 *
6 * Copyright (C) 1999-2026 Nullsoft and Contributors
7 *
8 * Licensed under the zlib/libpng license (the "License");
9 * you may not use this file except in compliance with the License.
10 *
11 * Licence details can be found in the file COPYING.
12 *
13 * This software is provided 'as-is', without any express or implied
14 * warranty.
15 *
16 * Modified for Unicode support by Jim Park -- 08/21/2007
17 */
18
19 #include "ResourceVersionInfo.h"
20
21 #include "Platform.h"
22 #include "util.h"
23 #include "winchar.h"
24 #include "utf.h"
25
26 #ifdef NSIS_SUPPORT_VERSION_INFO
27
28 #ifndef VOS__WINDOWS32
29 # define VOS__WINDOWS32 4
30 #endif
31 #ifndef VFT_APP
32 # define VFT_APP 1
33 #endif
34
35 #ifndef _WIN32
36 # include <iconv.h>
37 #endif
38
39 struct version_string_list
40 {
41 int codepage;
42 LANGID lang_id;
43 int name;
44 DefineList *pChildStrings;
45 };
46
47 CVersionStrigList::~CVersionStrigList()
48 {
49 struct version_string_list *itr = (struct version_string_list *) m_gr.get();
50 int i = m_gr.getlen() / sizeof(struct version_string_list);
51
52 while (i--)
53 {
54 delete itr[i].pChildStrings;
55 }
56 }
57
58 int CVersionStrigList::add(LANGID langid, int codepage)
59 {
60 TCHAR Buff[10];
61 _stprintf(Buff, _T("%04x"), langid);
62 int pos = SortedStringListND<struct version_string_list>::add(Buff);
63 if (pos == -1) return false;
64
65 version_string_list *data = ((version_string_list *)m_gr.get())+ pos;
66 data->pChildStrings = new DefineList;
67 data->codepage = codepage;
68 data->lang_id = langid;
69 return pos;
70 }
71
72 LANGID CVersionStrigList::get_lang(int idx)
73 {
74 version_string_list *data=(version_string_list *)m_gr.get();
75 return data[idx].lang_id;
76 }
77
78 int CVersionStrigList::get_codepage(int idx)
79 {
80 version_string_list *data=(version_string_list *)m_gr.get();
81 return data[idx].codepage;
82 }
83
84 DefineList* CVersionStrigList::get_strings(int idx)
85 {
86 version_string_list *data=(version_string_list *)m_gr.get();
87 return data[idx].pChildStrings;
88 }
89
90 int CVersionStrigList::find(LANGID lang_id, int codepage)
91 {
92 TCHAR Buff[10];
93 _stprintf(Buff, _T("%04x"), lang_id);
94 return SortedStringListND<struct version_string_list>::find(Buff);
95 }
96
97 int CVersionStrigList::getnum()
98 {
99 return m_gr.getlen()/sizeof(struct version_string_list);
100 }
101
102 //////////////////////////////////////////////////////////////////////
103 // Construction/Destruction
104 //////////////////////////////////////////////////////////////////////
105 CResourceVersionInfo::CResourceVersionInfo()
106 {
107 memset(&m_FixedInfo, 0, sizeof(VS_FIXEDFILEINFO));
108 m_FixedInfo.dwSignature = 0xFEEF04BD;
109 m_FixedInfo.dwFileOS = VOS__WINDOWS32;
110 m_FixedInfo.dwFileType = VFT_APP;
111 }
112
113 CResourceVersionInfo::~CResourceVersionInfo()
114 {
115
116 }
117
118 void CResourceVersionInfo::SetFileVersion(int HighPart, int LowPart)
119 {
120 m_FixedInfo.dwFileVersionLS = LowPart;
121 m_FixedInfo.dwFileVersionMS = HighPart;
122 }
123
124 void CResourceVersionInfo::SetProductVersion(int HighPart, int LowPart)
125 {
126 m_FixedInfo.dwProductVersionLS = LowPart;
127 m_FixedInfo.dwProductVersionMS = HighPart;
128 }
129
130 // Jim Park: Not sure where this is used.
131 int GetVersionHeader (LPSTR &p, WORD &wLength, WORD &wValueLength, WORD &wType)
132 {
133 WINWCHAR *szKey;
134 char *baseP = p;
135
136 wLength = *(WORD*)p;
137 p += sizeof(WORD);
138 wValueLength = *(WORD*)p;
139 p += sizeof(WORD);
140 wType = *(WORD*)p;
141 p += sizeof(WORD);
142 szKey = (WINWCHAR*)p;
143 p += (WinWStrLen(szKey) + 1) * sizeof (WINWCHAR);
144 while ( ((ULONG_PTR)p % 4) != 0 ) p++;
145
146 return (int)(p - baseP);
147 }
148
149 DWORD ZEROS = 0;
150
151 void PadStream (GrowBuf &strm)
152 {
153 if ( (strm.getlen() % 4) != 0 )
154 strm.add (&ZEROS, 4 - (strm.getlen() % 4));
155 }
156
157 // Helper function only used by CResourceVersionInfo::ExportToStream
158 // Cannot handle anything longer than 65K objects.
159 //
160 // @param wLength Size in bytes of the entire object we are storing.
161 // @param wValueLength The value length in bytes.
162 // @param wType If type is 1, it's a wchar_t string, so save value length appropriately.
163 // @param key The string key
164 // @param value The value mapped to string key.
165 static void SaveVersionHeaderUTF16LE(GrowBuf &strm, WORD wLength, WORD wValueLength, WORD wType, const unsigned short *key, void *value)
166 {
167 WORD valueLen;
168 WORD keyLen;
169
170 strm.add (&wLength, sizeof(wLength));
171
172 strm.add (&wValueLength, sizeof(wValueLength));
173 strm.add (&wType, sizeof (wType));
174 keyLen = WORD((StrLenUTF16(key) + 1) * sizeof(WINWCHAR));
175 strm.add ((void*)key, keyLen);
176
177 PadStream(strm);
178
179 if ( wValueLength > 0 )
180 {
181 valueLen = wValueLength;
182 if ( wType == 1 )
183 valueLen = valueLen * WORD(sizeof(WINWCHAR));
184 strm.add (value, valueLen);
185 }
186 }
187 static void SaveVersionHeader(GrowBuf &strm, WORD wLength, WORD wValueLength, WORD wType, const wchar_t *key, void *value)
188 {
189 WCToUTF16LEHlpr cnv;
190 if (!cnv.Create(key)) throw std::runtime_error("Unicode conversion failed");
191 SaveVersionHeaderUTF16LE(strm, wLength, wValueLength, wType, cnv.Get(), value);
192 cnv.Destroy();
193 }
194
195 void CResourceVersionInfo::ExportToStream(GrowBuf &strm, int Index)
196 {
197 DWORD v;
198 WORD wSize;
199 int p, p1;
200
201 strm.resize(0);
202 SaveVersionHeader(strm, 0, sizeof (VS_FIXEDFILEINFO), 0, L"VS_VERSION_INFO", &m_FixedInfo);
203
204 DefineList *pChildStrings = m_ChildStringLists.get_strings(Index);
205 if ( pChildStrings->getnum() > 0 )
206 {
207 GrowBuf stringInfoStream;
208 int codepage = m_ChildStringLists.get_codepage(Index);
209 LANGID langid = m_ChildStringLists.get_lang(Index);
210 wchar_t Buff[16];
211 _snwprintf(Buff, COUNTOF(Buff), L"%04x%04x", langid, codepage);
212 SaveVersionHeader(stringInfoStream, 0, 0, 0, Buff, &ZEROS);
213
214 for ( int i = 0; i < pChildStrings->getnum(); i++ )
215 {
216 PadStream (stringInfoStream);
217 WCToUTF16LEHlpr cnvName, cnvValue;
218 if (!cnvName.Create(pChildStrings->getname(i), codepage)) throw std::runtime_error("Unicode conversion failed");
219 if (!cnvValue.Create(pChildStrings->getvalue(i), codepage)) throw std::runtime_error("Unicode conversion failed");
220 p = stringInfoStream.getlen();
221 SaveVersionHeaderUTF16LE(stringInfoStream, 0, WORD(StrLenUTF16(cnvValue.Get()) + 1), 1, cnvName.Get(), (void*)cnvValue.Get());
222 cnvName.Destroy(), cnvValue.Destroy();
223 wSize = WORD(stringInfoStream.getlen() - p);
224
225 *(WORD*)((PBYTE)stringInfoStream.get()+p)=wSize;
226 }
227
228 wSize = WORD(stringInfoStream.getlen());
229 *(WORD*)((PBYTE)stringInfoStream.get())=wSize;
230
231 PadStream (strm);
232 p = strm.getlen();
233 SaveVersionHeader(strm, 0, 0, 0, L"StringFileInfo", &ZEROS);
234 strm.add (stringInfoStream.get(), stringInfoStream.getlen());
235 wSize = WORD(strm.getlen() - p);
236
237 *(WORD*)((PBYTE)strm.get()+p)=wSize;
238 }
239
240 // Show all languages available using Var-Translations
241 if ( m_ChildStringLists.getnum() > 0 )
242 {
243 PadStream (strm);
244 p = strm.getlen();
245 SaveVersionHeader(strm, 0, 0, 0, L"VarFileInfo", &ZEROS);
246 PadStream (strm);
247
248 p1 = strm.getlen();
249 SaveVersionHeader(strm, 0, 0, 0, L"Translation", &ZEROS);
250
251 // First add selected code language translation
252 v = MAKELONG(m_ChildStringLists.get_lang(Index), m_ChildStringLists.get_codepage(Index));
253 strm.add (&v, sizeof (v));
254
255 for ( int k =0; k < m_ChildStringLists.getnum(); k++ )
256 {
257 if ( k != Index )
258 {
259 v = MAKELONG(m_ChildStringLists.get_lang(k), m_ChildStringLists.get_codepage(k));
260 strm.add (&v, sizeof (v));
261 }
262 }
263
264 wSize = WORD(strm.getlen() - p1);
265 *(WORD*)((PBYTE)strm.get()+p1)=wSize;
266 wSize = WORD(sizeof (int) * m_ChildStringLists.getnum());
267 p1+=sizeof(WORD);
268 *(WORD*)((PBYTE)strm.get()+p1)=wSize;
269
270 wSize = WORD(strm.getlen() - p);
271 *(WORD*)((PBYTE)strm.get()+p)=wSize;
272 }
273
274 wSize = WORD(strm.getlen());
275 *(WORD*)((PBYTE)strm.get())=wSize;
276 }
277
278 // Returns 0 if success, 1 if already defined
279 int CResourceVersionInfo::SetKeyValue(LANGID lang_id, int codepage, TCHAR* AKeyName, TCHAR* AValue)
280 {
281 int pos = m_ChildStringLists.find(lang_id, codepage);
282 if ( pos == -1 )
283 {
284 pos = m_ChildStringLists.add(lang_id, codepage);
285 }
286 DefineList *pStrings = m_ChildStringLists.get_strings(pos);
287 return pStrings->add(AKeyName, AValue);
288 }
289
290 int CResourceVersionInfo::GetStringTablesCount()
291 {
292 return m_ChildStringLists.getnum();
293 }
294
295 LANGID CResourceVersionInfo::GetLangID(int Index)
296 {
297 return m_ChildStringLists.get_lang(Index);
298 }
299
300 int CResourceVersionInfo::GetCodePage(int Index)
301 {
302 return m_ChildStringLists.get_codepage(Index);
303 }
304
305 TCHAR *CResourceVersionInfo::FindKey(LANGID LangID, int codepage, const TCHAR *pKeyName)
306 {
307 int pos = m_ChildStringLists.find(LangID, codepage);
308 if ( pos == -1 )
309 {
310 return NULL;
311 }
312 DefineList *pStrings = m_ChildStringLists.get_strings(pos);
313 return pStrings->find(pKeyName);
314 }
315
316 #endif