]> wimlib.net Git - wimlib/blob - src/xml_windows.c
xml: support ARM64 architecture code
[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 (C) 2016 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 http://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/endianness.h"
35 #include "wimlib/error.h"
36 #include "wimlib/registry.h"
37 #include "wimlib/wim.h"
38 #include "wimlib/xml_windows.h"
39
40 /* Context for a call to set_windows_specific_info()  */
41 struct windows_info_ctx {
42         WIMStruct *wim;
43         int image;
44         bool oom_encountered;
45         bool debug_enabled;
46 };
47
48 /* For debugging purposes, the environmental variable WIMLIB_DEBUG_XML_INFO can
49  * be set to enable messages about certain things not being as expected in the
50  * registry or other files used as information sources.  */
51
52 #define XML_WARN(format, ...)                   \
53         if (ctx->debug_enabled)                 \
54                 WARNING(format, ##__VA_ARGS__)
55
56 /* Path to the SOFTWARE registry hive  */
57 static const tchar * const software_hive_path =
58         WIMLIB_WIM_PATH_SEPARATOR_STRING T("Windows")
59         WIMLIB_WIM_PATH_SEPARATOR_STRING T("System32")
60         WIMLIB_WIM_PATH_SEPARATOR_STRING T("config")
61         WIMLIB_WIM_PATH_SEPARATOR_STRING T("SOFTWARE");
62
63 /* Path to the SYSTEM registry hive  */
64 static const tchar * const system_hive_path =
65         WIMLIB_WIM_PATH_SEPARATOR_STRING T("Windows")
66         WIMLIB_WIM_PATH_SEPARATOR_STRING T("System32")
67         WIMLIB_WIM_PATH_SEPARATOR_STRING T("config")
68         WIMLIB_WIM_PATH_SEPARATOR_STRING T("SYSTEM");
69
70 /* Path to kernel32.dll  */
71 static const tchar * const kernel32_dll_path =
72         WIMLIB_WIM_PATH_SEPARATOR_STRING T("Windows")
73         WIMLIB_WIM_PATH_SEPARATOR_STRING T("System32")
74         WIMLIB_WIM_PATH_SEPARATOR_STRING T("kernel32.dll");
75
76 /* Set a property in the XML document, with error checking.  */
77 static void
78 set_string_property(struct windows_info_ctx *ctx,
79                     const tchar *name, const tchar *value)
80 {
81         int ret = wimlib_set_image_property(ctx->wim, ctx->image, name, value);
82         if (likely(!ret))
83                 return;
84
85         ctx->oom_encountered |= (ret == WIMLIB_ERR_NOMEM);
86         WARNING("Failed to set image property \"%"TS"\" to value "
87                 "\"%"TS"\": %"TS, name, value, wimlib_get_error_string(ret));
88 }
89
90 /* Set a property in the XML document, with error checking.  */
91 static void
92 set_number_property(struct windows_info_ctx *ctx, const tchar *name, s64 value)
93 {
94         tchar buffer[32];
95         tsprintf(buffer, T("%"PRIi64""), value);
96         set_string_property(ctx, name, buffer);
97 }
98
99 /* Check the result of a registry hive operation.  If unsuccessful, possibly
100  * print debugging information.  Return true iff successful.  */
101 static bool
102 check_hive_status(struct windows_info_ctx *ctx, enum hive_status status,
103                   const tchar *key, const tchar *value)
104 {
105         if (likely(status == HIVE_OK))
106                 return true;
107
108         ctx->oom_encountered |= (status == HIVE_OUT_OF_MEMORY);
109         XML_WARN("%s; key=%"TS" value=%"TS, hive_status_to_string(status),
110                  (key ? key : T("(null)")), (value ? value : T("(null)")));
111         return false;
112 }
113
114 static bool
115 is_registry_valid(struct windows_info_ctx *ctx, const void *hive_mem,
116                   size_t hive_size)
117 {
118         enum hive_status status;
119
120         status = hive_validate(hive_mem, hive_size);
121         return check_hive_status(ctx, status, NULL, NULL);
122 }
123
124 static bool
125 get_string_from_registry(struct windows_info_ctx *ctx, const struct regf *regf,
126                          const tchar *key_name, const tchar *value_name,
127                          tchar **value_ret)
128 {
129         enum hive_status status;
130
131         status = hive_get_string(regf, key_name, value_name, value_ret);
132         return check_hive_status(ctx, status, key_name, value_name);
133 }
134
135 static bool
136 get_number_from_registry(struct windows_info_ctx *ctx, const struct regf *regf,
137                          const tchar *key_name, const tchar *value_name,
138                          s64 *value_ret)
139 {
140         enum hive_status status;
141
142         status = hive_get_number(regf, key_name, value_name, value_ret);
143         return check_hive_status(ctx, status, key_name, value_name);
144 }
145
146 static bool
147 list_subkeys_in_registry(struct windows_info_ctx *ctx, const struct regf *regf,
148                          const tchar *key_name, tchar ***subkeys_ret)
149 {
150         enum hive_status status;
151
152         status = hive_list_subkeys(regf, key_name, subkeys_ret);
153         return check_hive_status(ctx, status, key_name, NULL);
154 }
155
156 /* Copy a string value from a registry hive to the XML document.  */
157 static void
158 copy_registry_string(struct windows_info_ctx *ctx, const struct regf *regf,
159                      const tchar *key_name, const tchar *value_name,
160                      const tchar *property_name)
161 {
162         tchar *string;
163
164         if (get_string_from_registry(ctx, regf, key_name, value_name, &string)) {
165                 set_string_property(ctx, property_name, string);
166                 FREE(string);
167         }
168 }
169
170 /* A table that map Windows language IDs, sorted numerically, to their language
171  * names.  It was generated by tools/generate_language_id_map.c.  */
172 static const struct {
173         u16 id;
174         u16 name_start_offset;
175 } language_id_map[452] = {
176         {0x0000,    0}, {0x0001,    6}, {0x0002,   12}, {0x0003,   18},
177         {0x0004,   24}, {0x0005,   30}, {0x0006,   36}, {0x0007,   42},
178         {0x0008,   48}, {0x0009,   54}, {0x000a,   60}, {0x000b,   66},
179         {0x000c,   72}, {0x000d,   78}, {0x000e,   84}, {0x000f,   90},
180         {0x0010,   96}, {0x0011,  102}, {0x0012,  108}, {0x0013,  114},
181         {0x0014,  120}, {0x0015,  126}, {0x0016,  132}, {0x0017,  138},
182         {0x0018,  144}, {0x0019,  150}, {0x001a,  156}, {0x001b,  162},
183         {0x001c,  168}, {0x001d,  174}, {0x001e,  180}, {0x001f,  186},
184         {0x0020,  192}, {0x0021,  198}, {0x0022,  204}, {0x0023,  210},
185         {0x0024,  216}, {0x0025,  222}, {0x0026,  228}, {0x0027,  234},
186         {0x0028,  240}, {0x0029,  251}, {0x002a,  257}, {0x002b,  263},
187         {0x002c,  269}, {0x002d,  280}, {0x002e,  286}, {0x002f,  293},
188         {0x0030,  299}, {0x0031,  305}, {0x0032,  311}, {0x0033,  317},
189         {0x0034,  323}, {0x0035,  329}, {0x0036,  335}, {0x0037,  341},
190         {0x0038,  347}, {0x0039,  353}, {0x003a,  359}, {0x003b,  365},
191         {0x003c,  371}, {0x003d,  377}, {0x003e,  384}, {0x003f,  390},
192         {0x0040,  396}, {0x0041,  402}, {0x0042,  408}, {0x0043,  414},
193         {0x0044,  425}, {0x0045,  431}, {0x0046,  437}, {0x0047,  443},
194         {0x0048,  449}, {0x0049,  455}, {0x004a,  461}, {0x004b,  467},
195         {0x004c,  473}, {0x004d,  479}, {0x004e,  485}, {0x004f,  491},
196         {0x0050,  497}, {0x0051,  503}, {0x0052,  509}, {0x0053,  515},
197         {0x0054,  521}, {0x0055,  527}, {0x0056,  533}, {0x0057,  539},
198         {0x0058,  546}, {0x0059,  553}, {0x005a,  564}, {0x005b,  571},
199         {0x005c,  577}, {0x005d,  589}, {0x005e,  600}, {0x005f,  606},
200         {0x0060,  618}, {0x0061,  629}, {0x0062,  635}, {0x0063,  641},
201         {0x0064,  647}, {0x0065,  654}, {0x0066,  660}, {0x0067,  667},
202         {0x0068,  678}, {0x0069,  689}, {0x006a,  696}, {0x006b,  702},
203         {0x006c,  709}, {0x006d,  716}, {0x006e,  722}, {0x006f,  728},
204         {0x0070,  734}, {0x0071,  740}, {0x0072,  746}, {0x0073,  752},
205         {0x0074,  758}, {0x0075,  764}, {0x0076,  771}, {0x0077,  778},
206         {0x0078,  784}, {0x0079,  790}, {0x007a,  798}, {0x007c,  805},
207         {0x007e,  812}, {0x007f,  818}, {0x0080,  819}, {0x0081,  825},
208         {0x0082,  831}, {0x0083,  837}, {0x0084,  843}, {0x0085,  850},
209         {0x0086,  857}, {0x0087,  869}, {0x0088,  875}, {0x008c,  881},
210         {0x0091,  888}, {0x0092,  894}, {0x0400,  905}, {0x0401,  911},
211         {0x0402,  917}, {0x0403,  923}, {0x0404,  929}, {0x0405,  935},
212         {0x0406,  941}, {0x0407,  947}, {0x0408,  953}, {0x0409,  959},
213         {0x040a,  965}, {0x040b,  978}, {0x040c,  984}, {0x040d,  990},
214         {0x040e,  996}, {0x040f, 1002}, {0x0410, 1008}, {0x0411, 1014},
215         {0x0412, 1020}, {0x0413, 1026}, {0x0414, 1032}, {0x0415, 1038},
216         {0x0416, 1044}, {0x0417, 1050}, {0x0418, 1056}, {0x0419, 1062},
217         {0x041a, 1068}, {0x041b, 1074}, {0x041c, 1080}, {0x041d, 1086},
218         {0x041e, 1092}, {0x041f, 1098}, {0x0420, 1104}, {0x0421, 1110},
219         {0x0422, 1116}, {0x0423, 1122}, {0x0424, 1128}, {0x0425, 1134},
220         {0x0426, 1140}, {0x0427, 1146}, {0x0428, 1152}, {0x0429, 1163},
221         {0x042a, 1169}, {0x042b, 1175}, {0x042c, 1181}, {0x042d, 1192},
222         {0x042e, 1198}, {0x042f, 1205}, {0x0430, 1211}, {0x0431, 1217},
223         {0x0432, 1223}, {0x0433, 1229}, {0x0434, 1235}, {0x0435, 1241},
224         {0x0436, 1247}, {0x0437, 1253}, {0x0438, 1259}, {0x0439, 1265},
225         {0x043a, 1271}, {0x043b, 1277}, {0x043d, 1283}, {0x043e, 1290},
226         {0x043f, 1296}, {0x0440, 1302}, {0x0441, 1308}, {0x0442, 1314},
227         {0x0443, 1320}, {0x0444, 1331}, {0x0445, 1337}, {0x0446, 1343},
228         {0x0447, 1349}, {0x0448, 1355}, {0x0449, 1361}, {0x044a, 1367},
229         {0x044b, 1373}, {0x044c, 1379}, {0x044d, 1385}, {0x044e, 1391},
230         {0x044f, 1397}, {0x0450, 1403}, {0x0451, 1409}, {0x0452, 1415},
231         {0x0453, 1421}, {0x0454, 1427}, {0x0455, 1433}, {0x0456, 1439},
232         {0x0457, 1445}, {0x0458, 1452}, {0x0459, 1459}, {0x045a, 1470},
233         {0x045b, 1477}, {0x045c, 1483}, {0x045d, 1495}, {0x045e, 1506},
234         {0x045f, 1512}, {0x0460, 1524}, {0x0461, 1535}, {0x0462, 1541},
235         {0x0463, 1547}, {0x0464, 1553}, {0x0465, 1560}, {0x0466, 1566},
236         {0x0467, 1573}, {0x0468, 1579}, {0x0469, 1590}, {0x046a, 1597},
237         {0x046b, 1603}, {0x046c, 1610}, {0x046d, 1617}, {0x046e, 1623},
238         {0x046f, 1629}, {0x0470, 1635}, {0x0471, 1641}, {0x0472, 1647},
239         {0x0473, 1653}, {0x0474, 1659}, {0x0475, 1665}, {0x0476, 1672},
240         {0x0477, 1679}, {0x0478, 1685}, {0x0479, 1691}, {0x047a, 1699},
241         {0x047c, 1706}, {0x047e, 1713}, {0x0480, 1719}, {0x0481, 1725},
242         {0x0482, 1731}, {0x0483, 1737}, {0x0484, 1743}, {0x0485, 1750},
243         {0x0486, 1757}, {0x0487, 1769}, {0x0488, 1775}, {0x048c, 1781},
244         {0x0491, 1788}, {0x0492, 1794}, {0x0501, 1805}, {0x05fe, 1814},
245         {0x0800, 1824}, {0x0801, 1830}, {0x0803, 1836}, {0x0804, 1851},
246         {0x0807, 1857}, {0x0809, 1863}, {0x080a, 1869}, {0x080c, 1875},
247         {0x0810, 1881}, {0x0813, 1887}, {0x0814, 1893}, {0x0816, 1899},
248         {0x0818, 1905}, {0x0819, 1911}, {0x081a, 1917}, {0x081d, 1928},
249         {0x0820, 1934}, {0x082c, 1940}, {0x082e, 1951}, {0x0832, 1958},
250         {0x083b, 1964}, {0x083c, 1970}, {0x083e, 1976}, {0x0843, 1982},
251         {0x0845, 1993}, {0x0846, 1999}, {0x0849, 2010}, {0x0850, 2016},
252         {0x0859, 2027}, {0x085d, 2038}, {0x085f, 2049}, {0x0860, 2061},
253         {0x0861, 2072}, {0x0867, 2078}, {0x086b, 2089}, {0x0873, 2096},
254         {0x0901, 2102}, {0x09ff, 2116}, {0x0c00, 2126}, {0x0c01, 2132},
255         {0x0c04, 2138}, {0x0c07, 2144}, {0x0c09, 2150}, {0x0c0a, 2156},
256         {0x0c0c, 2162}, {0x0c1a, 2168}, {0x0c3b, 2179}, {0x0c50, 2185},
257         {0x0c51, 2196}, {0x0c6b, 2202}, {0x1000, 2209}, {0x1001, 2220},
258         {0x1004, 2226}, {0x1007, 2232}, {0x1009, 2238}, {0x100a, 2244},
259         {0x100c, 2250}, {0x101a, 2256}, {0x103b, 2262}, {0x105f, 2269},
260         {0x1401, 2281}, {0x1404, 2287}, {0x1407, 2293}, {0x1409, 2299},
261         {0x140a, 2305}, {0x140c, 2311}, {0x141a, 2317}, {0x143b, 2328},
262         {0x1801, 2335}, {0x1809, 2341}, {0x180a, 2347}, {0x180c, 2353},
263         {0x181a, 2359}, {0x183b, 2370}, {0x1c01, 2377}, {0x1c09, 2383},
264         {0x1c0a, 2389}, {0x1c0c, 2395}, {0x1c1a, 2402}, {0x1c3b, 2413},
265         {0x2000, 2420}, {0x2001, 2426}, {0x2009, 2432}, {0x200a, 2438},
266         {0x200c, 2444}, {0x201a, 2450}, {0x203b, 2461}, {0x2400, 2468},
267         {0x2401, 2474}, {0x2409, 2480}, {0x240a, 2487}, {0x240c, 2493},
268         {0x241a, 2499}, {0x243b, 2510}, {0x2800, 2517}, {0x2801, 2523},
269         {0x2809, 2529}, {0x280a, 2535}, {0x280c, 2541}, {0x281a, 2547},
270         {0x2c00, 2558}, {0x2c01, 2564}, {0x2c09, 2570}, {0x2c0a, 2576},
271         {0x2c0c, 2582}, {0x2c1a, 2588}, {0x3000, 2599}, {0x3001, 2605},
272         {0x3009, 2611}, {0x300a, 2617}, {0x300c, 2623}, {0x301a, 2629},
273         {0x3400, 2640}, {0x3401, 2646}, {0x3409, 2652}, {0x340a, 2658},
274         {0x340c, 2664}, {0x3800, 2670}, {0x3801, 2676}, {0x3809, 2682},
275         {0x380a, 2688}, {0x380c, 2694}, {0x3c00, 2700}, {0x3c01, 2706},
276         {0x3c09, 2712}, {0x3c0a, 2718}, {0x3c0c, 2724}, {0x4000, 2730},
277         {0x4001, 2736}, {0x4009, 2742}, {0x400a, 2748}, {0x4400, 2754},
278         {0x4409, 2760}, {0x440a, 2766}, {0x4800, 2772}, {0x4809, 2778},
279         {0x480a, 2784}, {0x4c00, 2790}, {0x4c0a, 2796}, {0x500a, 2802},
280         {0x540a, 2808}, {0x580a, 2814}, {0x5c0a, 2821}, {0x641a, 2827},
281         {0x681a, 2838}, {0x6c1a, 2849}, {0x701a, 2860}, {0x703b, 2871},
282         {0x742c, 2878}, {0x743b, 2889}, {0x7804, 2896}, {0x7814, 2902},
283         {0x781a, 2908}, {0x782c, 2919}, {0x783b, 2930}, {0x7843, 2937},
284         {0x7850, 2948}, {0x785d, 2954}, {0x785f, 2965}, {0x7c04, 2977},
285         {0x7c14, 2983}, {0x7c1a, 2989}, {0x7c28, 3000}, {0x7c2e, 3011},
286         {0x7c3b, 3018}, {0x7c43, 3025}, {0x7c46, 3036}, {0x7c50, 3047},
287         {0x7c59, 3058}, {0x7c5c, 3069}, {0x7c5d, 3081}, {0x7c5f, 3092},
288         {0x7c67, 3104}, {0x7c68, 3115}, {0x7c86, 3126}, {0x7c92, 3138},
289 };
290
291 /* All the language names; generated by tools/generate_language_id_map.c.
292  * For compactness, this is a 'char' string rather than a 'tchar' string.  */
293 static const char language_names[3149] =
294         "en-US\0ar-SA\0bg-BG\0ca-ES\0zh-CN\0cs-CZ\0da-DK\0de-DE\0el-GR\0en-US\0"
295         "es-ES\0fi-FI\0fr-FR\0he-IL\0hu-HU\0is-IS\0it-IT\0ja-JP\0ko-KR\0nl-NL\0"
296         "nb-NO\0pl-PL\0pt-BR\0rm-CH\0ro-RO\0ru-RU\0hr-HR\0sk-SK\0sq-AL\0sv-SE\0"
297         "th-TH\0tr-TR\0ur-PK\0id-ID\0uk-UA\0be-BY\0sl-SI\0et-EE\0lv-LV\0lt-LT\0"
298         "tg-Cyrl-TJ\0fa-IR\0vi-VN\0hy-AM\0az-Latn-AZ\0eu-ES\0hsb-DE\0mk-MK\0"
299         "st-ZA\0ts-ZA\0tn-ZA\0ve-ZA\0xh-ZA\0zu-ZA\0af-ZA\0ka-GE\0fo-FO\0hi-IN\0"
300         "mt-MT\0se-NO\0ga-IE\0yi-001\0ms-MY\0kk-KZ\0ky-KG\0sw-KE\0tk-TM\0"
301         "uz-Latn-UZ\0tt-RU\0bn-IN\0pa-IN\0gu-IN\0or-IN\0ta-IN\0te-IN\0kn-IN\0"
302         "ml-IN\0as-IN\0mr-IN\0sa-IN\0mn-MN\0bo-CN\0cy-GB\0km-KH\0lo-LA\0my-MM\0"
303         "gl-ES\0kok-IN\0mni-IN\0sd-Arab-PK\0syr-SY\0si-LK\0chr-Cher-US\0"
304         "iu-Latn-CA\0am-ET\0tzm-Latn-DZ\0ks-Arab-IN\0ne-NP\0fy-NL\0ps-AF\0"
305         "fil-PH\0dv-MV\0bin-NG\0ff-Latn-SN\0ha-Latn-NG\0ibb-NG\0yo-NG\0quz-BO\0"
306         "nso-ZA\0ba-RU\0lb-LU\0kl-GL\0ig-NG\0kr-NG\0om-ET\0ti-ER\0gn-PY\0"
307         "haw-US\0la-001\0so-SO\0ii-CN\0pap-029\0arn-CL\0moh-CA\0br-FR\0\0"
308         "ug-CN\0mi-NZ\0oc-FR\0co-FR\0gsw-FR\0sah-RU\0quc-Latn-GT\0rw-RW\0"
309         "wo-SN\0prs-AF\0gd-GB\0ku-Arab-IQ\0en-US\0ar-SA\0bg-BG\0ca-ES\0zh-TW\0"
310         "cs-CZ\0da-DK\0de-DE\0el-GR\0en-US\0es-ES_tradnl\0fi-FI\0fr-FR\0he-IL\0"
311         "hu-HU\0is-IS\0it-IT\0ja-JP\0ko-KR\0nl-NL\0nb-NO\0pl-PL\0pt-BR\0rm-CH\0"
312         "ro-RO\0ru-RU\0hr-HR\0sk-SK\0sq-AL\0sv-SE\0th-TH\0tr-TR\0ur-PK\0id-ID\0"
313         "uk-UA\0be-BY\0sl-SI\0et-EE\0lv-LV\0lt-LT\0tg-Cyrl-TJ\0fa-IR\0vi-VN\0"
314         "hy-AM\0az-Latn-AZ\0eu-ES\0hsb-DE\0mk-MK\0st-ZA\0ts-ZA\0tn-ZA\0ve-ZA\0"
315         "xh-ZA\0zu-ZA\0af-ZA\0ka-GE\0fo-FO\0hi-IN\0mt-MT\0se-NO\0yi-001\0"
316         "ms-MY\0kk-KZ\0ky-KG\0sw-KE\0tk-TM\0uz-Latn-UZ\0tt-RU\0bn-IN\0pa-IN\0"
317         "gu-IN\0or-IN\0ta-IN\0te-IN\0kn-IN\0ml-IN\0as-IN\0mr-IN\0sa-IN\0mn-MN\0"
318         "bo-CN\0cy-GB\0km-KH\0lo-LA\0my-MM\0gl-ES\0kok-IN\0mni-IN\0sd-Deva-IN\0"
319         "syr-SY\0si-LK\0chr-Cher-US\0iu-Cans-CA\0am-ET\0tzm-Arab-MA\0"
320         "ks-Arab-IN\0ne-NP\0fy-NL\0ps-AF\0fil-PH\0dv-MV\0bin-NG\0ff-NG\0"
321         "ha-Latn-NG\0ibb-NG\0yo-NG\0quz-BO\0nso-ZA\0ba-RU\0lb-LU\0kl-GL\0"
322         "ig-NG\0kr-NG\0om-ET\0ti-ET\0gn-PY\0haw-US\0la-001\0so-SO\0ii-CN\0"
323         "pap-029\0arn-CL\0moh-CA\0br-FR\0ug-CN\0mi-NZ\0oc-FR\0co-FR\0gsw-FR\0"
324         "sah-RU\0quc-Latn-GT\0rw-RW\0wo-SN\0prs-AF\0gd-GB\0ku-Arab-IQ\0"
325         "qps-ploc\0qps-ploca\0en-US\0ar-IQ\0ca-ES-valencia\0zh-CN\0de-CH\0"
326         "en-GB\0es-MX\0fr-BE\0it-CH\0nl-BE\0nn-NO\0pt-PT\0ro-MD\0ru-MD\0"
327         "sr-Latn-CS\0sv-FI\0ur-IN\0az-Cyrl-AZ\0dsb-DE\0tn-BW\0se-SE\0ga-IE\0"
328         "ms-BN\0uz-Cyrl-UZ\0bn-BD\0pa-Arab-PK\0ta-LK\0mn-Mong-CN\0sd-Arab-PK\0"
329         "iu-Latn-CA\0tzm-Latn-DZ\0ks-Deva-IN\0ne-IN\0ff-Latn-SN\0quz-EC\0"
330         "ti-ER\0qps-Latn-x-sh\0qps-plocm\0en-US\0ar-EG\0zh-HK\0de-AT\0en-AU\0"
331         "es-ES\0fr-CA\0sr-Cyrl-CS\0se-FI\0mn-Mong-MN\0dz-BT\0quz-PE\0"
332         "ks-Arab-IN\0ar-LY\0zh-SG\0de-LU\0en-CA\0es-GT\0fr-CH\0hr-BA\0smj-NO\0"
333         "tzm-Tfng-MA\0ar-DZ\0zh-MO\0de-LI\0en-NZ\0es-CR\0fr-LU\0bs-Latn-BA\0"
334         "smj-SE\0ar-MA\0en-IE\0es-PA\0fr-MC\0sr-Latn-BA\0sma-NO\0ar-TN\0en-ZA\0"
335         "es-DO\0fr-029\0sr-Cyrl-BA\0sma-SE\0en-US\0ar-OM\0en-JM\0es-VE\0fr-RE\0"
336         "bs-Cyrl-BA\0sms-FI\0en-US\0ar-YE\0en-029\0es-CO\0fr-CD\0sr-Latn-RS\0"
337         "smn-FI\0en-US\0ar-SY\0en-BZ\0es-PE\0fr-SN\0sr-Cyrl-RS\0en-US\0ar-JO\0"
338         "en-TT\0es-AR\0fr-CM\0sr-Latn-ME\0en-US\0ar-LB\0en-ZW\0es-EC\0fr-CI\0"
339         "sr-Cyrl-ME\0en-US\0ar-KW\0en-PH\0es-CL\0fr-ML\0en-US\0ar-AE\0en-ID\0"
340         "es-UY\0fr-MA\0en-US\0ar-BH\0en-HK\0es-PY\0fr-HT\0en-US\0ar-QA\0en-IN\0"
341         "es-BO\0en-US\0en-MY\0es-SV\0en-US\0en-SG\0es-HN\0en-US\0es-NI\0es-PR\0"
342         "es-US\0es-419\0es-CU\0bs-Cyrl-BA\0bs-Latn-BA\0sr-Cyrl-RS\0sr-Latn-RS\0"
343         "smn-FI\0az-Cyrl-AZ\0sms-FI\0zh-CN\0nn-NO\0bs-Latn-BA\0az-Latn-AZ\0"
344         "sma-SE\0uz-Cyrl-UZ\0mn-MN\0iu-Cans-CA\0tzm-Tfng-MA\0zh-HK\0nb-NO\0"
345         "sr-Latn-RS\0tg-Cyrl-TJ\0dsb-DE\0smj-SE\0uz-Latn-UZ\0pa-Arab-PK\0"
346         "mn-Mong-CN\0sd-Arab-PK\0chr-Cher-US\0iu-Latn-CA\0tzm-Latn-DZ\0"
347         "ff-Latn-SN\0ha-Latn-NG\0quc-Latn-GT\0ku-Arab-IQ\0";
348
349 /* Translate a Windows language ID to its name.  Returns NULL if the ID is not
350  * recognized.  */
351 static const char *
352 language_id_to_name(u16 id)
353 {
354         int l = 0;
355         int r = ARRAY_LEN(language_id_map) - 1;
356         do {
357                 int m = (l + r) / 2;
358                 if (id < language_id_map[m].id)
359                         r = m - 1;
360                 else if (id > language_id_map[m].id)
361                         l = m + 1;
362                 else
363                         return &language_names[language_id_map[m].name_start_offset];
364         } while (l <= r);
365         return NULL;
366 }
367
368 /* PE binary processor architecture codes (common ones only)  */
369 #define IMAGE_FILE_MACHINE_I386         0x014C
370 #define IMAGE_FILE_MACHINE_ARM          0x01C0
371 #define IMAGE_FILE_MACHINE_ARMV7        0x01C4
372 #define IMAGE_FILE_MACHINE_THUMB        0x01C2
373 #define IMAGE_FILE_MACHINE_IA64         0x0200
374 #define IMAGE_FILE_MACHINE_AMD64        0x8664
375 #define IMAGE_FILE_MACHINE_ARM64        0xAA64
376
377 /* Windows API processor architecture codes (common ones only)  */
378 #define PROCESSOR_ARCHITECTURE_INTEL    0
379 #define PROCESSOR_ARCHITECTURE_ARM      5
380 #define PROCESSOR_ARCHITECTURE_IA64     6
381 #define PROCESSOR_ARCHITECTURE_AMD64    9
382 #define PROCESSOR_ARCHITECTURE_ARM64    12
383
384 /* Translate a processor architecture code as given in a PE binary to the code
385  * used by the Windows API.  Returns -1 if the code is not recognized.  */
386 static int
387 pe_arch_to_windows_arch(unsigned pe_arch)
388 {
389         switch (pe_arch) {
390         case IMAGE_FILE_MACHINE_I386:
391                 return PROCESSOR_ARCHITECTURE_INTEL;
392         case IMAGE_FILE_MACHINE_ARM:
393         case IMAGE_FILE_MACHINE_ARMV7:
394         case IMAGE_FILE_MACHINE_THUMB:
395                 return PROCESSOR_ARCHITECTURE_ARM;
396         case IMAGE_FILE_MACHINE_IA64:
397                 return PROCESSOR_ARCHITECTURE_IA64;
398         case IMAGE_FILE_MACHINE_AMD64:
399                 return PROCESSOR_ARCHITECTURE_AMD64;
400         case IMAGE_FILE_MACHINE_ARM64:
401                 return PROCESSOR_ARCHITECTURE_ARM64;
402         }
403         return -1;
404 }
405
406 /* Gather information from kernel32.dll.  */
407 static void
408 set_info_from_kernel32(struct windows_info_ctx *ctx,
409                        const void *contents, size_t size)
410 {
411         u32 e_lfanew;
412         const u8 *pe_hdr;
413         unsigned pe_arch;
414         int arch;
415
416         /* Read the processor architecture from the executable header.  */
417
418         if (size < 0x40)
419                 goto invalid;
420
421         e_lfanew = le32_to_cpu(*(le32 *)((u8 *)contents + 0x3C));
422         if (e_lfanew > size || size - e_lfanew < 6 || (e_lfanew & 3))
423                 goto invalid;
424
425         pe_hdr = (u8 *)contents + e_lfanew;
426         if (*(u32 *)pe_hdr != cpu_to_le32(0x00004550))  /* "PE\0\0"  */
427                 goto invalid;
428
429         pe_arch = le16_to_cpu(*(le16 *)(pe_hdr + 4));
430         arch = pe_arch_to_windows_arch(pe_arch);
431         if (arch >= 0) {
432                 /* Save the processor architecture in the XML document.  */
433                 set_number_property(ctx, T("WINDOWS/ARCH"), arch);
434         } else {
435                 XML_WARN("Architecture value %x from kernel32.dll "
436                          "header not recognized", pe_arch);
437         }
438         return;
439
440 invalid:
441         XML_WARN("kernel32.dll is not a valid PE binary.");
442 }
443
444 /* Gather information from the SOFTWARE registry hive.  */
445 static void
446 set_info_from_software_hive(struct windows_info_ctx *ctx,
447                             const struct regf *regf)
448 {
449         const tchar *version_key = T("Microsoft\\Windows NT\\CurrentVersion");
450         s64 major_version = -1;
451         s64 minor_version = -1;
452         tchar *version_string;
453
454         /* Image flags  */
455         copy_registry_string(ctx, regf, version_key, T("EditionID"),
456                              T("FLAGS"));
457
458         /* Image display name  */
459         copy_registry_string(ctx, regf, version_key, T("ProductName"),
460                              T("DISPLAYNAME"));
461
462         /* Image display description  */
463         copy_registry_string(ctx, regf, version_key, T("ProductName"),
464                              T("DISPLAYDESCRIPTION"));
465
466         /* Edition ID  */
467         copy_registry_string(ctx, regf, version_key, T("EditionID"),
468                              T("WINDOWS/EDITIONID"));
469
470         /* Installation type  */
471         copy_registry_string(ctx, regf, version_key, T("InstallationType"),
472                              T("WINDOWS/INSTALLATIONTYPE"));
473
474         /* Product name  */
475         copy_registry_string(ctx, regf, version_key, T("ProductName"),
476                              T("WINDOWS/PRODUCTNAME"));
477
478         /* Major and minor version number  */
479
480         /* Note: in Windows 10, CurrentVersion was apparently fixed at 6.3.
481          * Instead, the new values CurrentMajorVersionNumber and
482          * CurrentMinorVersionNumber should be used.  */
483
484         get_number_from_registry(ctx, regf, version_key,
485                                  T("CurrentMajorVersionNumber"), &major_version);
486
487         get_number_from_registry(ctx, regf, version_key,
488                                  T("CurrentMinorVersionNumber"), &minor_version);
489
490         if (major_version < 0 || minor_version < 0) {
491                 if (get_string_from_registry(ctx, regf, version_key,
492                                              T("CurrentVersion"),
493                                              &version_string))
494                 {
495                         if (2 != tscanf(version_string, T("%"PRIi64".%"PRIi64),
496                                         &major_version, &minor_version))
497                         {
498                                 XML_WARN("Unrecognized CurrentVersion: %"TS,
499                                          version_string);
500                         }
501                         FREE(version_string);
502                 }
503         }
504
505         if (major_version >= 0) {
506                 set_number_property(ctx, T("WINDOWS/VERSION/MAJOR"),
507                                     major_version);
508                 if (minor_version >= 0) {
509                         set_number_property(ctx, T("WINDOWS/VERSION/MINOR"),
510                                             minor_version);
511                 }
512         }
513
514         /* Build number  */
515         copy_registry_string(ctx, regf, version_key, T("CurrentBuild"),
516                              T("WINDOWS/VERSION/BUILD"));
517 }
518
519 /* Gather the default language from the SYSTEM registry hive.  */
520 static void
521 set_default_language(struct windows_info_ctx *ctx, const struct regf *regf)
522 {
523         tchar *string;
524         unsigned language_id;
525
526         if (!get_string_from_registry(ctx, regf,
527                                       T("ControlSet001\\Control\\Nls\\Language"),
528                                       T("InstallLanguage"), &string))
529                 return;
530
531         if (1 == tscanf(string, T("%x"), &language_id)) {
532                 const char *language_name = language_id_to_name(language_id);
533                 if (language_name) {
534                         size_t len = strlen(language_name);
535                         tchar tstr[len + 1];
536                         for (size_t i = 0; i <= len; i++)
537                                 tstr[i] = language_name[i];
538                         set_string_property(ctx, T("WINDOWS/LANGUAGES/DEFAULT"),
539                                             tstr);
540                         FREE(string);
541                         return;
542                 }
543         }
544         XML_WARN("Unrecognized InstallLanguage: %"TS, string);
545         FREE(string);
546 }
547
548 /* Gather information from the SYSTEM registry hive.  */
549 static void
550 set_info_from_system_hive(struct windows_info_ctx *ctx, const struct regf *regf)
551 {
552         const tchar *windows_key = T("ControlSet001\\Control\\Windows");
553         const tchar *uilanguages_key = T("ControlSet001\\Control\\MUI\\UILanguages");
554         const tchar *productoptions_key = T("ControlSet001\\Control\\ProductOptions");
555         s64 spbuild;
556         s64 splevel;
557         tchar **subkeys;
558
559         /* Service pack build  */
560         if (get_number_from_registry(ctx, regf, windows_key,
561                                      T("CSDBuildNumber"), &spbuild))
562                 set_number_property(ctx, T("WINDOWS/VERSION/SPBUILD"), spbuild);
563
564         /* Service pack level  */
565         if (get_number_from_registry(ctx, regf, windows_key,
566                                      T("CSDVersion"), &splevel))
567                 set_number_property(ctx, T("WINDOWS/VERSION/SPLEVEL"), splevel >> 8);
568
569         /* Product type  */
570         copy_registry_string(ctx, regf, productoptions_key, T("ProductType"),
571                              T("WINDOWS/PRODUCTTYPE"));
572
573         /* Product suite  */
574         copy_registry_string(ctx, regf, productoptions_key, T("ProductSuite"),
575                              T("WINDOWS/PRODUCTSUITE"));
576
577         /* Hardware abstraction layer  */
578         copy_registry_string(ctx, regf,
579                              T("ControlSet001\\Control\\Class\\{4D36E966-E325-11CE-BFC1-08002BE10318}\\0000"),
580                              T("MatchingDeviceId"),
581                              T("WINDOWS/HAL"));
582
583         /* Languages  */
584         if (list_subkeys_in_registry(ctx, regf, uilanguages_key, &subkeys)) {
585                 tchar property_name[64];
586                 for (tchar **p = subkeys; *p; p++) {
587                         tsprintf(property_name,
588                                  T("WINDOWS/LANGUAGES/LANGUAGE[%zu]"), p - subkeys + 1);
589                         set_string_property(ctx, property_name, *p);
590                 }
591                 hive_free_subkeys_list(subkeys);
592         }
593
594         /* Default language  */
595         set_default_language(ctx, regf);
596 }
597
598 /* Load the contents of a file in the currently selected WIM image into memory.
599  */
600 static void *
601 load_file_contents(struct windows_info_ctx *ctx,
602                    const tchar *path, size_t *size_ret)
603 {
604         const struct wim_dentry *dentry;
605         const struct blob_descriptor *blob;
606         void *contents;
607         int ret;
608
609         dentry = get_dentry(ctx->wim, path, WIMLIB_CASE_INSENSITIVE);
610         if (!dentry) {
611                 XML_WARN("File \"%"TS"\" not found", path);
612                 return NULL;
613         }
614
615         blob = inode_get_blob_for_unnamed_data_stream(dentry->d_inode,
616                                                       ctx->wim->blob_table);
617         if (!blob) {
618                 XML_WARN("File \"%"TS"\" has no contents", path);
619                 return NULL;
620         }
621
622         ret = read_blob_into_alloc_buf(blob, &contents);
623         if (ret) {
624                 XML_WARN("Error loading file \"%"TS"\" (size=%"PRIu64"): %"TS,
625                          path, blob->size, wimlib_get_error_string(ret));
626                 ctx->oom_encountered |= (ret == WIMLIB_ERR_NOMEM &&
627                                          blob->size < 100000000);
628                 return NULL;
629         }
630
631         *size_ret = blob->size;
632         return contents;
633 }
634
635 /* Load and validate a registry hive file.  */
636 static void *
637 load_hive(struct windows_info_ctx *ctx, const tchar *path)
638 {
639         void *hive_mem;
640         size_t hive_size;
641
642         hive_mem = load_file_contents(ctx, path, &hive_size);
643         if (hive_mem && !is_registry_valid(ctx, hive_mem, hive_size)) {
644                 XML_WARN("\"%"TS"\" is not a valid registry hive!", path);
645                 FREE(hive_mem);
646                 hive_mem = NULL;
647         }
648         return hive_mem;
649 }
650
651 /*
652  * Set Windows-specific XML information for the currently selected WIM image.
653  *
654  * This process is heavily based on heuristics and hard-coded logic related to
655  * where Windows stores certain types of information.  Therefore, it simply
656  * tries to set as much information as possible.  If there's a problem, it skips
657  * the affected information and proceeds to the next part.  It only returns an
658  * error code if there was a severe problem such as out-of-memory.
659  */
660 int
661 set_windows_specific_info(WIMStruct *wim)
662 {
663         void *contents;
664         size_t size;
665         struct windows_info_ctx _ctx = {
666                 .wim = wim,
667                 .image = wim->current_image,
668                 .oom_encountered = false,
669                 .debug_enabled = (tgetenv(T("WIMLIB_DEBUG_XML_INFO")) != NULL),
670         }, *ctx = &_ctx;
671
672         if ((contents = load_file_contents(ctx, kernel32_dll_path, &size))) {
673                 set_string_property(ctx, T("WINDOWS/SYSTEMROOT"), T("WINDOWS"));
674                 set_info_from_kernel32(ctx, contents, size);
675                 FREE(contents);
676         }
677
678         if ((contents = load_hive(ctx, software_hive_path))) {
679                 set_info_from_software_hive(ctx, contents);
680                 FREE(contents);
681         }
682
683         if ((contents = load_hive(ctx, system_hive_path))) {
684                 set_info_from_system_hive(ctx, contents);
685                 FREE(contents);
686         }
687
688         if (ctx->oom_encountered) {
689                 ERROR("Ran out of memory while setting Windows-specific "
690                       "metadata in the WIM file's XML document.");
691                 return WIMLIB_ERR_NOMEM;
692         }
693
694         return 0;
695 }