|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import binascii |
| 3 | +import chardet |
| 4 | +import os |
| 5 | +import re |
| 6 | +import sys |
| 7 | + |
| 8 | +def get_encoding(filename): |
| 9 | + with open(filename, "rb") as f: |
| 10 | + rawdata = f.read() |
| 11 | + return chardet.detect(rawdata) |
| 12 | + |
| 13 | +def stoh(src): |
| 14 | + return str(binascii.hexlify(bytes(src, 'utf-8')),'ascii') |
| 15 | + |
| 16 | +def find_define(filename, name, codec, isopen=True): |
| 17 | + additional = {} |
| 18 | + additional["/tmp/Windows Kits/10/Include/10.0.26100.0/um/mfapi.h"] = "/tmp/Windows Kits/10/Include/10.0.26100.0/shared/mmreg.h" |
| 19 | + |
| 20 | + #print(f"Searching in {filename} for {name}") |
| 21 | + |
| 22 | + if not isopen: |
| 23 | + codec = get_encoding(filename) |
| 24 | + |
| 25 | + merged = "" |
| 26 | + try: |
| 27 | + with open(filename, 'r', encoding=codec['encoding']) as f: |
| 28 | + for line in f: |
| 29 | + stripped = line.strip() |
| 30 | + if stripped.startswith("#define"): |
| 31 | + merged = stripped |
| 32 | + elif merged: |
| 33 | + merged += " " + stripped |
| 34 | + else: |
| 35 | + merged = "" |
| 36 | + |
| 37 | + if merged: |
| 38 | + if merged.endswith('\\'): |
| 39 | + pos = len(merged) - 1 |
| 40 | + merged = list(merged) |
| 41 | + merged[pos] = "" |
| 42 | + merged = ''.join(merged) |
| 43 | + |
| 44 | + elif f"define{name}" in merged.replace(" ", "").replace("\t", ""): |
| 45 | + match = re.search(r"#define.*0[Xx]([^L,]*).*0[Xx]([^,]*).*0[Xx]([^,]*).*0[Xx]([^,]*).*0[Xx]([^,]*).*0[Xx]([^,]*).*0[Xx]([^,]*).*0[Xx]([^,]*).*0[Xx]([^,]*).*0[Xx]([^,]*).*0[Xx]([^\s\n]*).*", merged) |
| 46 | + if match: |
| 47 | + return f"{match.group(1).zfill(8)}-{match.group(2).zfill(4)}-{match.group(3).zfill(4)}-{match.group(4).zfill(2)}{match.group(5).zfill(2)}-{match.group(6).zfill(2)}{match.group(7).zfill(2)}{match.group(8).zfill(2)}{match.group(9).zfill(2)}{match.group(10).zfill(2)}{match.group(11).zfill(2)}".upper() |
| 48 | + else: |
| 49 | + match = re.search(r"#define\s*[^\s]*.*0[Xx]([A-F0-9]*).*", merged) |
| 50 | + if match: |
| 51 | + return match.group(1) |
| 52 | + else: |
| 53 | + match = re.search(r"#define\s*[^\s]*[^\d]*(\d*[^\s]).*", merged) |
| 54 | + if match: |
| 55 | + return str(hex(int(match.group(1)))).replace("0x", "") |
| 56 | + else: |
| 57 | + print(f"Wrong define: {merged}") |
| 58 | + sys.exit() |
| 59 | + # Add additional includes |
| 60 | + ret = "" |
| 61 | + if filename in additional: |
| 62 | + ret = find_define(additional[filename], name, codec, False) |
| 63 | + if ret: |
| 64 | + return ret |
| 65 | + if isopen: |
| 66 | + print(f"{name} not defined") |
| 67 | + sys.exit() |
| 68 | + |
| 69 | + except FileNotFoundError: |
| 70 | + print(f"Error: File '{filename}' not found.") |
| 71 | + |
| 72 | + |
| 73 | +def find_guid_definitions(filename): |
| 74 | + guids = [] |
| 75 | + guid_keywords = ["MIDL_DEFINE_GUID", "DEFINE_GUIDSTRUCT", "DEFINE_GUID", "DEFINE_OLEGUID", "DECLARE_INTERFACE_IID", "DECLARE_INTERFACE_IID_", "#define GUID_IID", "#define GUID_LIBID", "#define GUID_CLSID", "DEFINE_BLUETOOTH_UUID128", "interface", "OUR_GUID_ENTRY", "DEFINE_MEDIATYPE_GUID", "DEFINE_BINARY_MEDIATYPE", "DEFINE_AVIGUID", "EXTERN_GUID", "DEFINE_NWF_GUID", "DEFINE_CODECAPI_GUID", "DEFINE_DAOGUID", "GUID_BUILDER"] |
| 76 | + |
| 77 | + try: |
| 78 | + codec = get_encoding(filename) |
| 79 | + with open(filename, 'r', encoding=codec['encoding']) as f: |
| 80 | + mergedLine = "" |
| 81 | + for line in f: |
| 82 | + stripped_line = re.sub(r'/\*.*?\*/', '', line, flags=re.DOTALL) |
| 83 | + stripped_line = re.sub(r'//.*', '', stripped_line, flags=re.DOTALL) |
| 84 | + stripped_line = stripped_line.strip() |
| 85 | + if (stripped_line.startswith(tuple(guid_keywords)) or re.search(r"^#define.*GUID_BUILDER.*", stripped_line)) and not "##" in stripped_line: |
| 86 | + mergedLine = stripped_line |
| 87 | + elif mergedLine: # If we're in the middle of a multiline definition |
| 88 | + mergedLine += stripped_line |
| 89 | + else: |
| 90 | + mergedLine = "" |
| 91 | + |
| 92 | + if mergedLine and (';' in stripped_line or stripped_line.startswith("#") or not re.search(r'\([^\)]*$', mergedLine)) and not stripped_line.endswith("\\"): |
| 93 | + mergedLine = mergedLine.replace(" ", "").replace("\t", "").replace("\\", "") |
| 94 | + if mergedLine.startswith("DEFINE_GUIDSTRUCT"): |
| 95 | + match = re.search(r".*\"(.*)\",(.*)\).*", mergedLine) |
| 96 | + if match: |
| 97 | + guid_string = f"{match.group(1).upper()} {match.group(2)}\n" |
| 98 | + guids.append(guid_string) |
| 99 | + else: |
| 100 | + print(f"Invalid: {mergedLine}") |
| 101 | + elif mergedLine.startswith('DEFINE_GUID'): |
| 102 | + if not "(" in mergedLine[11]: |
| 103 | + mergedLine = "" |
| 104 | + continue |
| 105 | + match = re.search(r".*\(([^,]*).*0[Xx]([^L,]*).*0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^\)]*).*", mergedLine) |
| 106 | + if match: |
| 107 | + guid_string = f"{match.group(2).zfill(8)}-{match.group(3).zfill(4)}-{match.group(4).zfill(4)}-{match.group(5).zfill(2)}{match.group(6).zfill(2)}-{match.group(7).zfill(2)}{match.group(8).zfill(2)}{match.group(9).zfill(2)}{match.group(10).zfill(2)}{match.group(11).zfill(2)}{match.group(12).zfill(2)}" |
| 108 | + guid_string = f"{guid_string.upper()} {match.group(1)}\n" |
| 109 | + guids.append(guid_string) |
| 110 | + elif not "DEFINE_GUID\(name" in mergedLine: |
| 111 | + match = re.search(r".*\(([^,]*),(STATIC[^\)]*).*", mergedLine) |
| 112 | + if match: |
| 113 | + defined = find_define(filename, match.group(2), codec) |
| 114 | + guid_string = f"{defined.upper()} {match.group(1)}\n" |
| 115 | + guids.append(guid_string) |
| 116 | + else: |
| 117 | + match = re.search(r"^.*\(([^,]*),0[Xx]([^L,]*).*,0,0,0xC0,0,0,0,0,0,0,0x46\);", mergedLine) |
| 118 | + if match: |
| 119 | + guid_string = f"{match.group(2).zfill(8).upper()}-0000-0000-C000-000000000046 {match.group(1)}\n" |
| 120 | + guids.append(guid_string) |
| 121 | + else: |
| 122 | + print(f"Invalid: {mergedLine}") |
| 123 | + elif mergedLine.startswith("OUR_GUID_ENTRY"): |
| 124 | + match = re.search(r".*\(([^,]*).*0[Xx]([^L,]*).*0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^\)]*).*", mergedLine) |
| 125 | + if match: |
| 126 | + guid_string = f"{match.group(2).zfill(8)}-{match.group(3).zfill(4)}-{match.group(4).zfill(4)}-{match.group(5).zfill(2)}{match.group(6).zfill(2)}-{match.group(7).zfill(2)}{match.group(8).zfill(2)}{match.group(9).zfill(2)}{match.group(10).zfill(2)}{match.group(11).zfill(2)}{match.group(12).zfill(2)}" |
| 127 | + guid_string = f"{guid_string.upper()} {match.group(1)}\n" |
| 128 | + guids.append(guid_string) |
| 129 | + else: |
| 130 | + match = re.search(r".*\(([^,]*).*[\"\']([^\"\']*).*0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^\)]*).*", mergedLine) |
| 131 | + if match: |
| 132 | + guid_string = stoh(match.group(2)) |
| 133 | + guid_string = f"{guid_string.zfill(8)}-{match.group(3).zfill(4)}-{match.group(4).zfill(4)}-{match.group(5).zfill(2)}{match.group(6).zfill(2)}-{match.group(7).zfill(2)}{match.group(8).zfill(2)}{match.group(9).zfill(2)}{match.group(10).zfill(2)}{match.group(11).zfill(2)}{match.group(12).zfill(2)}" |
| 134 | + guid_string = f"{guid_string.upper()} {match.group(1)}\n" |
| 135 | + guids.append(guid_string) |
| 136 | + else: |
| 137 | + print(f"Invalid: {mergedLine}") |
| 138 | + elif mergedLine.startswith("DEFINE_OLEGUID"): |
| 139 | + match = re.search(r".*\(([^,]*),0[Xx]([^L,]*).*", mergedLine) |
| 140 | + if match: |
| 141 | + guid_string = f"{match.group(2).zfill(8).upper()}-0000-0000-C000-000000000046 {match.group(1)}\n" |
| 142 | + guids.append(guid_string) |
| 143 | + else: |
| 144 | + print(f"Invalid: {mergedLine}") |
| 145 | + elif mergedLine.startswith("MIDL_DEFINE_GUID"): |
| 146 | + match = re.search(r".*,([^,]*),0[Xx]([^L,]*).*0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^\)]*).*", mergedLine) |
| 147 | + if match: |
| 148 | + guid_string = f"{match.group(2).zfill(8)}-{match.group(3).zfill(4)}-{match.group(4).zfill(4)}-{match.group(5).zfill(2)}{match.group(6).zfill(2)}-{match.group(7).zfill(2)}{match.group(8).zfill(2)}{match.group(9).zfill(2)}{match.group(10).zfill(2)}{match.group(11).zfill(2)}{match.group(12).zfill(2)}" |
| 149 | + guid_string = f"{guid_string.upper()} {match.group(1)}\n" |
| 150 | + guids.append(guid_string) |
| 151 | + else: |
| 152 | + print(f"Invalid: {mergedLine}") |
| 153 | + elif mergedLine.startswith("DECLARE_INTERFACE"): |
| 154 | + match = re.search(r"^.*\(([^,]*)[^\"\']*[\"\']([^\"\']*).*", mergedLine) |
| 155 | + if match: |
| 156 | + guid_string = f"{match.group(2).upper()} {match.group(1)}\n" |
| 157 | + guids.append(guid_string) |
| 158 | + else: |
| 159 | + print(f"Invalid: {mergedLine}") |
| 160 | + elif mergedLine.startswith("#defineGUID_"): |
| 161 | + match = re.search(r"#define\s*([^\s]*)\s*([^\s]*).*", stripped_line) |
| 162 | + if match: |
| 163 | + guid_string = f"{match.group(2).upper()} {match.group(1)}\n" |
| 164 | + guids.append(guid_string) |
| 165 | + else: |
| 166 | + print(f"Invalid: {mergedLine}") |
| 167 | + elif mergedLine.startswith("DEFINE_BLUETOOTH_UUID128"): |
| 168 | + match = re.search(r".*\(([^,]*),([^\)]*).*", mergedLine) |
| 169 | + if match: |
| 170 | + defined = find_define(filename, match.group(2), codec) |
| 171 | + guid_string = f"{defined.zfill(8).upper()}-0000-1000-8000-00805F9B34FB {match.group(1)}\n" |
| 172 | + guids.append(guid_string) |
| 173 | + else: |
| 174 | + print(f"Invalid: {mergedLine}") |
| 175 | + elif mergedLine.startswith("interfaceDECLSPEC_UUID"): |
| 176 | + if not ":" in mergedLine: |
| 177 | + continue # fetch one more line |
| 178 | + match = re.search(r"^[^\"]*\"([^\"]*)[^\)]*\)(DECLSPEC_NOVTABLE)?([^;:]*).*", mergedLine) |
| 179 | + if match: |
| 180 | + guid_string = f"{match.group(1).upper()} {match.group(3)}\n" |
| 181 | + guids.append(guid_string) |
| 182 | + else: |
| 183 | + match = re.search(r"^[^\"]*\"([^\"]*)[^\)]\)(.*);", mergedLine) |
| 184 | + if match: |
| 185 | + guid_string = f"{match.group(1).upper()} {match.group(2)}\n" |
| 186 | + guids.append(guid_string) |
| 187 | + else: |
| 188 | + print(f"Invalid: {mergedLine}") |
| 189 | + elif mergedLine.startswith("DEFINE_MEDIATYPE_GUID"): |
| 190 | + match = re.search(r"^.*\(([^,]*),0[Xx]([^L\)]*).*", mergedLine) |
| 191 | + if match: |
| 192 | + guid_string = f"{match.group(2).zfill(8).upper()}-0000-0010-8000-00AA00389B71 {match.group(1)}\n" |
| 193 | + guids.append(guid_string) |
| 194 | + else: |
| 195 | + match = re.search(r"^.*\(([^,]*),FCC\([\"\']([^\"\']*).*", mergedLine) |
| 196 | + if match: |
| 197 | + guid_string = stoh(match.group(2)[::-1]) |
| 198 | + guid_string = f"{guid_string.zfill(8).upper()}-0000-0010-8000-00AA00389B71 {match.group(1)}\n" |
| 199 | + guids.append(guid_string) |
| 200 | + else: |
| 201 | + match = re.search(r".*\(([^,]*),([^\)]*).*", mergedLine) |
| 202 | + if match: |
| 203 | + defined = find_define(filename, match.group(2), codec) |
| 204 | + guid_string = f"{defined.zfill(8).upper()}-0000-1000-8000-00805F9B34FB {match.group(1)}\n" |
| 205 | + guids.append(guid_string) |
| 206 | + else: |
| 207 | + print(f"Invalid: {mergedLine}") |
| 208 | + elif mergedLine.startswith("DEFINE_BINARY_MEDIATYPE"): |
| 209 | + match = re.search(r"^.*\(([^,]*),0[Xx]([^\)]*).*", mergedLine) |
| 210 | + if match: |
| 211 | + guid_string = f"{match.group(2).zfill(8).upper()}-BF10-48B4-BC18-593DC1DB950F {match.group(1)}\n" |
| 212 | + guids.append(guid_string) |
| 213 | + else: |
| 214 | + match = re.search(r"^.*\(([^,]*),[\"\']([^\"\']*).*", mergedLine) |
| 215 | + if match: |
| 216 | + guid_string = stoh(match.group(2)) |
| 217 | + guid_string = f"{guid_string.zfill(8).upper()}-BF10-48B4-BC18-593DC1DB950F {match.group(1)}\n" |
| 218 | + guids.append(guid_string) |
| 219 | + else: |
| 220 | + match = re.search(r"^.*\(([^,]*),([^\)]*).*", mergedLine) |
| 221 | + if match: |
| 222 | + defined = find_define(filename, match.group(2), codec) |
| 223 | + guid_string = f"{defined.zfill(8).upper()}-BF10-48B4-BC18-593DC1DB950F {match.group(1)}\n" |
| 224 | + guids.append(guid_string) |
| 225 | + else: |
| 226 | + print(f"Invalid: {mergedLine}") |
| 227 | + elif mergedLine.startswith("DEFINE_AVIGUID"): |
| 228 | + match = re.search(r"^.*\(([^,]*),0[Xx]([^,]*).*", mergedLine) |
| 229 | + if match: |
| 230 | + guid_string = f"{match.group(2).zfill(8).upper()}-0000-0000-C000-000000000046 {match.group(1)}\n" |
| 231 | + guids.append(guid_string) |
| 232 | + else: |
| 233 | + print(f"Invalid: {mergedLine}") |
| 234 | + elif mergedLine.startswith("EXTERN_GUID"): |
| 235 | + match = re.search(r".*\(([^,]*).*0[Xx]([^L,]*).*0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^,]*),0[Xx]([^\)]*).*", mergedLine) |
| 236 | + if match: |
| 237 | + guid_string = f"{match.group(2).zfill(8)}-{match.group(3).zfill(4)}-{match.group(4).zfill(4)}-{match.group(5).zfill(2)}{match.group(6).zfill(2)}-{match.group(7).zfill(2)}{match.group(8).zfill(2)}{match.group(9).zfill(2)}{match.group(10).zfill(2)}{match.group(11).zfill(2)}{match.group(12).zfill(2)}" |
| 238 | + guid_string = f"{guid_string.upper()} {match.group(1)}\n" |
| 239 | + guids.append(guid_string) |
| 240 | + else: |
| 241 | + print(f"Invalid: {mergedLine}") |
| 242 | + elif mergedLine.startswith("DEFINE_NWF_GUID"): |
| 243 | + match = re.search(r".*\(([^,]*),([^\+]*)\+([^\)]*).*", mergedLine) |
| 244 | + if match: |
| 245 | + val = hex(int(match.group(2)) + int(match.group(3)) + 0x6cb9a43e).replace("0x", "").upper() |
| 246 | + guid_string = f"{val}-C45F-4039-9FE6-D08CB057184C {match.group(1)}\n" |
| 247 | + guids.append(guid_string) |
| 248 | + else: |
| 249 | + match = re.search(r".*\(([^,]*),([^\)]*).*", mergedLine) |
| 250 | + if match: |
| 251 | + val = hex(int(match.group(2)) + 0x6cb9a43e).replace("0x", "").upper() |
| 252 | + guid_string = f"{val}-C45F-4039-9FE6-D08CB057184C {match.group(1)}\n" |
| 253 | + guids.append(guid_string) |
| 254 | + else: |
| 255 | + print(f"Invalid: {mergedLine}") |
| 256 | + elif mergedLine.startswith("DEFINE_CODECAPI_GUID"): |
| 257 | + if not "(" in mergedLine[20]: |
| 258 | + mergedLine = "" |
| 259 | + continue |
| 260 | + match = re.search(r"^.*\(([^,]*),[\'\"]([^\'\"]*)[\'\"],.*", mergedLine) |
| 261 | + if match: |
| 262 | + guid_string = f"{match.group(2).upper()} CODECAPI_{match.group(1)}\n" |
| 263 | + guids.append(guid_string) |
| 264 | + else: |
| 265 | + print(f"Invalid: {mergedLine}") |
| 266 | + elif mergedLine.startswith("DEFINE_DAOGUID"): |
| 267 | + match = re.search(r"^.*\(([^,]*),0[Xx]([^\)]*).*", mergedLine) |
| 268 | + if match: |
| 269 | + guid_string = f"{match.group(2).upper()}-0000-0010-8000-00AA006D2EA4 {match.group(1)}\n" |
| 270 | + guids.append(guid_string) |
| 271 | + else: |
| 272 | + print(f"Invalid: {mergedLine}") |
| 273 | + elif re.search(r"^#define.*GUID_BUILDER\(", mergedLine): |
| 274 | + match = re.search(r".*\(([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^\)]*).*", mergedLine) |
| 275 | + if match: |
| 276 | + guid_string = f"{match.group(2).zfill(8)}-{match.group(3).zfill(4)}-{match.group(4).zfill(4)}-{match.group(5).zfill(2)}{match.group(6).zfill(2)}-{match.group(7).zfill(2)}{match.group(8).zfill(2)}{match.group(9).zfill(2)}{match.group(10).zfill(2)}{match.group(11).zfill(2)}{match.group(12).zfill(2)}" |
| 277 | + guid_string = f"{guid_string.upper()} {match.group(1)}\n" |
| 278 | + guids.append(guid_string) |
| 279 | + else: |
| 280 | + print(f"Invalid: {mergedLine}") |
| 281 | + |
| 282 | + guid_string = "" |
| 283 | + mergedLine = "" # Reset mergedLine for the next definition |
| 284 | + |
| 285 | + except FileNotFoundError: |
| 286 | + print(f"Error: File '{filename}' not found.") |
| 287 | + |
| 288 | + return guids |
| 289 | + |
| 290 | +# Example usage |
| 291 | + |
| 292 | +def find_files(rootdir): |
| 293 | + files = [] |
| 294 | + for dirpath, dirnames, filenames in os.walk(rootdir): |
| 295 | + for filename in filenames: |
| 296 | + files.append(os.path.join(dirpath, filename)) |
| 297 | + return files |
| 298 | + |
| 299 | +def is_binary_file(filename): |
| 300 | + """ |
| 301 | + Checks if the given file is binary. |
| 302 | +
|
| 303 | + Args: |
| 304 | + filename: The path to the file. |
| 305 | +
|
| 306 | + Returns: |
| 307 | + True if the file is binary, False otherwise. |
| 308 | + """ |
| 309 | + try: |
| 310 | + with open(filename, 'r', encoding='utf-8') as f: |
| 311 | + f.read() |
| 312 | + return False # If no UnicodeDecodeError, it's likely text |
| 313 | + except UnicodeDecodeError: |
| 314 | + return True # If UnicodeDecodeError, it's likely binary |
| 315 | + |
| 316 | +#rootdirectory = "/tmp/Include" |
| 317 | +rootdirectory = "/tmp/Windows Kits" |
| 318 | +outfile = "/tmp/guids-export.txt" |
| 319 | + |
| 320 | +all_files = [] |
| 321 | +if os.path.isfile(rootdirectory): |
| 322 | + all_files.append(rootdirectory) |
| 323 | +else: |
| 324 | + all_files = find_files(rootdirectory) |
| 325 | +all_guids = [] |
| 326 | +for file in all_files: |
| 327 | +# print(f"Processing {file}") |
| 328 | + if is_binary_file(file): |
| 329 | +# print(f"Skipping {file}") |
| 330 | + continue |
| 331 | + found_guids = find_guid_definitions(file) |
| 332 | + all_guids += found_guids |
| 333 | + |
| 334 | +if all_guids: |
| 335 | + all_guids.sort() |
| 336 | + print(f"Found {len(all_guids)} GUID definition:") |
| 337 | + with open(outfile, 'w', encoding='utf-8') as file: |
| 338 | + file.writelines(all_guids) |
| 339 | +else: |
| 340 | + print(f"No GUID definitions found.") |
0 commit comments