regsvr.c 67.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 * Copyright 2009 Vincent Povirk for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#define NONAMELESSUNION
#define NONAMELESSSTRUCT
#define COBJMACROS
#include <stdarg.h>
#include <string.h>

#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
#include "winuser.h"
#include "winreg.h"
#include "winerror.h"

#include "objbase.h"
#include "ocidl.h"
#include "wincodec.h"
35
#include "wincodecsdk.h"
36 37

#include "wine/debug.h"
38
#include "wine/unicode.h"
39

40 41
#include "wincodecs_private.h"

42 43 44 45 46
WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);

/***********************************************************************
 *		interface for self-registering
 */
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
struct decoder_pattern
{
    DWORD length;    /* 0 for end of list */
    DWORD position;
    const BYTE *pattern;
    const BYTE *mask;
    DWORD endofstream;
};

struct regsvr_decoder
{
    CLSID const *clsid;         /* NULL for end of list */
    LPCSTR author;
    LPCSTR friendlyname;
    LPCSTR version;
    GUID const *vendor;
63
    GUID const *container_format;
64 65 66 67 68 69 70 71 72
    LPCSTR mimetypes;
    LPCSTR extensions;
    GUID const * const *formats;
    const struct decoder_pattern *patterns;
};

static HRESULT register_decoders(struct regsvr_decoder const *list);
static HRESULT unregister_decoders(struct regsvr_decoder const *list);

73 74 75 76 77 78 79
struct regsvr_encoder
{
    CLSID const *clsid;         /* NULL for end of list */
    LPCSTR author;
    LPCSTR friendlyname;
    LPCSTR version;
    GUID const *vendor;
80
    GUID const *container_format;
81 82 83 84 85 86 87 88
    LPCSTR mimetypes;
    LPCSTR extensions;
    GUID const * const *formats;
};

static HRESULT register_encoders(struct regsvr_encoder const *list);
static HRESULT unregister_encoders(struct regsvr_encoder const *list);

89 90 91 92 93 94 95 96 97 98 99 100 101
struct regsvr_converter
{
    CLSID const *clsid;         /* NULL for end of list */
    LPCSTR author;
    LPCSTR friendlyname;
    LPCSTR version;
    GUID const *vendor;
    GUID const * const *formats;
};

static HRESULT register_converters(struct regsvr_converter const *list);
static HRESULT unregister_converters(struct regsvr_converter const *list);

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
struct metadata_pattern
{
    DWORD position;
    DWORD length;
    const BYTE *pattern;
    const BYTE *mask;
    DWORD data_offset;
};

struct reader_containers
{
    GUID const *format;
    const struct metadata_pattern *patterns;
};

struct regsvr_metadatareader
{
    CLSID const *clsid;         /* NULL for end of list */
    LPCSTR author;
    LPCSTR friendlyname;
    LPCSTR version;
    LPCSTR specversion;
    GUID const *vendor;
    GUID const *metadata_format;
    DWORD requires_fullstream;
    DWORD supports_padding;
    DWORD requires_fixedsize;
    const struct reader_containers *containers;
};

static HRESULT register_metadatareaders(struct regsvr_metadatareader const *list);
static HRESULT unregister_metadatareaders(struct regsvr_metadatareader const *list);

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
struct regsvr_pixelformat
{
    CLSID const *clsid;         /* NULL for end of list */
    LPCSTR author;
    LPCSTR friendlyname;
    LPCSTR version;
    GUID const *vendor;
    UINT bitsperpixel;
    UINT channelcount;
    BYTE const * const *channelmasks;
    WICPixelFormatNumericRepresentation numericrepresentation;
    UINT supportsalpha;
};

static HRESULT register_pixelformats(struct regsvr_pixelformat const *list);
static HRESULT unregister_pixelformats(struct regsvr_pixelformat const *list);

152 153 154
/***********************************************************************
 *		static string constants
 */
155
static const WCHAR clsid_keyname[] = {
156
    'C', 'L', 'S', 'I', 'D', 0 };
157
static const WCHAR curver_keyname[] = {
158
    'C', 'u', 'r', 'V', 'e', 'r', 0 };
159
static const WCHAR ips_keyname[] = {
160 161
    'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
    0 };
162
static const WCHAR ips32_keyname[] = {
163 164
    'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
    '3', '2', 0 };
165
static const WCHAR progid_keyname[] = {
166
    'P', 'r', 'o', 'g', 'I', 'D', 0 };
167
static const WCHAR viprogid_keyname[] = {
168 169 170
    'V', 'e', 'r', 's', 'i', 'o', 'n', 'I', 'n', 'd', 'e', 'p',
    'e', 'n', 'd', 'e', 'n', 't', 'P', 'r', 'o', 'g', 'I', 'D',
    0 };
171 172 173 174
static const char tmodel_valuename[] = "ThreadingModel";
static const char author_valuename[] = "Author";
static const char friendlyname_valuename[] = "FriendlyName";
static const WCHAR vendor_valuename[] = {'V','e','n','d','o','r',0};
175
static const WCHAR containerformat_valuename[] = {'C','o','n','t','a','i','n','e','r','F','o','r','m','a','t',0};
176 177 178 179 180 181 182 183 184 185 186 187 188
static const char version_valuename[] = "Version";
static const char mimetypes_valuename[] = "MimeTypes";
static const char extensions_valuename[] = "FileExtensions";
static const WCHAR formats_keyname[] = {'F','o','r','m','a','t','s',0};
static const WCHAR patterns_keyname[] = {'P','a','t','t','e','r','n','s',0};
static const WCHAR instance_keyname[] = {'I','n','s','t','a','n','c','e',0};
static const WCHAR clsid_valuename[] = {'C','L','S','I','D',0};
static const char length_valuename[] = "Length";
static const char position_valuename[] = "Position";
static const char pattern_valuename[] = "Pattern";
static const char mask_valuename[] = "Mask";
static const char endofstream_valuename[] = "EndOfStream";
static const WCHAR pixelformats_keyname[] = {'P','i','x','e','l','F','o','r','m','a','t','s',0};
189 190 191 192 193 194 195
static const WCHAR metadataformat_valuename[] = {'M','e','t','a','d','a','t','a','F','o','r','m','a','t',0};
static const char specversion_valuename[] = "SpecVersion";
static const char requiresfullstream_valuename[] = "RequiresFullStream";
static const char supportspadding_valuename[] = "SupportsPadding";
static const char requiresfixedsize_valuename[] = "FixedSize";
static const WCHAR containers_keyname[] = {'C','o','n','t','a','i','n','e','r','s',0};
static const char dataoffset_valuename[] = "DataOffset";
196 197 198 199 200
static const char bitsperpixel_valuename[] = "BitLength";
static const char channelcount_valuename[] = "ChannelCount";
static const char numericrepresentation_valuename[] = "NumericRepresentation";
static const char supportstransparency_valuename[] = "SupportsTransparency";
static const WCHAR channelmasks_keyname[] = {'C','h','a','n','n','e','l','M','a','s','k','s',0};
201

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
/***********************************************************************
 *		register_decoders
 */
static HRESULT register_decoders(struct regsvr_decoder const *list)
{
    LONG res = ERROR_SUCCESS;
    HKEY coclass_key;
    WCHAR buf[39];
    HKEY decoders_key;
    HKEY instance_key;

    res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
			  KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
    if (res == ERROR_SUCCESS)  {
        StringFromGUID2(&CATID_WICBitmapDecoders, buf, 39);
        res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
			      KEY_READ | KEY_WRITE, NULL, &decoders_key, NULL);
        if (res == ERROR_SUCCESS)
        {
            res = RegCreateKeyExW(decoders_key, instance_keyname, 0, NULL, 0,
		              KEY_READ | KEY_WRITE, NULL, &instance_key, NULL);
            if (res != ERROR_SUCCESS) goto error_close_coclass_key;
        }
        if (res != ERROR_SUCCESS)
            RegCloseKey(coclass_key);
    }
    if (res != ERROR_SUCCESS) goto error_return;

    for (; res == ERROR_SUCCESS && list->clsid; ++list) {
	HKEY clsid_key;
	HKEY instance_clsid_key;

	StringFromGUID2(list->clsid, buf, 39);
	res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
			      KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
	if (res != ERROR_SUCCESS) goto error_close_coclass_key;

	StringFromGUID2(list->clsid, buf, 39);
	res = RegCreateKeyExW(instance_key, buf, 0, NULL, 0,
			      KEY_READ | KEY_WRITE, NULL, &instance_clsid_key, NULL);
	if (res == ERROR_SUCCESS) {
	    res = RegSetValueExW(instance_clsid_key, clsid_valuename, 0, REG_SZ,
244
                                 (const BYTE*)buf, 78);
245 246 247 248 249 250
	    RegCloseKey(instance_clsid_key);
	}
	if (res != ERROR_SUCCESS) goto error_close_clsid_key;

        if (list->author) {
	    res = RegSetValueExA(clsid_key, author_valuename, 0, REG_SZ,
251
                                 (const BYTE*)list->author,
252 253 254 255 256 257
				 strlen(list->author) + 1);
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        if (list->friendlyname) {
	    res = RegSetValueExA(clsid_key, friendlyname_valuename, 0, REG_SZ,
258
                                 (const BYTE*)list->friendlyname,
259 260 261 262 263 264 265
				 strlen(list->friendlyname) + 1);
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        if (list->vendor) {
            StringFromGUID2(list->vendor, buf, 39);
	    res = RegSetValueExW(clsid_key, vendor_valuename, 0, REG_SZ,
266
                                 (const BYTE*)buf, 78);
267 268 269
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

270 271 272
        if (list->container_format) {
            StringFromGUID2(list->container_format, buf, 39);
	    res = RegSetValueExW(clsid_key, containerformat_valuename, 0, REG_SZ,
273
                                 (const BYTE*)buf, 78);
274 275 276
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

277 278
        if (list->version) {
	    res = RegSetValueExA(clsid_key, version_valuename, 0, REG_SZ,
279
                                 (const BYTE*)list->version,
280 281 282 283 284 285
				 strlen(list->version) + 1);
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        if (list->mimetypes) {
	    res = RegSetValueExA(clsid_key, mimetypes_valuename, 0, REG_SZ,
286
                                 (const BYTE*)list->mimetypes,
287 288 289 290 291 292
				 strlen(list->mimetypes) + 1);
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        if (list->extensions) {
	    res = RegSetValueExA(clsid_key, extensions_valuename, 0, REG_SZ,
293
                                 (const BYTE*)list->extensions,
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
				 strlen(list->extensions) + 1);
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        if (list->formats) {
            HKEY formats_key;
            GUID const * const *format;

            res = RegCreateKeyExW(clsid_key, formats_keyname, 0, NULL, 0,
                                  KEY_READ | KEY_WRITE, NULL, &formats_key, NULL);
            if (res != ERROR_SUCCESS) goto error_close_clsid_key;
            for (format=list->formats; *format; ++format)
            {
                HKEY format_key;
                StringFromGUID2(*format, buf, 39);
                res = RegCreateKeyExW(formats_key, buf, 0, NULL, 0,
                                      KEY_READ | KEY_WRITE, NULL, &format_key, NULL);
                if (res != ERROR_SUCCESS) break;
                RegCloseKey(format_key);
            }
            RegCloseKey(formats_key);
            if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        if (list->patterns) {
            HKEY patterns_key;
            int i;

            res = RegCreateKeyExW(clsid_key, patterns_keyname, 0, NULL, 0,
                                  KEY_READ | KEY_WRITE, NULL, &patterns_key, NULL);
            if (res != ERROR_SUCCESS) goto error_close_clsid_key;
            for (i=0; list->patterns[i].length; i++)
            {
                HKEY pattern_key;
                static const WCHAR int_format[] = {'%','i',0};
                snprintfW(buf, 39, int_format, i);
                res = RegCreateKeyExW(patterns_key, buf, 0, NULL, 0,
                                      KEY_READ | KEY_WRITE, NULL, &pattern_key, NULL);
                if (res != ERROR_SUCCESS) break;
	        res = RegSetValueExA(pattern_key, length_valuename, 0, REG_DWORD,
334
                                     (const BYTE*)&list->patterns[i].length, 4);
335 336
                if (res == ERROR_SUCCESS)
	            res = RegSetValueExA(pattern_key, position_valuename, 0, REG_DWORD,
337
                                         (const BYTE*)&list->patterns[i].position, 4);
338 339 340 341 342 343 344 345 346 347
                if (res == ERROR_SUCCESS)
	            res = RegSetValueExA(pattern_key, pattern_valuename, 0, REG_BINARY,
				         list->patterns[i].pattern,
				         list->patterns[i].length);
                if (res == ERROR_SUCCESS)
	            res = RegSetValueExA(pattern_key, mask_valuename, 0, REG_BINARY,
				         list->patterns[i].mask,
				         list->patterns[i].length);
                if (res == ERROR_SUCCESS)
	            res = RegSetValueExA(pattern_key, endofstream_valuename, 0, REG_DWORD,
348
                                         (const BYTE*)&list->patterns[i].endofstream, 4);
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
                RegCloseKey(pattern_key);
            }
            RegCloseKey(patterns_key);
            if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

    error_close_clsid_key:
	RegCloseKey(clsid_key);
    }

error_close_coclass_key:
    RegCloseKey(instance_key);
    RegCloseKey(decoders_key);
    RegCloseKey(coclass_key);
error_return:
    return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
}

/***********************************************************************
 *		unregister_decoders
 */
static HRESULT unregister_decoders(struct regsvr_decoder const *list)
{
    LONG res = ERROR_SUCCESS;
    HKEY coclass_key;
    WCHAR buf[39];
    HKEY decoders_key;
    HKEY instance_key;

    res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
			KEY_READ | KEY_WRITE, &coclass_key);
    if (res == ERROR_FILE_NOT_FOUND) return S_OK;

    if (res == ERROR_SUCCESS)  {
        StringFromGUID2(&CATID_WICBitmapDecoders, buf, 39);
        res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
			      KEY_READ | KEY_WRITE, NULL, &decoders_key, NULL);
        if (res == ERROR_SUCCESS)
        {
            res = RegCreateKeyExW(decoders_key, instance_keyname, 0, NULL, 0,
		              KEY_READ | KEY_WRITE, NULL, &instance_key, NULL);
            if (res != ERROR_SUCCESS) goto error_close_coclass_key;
        }
        if (res != ERROR_SUCCESS)
            RegCloseKey(coclass_key);
    }
    if (res != ERROR_SUCCESS) goto error_return;

    for (; res == ERROR_SUCCESS && list->clsid; ++list) {
	StringFromGUID2(list->clsid, buf, 39);

	res = RegDeleteTreeW(coclass_key, buf);
	if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
	if (res != ERROR_SUCCESS) goto error_close_coclass_key;

	res = RegDeleteTreeW(instance_key, buf);
	if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
	if (res != ERROR_SUCCESS) goto error_close_coclass_key;
    }

error_close_coclass_key:
    RegCloseKey(instance_key);
    RegCloseKey(decoders_key);
    RegCloseKey(coclass_key);
error_return:
    return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
}

417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
/***********************************************************************
 *		register_encoders
 */
static HRESULT register_encoders(struct regsvr_encoder const *list)
{
    LONG res = ERROR_SUCCESS;
    HKEY coclass_key;
    WCHAR buf[39];
    HKEY encoders_key;
    HKEY instance_key;

    res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
			  KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
    if (res == ERROR_SUCCESS)  {
        StringFromGUID2(&CATID_WICBitmapEncoders, buf, 39);
        res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
			      KEY_READ | KEY_WRITE, NULL, &encoders_key, NULL);
        if (res == ERROR_SUCCESS)
        {
            res = RegCreateKeyExW(encoders_key, instance_keyname, 0, NULL, 0,
		              KEY_READ | KEY_WRITE, NULL, &instance_key, NULL);
            if (res != ERROR_SUCCESS) goto error_close_coclass_key;
        }
        if (res != ERROR_SUCCESS)
            RegCloseKey(coclass_key);
    }
    if (res != ERROR_SUCCESS) goto error_return;

    for (; res == ERROR_SUCCESS && list->clsid; ++list) {
	HKEY clsid_key;
	HKEY instance_clsid_key;

	StringFromGUID2(list->clsid, buf, 39);
	res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
			      KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
	if (res != ERROR_SUCCESS) goto error_close_coclass_key;

	StringFromGUID2(list->clsid, buf, 39);
	res = RegCreateKeyExW(instance_key, buf, 0, NULL, 0,
			      KEY_READ | KEY_WRITE, NULL, &instance_clsid_key, NULL);
	if (res == ERROR_SUCCESS) {
	    res = RegSetValueExW(instance_clsid_key, clsid_valuename, 0, REG_SZ,
459
                                 (const BYTE*)buf, 78);
460 461 462 463 464 465
	    RegCloseKey(instance_clsid_key);
	}
	if (res != ERROR_SUCCESS) goto error_close_clsid_key;

        if (list->author) {
	    res = RegSetValueExA(clsid_key, author_valuename, 0, REG_SZ,
466
                                 (const BYTE*)list->author,
467 468 469 470 471 472
				 strlen(list->author) + 1);
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        if (list->friendlyname) {
	    res = RegSetValueExA(clsid_key, friendlyname_valuename, 0, REG_SZ,
473
                                 (const BYTE*)list->friendlyname,
474 475 476 477 478 479 480
				 strlen(list->friendlyname) + 1);
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        if (list->vendor) {
            StringFromGUID2(list->vendor, buf, 39);
	    res = RegSetValueExW(clsid_key, vendor_valuename, 0, REG_SZ,
481
                                 (const BYTE*)buf, 78);
482 483 484
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

485 486 487
        if (list->container_format) {
            StringFromGUID2(list->container_format, buf, 39);
	    res = RegSetValueExW(clsid_key, containerformat_valuename, 0, REG_SZ,
488
                                 (const BYTE*)buf, 78);
489 490 491
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

492 493
        if (list->version) {
	    res = RegSetValueExA(clsid_key, version_valuename, 0, REG_SZ,
494
                                 (const BYTE*)list->version,
495 496 497 498 499 500
				 strlen(list->version) + 1);
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        if (list->mimetypes) {
	    res = RegSetValueExA(clsid_key, mimetypes_valuename, 0, REG_SZ,
501
                                 (const BYTE*)list->mimetypes,
502 503 504 505 506 507
				 strlen(list->mimetypes) + 1);
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        if (list->extensions) {
	    res = RegSetValueExA(clsid_key, extensions_valuename, 0, REG_SZ,
508
                                 (const BYTE*)list->extensions,
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
				 strlen(list->extensions) + 1);
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        if (list->formats) {
            HKEY formats_key;
            GUID const * const *format;

            res = RegCreateKeyExW(clsid_key, formats_keyname, 0, NULL, 0,
                                  KEY_READ | KEY_WRITE, NULL, &formats_key, NULL);
            if (res != ERROR_SUCCESS) goto error_close_clsid_key;
            for (format=list->formats; *format; ++format)
            {
                HKEY format_key;
                StringFromGUID2(*format, buf, 39);
                res = RegCreateKeyExW(formats_key, buf, 0, NULL, 0,
                                      KEY_READ | KEY_WRITE, NULL, &format_key, NULL);
                if (res != ERROR_SUCCESS) break;
                RegCloseKey(format_key);
            }
            RegCloseKey(formats_key);
            if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

    error_close_clsid_key:
	RegCloseKey(clsid_key);
    }

error_close_coclass_key:
    RegCloseKey(instance_key);
    RegCloseKey(encoders_key);
    RegCloseKey(coclass_key);
error_return:
    return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
}

/***********************************************************************
 *		unregister_encoders
 */
static HRESULT unregister_encoders(struct regsvr_encoder const *list)
{
    LONG res = ERROR_SUCCESS;
    HKEY coclass_key;
    WCHAR buf[39];
    HKEY encoders_key;
    HKEY instance_key;

    res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
			KEY_READ | KEY_WRITE, &coclass_key);
    if (res == ERROR_FILE_NOT_FOUND) return S_OK;

    if (res == ERROR_SUCCESS)  {
        StringFromGUID2(&CATID_WICBitmapEncoders, buf, 39);
        res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
			      KEY_READ | KEY_WRITE, NULL, &encoders_key, NULL);
        if (res == ERROR_SUCCESS)
        {
            res = RegCreateKeyExW(encoders_key, instance_keyname, 0, NULL, 0,
		              KEY_READ | KEY_WRITE, NULL, &instance_key, NULL);
            if (res != ERROR_SUCCESS) goto error_close_coclass_key;
        }
        if (res != ERROR_SUCCESS)
            RegCloseKey(coclass_key);
    }
    if (res != ERROR_SUCCESS) goto error_return;

    for (; res == ERROR_SUCCESS && list->clsid; ++list) {
	StringFromGUID2(list->clsid, buf, 39);

	res = RegDeleteTreeW(coclass_key, buf);
	if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
	if (res != ERROR_SUCCESS) goto error_close_coclass_key;

	res = RegDeleteTreeW(instance_key, buf);
	if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
	if (res != ERROR_SUCCESS) goto error_close_coclass_key;
    }

error_close_coclass_key:
    RegCloseKey(instance_key);
    RegCloseKey(encoders_key);
    RegCloseKey(coclass_key);
error_return:
    return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
}

595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
/***********************************************************************
 *		register_converters
 */
static HRESULT register_converters(struct regsvr_converter const *list)
{
    LONG res = ERROR_SUCCESS;
    HKEY coclass_key;
    WCHAR buf[39];
    HKEY converters_key;
    HKEY instance_key;

    res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
			  KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
    if (res == ERROR_SUCCESS)  {
        StringFromGUID2(&CATID_WICFormatConverters, buf, 39);
        res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
			      KEY_READ | KEY_WRITE, NULL, &converters_key, NULL);
        if (res == ERROR_SUCCESS)
        {
            res = RegCreateKeyExW(converters_key, instance_keyname, 0, NULL, 0,
		              KEY_READ | KEY_WRITE, NULL, &instance_key, NULL);
            if (res != ERROR_SUCCESS) goto error_close_coclass_key;
        }
        if (res != ERROR_SUCCESS)
            RegCloseKey(coclass_key);
    }
    if (res != ERROR_SUCCESS) goto error_return;

    for (; res == ERROR_SUCCESS && list->clsid; ++list) {
	HKEY clsid_key;
	HKEY instance_clsid_key;

	StringFromGUID2(list->clsid, buf, 39);
	res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
			      KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
	if (res != ERROR_SUCCESS) goto error_close_coclass_key;

	StringFromGUID2(list->clsid, buf, 39);
	res = RegCreateKeyExW(instance_key, buf, 0, NULL, 0,
			      KEY_READ | KEY_WRITE, NULL, &instance_clsid_key, NULL);
	if (res == ERROR_SUCCESS) {
	    res = RegSetValueExW(instance_clsid_key, clsid_valuename, 0, REG_SZ,
637
                                 (const BYTE*)buf, 78);
638 639 640 641 642 643
	    RegCloseKey(instance_clsid_key);
	}
	if (res != ERROR_SUCCESS) goto error_close_clsid_key;

        if (list->author) {
	    res = RegSetValueExA(clsid_key, author_valuename, 0, REG_SZ,
644
                                 (const BYTE*)list->author,
645 646 647 648 649 650
				 strlen(list->author) + 1);
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        if (list->friendlyname) {
	    res = RegSetValueExA(clsid_key, friendlyname_valuename, 0, REG_SZ,
651
                                 (const BYTE*)list->friendlyname,
652 653 654 655 656 657 658
				 strlen(list->friendlyname) + 1);
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        if (list->vendor) {
            StringFromGUID2(list->vendor, buf, 39);
	    res = RegSetValueExW(clsid_key, vendor_valuename, 0, REG_SZ,
659
                                 (const BYTE*)buf, 78);
660 661 662 663 664
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        if (list->version) {
	    res = RegSetValueExA(clsid_key, version_valuename, 0, REG_SZ,
665
                                 (const BYTE*)list->version,
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
				 strlen(list->version) + 1);
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        if (list->formats) {
            HKEY formats_key;
            GUID const * const *format;

            res = RegCreateKeyExW(clsid_key, pixelformats_keyname, 0, NULL, 0,
                                  KEY_READ | KEY_WRITE, NULL, &formats_key, NULL);
            if (res != ERROR_SUCCESS) goto error_close_clsid_key;
            for (format=list->formats; *format; ++format)
            {
                HKEY format_key;
                StringFromGUID2(*format, buf, 39);
                res = RegCreateKeyExW(formats_key, buf, 0, NULL, 0,
                                      KEY_READ | KEY_WRITE, NULL, &format_key, NULL);
                if (res != ERROR_SUCCESS) break;
                RegCloseKey(format_key);
            }
            RegCloseKey(formats_key);
            if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

    error_close_clsid_key:
	RegCloseKey(clsid_key);
    }

error_close_coclass_key:
    RegCloseKey(instance_key);
    RegCloseKey(converters_key);
    RegCloseKey(coclass_key);
error_return:
    return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
}

/***********************************************************************
 *		unregister_converters
 */
static HRESULT unregister_converters(struct regsvr_converter const *list)
{
    LONG res = ERROR_SUCCESS;
    HKEY coclass_key;
    WCHAR buf[39];
    HKEY converters_key;
    HKEY instance_key;

    res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
			KEY_READ | KEY_WRITE, &coclass_key);
    if (res == ERROR_FILE_NOT_FOUND) return S_OK;

    if (res == ERROR_SUCCESS)  {
        StringFromGUID2(&CATID_WICFormatConverters, buf, 39);
        res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
			      KEY_READ | KEY_WRITE, NULL, &converters_key, NULL);
        if (res == ERROR_SUCCESS)
        {
            res = RegCreateKeyExW(converters_key, instance_keyname, 0, NULL, 0,
		              KEY_READ | KEY_WRITE, NULL, &instance_key, NULL);
            if (res != ERROR_SUCCESS) goto error_close_coclass_key;
        }
        if (res != ERROR_SUCCESS)
            RegCloseKey(coclass_key);
    }
    if (res != ERROR_SUCCESS) goto error_return;

    for (; res == ERROR_SUCCESS && list->clsid; ++list) {
	StringFromGUID2(list->clsid, buf, 39);

	res = RegDeleteTreeW(coclass_key, buf);
	if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
	if (res != ERROR_SUCCESS) goto error_close_coclass_key;

	res = RegDeleteTreeW(instance_key, buf);
	if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
	if (res != ERROR_SUCCESS) goto error_close_coclass_key;
    }

error_close_coclass_key:
    RegCloseKey(instance_key);
    RegCloseKey(converters_key);
    RegCloseKey(coclass_key);
error_return:
    return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
}

752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
/***********************************************************************
 *		register_metadatareaders
 */
static HRESULT register_metadatareaders(struct regsvr_metadatareader const *list)
{
    LONG res = ERROR_SUCCESS;
    HKEY coclass_key;
    WCHAR buf[39];
    HKEY readers_key;
    HKEY instance_key;

    res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
			  KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
    if (res == ERROR_SUCCESS)  {
        StringFromGUID2(&CATID_WICMetadataReader, buf, 39);
        res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
			      KEY_READ | KEY_WRITE, NULL, &readers_key, NULL);
        if (res == ERROR_SUCCESS)
        {
            res = RegCreateKeyExW(readers_key, instance_keyname, 0, NULL, 0,
		              KEY_READ | KEY_WRITE, NULL, &instance_key, NULL);
            if (res != ERROR_SUCCESS) goto error_close_coclass_key;
        }
        if (res != ERROR_SUCCESS)
            RegCloseKey(coclass_key);
    }
    if (res != ERROR_SUCCESS) goto error_return;

    for (; res == ERROR_SUCCESS && list->clsid; ++list) {
	HKEY clsid_key;
	HKEY instance_clsid_key;

	StringFromGUID2(list->clsid, buf, 39);
	res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
			      KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
	if (res != ERROR_SUCCESS) goto error_close_coclass_key;

	StringFromGUID2(list->clsid, buf, 39);
	res = RegCreateKeyExW(instance_key, buf, 0, NULL, 0,
			      KEY_READ | KEY_WRITE, NULL, &instance_clsid_key, NULL);
	if (res == ERROR_SUCCESS) {
	    res = RegSetValueExW(instance_clsid_key, clsid_valuename, 0, REG_SZ,
794
                                 (const BYTE*)buf, 78);
795 796 797 798 799 800
	    RegCloseKey(instance_clsid_key);
	}
	if (res != ERROR_SUCCESS) goto error_close_clsid_key;

        if (list->author) {
	    res = RegSetValueExA(clsid_key, author_valuename, 0, REG_SZ,
801
                                 (const BYTE*)list->author,
802 803 804 805 806 807
				 strlen(list->author) + 1);
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        if (list->friendlyname) {
	    res = RegSetValueExA(clsid_key, friendlyname_valuename, 0, REG_SZ,
808
                                 (const BYTE*)list->friendlyname,
809 810 811 812 813 814 815
				 strlen(list->friendlyname) + 1);
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        if (list->vendor) {
            StringFromGUID2(list->vendor, buf, 39);
	    res = RegSetValueExW(clsid_key, vendor_valuename, 0, REG_SZ,
816
                                 (const BYTE*)buf, 78);
817 818 819
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

820
        if (list->metadata_format) {
821 822
            StringFromGUID2(list->metadata_format, buf, 39);
	    res = RegSetValueExW(clsid_key, metadataformat_valuename, 0, REG_SZ,
823
                                 (const BYTE*)buf, 78);
824 825 826 827 828
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        if (list->version) {
	    res = RegSetValueExA(clsid_key, version_valuename, 0, REG_SZ,
829
                                 (const BYTE*)list->version,
830 831 832 833 834 835
				 strlen(list->version) + 1);
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        if (list->specversion) {
	    res = RegSetValueExA(clsid_key, specversion_valuename, 0, REG_SZ,
836
                                 (const BYTE*)list->version,
837 838 839 840 841
				 strlen(list->version) + 1);
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        res = RegSetValueExA(clsid_key, requiresfullstream_valuename, 0, REG_DWORD,
842
                             (const BYTE*)&list->requires_fullstream, 4);
843 844 845
        if (res != ERROR_SUCCESS) goto error_close_clsid_key;

        res = RegSetValueExA(clsid_key, supportspadding_valuename, 0, REG_DWORD,
846
                             (const BYTE*)&list->supports_padding, 4);
847 848 849 850
        if (res != ERROR_SUCCESS) goto error_close_clsid_key;

        if (list->requires_fixedsize) {
	    res = RegSetValueExA(clsid_key, requiresfixedsize_valuename, 0, REG_DWORD,
851
                                 (const BYTE*)&list->requires_fixedsize, 4);
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
	    if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        if (list->containers) {
            HKEY containers_key;
            const struct reader_containers *container;

            res = RegCreateKeyExW(clsid_key, containers_keyname, 0, NULL, 0,
                                  KEY_READ | KEY_WRITE, NULL, &containers_key, NULL);
            if (res != ERROR_SUCCESS) goto error_close_clsid_key;
            for (container=list->containers; container->format; ++container)
            {
                HKEY format_key;
                int i;
                StringFromGUID2(container->format, buf, 39);
                res = RegCreateKeyExW(containers_key, buf, 0, NULL, 0,
                                      KEY_READ | KEY_WRITE, NULL, &format_key, NULL);
                if (res != ERROR_SUCCESS) break;

                for (i=0; container->patterns[i].length; i++)
                {
                    HKEY pattern_key;
                    static const WCHAR int_format[] = {'%','i',0};
                    snprintfW(buf, 39, int_format, i);
                    res = RegCreateKeyExW(format_key, buf, 0, NULL, 0,
                                          KEY_READ | KEY_WRITE, NULL, &pattern_key, NULL);
                    if (res != ERROR_SUCCESS) break;
                    res = RegSetValueExA(pattern_key, position_valuename, 0, REG_DWORD,
880
                                         (const BYTE*)&container->patterns[i].position, 4);
881 882 883 884 885 886 887 888 889 890
                    if (res == ERROR_SUCCESS)
                        res = RegSetValueExA(pattern_key, pattern_valuename, 0, REG_BINARY,
                                             container->patterns[i].pattern,
                                             container->patterns[i].length);
                    if (res == ERROR_SUCCESS)
                        res = RegSetValueExA(pattern_key, mask_valuename, 0, REG_BINARY,
                                             container->patterns[i].mask,
                                             container->patterns[i].length);
                    if (res == ERROR_SUCCESS && container->patterns[i].data_offset)
                        res = RegSetValueExA(pattern_key, dataoffset_valuename, 0, REG_DWORD,
891
                                             (const BYTE*)&container->patterns[i].data_offset, 4);
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
                    RegCloseKey(pattern_key);
                }

                RegCloseKey(format_key);
            }
            RegCloseKey(containers_key);
        }

    error_close_clsid_key:
	RegCloseKey(clsid_key);
    }

error_close_coclass_key:
    RegCloseKey(instance_key);
    RegCloseKey(readers_key);
    RegCloseKey(coclass_key);
error_return:
    return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
}

/***********************************************************************
 *		unregister_metadatareaders
 */
static HRESULT unregister_metadatareaders(struct regsvr_metadatareader const *list)
{
    LONG res = ERROR_SUCCESS;
    HKEY coclass_key;
    WCHAR buf[39];
    HKEY readers_key;
    HKEY instance_key;

    res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
			KEY_READ | KEY_WRITE, &coclass_key);
    if (res == ERROR_FILE_NOT_FOUND) return S_OK;

    if (res == ERROR_SUCCESS)  {
        StringFromGUID2(&CATID_WICMetadataReader, buf, 39);
        res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
			      KEY_READ | KEY_WRITE, NULL, &readers_key, NULL);
        if (res == ERROR_SUCCESS)
        {
            res = RegCreateKeyExW(readers_key, instance_keyname, 0, NULL, 0,
		              KEY_READ | KEY_WRITE, NULL, &instance_key, NULL);
            if (res != ERROR_SUCCESS) goto error_close_coclass_key;
        }
        if (res != ERROR_SUCCESS)
            RegCloseKey(coclass_key);
    }
    if (res != ERROR_SUCCESS) goto error_return;

    for (; res == ERROR_SUCCESS && list->clsid; ++list) {
	StringFromGUID2(list->clsid, buf, 39);

	res = RegDeleteTreeW(coclass_key, buf);
	if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
	if (res != ERROR_SUCCESS) goto error_close_coclass_key;

	res = RegDeleteTreeW(instance_key, buf);
	if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
	if (res != ERROR_SUCCESS) goto error_close_coclass_key;
    }

error_close_coclass_key:
    RegCloseKey(instance_key);
    RegCloseKey(readers_key);
    RegCloseKey(coclass_key);
error_return:
    return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
}

962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
/***********************************************************************
 *                register_pixelformats
 */
static HRESULT register_pixelformats(struct regsvr_pixelformat const *list)
{
    LONG res = ERROR_SUCCESS;
    HKEY coclass_key;
    WCHAR buf[39];
    HKEY formats_key;
    HKEY instance_key;

    res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
                          KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
    if (res == ERROR_SUCCESS)  {
        StringFromGUID2(&CATID_WICPixelFormats, buf, 39);
        res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
                              KEY_READ | KEY_WRITE, NULL, &formats_key, NULL);
        if (res == ERROR_SUCCESS)
        {
            res = RegCreateKeyExW(formats_key, instance_keyname, 0, NULL, 0,
                              KEY_READ | KEY_WRITE, NULL, &instance_key, NULL);
            if (res != ERROR_SUCCESS) goto error_close_coclass_key;
        }
        if (res != ERROR_SUCCESS)
            RegCloseKey(coclass_key);
    }
    if (res != ERROR_SUCCESS) goto error_return;

    for (; res == ERROR_SUCCESS && list->clsid; ++list) {
        HKEY clsid_key;
        HKEY instance_clsid_key;

        StringFromGUID2(list->clsid, buf, 39);
        res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
                              KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
        if (res != ERROR_SUCCESS) goto error_close_coclass_key;

        StringFromGUID2(list->clsid, buf, 39);
        res = RegCreateKeyExW(instance_key, buf, 0, NULL, 0,
                              KEY_READ | KEY_WRITE, NULL, &instance_clsid_key, NULL);
        if (res == ERROR_SUCCESS) {
            res = RegSetValueExW(instance_clsid_key, clsid_valuename, 0, REG_SZ,
1004
                                 (const BYTE*)buf, 78);
1005 1006 1007 1008 1009 1010
            RegCloseKey(instance_clsid_key);
        }
        if (res != ERROR_SUCCESS) goto error_close_clsid_key;

        if (list->author) {
            res = RegSetValueExA(clsid_key, author_valuename, 0, REG_SZ,
1011
                                 (const BYTE*)list->author,
1012 1013 1014 1015 1016 1017
                                 strlen(list->author) + 1);
            if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        if (list->friendlyname) {
            res = RegSetValueExA(clsid_key, friendlyname_valuename, 0, REG_SZ,
1018
                                 (const BYTE*)list->friendlyname,
1019 1020 1021 1022 1023 1024 1025
                                 strlen(list->friendlyname) + 1);
            if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        if (list->vendor) {
            StringFromGUID2(list->vendor, buf, 39);
            res = RegSetValueExW(clsid_key, vendor_valuename, 0, REG_SZ,
1026
                                 (const BYTE*)buf, 78);
1027 1028 1029 1030 1031
            if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        if (list->version) {
            res = RegSetValueExA(clsid_key, version_valuename, 0, REG_SZ,
1032
                                 (const BYTE*)list->version,
1033 1034 1035 1036 1037
                                 strlen(list->version) + 1);
            if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

        res = RegSetValueExA(clsid_key, bitsperpixel_valuename, 0, REG_DWORD,
1038
                             (const BYTE*)&list->bitsperpixel, 4);
1039 1040 1041
        if (res != ERROR_SUCCESS) goto error_close_clsid_key;

        res = RegSetValueExA(clsid_key, channelcount_valuename, 0, REG_DWORD,
1042
                             (const BYTE*)&list->channelcount, 4);
1043 1044 1045
        if (res != ERROR_SUCCESS) goto error_close_clsid_key;

        res = RegSetValueExA(clsid_key, numericrepresentation_valuename, 0, REG_DWORD,
1046
                             (const BYTE*)&list->numericrepresentation, 4);
1047 1048 1049
        if (res != ERROR_SUCCESS) goto error_close_clsid_key;

        res = RegSetValueExA(clsid_key, supportstransparency_valuename, 0, REG_DWORD,
1050
                             (const BYTE*)&list->supportsalpha, 4);
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
        if (res != ERROR_SUCCESS) goto error_close_clsid_key;

        if (list->channelmasks) {
            HKEY masks_key;
            UINT i, mask_size;
            WCHAR mask_valuename[11];
            const WCHAR valuename_format[] = {'%','d',0};

            mask_size = (list->bitsperpixel + 7)/8;

            res = RegCreateKeyExW(clsid_key, channelmasks_keyname, 0, NULL, 0,
                                  KEY_READ | KEY_WRITE, NULL, &masks_key, NULL);
            if (res != ERROR_SUCCESS) goto error_close_clsid_key;
            for (i=0; i < list->channelcount; i++)
            {
                sprintfW(mask_valuename, valuename_format, i);
                res = RegSetValueExW(masks_key, mask_valuename, 0, REG_BINARY,
                                     list->channelmasks[i], mask_size);
                if (res != ERROR_SUCCESS) break;
            }
            RegCloseKey(masks_key);
            if (res != ERROR_SUCCESS) goto error_close_clsid_key;
        }

    error_close_clsid_key:
        RegCloseKey(clsid_key);
    }

error_close_coclass_key:
    RegCloseKey(instance_key);
    RegCloseKey(formats_key);
    RegCloseKey(coclass_key);
error_return:
    return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
}

/***********************************************************************
 *                unregister_pixelformats
 */
static HRESULT unregister_pixelformats(struct regsvr_pixelformat const *list)
{
    LONG res = ERROR_SUCCESS;
    HKEY coclass_key;
    WCHAR buf[39];
    HKEY formats_key;
    HKEY instance_key;

    res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
                        KEY_READ | KEY_WRITE, &coclass_key);
    if (res == ERROR_FILE_NOT_FOUND) return S_OK;

    if (res == ERROR_SUCCESS)  {
        StringFromGUID2(&CATID_WICPixelFormats, buf, 39);
        res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
                              KEY_READ | KEY_WRITE, NULL, &formats_key, NULL);
        if (res == ERROR_SUCCESS)
        {
            res = RegCreateKeyExW(formats_key, instance_keyname, 0, NULL, 0,
                              KEY_READ | KEY_WRITE, NULL, &instance_key, NULL);
            if (res != ERROR_SUCCESS) goto error_close_coclass_key;
        }
        if (res != ERROR_SUCCESS)
            RegCloseKey(coclass_key);
    }
    if (res != ERROR_SUCCESS) goto error_return;

    for (; res == ERROR_SUCCESS && list->clsid; ++list) {
        StringFromGUID2(list->clsid, buf, 39);

        res = RegDeleteTreeW(coclass_key, buf);
        if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
        if (res != ERROR_SUCCESS) goto error_close_coclass_key;

        res = RegDeleteTreeW(instance_key, buf);
        if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
        if (res != ERROR_SUCCESS) goto error_close_coclass_key;
    }

error_close_coclass_key:
    RegCloseKey(instance_key);
    RegCloseKey(formats_key);
    RegCloseKey(coclass_key);
error_return:
    return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
}

1137 1138 1139
/***********************************************************************
 *		decoder list
 */
1140
static const BYTE mask_all[] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
1141

1142 1143 1144 1145 1146 1147 1148 1149
static const BYTE bmp_magic[] = {0x42,0x4d};

static GUID const * const bmp_formats[] = {
    &GUID_WICPixelFormat1bppIndexed,
    &GUID_WICPixelFormat2bppIndexed,
    &GUID_WICPixelFormat4bppIndexed,
    &GUID_WICPixelFormat8bppIndexed,
    &GUID_WICPixelFormat16bppBGR555,
1150
    &GUID_WICPixelFormat16bppBGR565,
1151 1152
    &GUID_WICPixelFormat24bppBGR,
    &GUID_WICPixelFormat32bppBGR,
1153
    &GUID_WICPixelFormat32bppBGRA,
1154 1155 1156 1157 1158 1159 1160 1161
    NULL
};

static struct decoder_pattern const bmp_patterns[] = {
    {2,0,bmp_magic,mask_all,0},
    {0}
};

1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
static const BYTE gif87a_magic[6] = "GIF87a";
static const BYTE gif89a_magic[6] = "GIF89a";

static GUID const * const gif_formats[] = {
    &GUID_WICPixelFormat8bppIndexed,
    NULL
};

static struct decoder_pattern const gif_patterns[] = {
    {6,0,gif87a_magic,mask_all,0},
    {6,0,gif89a_magic,mask_all,0},
    {0}
};

1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
static const BYTE ico_magic[] = {00,00,01,00};

static GUID const * const ico_formats[] = {
    &GUID_WICPixelFormat32bppBGRA,
    NULL
};

static struct decoder_pattern const ico_patterns[] = {
    {4,0,ico_magic,mask_all,0},
    {0}
};

1188
static const BYTE jpeg_magic[] = {0xff, 0xd8};
1189 1190 1191

static GUID const * const jpeg_formats[] = {
    &GUID_WICPixelFormat24bppBGR,
1192
    &GUID_WICPixelFormat32bppCMYK,
1193 1194 1195 1196 1197
    &GUID_WICPixelFormat8bppGray,
    NULL
};

static struct decoder_pattern const jpeg_patterns[] = {
1198
    {2,0,jpeg_magic,mask_all,0},
1199 1200 1201
    {0}
};

1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
static const BYTE png_magic[] = {137,80,78,71,13,10,26,10};

static GUID const * const png_formats[] = {
    &GUID_WICPixelFormatBlackWhite,
    &GUID_WICPixelFormat2bppGray,
    &GUID_WICPixelFormat4bppGray,
    &GUID_WICPixelFormat8bppGray,
    &GUID_WICPixelFormat16bppGray,
    &GUID_WICPixelFormat32bppBGRA,
    &GUID_WICPixelFormat64bppRGBA,
    &GUID_WICPixelFormat1bppIndexed,
    &GUID_WICPixelFormat2bppIndexed,
    &GUID_WICPixelFormat4bppIndexed,
    &GUID_WICPixelFormat8bppIndexed,
    &GUID_WICPixelFormat24bppBGR,
    &GUID_WICPixelFormat48bppRGB,
    NULL
};

static struct decoder_pattern const png_patterns[] = {
    {8,0,png_magic,mask_all,0},
    {0}
};

1226 1227 1228
static const BYTE tiff_magic_le[] = {0x49,0x49,42,0};
static const BYTE tiff_magic_be[] = {0x4d,0x4d,0,42};

1229
static GUID const * const tiff_decode_formats[] = {
1230 1231 1232 1233 1234
    &GUID_WICPixelFormatBlackWhite,
    &GUID_WICPixelFormat4bppGray,
    &GUID_WICPixelFormat8bppGray,
    &GUID_WICPixelFormat4bppIndexed,
    &GUID_WICPixelFormat8bppIndexed,
1235
    &GUID_WICPixelFormat24bppBGR,
1236 1237 1238
    &GUID_WICPixelFormat32bppBGR,
    &GUID_WICPixelFormat32bppBGRA,
    &GUID_WICPixelFormat32bppPBGRA,
1239
    &GUID_WICPixelFormat48bppRGB,
1240 1241
    &GUID_WICPixelFormat64bppRGBA,
    &GUID_WICPixelFormat64bppPRGBA,
1242 1243 1244 1245 1246 1247 1248 1249 1250
    NULL
};

static struct decoder_pattern const tiff_patterns[] = {
    {4,0,tiff_magic_le,mask_all,0},
    {4,0,tiff_magic_be,mask_all,0},
    {0}
};

1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
static const BYTE tga_footer_magic[18] = "TRUEVISION-XFILE.";

static const BYTE tga_indexed_magic[18] = {0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,8,0};
static const BYTE tga_indexed_mask[18] = {0,0xff,0xf7,0,0,0,0,0,0,0,0,0,0,0,0,0,0xff,0xcf};

static const BYTE tga_truecolor_magic[18] = {0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
static const BYTE tga_truecolor_mask[18] = {0,0xff,0xf7,0,0,0,0,0,0,0,0,0,0,0,0,0,0x87,0xc0};

static const BYTE tga_grayscale_magic[18] = {0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,8,0};
static const BYTE tga_grayscale_mask[18] = {0,0xff,0xf7,0,0,0,0,0,0,0,0,0,0,0,0,0,0xff,0xcf};

static GUID const * const tga_formats[] = {
    &GUID_WICPixelFormat8bppGray,
    &GUID_WICPixelFormat8bppIndexed,
    &GUID_WICPixelFormat16bppGray,
    &GUID_WICPixelFormat16bppBGR555,
    &GUID_WICPixelFormat24bppBGR,
    &GUID_WICPixelFormat32bppBGRA,
    &GUID_WICPixelFormat32bppPBGRA,
    NULL
};

static struct decoder_pattern const tga_patterns[] = {
    {18,18,tga_footer_magic,mask_all,1},
    {18,0,tga_indexed_magic,tga_indexed_mask,0},
    {18,0,tga_truecolor_magic,tga_truecolor_mask,0},
    {18,0,tga_grayscale_magic,tga_grayscale_mask,0},
    {0}
};

1281 1282 1283 1284 1285 1286
static struct regsvr_decoder const decoder_list[] = {
    {   &CLSID_WICBmpDecoder,
	"The Wine Project",
	"BMP Decoder",
	"1.0.0.0",
	&GUID_VendorMicrosoft,
1287
	&GUID_ContainerFormatBmp,
1288 1289 1290 1291 1292
	"image/bmp",
	".bmp,.dib,.rle",
	bmp_formats,
	bmp_patterns
    },
1293 1294 1295 1296 1297
    {   &CLSID_WICGifDecoder,
	"The Wine Project",
	"GIF Decoder",
	"1.0.0.0",
	&GUID_VendorMicrosoft,
1298
	&GUID_ContainerFormatGif,
1299 1300 1301 1302 1303
	"image/gif",
	".gif",
	gif_formats,
	gif_patterns
    },
1304 1305 1306 1307 1308
    {   &CLSID_WICIcoDecoder,
	"The Wine Project",
	"ICO Decoder",
	"1.0.0.0",
	&GUID_VendorMicrosoft,
1309
	&GUID_ContainerFormatIco,
1310 1311 1312 1313 1314
	"image/vnd.microsoft.icon",
	".ico",
	ico_formats,
	ico_patterns
    },
1315 1316 1317 1318 1319
    {   &CLSID_WICJpegDecoder,
	"The Wine Project",
	"JPEG Decoder",
	"1.0.0.0",
	&GUID_VendorMicrosoft,
1320
	&GUID_ContainerFormatJpeg,
1321 1322 1323 1324 1325
	"image/jpeg",
	".jpg;.jpeg;.jfif",
	jpeg_formats,
	jpeg_patterns
    },
1326 1327 1328 1329 1330
    {   &CLSID_WICPngDecoder,
	"The Wine Project",
	"PNG Decoder",
	"1.0.0.0",
	&GUID_VendorMicrosoft,
1331
	&GUID_ContainerFormatPng,
1332 1333 1334 1335 1336
	"image/png",
	".png",
	png_formats,
	png_patterns
    },
1337 1338 1339 1340 1341
    {   &CLSID_WICTiffDecoder,
	"The Wine Project",
	"TIFF Decoder",
	"1.0.0.0",
	&GUID_VendorMicrosoft,
1342
	&GUID_ContainerFormatTiff,
1343 1344
	"image/tiff",
	".tif;.tiff",
1345
	tiff_decode_formats,
1346 1347
	tiff_patterns
    },
1348 1349 1350 1351 1352
    {   &CLSID_WineTgaDecoder,
	"The Wine Project",
	"TGA Decoder",
	"1.0.0.0",
	&GUID_VendorWine,
1353
	&GUID_WineContainerFormatTga,
1354 1355 1356 1357 1358
	"image/x-targa",
	".tga;.tpic",
	tga_formats,
	tga_patterns
    },
1359 1360 1361
    { NULL }			/* list terminator */
};

1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
static GUID const * const bmp_encode_formats[] = {
    &GUID_WICPixelFormat16bppBGR555,
    &GUID_WICPixelFormat16bppBGR565,
    &GUID_WICPixelFormat24bppBGR,
    &GUID_WICPixelFormat32bppBGR,
    NULL
};

static GUID const * const png_encode_formats[] = {
    &GUID_WICPixelFormat24bppBGR,
    &GUID_WICPixelFormatBlackWhite,
    &GUID_WICPixelFormat2bppGray,
    &GUID_WICPixelFormat4bppGray,
    &GUID_WICPixelFormat8bppGray,
    &GUID_WICPixelFormat16bppGray,
    &GUID_WICPixelFormat32bppBGR,
    &GUID_WICPixelFormat32bppBGRA,
    &GUID_WICPixelFormat48bppRGB,
    &GUID_WICPixelFormat64bppRGBA,
    NULL
};

1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
static GUID const * const tiff_encode_formats[] = {
    &GUID_WICPixelFormatBlackWhite,
    &GUID_WICPixelFormat4bppGray,
    &GUID_WICPixelFormat8bppGray,
    &GUID_WICPixelFormat24bppBGR,
    &GUID_WICPixelFormat32bppBGRA,
    &GUID_WICPixelFormat32bppPBGRA,
    &GUID_WICPixelFormat48bppRGB,
    &GUID_WICPixelFormat64bppRGBA,
    &GUID_WICPixelFormat64bppPRGBA,
    NULL
};

1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
static GUID const * const icns_encode_formats[] = {
    &GUID_WICPixelFormat32bppBGRA,
    NULL
};

static struct regsvr_encoder const encoder_list[] = {
    {   &CLSID_WICBmpEncoder,
	"The Wine Project",
	"BMP Encoder",
	"1.0.0.0",
	&GUID_VendorMicrosoft,
1408
	&GUID_ContainerFormatBmp,
1409 1410 1411 1412
	"image/bmp",
	".bmp,.dib,.rle",
	bmp_encode_formats
    },
1413 1414 1415 1416 1417
    {   &CLSID_WICJpegEncoder,
	"The Wine Project",
	"JPEG Encoder",
	"1.0.0.0",
	&GUID_VendorMicrosoft,
1418
	&GUID_ContainerFormatJpeg,
1419 1420 1421 1422
	"image/jpeg",
	".jpg;.jpeg;.jfif",
	jpeg_formats
    },
1423 1424 1425 1426 1427
    {   &CLSID_WICPngEncoder,
	"The Wine Project",
	"PNG Encoder",
	"1.0.0.0",
	&GUID_VendorMicrosoft,
1428
	&GUID_ContainerFormatPng,
1429 1430 1431 1432
	"image/png",
	".png",
	png_encode_formats
    },
1433 1434 1435 1436 1437
    {   &CLSID_WICTiffEncoder,
	"The Wine Project",
	"TIFF Encoder",
	"1.0.0.0",
	&GUID_VendorMicrosoft,
1438
	&GUID_ContainerFormatTiff,
1439 1440 1441 1442
	"image/tiff",
	".tif;.tiff",
	tiff_encode_formats
    },
1443 1444 1445 1446 1447
    {   &CLSID_WICIcnsEncoder,
	"The Wine Project",
	"ICNS Encoder",
	"1.0.0.0",
	&GUID_VendorWine,
1448
	NULL, /* no container format guid */
1449 1450 1451 1452 1453 1454 1455
	"image/icns",
	".icns",
	icns_encode_formats
    },
    { NULL }			/* list terminator */
};

1456
static GUID const * const converter_formats[] = {
1457
    &GUID_WICPixelFormat1bppIndexed,
1458
    &GUID_WICPixelFormat2bppIndexed,
1459
    &GUID_WICPixelFormat4bppIndexed,
1460
    &GUID_WICPixelFormat8bppIndexed,
1461
    &GUID_WICPixelFormatBlackWhite,
1462
    &GUID_WICPixelFormat2bppGray,
1463
    &GUID_WICPixelFormat4bppGray,
1464
    &GUID_WICPixelFormat8bppGray,
1465
    &GUID_WICPixelFormat16bppGray,
1466
    &GUID_WICPixelFormat16bppBGR555,
1467
    &GUID_WICPixelFormat16bppBGR565,
1468
    &GUID_WICPixelFormat16bppBGRA5551,
1469
    &GUID_WICPixelFormat24bppBGR,
1470
    &GUID_WICPixelFormat24bppRGB,
1471 1472
    &GUID_WICPixelFormat32bppBGR,
    &GUID_WICPixelFormat32bppBGRA,
1473
    &GUID_WICPixelFormat32bppPBGRA,
1474
    &GUID_WICPixelFormat48bppRGB,
1475
    &GUID_WICPixelFormat64bppRGBA,
1476
    &GUID_WICPixelFormat32bppCMYK,
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
    NULL
};

static struct regsvr_converter const converter_list[] = {
    {   &CLSID_WICDefaultFormatConverter,
	"The Wine Project",
	"Default Pixel Format Converter",
	"1.0.0.0",
	&GUID_VendorMicrosoft,
	converter_formats
    },
    { NULL }			/* list terminator */
};

1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
static const BYTE no_magic[1] = { 0 };
static const BYTE no_mask[1] = { 0 };

static const struct metadata_pattern ifd_metadata_pattern[] = {
    { 0, 1, no_magic, no_mask, 0 },
    { 0 }
};

static const struct reader_containers ifd_containers[] = {
    {
        &GUID_ContainerFormatTiff,
        ifd_metadata_pattern
    },
    { NULL } /* list terminator */
};

1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
static const BYTE tEXt[] = "tEXt";

static const struct metadata_pattern pngtext_metadata_pattern[] = {
    { 4, 4, tEXt, mask_all, 4 },
    { 0 }
};

static const struct reader_containers pngtext_containers[] = {
    {
        &GUID_ContainerFormatPng,
        pngtext_metadata_pattern
    },
    { NULL } /* list terminator */
};

1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
static const struct metadata_pattern lsd_metadata_patterns[] = {
    { 0, 6, gif87a_magic, mask_all, 0 },
    { 0, 6, gif89a_magic, mask_all, 0 },
    { 0 }
};

static const struct reader_containers lsd_containers[] = {
    {
        &GUID_ContainerFormatGif,
        lsd_metadata_patterns
    },
    { NULL } /* list terminator */
};

1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
static const BYTE imd_magic[] = { 0x2c };

static const struct metadata_pattern imd_metadata_pattern[] = {
    { 0, 1, imd_magic, mask_all, 1 },
    { 0 }
};

static const struct reader_containers imd_containers[] = {
    {
        &GUID_ContainerFormatGif,
        imd_metadata_pattern
    },
    { NULL } /* list terminator */
};

1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
static const BYTE gce_magic[] = { 0x21, 0xf9, 0x04 };

static const struct metadata_pattern gce_metadata_pattern[] = {
    { 0, 3, gce_magic, mask_all, 3 },
    { 0 }
};

static const struct reader_containers gce_containers[] = {
    {
        &GUID_ContainerFormatGif,
        gce_metadata_pattern
    },
    { NULL } /* list terminator */
};

1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
static const BYTE ape_magic[] = { 0x21, 0xff, 0x0b };

static const struct metadata_pattern ape_metadata_pattern[] = {
    { 0, 3, ape_magic, mask_all, 0 },
    { 0 }
};

static const struct reader_containers ape_containers[] = {
    {
        &GUID_ContainerFormatGif,
        ape_metadata_pattern
    },
    { NULL } /* list terminator */
};

1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
static const BYTE gif_comment_magic[] = { 0x21, 0xfe };

static const struct metadata_pattern gif_comment_metadata_pattern[] = {
    { 0, 2, gif_comment_magic, mask_all, 0 },
    { 0 }
};

static const struct reader_containers gif_comment_containers[] = {
    {
        &GUID_ContainerFormatGif,
        gif_comment_metadata_pattern
    },
    { NULL } /* list terminator */
};

1596 1597
static struct regsvr_metadatareader const metadatareader_list[] = {
    {   &CLSID_WICUnknownMetadataReader,
1598 1599 1600 1601 1602 1603 1604 1605 1606
        "The Wine Project",
        "Unknown Metadata Reader",
        "1.0.0.0",
        "1.0.0.0",
        &GUID_VendorMicrosoft,
        &GUID_MetadataFormatUnknown,
        0, 0, 0,
        NULL
    },
1607
    {   &CLSID_WICIfdMetadataReader,
1608 1609 1610 1611 1612 1613 1614 1615
        "The Wine Project",
        "Ifd Reader",
        "1.0.0.0",
        "1.0.0.0",
        &GUID_VendorMicrosoft,
        &GUID_MetadataFormatIfd,
        1, 1, 0,
        ifd_containers
1616
    },
1617 1618
    {   &CLSID_WICPngTextMetadataReader,
        "The Wine Project",
1619
        "Chunk tEXt Reader",
1620 1621 1622 1623 1624 1625 1626
        "1.0.0.0",
        "1.0.0.0",
        &GUID_VendorMicrosoft,
        &GUID_MetadataFormatChunktEXt,
        0, 0, 0,
        pngtext_containers
    },
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
    {   &CLSID_WICLSDMetadataReader,
        "The Wine Project",
        "Logical Screen Descriptor Reader",
        "1.0.0.0",
        "1.0.0.0",
        &GUID_VendorMicrosoft,
        &GUID_MetadataFormatLSD,
        0, 0, 0,
        lsd_containers
    },
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
    {   &CLSID_WICIMDMetadataReader,
        "The Wine Project",
        "Image Descriptor Reader",
        "1.0.0.0",
        "1.0.0.0",
        &GUID_VendorMicrosoft,
        &GUID_MetadataFormatIMD,
        0, 0, 0,
        imd_containers
    },
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
    {   &CLSID_WICGCEMetadataReader,
        "The Wine Project",
        "Graphic Control Extension Reader",
        "1.0.0.0",
        "1.0.0.0",
        &GUID_VendorMicrosoft,
        &GUID_MetadataFormatGCE,
        0, 0, 0,
        gce_containers
    },
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
    {   &CLSID_WICAPEMetadataReader,
        "The Wine Project",
        "Application Extension Reader",
        "1.0.0.0",
        "1.0.0.0",
        &GUID_VendorMicrosoft,
        &GUID_MetadataFormatAPE,
        0, 0, 0,
        ape_containers
    },
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676
    {   &CLSID_WICGifCommentMetadataReader,
        "The Wine Project",
        "Comment Extension Reader",
        "1.0.0.0",
        "1.0.0.0",
        &GUID_VendorMicrosoft,
        &GUID_MetadataFormatGifComment,
        0, 0, 0,
        gif_comment_containers
    },
1677 1678 1679
    { NULL }			/* list terminator */
};

1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
static BYTE const channel_mask_1bit[] = { 0x01 };
static BYTE const channel_mask_2bit[] = { 0x03 };
static BYTE const channel_mask_4bit[] = { 0x0f };

static BYTE const channel_mask_8bit[] = { 0xff, 0x00, 0x00, 0x00 };
static BYTE const channel_mask_8bit2[] = { 0x00, 0xff, 0x00, 0x00 };
static BYTE const channel_mask_8bit3[] = { 0x00, 0x00, 0xff, 0x00 };
static BYTE const channel_mask_8bit4[] = { 0x00, 0x00, 0x00, 0xff };

static BYTE const channel_mask_16bit[] = { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
static BYTE const channel_mask_16bit2[] = { 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 };
static BYTE const channel_mask_16bit3[] = { 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00 };
static BYTE const channel_mask_16bit4[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff };

static BYTE const channel_mask_5bit[] = { 0x1f, 0x00 };
static BYTE const channel_mask_5bit2[] = { 0xe0, 0x03 };
static BYTE const channel_mask_5bit3[] = { 0x00, 0x7c };
static BYTE const channel_mask_5bit4[] = { 0x00, 0x80 };

static BYTE const channel_mask_BGR565_2[] = { 0xe0, 0x07 };
static BYTE const channel_mask_BGR565_3[] = { 0x00, 0xf8 };

static BYTE const * const channel_masks_1bit[] = { channel_mask_1bit };
static BYTE const * const channel_masks_2bit[] = { channel_mask_2bit };
static BYTE const * const channel_masks_4bit[] = { channel_mask_4bit };
static BYTE const * const channel_masks_8bit[] = { channel_mask_8bit,
    channel_mask_8bit2, channel_mask_8bit3, channel_mask_8bit4 };
static BYTE const * const channel_masks_16bit[] = { channel_mask_16bit,
    channel_mask_16bit2, channel_mask_16bit3, channel_mask_16bit4};

static BYTE const * const channel_masks_BGRA5551[] = { channel_mask_5bit,
    channel_mask_5bit2, channel_mask_5bit3, channel_mask_5bit4 };

static BYTE const * const channel_masks_BGR565[] = { channel_mask_5bit,
    channel_mask_BGR565_2, channel_mask_BGR565_3 };

static struct regsvr_pixelformat const pixelformat_list[] = {
    {   &GUID_WICPixelFormat1bppIndexed,
        "The Wine Project",
        "1bpp Indexed",
        NULL, /* no version */
        &GUID_VendorMicrosoft,
        1, /* bitsperpixel */
        1, /* channel count */
        channel_masks_1bit,
        WICPixelFormatNumericRepresentationIndexed,
        1
    },
    {   &GUID_WICPixelFormat2bppIndexed,
        "The Wine Project",
        "2bpp Indexed",
        NULL, /* no version */
        &GUID_VendorMicrosoft,
        2, /* bitsperpixel */
        1, /* channel count */
        channel_masks_2bit,
        WICPixelFormatNumericRepresentationIndexed,
        1
    },
    {   &GUID_WICPixelFormat4bppIndexed,
        "The Wine Project",
        "4bpp Indexed",
        NULL, /* no version */
        &GUID_VendorMicrosoft,
        4, /* bitsperpixel */
        1, /* channel count */
        channel_masks_4bit,
        WICPixelFormatNumericRepresentationIndexed,
        1
    },
    {   &GUID_WICPixelFormat8bppIndexed,
        "The Wine Project",
        "8bpp Indexed",
        NULL, /* no version */
        &GUID_VendorMicrosoft,
        8, /* bitsperpixel */
        1, /* channel count */
        channel_masks_8bit,
        WICPixelFormatNumericRepresentationIndexed,
        1
    },
    {   &GUID_WICPixelFormatBlackWhite,
        "The Wine Project",
        "Black and White",
        NULL, /* no version */
        &GUID_VendorMicrosoft,
        1, /* bitsperpixel */
        1, /* channel count */
        channel_masks_1bit,
        WICPixelFormatNumericRepresentationUnsignedInteger,
        0
    },
    {   &GUID_WICPixelFormat2bppGray,
        "The Wine Project",
        "2bpp Grayscale",
        NULL, /* no version */
        &GUID_VendorMicrosoft,
        2, /* bitsperpixel */
        1, /* channel count */
        channel_masks_2bit,
        WICPixelFormatNumericRepresentationUnsignedInteger,
        0
    },
    {   &GUID_WICPixelFormat4bppGray,
        "The Wine Project",
        "4bpp Grayscale",
        NULL, /* no version */
        &GUID_VendorMicrosoft,
        4, /* bitsperpixel */
        1, /* channel count */
        channel_masks_4bit,
        WICPixelFormatNumericRepresentationUnsignedInteger,
        0
    },
    {   &GUID_WICPixelFormat8bppGray,
        "The Wine Project",
        "8bpp Grayscale",
        NULL, /* no version */
        &GUID_VendorMicrosoft,
        8, /* bitsperpixel */
        1, /* channel count */
        channel_masks_8bit,
        WICPixelFormatNumericRepresentationUnsignedInteger,
        0
    },
    {   &GUID_WICPixelFormat16bppGray,
        "The Wine Project",
        "16bpp Grayscale",
        NULL, /* no version */
        &GUID_VendorMicrosoft,
        16, /* bitsperpixel */
        1, /* channel count */
        channel_masks_16bit,
        WICPixelFormatNumericRepresentationUnsignedInteger,
        0
    },
    {   &GUID_WICPixelFormat16bppBGR555,
        "The Wine Project",
        "16bpp BGR555",
        NULL, /* no version */
        &GUID_VendorMicrosoft,
        16, /* bitsperpixel */
        3, /* channel count */
        channel_masks_BGRA5551,
        WICPixelFormatNumericRepresentationUnsignedInteger,
        0
    },
    {   &GUID_WICPixelFormat16bppBGR565,
        "The Wine Project",
        "16bpp BGR565",
        NULL, /* no version */
        &GUID_VendorMicrosoft,
        16, /* bitsperpixel */
        3, /* channel count */
        channel_masks_BGR565,
        WICPixelFormatNumericRepresentationUnsignedInteger,
        0
    },
    {   &GUID_WICPixelFormat16bppBGRA5551,
        "The Wine Project",
        "16bpp BGRA5551",
        NULL, /* no version */
        &GUID_VendorMicrosoft,
        16, /* bitsperpixel */
        4, /* channel count */
        channel_masks_BGRA5551,
        WICPixelFormatNumericRepresentationUnsignedInteger,
        1
    },
    {   &GUID_WICPixelFormat24bppBGR,
        "The Wine Project",
        "24bpp BGR",
        NULL, /* no version */
        &GUID_VendorMicrosoft,
        24, /* bitsperpixel */
        3, /* channel count */
        channel_masks_8bit,
        WICPixelFormatNumericRepresentationUnsignedInteger,
        0
    },
1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
    {   &GUID_WICPixelFormat24bppRGB,
        "The Wine Project",
        "24bpp RGB",
        NULL, /* no version */
        &GUID_VendorMicrosoft,
        24, /* bitsperpixel */
        3, /* channel count */
        channel_masks_8bit,
        WICPixelFormatNumericRepresentationUnsignedInteger,
        0
    },
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950
    {   &GUID_WICPixelFormat32bppBGR,
        "The Wine Project",
        "32bpp BGR",
        NULL, /* no version */
        &GUID_VendorMicrosoft,
        32, /* bitsperpixel */
        3, /* channel count */
        channel_masks_8bit,
        WICPixelFormatNumericRepresentationUnsignedInteger,
        0
    },
    {   &GUID_WICPixelFormat32bppBGRA,
        "The Wine Project",
        "32bpp BGRA",
        NULL, /* no version */
        &GUID_VendorMicrosoft,
        32, /* bitsperpixel */
        4, /* channel count */
        channel_masks_8bit,
        WICPixelFormatNumericRepresentationUnsignedInteger,
        1
    },
    {   &GUID_WICPixelFormat32bppPBGRA,
        "The Wine Project",
        "32bpp PBGRA",
        NULL, /* no version */
        &GUID_VendorMicrosoft,
        32, /* bitsperpixel */
        4, /* channel count */
        channel_masks_8bit,
        WICPixelFormatNumericRepresentationUnsignedInteger,
        1
    },
    {   &GUID_WICPixelFormat48bppRGB,
        "The Wine Project",
        "48bpp RGB",
        NULL, /* no version */
        &GUID_VendorMicrosoft,
        48, /* bitsperpixel */
        3, /* channel count */
        channel_masks_16bit,
        WICPixelFormatNumericRepresentationUnsignedInteger,
        0
    },
    {   &GUID_WICPixelFormat64bppRGBA,
        "The Wine Project",
        "64bpp RGBA",
        NULL, /* no version */
        &GUID_VendorMicrosoft,
        64, /* bitsperpixel */
        4, /* channel count */
        channel_masks_16bit,
        WICPixelFormatNumericRepresentationUnsignedInteger,
        1
    },
    {   &GUID_WICPixelFormat64bppPRGBA,
        "The Wine Project",
        "64bpp PRGBA",
        NULL, /* no version */
        &GUID_VendorMicrosoft,
        64, /* bitsperpixel */
        4, /* channel count */
        channel_masks_16bit,
        WICPixelFormatNumericRepresentationUnsignedInteger,
        1
    },
    {   &GUID_WICPixelFormat32bppCMYK,
        "The Wine Project",
        "32bpp CMYK",
        NULL, /* no version */
        &GUID_VendorMicrosoft,
        32, /* bitsperpixel */
        4, /* channel count */
        channel_masks_8bit,
        WICPixelFormatNumericRepresentationUnsignedInteger,
        0
    },
    { NULL }			/* list terminator */
};

1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048
struct regsvr_category
{
    const CLSID *clsid; /* NULL for end of list */
};

static const struct regsvr_category category_list[] = {
    { &CATID_WICBitmapDecoders },
    { &CATID_WICBitmapEncoders },
    { &CATID_WICFormatConverters },
    { &CATID_WICMetadataReader },
    { &CATID_WICPixelFormats },
    { NULL }
};

static HRESULT register_categories(const struct regsvr_category *list)
{
    LONG res;
    WCHAR buf[39];
    HKEY coclass_key, categories_key, instance_key;

    res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
                          KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
    if (res != ERROR_SUCCESS) return HRESULT_FROM_WIN32(res);

    StringFromGUID2(&CLSID_WICImagingCategories, buf, 39);
    res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
                          KEY_READ | KEY_WRITE, NULL, &categories_key, NULL);
    if (res != ERROR_SUCCESS)
    {
        RegCloseKey(coclass_key);
        return HRESULT_FROM_WIN32(res);
    }

    res = RegCreateKeyExW(categories_key, instance_keyname, 0, NULL, 0,
                          KEY_READ | KEY_WRITE, NULL, &instance_key, NULL);

    for (; res == ERROR_SUCCESS && list->clsid; list++)
    {
        HKEY instance_clsid_key;

        StringFromGUID2(list->clsid, buf, 39);
        res = RegCreateKeyExW(instance_key, buf, 0, NULL, 0,
                              KEY_READ | KEY_WRITE, NULL, &instance_clsid_key, NULL);
        if (res == ERROR_SUCCESS)
        {
            res = RegSetValueExW(instance_clsid_key, clsid_valuename, 0, REG_SZ,
                                 (const BYTE *)buf, 78);
            RegCloseKey(instance_clsid_key);
        }
    }

    RegCloseKey(instance_key);
    RegCloseKey(categories_key);
    RegCloseKey(coclass_key);

    return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
}

static HRESULT unregister_categories(const struct regsvr_category *list)
{
    LONG res;
    WCHAR buf[39];
    HKEY coclass_key, categories_key, instance_key;

    res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
                        KEY_READ | KEY_WRITE, &coclass_key);
    if (res != ERROR_SUCCESS) return HRESULT_FROM_WIN32(res);

    StringFromGUID2(&CLSID_WICImagingCategories, buf, 39);
    res = RegOpenKeyExW(coclass_key, buf, 0,
                        KEY_READ | KEY_WRITE, &categories_key);
    if (res != ERROR_SUCCESS)
    {
        if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
        RegCloseKey(coclass_key);
        return HRESULT_FROM_WIN32(res);
    }

    res = RegOpenKeyExW(categories_key, instance_keyname, 0,
                          KEY_READ | KEY_WRITE, &instance_key);

    for (; res == ERROR_SUCCESS && list->clsid; list++)
    {
        StringFromGUID2(list->clsid, buf, 39);
        res = RegDeleteTreeW(instance_key, buf);
    }

    RegCloseKey(instance_key);
    RegCloseKey(categories_key);

    StringFromGUID2(&CLSID_WICImagingCategories, buf, 39);
    res = RegDeleteTreeW(coclass_key, buf);

    RegCloseKey(coclass_key);

    return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
}

2049 2050 2051
extern HRESULT WINAPI WIC_DllRegisterServer(void) DECLSPEC_HIDDEN;
extern HRESULT WINAPI WIC_DllUnregisterServer(void) DECLSPEC_HIDDEN;

2052 2053 2054 2055 2056 2057
HRESULT WINAPI DllRegisterServer(void)
{
    HRESULT hr;

    TRACE("\n");

2058
    hr = WIC_DllRegisterServer();
2059 2060
    if (SUCCEEDED(hr))
        hr = register_categories(category_list);
2061
    if (SUCCEEDED(hr))
2062
        hr = register_decoders(decoder_list);
2063
    if (SUCCEEDED(hr))
2064
        hr = register_encoders(encoder_list);
2065
    if (SUCCEEDED(hr))
2066 2067 2068
        hr = register_converters(converter_list);
    if (SUCCEEDED(hr))
        hr = register_metadatareaders(metadatareader_list);
2069 2070
    if (SUCCEEDED(hr))
        hr = register_pixelformats(pixelformat_list);
2071 2072 2073 2074 2075 2076 2077 2078 2079
    return hr;
}

HRESULT WINAPI DllUnregisterServer(void)
{
    HRESULT hr;

    TRACE("\n");

2080
    hr = WIC_DllUnregisterServer();
2081 2082
    if (SUCCEEDED(hr))
        hr = unregister_categories(category_list);
2083
    if (SUCCEEDED(hr))
2084 2085 2086
        hr = unregister_decoders(decoder_list);
    if (SUCCEEDED(hr))
        hr = unregister_encoders(encoder_list);
2087
    if (SUCCEEDED(hr))
2088
        hr = unregister_converters(converter_list);
2089
    if (SUCCEEDED(hr))
2090
        hr = unregister_metadatareaders(metadatareader_list);
2091 2092
    if (SUCCEEDED(hr))
        hr = unregister_pixelformats(pixelformat_list);
2093 2094
    return hr;
}