]> wimlib.net Git - wimlib/blob - src/xml_windows.c
mount_image.c: add fallback definitions of RENAME_* constants
[wimlib] / src / xml_windows.c
1 /*
2  * xml_windows.c
3  *
4  * Set Windows-specific metadata in a WIM file's XML document based on the image
5  * contents.
6  */
7
8 /*
9  * Copyright 2016-2023 Eric Biggers
10  *
11  * This file is free software; you can redistribute it and/or modify it under
12  * the terms of the GNU Lesser General Public License as published by the Free
13  * Software Foundation; either version 3 of the License, or (at your option) any
14  * later version.
15  *
16  * This file is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this file; if not, see https://www.gnu.org/licenses/.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #  include "config.h"
27 #endif
28
29 #include <stdlib.h>
30
31 #include "wimlib.h"
32 #include "wimlib/blob_table.h"
33 #include "wimlib/dentry.h"
34 #include "wimlib/encoding.h"
35 #include "wimlib/endianness.h"
36 #include "wimlib/error.h"
37 #include "wimlib/metadata.h"
38 #include "wimlib/registry.h"
39 #include "wimlib/wim.h"
40 #include "wimlib/xml_windows.h"
41
42 /* Context for a call to set_windows_specific_info()  */
43 struct windows_info_ctx {
44         WIMStruct *wim;
45         int image;
46         bool oom_encountered;
47         bool debug_enabled;
48 };
49
50 /* For debugging purposes, the environmental variable WIMLIB_DEBUG_XML_INFO can
51  * be set to enable messages about certain things not being as expected in the
52  * registry or other files used as information sources.  */
53
54 #define XML_WARN(format, ...)                   \
55         if (ctx->debug_enabled)                 \
56                 WARNING(format, ##__VA_ARGS__)
57
58 /* Set a property in the XML document, with error checking.  */
59 static void
60 set_string_property(struct windows_info_ctx *ctx,
61                     const tchar *name, const tchar *value)
62 {
63         int ret = wimlib_set_image_property(ctx->wim, ctx->image, name, value);
64         if (likely(!ret))
65                 return;
66
67         ctx->oom_encountered |= (ret == WIMLIB_ERR_NOMEM);
68         WARNING("Failed to set image property \"%"TS"\" to value "
69                 "\"%"TS"\": %"TS, name, value, wimlib_get_error_string(ret));
70 }
71
72 /* Set a property in the XML document, with error checking.  */
73 static void
74 set_number_property(struct windows_info_ctx *ctx, const tchar *name, s64 value)
75 {
76         tchar buffer[32];
77         tsprintf(buffer, T("%"PRIi64""), value);
78         set_string_property(ctx, name, buffer);
79 }
80
81 /* Check the result of a registry hive operation.  If unsuccessful, possibly
82  * print debugging information.  Return true iff successful.  */
83 static bool
84 check_hive_status(struct windows_info_ctx *ctx, enum hive_status status,
85                   const tchar *key, const tchar *value)
86 {
87         if (likely(status == HIVE_OK))
88                 return true;
89
90         ctx->oom_encountered |= (status == HIVE_OUT_OF_MEMORY);
91         XML_WARN("%s; key=%"TS" value=%"TS, hive_status_to_string(status),
92                  (key ? key : T("(null)")), (value ? value : T("(null)")));
93         return false;
94 }
95
96 static bool
97 is_registry_valid(struct windows_info_ctx *ctx, const void *hive_mem,
98                   size_t hive_size)
99 {
100         enum hive_status status;
101
102         status = hive_validate(hive_mem, hive_size);
103         return check_hive_status(ctx, status, NULL, NULL);
104 }
105
106 static bool
107 get_string_from_registry(struct windows_info_ctx *ctx, const struct regf *regf,
108                          const tchar *key_name, const tchar *value_name,
109                          tchar **value_ret)
110 {
111         enum hive_status status;
112
113         status = hive_get_string(regf, key_name, value_name, value_ret);
114         return check_hive_status(ctx, status, key_name, value_name);
115 }
116
117 static bool
118 get_number_from_registry(struct windows_info_ctx *ctx, const struct regf *regf,
119                          const tchar *key_name, const tchar *value_name,
120                          s64 *value_ret)
121 {
122         enum hive_status status;
123
124         status = hive_get_number(regf, key_name, value_name, value_ret);
125         return check_hive_status(ctx, status, key_name, value_name);
126 }
127
128 static bool
129 list_subkeys_in_registry(struct windows_info_ctx *ctx, const struct regf *regf,
130                          const tchar *key_name, tchar ***subkeys_ret)
131 {
132         enum hive_status status;
133
134         status = hive_list_subkeys(regf, key_name, subkeys_ret);
135         return check_hive_status(ctx, status, key_name, NULL);
136 }
137
138 /* Copy a string value from a registry hive to the XML document.  */
139 static void
140 copy_registry_string(struct windows_info_ctx *ctx, const struct regf *regf,
141                      const tchar *key_name, const tchar *value_name,
142                      const tchar *property_name)
143 {
144         tchar *string;
145
146         if (get_string_from_registry(ctx, regf, key_name, value_name, &string)) {
147                 set_string_property(ctx, property_name, string);
148                 FREE(string);
149         }
150 }
151
152 /* A table that map Windows language IDs, sorted numerically, to their language
153  * names.  It was generated by tools/generate_language_id_map.c.  */
154 static const struct {
155         u16 id;
156         u16 name_start_offset;
157 } language_id_map[453] = {
158         {0x0000,    0}, {0x0001,    6}, {0x0002,   12}, {0x0003,   18},
159         {0x0004,   24}, {0x0005,   30}, {0x0006,   36}, {0x0007,   42},
160         {0x0008,   48}, {0x0009,   54}, {0x000a,   60}, {0x000b,   66},
161         {0x000c,   72}, {0x000d,   78}, {0x000e,   84}, {0x000f,   90},
162         {0x0010,   96}, {0x0011,  102}, {0x0012,  108}, {0x0013,  114},
163         {0x0014,  120}, {0x0015,  126}, {0x0016,  132}, {0x0017,  138},
164         {0x0018,  144}, {0x0019,  150}, {0x001a,  156}, {0x001b,  162},
165         {0x001c,  168}, {0x001d,  174}, {0x001e,  180}, {0x001f,  186},
166         {0x0020,  192}, {0x0021,  198}, {0x0022,  204}, {0x0023,  210},
167         {0x0024,  216}, {0x0025,  222}, {0x0026,  228}, {0x0027,  234},
168         {0x0028,  240}, {0x0029,  251}, {0x002a,  257}, {0x002b,  263},
169         {0x002c,  269}, {0x002d,  280}, {0x002e,  286}, {0x002f,  293},
170         {0x0030,  299}, {0x0031,  305}, {0x0032,  311}, {0x0033,  317},
171         {0x0034,  323}, {0x0035,  329}, {0x0036,  335}, {0x0037,  341},
172         {0x0038,  347}, {0x0039,  353}, {0x003a,  359}, {0x003b,  365},
173         {0x003c,  371}, {0x003d,  377}, {0x003e,  384}, {0x003f,  390},
174         {0x0040,  396}, {0x0041,  402}, {0x0042,  408}, {0x0043,  414},
175         {0x0044,  425}, {0x0045,  431}, {0x0046,  437}, {0x0047,  443},
176         {0x0048,  449}, {0x0049,  455}, {0x004a,  461}, {0x004b,  467},
177         {0x004c,  473}, {0x004d,  479}, {0x004e,  485}, {0x004f,  491},
178         {0x0050,  497}, {0x0051,  503}, {0x0052,  509}, {0x0053,  515},
179         {0x0054,  521}, {0x0055,  527}, {0x0056,  533}, {0x0057,  539},
180         {0x0058,  546}, {0x0059,  553}, {0x005a,  564}, {0x005b,  571},
181         {0x005c,  577}, {0x005d,  589}, {0x005e,  600}, {0x005f,  606},
182         {0x0060,  618}, {0x0061,  629}, {0x0062,  635}, {0x0063,  641},
183         {0x0064,  647}, {0x0065,  654}, {0x0066,  660}, {0x0067,  667},
184         {0x0068,  678}, {0x0069,  689}, {0x006a,  696}, {0x006b,  702},
185         {0x006c,  709}, {0x006d,  716}, {0x006e,  722}, {0x006f,  728},
186         {0x0070,  734}, {0x0071,  740}, {0x0072,  751}, {0x0073,  757},
187         {0x0074,  763}, {0x0075,  769}, {0x0076,  776}, {0x0077,  783},
188         {0x0078,  789}, {0x0079,  795}, {0x007a,  803}, {0x007c,  810},
189         {0x007e,  817}, {0x007f,  823}, {0x0080,  824}, {0x0081,  830},
190         {0x0082,  836}, {0x0083,  842}, {0x0084,  848}, {0x0085,  855},
191         {0x0086,  862}, {0x0087,  874}, {0x0088,  880}, {0x008c,  886},
192         {0x0091,  893}, {0x0092,  899}, {0x0400,  910}, {0x0401,  916},
193         {0x0402,  922}, {0x0403,  928}, {0x0404,  934}, {0x0405,  940},
194         {0x0406,  946}, {0x0407,  952}, {0x0408,  958}, {0x0409,  964},
195         {0x040a,  970}, {0x040b,  983}, {0x040c,  989}, {0x040d,  995},
196         {0x040e, 1001}, {0x040f, 1007}, {0x0410, 1013}, {0x0411, 1019},
197         {0x0412, 1025}, {0x0413, 1031}, {0x0414, 1037}, {0x0415, 1043},
198         {0x0416, 1049}, {0x0417, 1055}, {0x0418, 1061}, {0x0419, 1067},
199         {0x041a, 1073}, {0x041b, 1079}, {0x041c, 1085}, {0x041d, 1091},
200         {0x041e, 1097}, {0x041f, 1103}, {0x0420, 1109}, {0x0421, 1115},
201         {0x0422, 1121}, {0x0423, 1127}, {0x0424, 1133}, {0x0425, 1139},
202         {0x0426, 1145}, {0x0427, 1151}, {0x0428, 1157}, {0x0429, 1168},
203         {0x042a, 1174}, {0x042b, 1180}, {0x042c, 1186}, {0x042d, 1197},
204         {0x042e, 1203}, {0x042f, 1210}, {0x0430, 1216}, {0x0431, 1222},
205         {0x0432, 1228}, {0x0433, 1234}, {0x0434, 1240}, {0x0435, 1246},
206         {0x0436, 1252}, {0x0437, 1258}, {0x0438, 1264}, {0x0439, 1270},
207         {0x043a, 1276}, {0x043b, 1282}, {0x043d, 1288}, {0x043e, 1295},
208         {0x043f, 1301}, {0x0440, 1307}, {0x0441, 1313}, {0x0442, 1319},
209         {0x0443, 1325}, {0x0444, 1336}, {0x0445, 1342}, {0x0446, 1348},
210         {0x0447, 1354}, {0x0448, 1360}, {0x0449, 1366}, {0x044a, 1372},
211         {0x044b, 1378}, {0x044c, 1384}, {0x044d, 1390}, {0x044e, 1396},
212         {0x044f, 1402}, {0x0450, 1408}, {0x0451, 1414}, {0x0452, 1420},
213         {0x0453, 1426}, {0x0454, 1432}, {0x0455, 1438}, {0x0456, 1444},
214         {0x0457, 1450}, {0x0458, 1457}, {0x0459, 1464}, {0x045a, 1475},
215         {0x045b, 1482}, {0x045c, 1488}, {0x045d, 1500}, {0x045e, 1511},
216         {0x045f, 1517}, {0x0460, 1529}, {0x0461, 1540}, {0x0462, 1546},
217         {0x0463, 1552}, {0x0464, 1558}, {0x0465, 1565}, {0x0466, 1571},
218         {0x0467, 1578}, {0x0468, 1589}, {0x0469, 1600}, {0x046a, 1607},
219         {0x046b, 1613}, {0x046c, 1620}, {0x046d, 1627}, {0x046e, 1633},
220         {0x046f, 1639}, {0x0470, 1645}, {0x0471, 1651}, {0x0472, 1662},
221         {0x0473, 1668}, {0x0474, 1674}, {0x0475, 1680}, {0x0476, 1687},
222         {0x0477, 1694}, {0x0478, 1700}, {0x0479, 1706}, {0x047a, 1714},
223         {0x047c, 1721}, {0x047e, 1728}, {0x0480, 1734}, {0x0481, 1740},
224         {0x0482, 1746}, {0x0483, 1752}, {0x0484, 1758}, {0x0485, 1765},
225         {0x0486, 1772}, {0x0487, 1784}, {0x0488, 1790}, {0x048c, 1796},
226         {0x0491, 1803}, {0x0492, 1809}, {0x0501, 1820}, {0x05fe, 1829},
227         {0x0800, 1839}, {0x0801, 1845}, {0x0803, 1851}, {0x0804, 1866},
228         {0x0807, 1872}, {0x0809, 1878}, {0x080a, 1884}, {0x080c, 1890},
229         {0x0810, 1896}, {0x0813, 1902}, {0x0814, 1908}, {0x0816, 1914},
230         {0x0818, 1920}, {0x0819, 1926}, {0x081a, 1932}, {0x081d, 1943},
231         {0x0820, 1949}, {0x082c, 1955}, {0x082e, 1966}, {0x0832, 1973},
232         {0x083b, 1979}, {0x083c, 1985}, {0x083e, 1991}, {0x0843, 1997},
233         {0x0845, 2008}, {0x0846, 2014}, {0x0849, 2025}, {0x0850, 2031},
234         {0x0859, 2042}, {0x085d, 2053}, {0x085f, 2064}, {0x0860, 2076},
235         {0x0861, 2087}, {0x0867, 2093}, {0x086b, 2104}, {0x0873, 2111},
236         {0x0901, 2117}, {0x09ff, 2131}, {0x0c00, 2141}, {0x0c01, 2147},
237         {0x0c04, 2153}, {0x0c07, 2159}, {0x0c09, 2165}, {0x0c0a, 2171},
238         {0x0c0c, 2177}, {0x0c1a, 2183}, {0x0c3b, 2194}, {0x0c50, 2200},
239         {0x0c51, 2211}, {0x0c6b, 2217}, {0x1000, 2224}, {0x1001, 2235},
240         {0x1004, 2241}, {0x1007, 2247}, {0x1009, 2253}, {0x100a, 2259},
241         {0x100c, 2265}, {0x101a, 2271}, {0x103b, 2277}, {0x105f, 2284},
242         {0x1401, 2296}, {0x1404, 2302}, {0x1407, 2308}, {0x1409, 2314},
243         {0x140a, 2320}, {0x140c, 2326}, {0x141a, 2332}, {0x143b, 2343},
244         {0x1801, 2350}, {0x1809, 2356}, {0x180a, 2362}, {0x180c, 2368},
245         {0x181a, 2374}, {0x183b, 2385}, {0x1c01, 2392}, {0x1c09, 2398},
246         {0x1c0a, 2404}, {0x1c0c, 2410}, {0x1c1a, 2417}, {0x1c3b, 2428},
247         {0x2000, 2435}, {0x2001, 2441}, {0x2009, 2447}, {0x200a, 2453},
248         {0x200c, 2459}, {0x201a, 2465}, {0x203b, 2476}, {0x2400, 2483},
249         {0x2401, 2489}, {0x2409, 2495}, {0x240a, 2502}, {0x240c, 2508},
250         {0x241a, 2514}, {0x243b, 2525}, {0x2800, 2532}, {0x2801, 2538},
251         {0x2809, 2544}, {0x280a, 2550}, {0x280c, 2556}, {0x281a, 2562},
252         {0x2c00, 2573}, {0x2c01, 2579}, {0x2c09, 2585}, {0x2c0a, 2591},
253         {0x2c0c, 2597}, {0x2c1a, 2603}, {0x3000, 2614}, {0x3001, 2620},
254         {0x3009, 2626}, {0x300a, 2632}, {0x300c, 2638}, {0x301a, 2644},
255         {0x3400, 2655}, {0x3401, 2661}, {0x3409, 2667}, {0x340a, 2673},
256         {0x340c, 2679}, {0x3800, 2685}, {0x3801, 2691}, {0x3809, 2697},
257         {0x380a, 2703}, {0x380c, 2709}, {0x3c00, 2715}, {0x3c01, 2721},
258         {0x3c09, 2727}, {0x3c0a, 2733}, {0x3c0c, 2739}, {0x4000, 2745},
259         {0x4001, 2751}, {0x4009, 2757}, {0x400a, 2763}, {0x4400, 2769},
260         {0x4409, 2775}, {0x440a, 2781}, {0x4800, 2787}, {0x4809, 2793},
261         {0x480a, 2799}, {0x4c00, 2805}, {0x4c09, 2811}, {0x4c0a, 2817},
262         {0x500a, 2823}, {0x540a, 2829}, {0x580a, 2835}, {0x5c0a, 2842},
263         {0x641a, 2848}, {0x681a, 2859}, {0x6c1a, 2870}, {0x701a, 2881},
264         {0x703b, 2892}, {0x742c, 2899}, {0x743b, 2910}, {0x7804, 2917},
265         {0x7814, 2923}, {0x781a, 2929}, {0x782c, 2940}, {0x783b, 2951},
266         {0x7843, 2958}, {0x7850, 2969}, {0x785d, 2975}, {0x785f, 2986},
267         {0x7c04, 2998}, {0x7c14, 3004}, {0x7c1a, 3010}, {0x7c28, 3021},
268         {0x7c2e, 3032}, {0x7c3b, 3039}, {0x7c43, 3046}, {0x7c46, 3057},
269         {0x7c50, 3068}, {0x7c59, 3079}, {0x7c5c, 3090}, {0x7c5d, 3102},
270         {0x7c5f, 3113}, {0x7c67, 3125}, {0x7c68, 3136}, {0x7c86, 3147},
271         {0x7c92, 3159},
272 };
273
274 /* All the language names; generated by tools/generate_language_id_map.c.
275  * For compactness, this is a 'char' string rather than a 'tchar' string.  */
276 static const char language_names[3170] =
277         "en-US\0ar-SA\0bg-BG\0ca-ES\0zh-CN\0cs-CZ\0da-DK\0de-DE\0el-GR\0en-US\0"
278         "es-ES\0fi-FI\0fr-FR\0he-IL\0hu-HU\0is-IS\0it-IT\0ja-JP\0ko-KR\0nl-NL\0"
279         "nb-NO\0pl-PL\0pt-BR\0rm-CH\0ro-RO\0ru-RU\0hr-HR\0sk-SK\0sq-AL\0sv-SE\0"
280         "th-TH\0tr-TR\0ur-PK\0id-ID\0uk-UA\0be-BY\0sl-SI\0et-EE\0lv-LV\0lt-LT\0"
281         "tg-Cyrl-TJ\0fa-IR\0vi-VN\0hy-AM\0az-Latn-AZ\0eu-ES\0hsb-DE\0mk-MK\0"
282         "st-ZA\0ts-ZA\0tn-ZA\0ve-ZA\0xh-ZA\0zu-ZA\0af-ZA\0ka-GE\0fo-FO\0hi-IN\0"
283         "mt-MT\0se-NO\0ga-IE\0yi-001\0ms-MY\0kk-KZ\0ky-KG\0sw-KE\0tk-TM\0"
284         "uz-Latn-UZ\0tt-RU\0bn-BD\0pa-IN\0gu-IN\0or-IN\0ta-IN\0te-IN\0kn-IN\0"
285         "ml-IN\0as-IN\0mr-IN\0sa-IN\0mn-MN\0bo-CN\0cy-GB\0km-KH\0lo-LA\0my-MM\0"
286         "gl-ES\0kok-IN\0mni-IN\0sd-Arab-PK\0syr-SY\0si-LK\0chr-Cher-US\0"
287         "iu-Latn-CA\0am-ET\0tzm-Latn-DZ\0ks-Arab-IN\0ne-NP\0fy-NL\0ps-AF\0"
288         "fil-PH\0dv-MV\0bin-NG\0ff-Latn-SN\0ha-Latn-NG\0ibb-NG\0yo-NG\0quz-BO\0"
289         "nso-ZA\0ba-RU\0lb-LU\0kl-GL\0ig-NG\0kr-Latn-NG\0om-ET\0ti-ER\0gn-PY\0"
290         "haw-US\0la-001\0so-SO\0ii-CN\0pap-029\0arn-CL\0moh-CA\0br-FR\0\0"
291         "ug-CN\0mi-NZ\0oc-FR\0co-FR\0gsw-CH\0sah-RU\0quc-Latn-GT\0rw-RW\0"
292         "wo-SN\0prs-AF\0gd-GB\0ku-Arab-IQ\0en-US\0ar-SA\0bg-BG\0ca-ES\0zh-TW\0"
293         "cs-CZ\0da-DK\0de-DE\0el-GR\0en-US\0es-ES_tradnl\0fi-FI\0fr-FR\0he-IL\0"
294         "hu-HU\0is-IS\0it-IT\0ja-JP\0ko-KR\0nl-NL\0nb-NO\0pl-PL\0pt-BR\0rm-CH\0"
295         "ro-RO\0ru-RU\0hr-HR\0sk-SK\0sq-AL\0sv-SE\0th-TH\0tr-TR\0ur-PK\0id-ID\0"
296         "uk-UA\0be-BY\0sl-SI\0et-EE\0lv-LV\0lt-LT\0tg-Cyrl-TJ\0fa-IR\0vi-VN\0"
297         "hy-AM\0az-Latn-AZ\0eu-ES\0hsb-DE\0mk-MK\0st-ZA\0ts-ZA\0tn-ZA\0ve-ZA\0"
298         "xh-ZA\0zu-ZA\0af-ZA\0ka-GE\0fo-FO\0hi-IN\0mt-MT\0se-NO\0yi-001\0"
299         "ms-MY\0kk-KZ\0ky-KG\0sw-KE\0tk-TM\0uz-Latn-UZ\0tt-RU\0bn-IN\0pa-IN\0"
300         "gu-IN\0or-IN\0ta-IN\0te-IN\0kn-IN\0ml-IN\0as-IN\0mr-IN\0sa-IN\0mn-MN\0"
301         "bo-CN\0cy-GB\0km-KH\0lo-LA\0my-MM\0gl-ES\0kok-IN\0mni-IN\0sd-Deva-IN\0"
302         "syr-SY\0si-LK\0chr-Cher-US\0iu-Cans-CA\0am-ET\0tzm-Arab-MA\0"
303         "ks-Arab-IN\0ne-NP\0fy-NL\0ps-AF\0fil-PH\0dv-MV\0bin-NG\0ff-Latn-NG\0"
304         "ha-Latn-NG\0ibb-NG\0yo-NG\0quz-BO\0nso-ZA\0ba-RU\0lb-LU\0kl-GL\0"
305         "ig-NG\0kr-Latn-NG\0om-ET\0ti-ET\0gn-PY\0haw-US\0la-001\0so-SO\0ii-CN\0"
306         "pap-029\0arn-CL\0moh-CA\0br-FR\0ug-CN\0mi-NZ\0oc-FR\0co-FR\0gsw-FR\0"
307         "sah-RU\0quc-Latn-GT\0rw-RW\0wo-SN\0prs-AF\0gd-GB\0ku-Arab-IQ\0"
308         "qps-ploc\0qps-ploca\0en-US\0ar-IQ\0ca-ES-valencia\0zh-CN\0de-CH\0"
309         "en-GB\0es-MX\0fr-BE\0it-CH\0nl-BE\0nn-NO\0pt-PT\0ro-MD\0ru-MD\0"
310         "sr-Latn-CS\0sv-FI\0ur-IN\0az-Cyrl-AZ\0dsb-DE\0tn-BW\0se-SE\0ga-IE\0"
311         "ms-BN\0uz-Cyrl-UZ\0bn-BD\0pa-Arab-PK\0ta-LK\0mn-Mong-CN\0sd-Arab-PK\0"
312         "iu-Latn-CA\0tzm-Latn-DZ\0ks-Deva-IN\0ne-IN\0ff-Latn-SN\0quz-EC\0"
313         "ti-ER\0qps-Latn-x-sh\0qps-plocm\0en-US\0ar-EG\0zh-HK\0de-AT\0en-AU\0"
314         "es-ES\0fr-CA\0sr-Cyrl-CS\0se-FI\0mn-Mong-MN\0dz-BT\0quz-PE\0"
315         "ks-Arab-IN\0ar-LY\0zh-SG\0de-LU\0en-CA\0es-GT\0fr-CH\0hr-BA\0smj-NO\0"
316         "tzm-Tfng-MA\0ar-DZ\0zh-MO\0de-LI\0en-NZ\0es-CR\0fr-LU\0bs-Latn-BA\0"
317         "smj-SE\0ar-MA\0en-IE\0es-PA\0fr-MC\0sr-Latn-BA\0sma-NO\0ar-TN\0en-ZA\0"
318         "es-DO\0fr-029\0sr-Cyrl-BA\0sma-SE\0en-US\0ar-OM\0en-JM\0es-VE\0fr-RE\0"
319         "bs-Cyrl-BA\0sms-FI\0en-US\0ar-YE\0en-029\0es-CO\0fr-CD\0sr-Latn-RS\0"
320         "smn-FI\0en-US\0ar-SY\0en-BZ\0es-PE\0fr-SN\0sr-Cyrl-RS\0en-US\0ar-JO\0"
321         "en-TT\0es-AR\0fr-CM\0sr-Latn-ME\0en-US\0ar-LB\0en-ZW\0es-EC\0fr-CI\0"
322         "sr-Cyrl-ME\0en-US\0ar-KW\0en-PH\0es-CL\0fr-ML\0en-US\0ar-AE\0en-ID\0"
323         "es-UY\0fr-MA\0en-US\0ar-BH\0en-HK\0es-PY\0fr-HT\0en-US\0ar-QA\0en-IN\0"
324         "es-BO\0en-US\0en-MY\0es-SV\0en-US\0en-SG\0es-HN\0en-US\0en-AE\0es-NI\0"
325         "es-PR\0es-US\0es-419\0es-CU\0bs-Cyrl-BA\0bs-Latn-BA\0sr-Cyrl-RS\0"
326         "sr-Latn-RS\0smn-FI\0az-Cyrl-AZ\0sms-FI\0zh-CN\0nn-NO\0bs-Latn-BA\0"
327         "az-Latn-AZ\0sma-SE\0uz-Cyrl-UZ\0mn-MN\0iu-Cans-CA\0tzm-Tfng-MA\0"
328         "zh-HK\0nb-NO\0sr-Latn-RS\0tg-Cyrl-TJ\0dsb-DE\0smj-SE\0uz-Latn-UZ\0"
329         "pa-Arab-PK\0mn-Mong-CN\0sd-Arab-PK\0chr-Cher-US\0iu-Latn-CA\0"
330         "tzm-Latn-DZ\0ff-Latn-SN\0ha-Latn-NG\0quc-Latn-GT\0ku-Arab-IQ\0";
331
332 /* Translate a Windows language ID to its name.  Returns NULL if the ID is not
333  * recognized.  */
334 static const char *
335 language_id_to_name(u16 id)
336 {
337         int l = 0;
338         int r = ARRAY_LEN(language_id_map) - 1;
339         do {
340                 int m = (l + r) / 2;
341                 if (id < language_id_map[m].id)
342                         r = m - 1;
343                 else if (id > language_id_map[m].id)
344                         l = m + 1;
345                 else
346                         return &language_names[language_id_map[m].name_start_offset];
347         } while (l <= r);
348         return NULL;
349 }
350
351 /* PE binary processor architecture codes (common ones only)  */
352 #define IMAGE_FILE_MACHINE_I386         0x014C
353 #define IMAGE_FILE_MACHINE_ARM          0x01C0
354 #define IMAGE_FILE_MACHINE_ARMV7        0x01C4
355 #define IMAGE_FILE_MACHINE_THUMB        0x01C2
356 #define IMAGE_FILE_MACHINE_IA64         0x0200
357 #define IMAGE_FILE_MACHINE_AMD64        0x8664
358 #define IMAGE_FILE_MACHINE_ARM64        0xAA64
359
360 /* Windows API processor architecture codes (common ones only)  */
361 #define PROCESSOR_ARCHITECTURE_INTEL    0
362 #define PROCESSOR_ARCHITECTURE_ARM      5
363 #define PROCESSOR_ARCHITECTURE_IA64     6
364 #define PROCESSOR_ARCHITECTURE_AMD64    9
365 #define PROCESSOR_ARCHITECTURE_ARM64    12
366
367 /* Translate a processor architecture code as given in a PE binary to the code
368  * used by the Windows API.  Returns -1 if the code is not recognized.  */
369 static int
370 pe_arch_to_windows_arch(unsigned pe_arch)
371 {
372         switch (pe_arch) {
373         case IMAGE_FILE_MACHINE_I386:
374                 return PROCESSOR_ARCHITECTURE_INTEL;
375         case IMAGE_FILE_MACHINE_ARM:
376         case IMAGE_FILE_MACHINE_ARMV7:
377         case IMAGE_FILE_MACHINE_THUMB:
378                 return PROCESSOR_ARCHITECTURE_ARM;
379         case IMAGE_FILE_MACHINE_IA64:
380                 return PROCESSOR_ARCHITECTURE_IA64;
381         case IMAGE_FILE_MACHINE_AMD64:
382                 return PROCESSOR_ARCHITECTURE_AMD64;
383         case IMAGE_FILE_MACHINE_ARM64:
384                 return PROCESSOR_ARCHITECTURE_ARM64;
385         }
386         return -1;
387 }
388
389 /* Gather information from kernel32.dll.  */
390 static void
391 set_info_from_kernel32(struct windows_info_ctx *ctx,
392                        const void *contents, size_t size)
393 {
394         u32 e_lfanew;
395         const u8 *pe_hdr;
396         unsigned pe_arch;
397         int arch;
398
399         /* Read the processor architecture from the executable header.  */
400
401         if (size < 0x40)
402                 goto invalid;
403
404         e_lfanew = le32_to_cpu(*(le32 *)((u8 *)contents + 0x3C));
405         if (e_lfanew > size || size - e_lfanew < 6 || (e_lfanew & 3))
406                 goto invalid;
407
408         pe_hdr = (u8 *)contents + e_lfanew;
409         if (*(le32 *)pe_hdr != cpu_to_le32(0x00004550)) /* "PE\0\0"  */
410                 goto invalid;
411
412         pe_arch = le16_to_cpu(*(le16 *)(pe_hdr + 4));
413         arch = pe_arch_to_windows_arch(pe_arch);
414         if (arch >= 0) {
415                 /* Save the processor architecture in the XML document.  */
416                 set_number_property(ctx, T("WINDOWS/ARCH"), arch);
417         } else {
418                 XML_WARN("Architecture value %x from kernel32.dll "
419                          "header not recognized", pe_arch);
420         }
421         return;
422
423 invalid:
424         XML_WARN("kernel32.dll is not a valid PE binary.");
425 }
426
427 /* Gather information from the SOFTWARE registry hive.  */
428 static void
429 set_info_from_software_hive(struct windows_info_ctx *ctx,
430                             const struct regf *regf)
431 {
432         const tchar *version_key = T("Microsoft\\Windows NT\\CurrentVersion");
433         s64 major_version = -1;
434         s64 minor_version = -1;
435         tchar *version_string;
436         tchar *build_string;
437
438         /* Image flags  */
439         copy_registry_string(ctx, regf, version_key, T("EditionID"),
440                              T("FLAGS"));
441
442         /* Image display name  */
443         copy_registry_string(ctx, regf, version_key, T("ProductName"),
444                              T("DISPLAYNAME"));
445
446         /* Image display description  */
447         copy_registry_string(ctx, regf, version_key, T("ProductName"),
448                              T("DISPLAYDESCRIPTION"));
449
450         /* Edition ID  */
451         copy_registry_string(ctx, regf, version_key, T("EditionID"),
452                              T("WINDOWS/EDITIONID"));
453
454         /* Installation type  */
455         copy_registry_string(ctx, regf, version_key, T("InstallationType"),
456                              T("WINDOWS/INSTALLATIONTYPE"));
457
458         /* Product name  */
459         copy_registry_string(ctx, regf, version_key, T("ProductName"),
460                              T("WINDOWS/PRODUCTNAME"));
461
462         /* Major and minor version number  */
463
464         /* Note: in Windows 10, CurrentVersion was apparently fixed at 6.3.
465          * Instead, the new values CurrentMajorVersionNumber and
466          * CurrentMinorVersionNumber should be used.  */
467
468         get_number_from_registry(ctx, regf, version_key,
469                                  T("CurrentMajorVersionNumber"), &major_version);
470
471         get_number_from_registry(ctx, regf, version_key,
472                                  T("CurrentMinorVersionNumber"), &minor_version);
473
474         if (major_version < 0 || minor_version < 0) {
475                 if (get_string_from_registry(ctx, regf, version_key,
476                                              T("CurrentVersion"),
477                                              &version_string))
478                 {
479                         if (2 != tscanf(version_string, T("%"PRIi64".%"PRIi64),
480                                         &major_version, &minor_version))
481                         {
482                                 XML_WARN("Unrecognized CurrentVersion: %"TS,
483                                          version_string);
484                         }
485                         FREE(version_string);
486                 }
487         }
488
489         if (major_version >= 0) {
490                 set_number_property(ctx, T("WINDOWS/VERSION/MAJOR"),
491                                     major_version);
492                 if (minor_version >= 0) {
493                         set_number_property(ctx, T("WINDOWS/VERSION/MINOR"),
494                                             minor_version);
495                 }
496         }
497
498         /* Build number  */
499
500         /* Note: "CurrentBuild" is marked as obsolete in Windows XP registries
501          * (example value:  "1.511.1 () (Obsolete data - do not use)"), and
502          * "CurrentBuildNumber" contains the correct value.  But oddly enough,
503          * it is "CurrentBuild" that contains the correct value on *later*
504          * versions of Windows.  */
505         if (get_string_from_registry(ctx, regf, version_key, T("CurrentBuild"),
506                                      &build_string))
507         {
508                 if (tstrchr(build_string, T('.'))) {
509                         FREE(build_string);
510                         build_string = NULL;
511                         get_string_from_registry(ctx, regf, version_key,
512                                                  T("CurrentBuildNumber"),
513                                                  &build_string);
514                 }
515                 if (build_string) {
516                         set_string_property(ctx, T("WINDOWS/VERSION/BUILD"),
517                                             build_string);
518                         FREE(build_string);
519                 }
520         }
521 }
522
523 /* Gather the default language from the SYSTEM registry hive.  */
524 static void
525 set_default_language(struct windows_info_ctx *ctx, const struct regf *regf)
526 {
527         tchar *string;
528         unsigned language_id;
529
530         if (!get_string_from_registry(ctx, regf,
531                                       T("ControlSet001\\Control\\Nls\\Language"),
532                                       T("InstallLanguage"), &string))
533                 return;
534
535         if (1 == tscanf(string, T("%x"), &language_id)) {
536                 const char *language_name = language_id_to_name(language_id);
537                 if (language_name) {
538                         size_t len = strlen(language_name);
539                         tchar tstr[len + 1];
540                         for (size_t i = 0; i <= len; i++)
541                                 tstr[i] = language_name[i];
542                         set_string_property(ctx, T("WINDOWS/LANGUAGES/DEFAULT"),
543                                             tstr);
544                         FREE(string);
545                         return;
546                 }
547         }
548         XML_WARN("Unrecognized InstallLanguage: %"TS, string);
549         FREE(string);
550 }
551
552 /* Gather information from the SYSTEM registry hive.  */
553 static void
554 set_info_from_system_hive(struct windows_info_ctx *ctx, const struct regf *regf)
555 {
556         const tchar *windows_key = T("ControlSet001\\Control\\Windows");
557         const tchar *uilanguages_key = T("ControlSet001\\Control\\MUI\\UILanguages");
558         const tchar *productoptions_key = T("ControlSet001\\Control\\ProductOptions");
559         s64 spbuild;
560         s64 splevel;
561         tchar **subkeys;
562
563         /* Service pack build  */
564         if (get_number_from_registry(ctx, regf, windows_key,
565                                      T("CSDBuildNumber"), &spbuild))
566                 set_number_property(ctx, T("WINDOWS/VERSION/SPBUILD"), spbuild);
567
568         /* Service pack level  */
569         if (get_number_from_registry(ctx, regf, windows_key,
570                                      T("CSDVersion"), &splevel))
571                 set_number_property(ctx, T("WINDOWS/VERSION/SPLEVEL"), splevel >> 8);
572
573         /* Product type  */
574         copy_registry_string(ctx, regf, productoptions_key, T("ProductType"),
575                              T("WINDOWS/PRODUCTTYPE"));
576
577         /* Product suite  */
578         copy_registry_string(ctx, regf, productoptions_key, T("ProductSuite"),
579                              T("WINDOWS/PRODUCTSUITE"));
580
581         /* Hardware abstraction layer  */
582         copy_registry_string(ctx, regf,
583                              T("ControlSet001\\Control\\Class\\{4D36E966-E325-11CE-BFC1-08002BE10318}\\0000"),
584                              T("MatchingDeviceId"),
585                              T("WINDOWS/HAL"));
586
587         /* Languages  */
588         if (list_subkeys_in_registry(ctx, regf, uilanguages_key, &subkeys)) {
589                 tchar property_name[64];
590                 for (tchar **p = subkeys; *p; p++) {
591                         tsprintf(property_name,
592                                  T("WINDOWS/LANGUAGES/LANGUAGE[%zu]"), p - subkeys + 1);
593                         set_string_property(ctx, property_name, *p);
594                 }
595                 hive_free_subkeys_list(subkeys);
596         }
597
598         /* Default language  */
599         set_default_language(ctx, regf);
600 }
601
602 /* Load the contents of a file in the currently selected WIM image into memory.
603  */
604 static void *
605 load_file_contents(struct windows_info_ctx *ctx,
606                    const struct wim_dentry *dentry, const char *filename,
607                    size_t *size_ret)
608 {
609         const struct blob_descriptor *blob;
610         void *contents;
611         int ret;
612
613         if (!dentry) {
614                 XML_WARN("%s does not exist", filename);
615                 return NULL;
616         }
617
618         blob = inode_get_blob_for_unnamed_data_stream(dentry->d_inode,
619                                                       ctx->wim->blob_table);
620         if (!blob) {
621                 XML_WARN("%s has no contents", filename);
622                 return NULL;
623         }
624
625         ret = read_blob_into_alloc_buf(blob, &contents);
626         if (ret) {
627                 XML_WARN("Error loading %s (size=%"PRIu64"): %"TS,
628                          filename, blob->size, wimlib_get_error_string(ret));
629                 ctx->oom_encountered |= (ret == WIMLIB_ERR_NOMEM &&
630                                          blob->size < 100000000);
631                 return NULL;
632         }
633
634         *size_ret = blob->size;
635         return contents;
636 }
637
638 /* Load and validate a registry hive file.  */
639 static void *
640 load_hive(struct windows_info_ctx *ctx, const struct wim_dentry *dentry,
641           const char *filename)
642 {
643         void *hive_mem;
644         size_t hive_size;
645
646         hive_mem = load_file_contents(ctx, dentry, filename, &hive_size);
647         if (hive_mem && !is_registry_valid(ctx, hive_mem, hive_size)) {
648                 XML_WARN("%s is not a valid registry hive!", filename);
649                 FREE(hive_mem);
650                 hive_mem = NULL;
651         }
652         return hive_mem;
653 }
654
655 /* Set the WINDOWS/SYSTEMROOT property to the name of the directory specified by
656  * 'systemroot'.  */
657 static void
658 set_systemroot_property(struct windows_info_ctx *ctx,
659                         const struct wim_dentry *systemroot)
660 {
661         utf16lechar *uname;
662         const tchar *name;
663         size_t name_nbytes;
664         int ret;
665
666         /* to uppercase ...  */
667         uname = utf16le_dupz(systemroot->d_name, systemroot->d_name_nbytes);
668         if (!uname) {
669                 ctx->oom_encountered = true;
670                 goto out;
671         }
672         for (size_t i = 0; i < systemroot->d_name_nbytes / 2; i++)
673                 uname[i] = cpu_to_le16(upcase[le16_to_cpu(uname[i])]);
674
675         /* to tstring ...  */
676         ret = utf16le_get_tstr(uname, systemroot->d_name_nbytes,
677                                &name, &name_nbytes);
678         if (ret) {
679                 ctx->oom_encountered |= (ret == WIMLIB_ERR_NOMEM);
680                 XML_WARN("Failed to get systemroot name: %"TS,
681                          wimlib_get_error_string(ret));
682                 goto out;
683         }
684         set_string_property(ctx, T("WINDOWS/SYSTEMROOT"), name);
685         utf16le_put_tstr(name);
686 out:
687         FREE(uname);
688 }
689
690 static int
691 do_set_windows_specific_info(WIMStruct *wim,
692                              const struct wim_dentry *systemroot,
693                              const struct wim_dentry *kernel32,
694                              const struct wim_dentry *software,
695                              const struct wim_dentry *system)
696 {
697         void *contents;
698         size_t size;
699         struct windows_info_ctx _ctx = {
700                 .wim = wim,
701                 .image = wim->current_image,
702                 .oom_encountered = false,
703                 .debug_enabled = (tgetenv(T("WIMLIB_DEBUG_XML_INFO")) != NULL),
704         }, *ctx = &_ctx;
705
706         set_systemroot_property(ctx, systemroot);
707
708         if ((contents = load_file_contents(ctx, kernel32, "kernel32.dll", &size))) {
709                 set_info_from_kernel32(ctx, contents, size);
710                 FREE(contents);
711         }
712
713         if ((contents = load_hive(ctx, software, "SOFTWARE"))) {
714                 set_info_from_software_hive(ctx, contents);
715                 FREE(contents);
716         }
717
718         if ((contents = load_hive(ctx, system, "SYSTEM"))) {
719                 set_info_from_system_hive(ctx, contents);
720                 FREE(contents);
721         }
722
723         if (ctx->oom_encountered) {
724                 ERROR("Ran out of memory while setting Windows-specific "
725                       "metadata in the WIM file's XML document.");
726                 return WIMLIB_ERR_NOMEM;
727         }
728
729         return 0;
730 }
731
732 /* Windows */
733 static const utf16lechar windows_name[] = {
734         cpu_to_le16('W'), cpu_to_le16('i'), cpu_to_le16('n'),
735         cpu_to_le16('d'), cpu_to_le16('o'), cpu_to_le16('w'),
736         cpu_to_le16('s'),
737 };
738
739 /* System32 */
740 static const utf16lechar system32_name[] = {
741         cpu_to_le16('S'), cpu_to_le16('y'), cpu_to_le16('s'),
742         cpu_to_le16('t'), cpu_to_le16('e'), cpu_to_le16('m'),
743         cpu_to_le16('3'), cpu_to_le16('2'),
744 };
745
746 /* kernel32.dll */
747 static const utf16lechar kernel32_name[] = {
748         cpu_to_le16('k'), cpu_to_le16('e'), cpu_to_le16('r'),
749         cpu_to_le16('n'), cpu_to_le16('e'), cpu_to_le16('l'),
750         cpu_to_le16('3'), cpu_to_le16('2'), cpu_to_le16('.'),
751         cpu_to_le16('d'), cpu_to_le16('l'), cpu_to_le16('l'),
752 };
753
754 /* config */
755 static const utf16lechar config_name[] = {
756         cpu_to_le16('c'), cpu_to_le16('o'), cpu_to_le16('n'),
757         cpu_to_le16('f'), cpu_to_le16('i'), cpu_to_le16('g'),
758 };
759
760 /* SOFTWARE */
761 static const utf16lechar software_name[] = {
762         cpu_to_le16('S'), cpu_to_le16('O'), cpu_to_le16('F'),
763         cpu_to_le16('T'), cpu_to_le16('W'), cpu_to_le16('A'),
764         cpu_to_le16('R'), cpu_to_le16('E'),
765 };
766
767 /* SYSTEM */
768 static const utf16lechar system_name[] = {
769         cpu_to_le16('S'), cpu_to_le16('Y'), cpu_to_le16('S'),
770         cpu_to_le16('T'), cpu_to_le16('E'), cpu_to_le16('M'),
771 };
772
773 #define GET_CHILD(parent, child_name)                           \
774         get_dentry_child_with_utf16le_name(parent,              \
775                                            child_name,          \
776                                            sizeof(child_name),  \
777                                            WIMLIB_CASE_INSENSITIVE)
778
779 static bool
780 is_default_systemroot(const struct wim_dentry *potential_systemroot)
781 {
782         return !cmp_utf16le_strings(potential_systemroot->d_name,
783                                     potential_systemroot->d_name_nbytes / 2,
784                                     windows_name,
785                                     ARRAY_LEN(windows_name),
786                                     true);
787 }
788
789 /*
790  * Set Windows-specific XML information for the currently selected WIM image.
791  *
792  * This process is heavily based on heuristics and hard-coded logic related to
793  * where Windows stores certain types of information.  Therefore, it simply
794  * tries to set as much information as possible.  If there's a problem, it skips
795  * the affected information and proceeds to the next part.  It only returns an
796  * error code if there was a severe problem such as out-of-memory.
797  */
798 int
799 set_windows_specific_info(WIMStruct *wim)
800 {
801         const struct wim_dentry *root, *potential_systemroot,
802                                 *best_systemroot = NULL,
803                                 *best_kernel32 = NULL,
804                                 *best_software = NULL,
805                                 *best_system = NULL;
806         int best_score = 0;
807
808         root = wim_get_current_root_dentry(wim);
809         if (!root)
810                 return 0;
811
812         /* Find the system root.  This is usually the toplevel directory
813          * "Windows", but it might be a different toplevel directory.  Choose
814          * the directory that contains the greatest number of the files we want:
815          * System32/kernel32.dll, System32/config/SOFTWARE, and
816          * System32/config/SYSTEM.  Compare all names case insensitively.  */
817         for_dentry_child(potential_systemroot, root) {
818                 const struct wim_dentry *system32, *kernel32, *config,
819                                         *software = NULL, *system = NULL;
820                 int score;
821
822                 if (!dentry_is_directory(potential_systemroot))
823                         continue;
824                 system32 = GET_CHILD(potential_systemroot, system32_name);
825                 if (!system32)
826                         continue;
827                 kernel32 = GET_CHILD(system32, kernel32_name);
828                 config = GET_CHILD(system32, config_name);
829                 if (config) {
830                         software = GET_CHILD(config, software_name);
831                         system = GET_CHILD(config, system_name);
832                 }
833
834                 score = !!kernel32 + !!software + !!system;
835                 if (score >= best_score) {
836                         /* If there's a tie, prefer the "Windows" directory.  */
837                         if (score > best_score ||
838                             is_default_systemroot(potential_systemroot))
839                         {
840                                 best_score = score;
841                                 best_systemroot = potential_systemroot;
842                                 best_kernel32 = kernel32;
843                                 best_software = software;
844                                 best_system = system;
845                         }
846                 }
847         }
848
849         if (likely(best_score == 0))
850                 return 0;  /* No Windows system root found.  */
851
852         /* Found the Windows system root.  */
853         return do_set_windows_specific_info(wim, best_systemroot, best_kernel32,
854                                             best_software, best_system);
855 }