package.c 56 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
/*
 * Copyright 2018 Nikolay Sivov 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 COBJMACROS

#include <stdarg.h>
#include "windef.h"
#include "winbase.h"
24
#include "ntsecapi.h"
25
#include "xmllite.h"
26 27

#include "wine/debug.h"
28
#include "wine/list.h"
29 30 31 32 33

#include "opc_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(msopc);

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
struct opc_content
{
    LONG refcount;
    BYTE *data;
    ULARGE_INTEGER size;
};

struct opc_content_stream
{
    IStream IStream_iface;
    LONG refcount;

    struct opc_content *content;
    ULARGE_INTEGER pos;
};

50 51 52 53
struct opc_package
{
    IOpcPackage IOpcPackage_iface;
    LONG refcount;
54 55

    IOpcPartSet *part_set;
56
    IOpcRelationshipSet *relationship_set;
57
    IOpcUri *source_uri;
58 59
};

60 61 62 63 64 65 66 67 68 69
struct opc_part_enum
{
    IOpcPartEnumerator IOpcPartEnumerator_iface;
    LONG refcount;

    struct opc_part_set *part_set;
    size_t pos;
    GUID id;
};

70 71 72 73
struct opc_part
{
    IOpcPart IOpcPart_iface;
    LONG refcount;
74 75 76 77

    IOpcPartUri *name;
    WCHAR *content_type;
    DWORD compression_options;
78
    IOpcRelationshipSet *relationship_set;
79
    struct opc_content *content;
80 81
};

82 83 84 85
struct opc_part_set
{
    IOpcPartSet IOpcPartSet_iface;
    LONG refcount;
86 87 88 89

    struct opc_part **parts;
    size_t size;
    size_t count;
90
    GUID id;
91 92
};

93 94 95 96 97 98 99 100 101 102
struct opc_rel_enum
{
    IOpcRelationshipEnumerator IOpcRelationshipEnumerator_iface;
    LONG refcount;

    struct opc_relationship_set *rel_set;
    size_t pos;
    GUID id;
};

103 104 105 106
struct opc_relationship
{
    IOpcRelationship IOpcRelationship_iface;
    LONG refcount;
107 108

    WCHAR *id;
109
    WCHAR *type;
110 111
    IUri *target;
    OPC_URI_TARGET_MODE target_mode;
112
    IOpcUri *source_uri;
113 114
};

115 116 117 118
struct opc_relationship_set
{
    IOpcRelationshipSet IOpcRelationshipSet_iface;
    LONG refcount;
119 120 121 122

    struct opc_relationship **relationships;
    size_t size;
    size_t count;
123
    IOpcUri *source_uri;
124
    GUID id;
125 126
};

127 128 129 130 131
static inline struct opc_package *impl_from_IOpcPackage(IOpcPackage *iface)
{
    return CONTAINING_RECORD(iface, struct opc_package, IOpcPackage_iface);
}

132 133 134 135 136
static inline struct opc_part_set *impl_from_IOpcPartSet(IOpcPartSet *iface)
{
    return CONTAINING_RECORD(iface, struct opc_part_set, IOpcPartSet_iface);
}

137 138 139 140 141
static inline struct opc_part *impl_from_IOpcPart(IOpcPart *iface)
{
    return CONTAINING_RECORD(iface, struct opc_part, IOpcPart_iface);
}

142 143 144 145 146
static inline struct opc_relationship_set *impl_from_IOpcRelationshipSet(IOpcRelationshipSet *iface)
{
    return CONTAINING_RECORD(iface, struct opc_relationship_set, IOpcRelationshipSet_iface);
}

147 148 149 150 151
static inline struct opc_relationship *impl_from_IOpcRelationship(IOpcRelationship *iface)
{
    return CONTAINING_RECORD(iface, struct opc_relationship, IOpcRelationship_iface);
}

152 153 154 155 156
static inline struct opc_content_stream *impl_from_IStream(IStream *iface)
{
    return CONTAINING_RECORD(iface, struct opc_content_stream, IStream_iface);
}

157 158 159 160 161
static inline struct opc_part_enum *impl_from_IOpcPartEnumerator(IOpcPartEnumerator *iface)
{
    return CONTAINING_RECORD(iface, struct opc_part_enum, IOpcPartEnumerator_iface);
}

162 163 164 165 166
static inline struct opc_rel_enum *impl_from_IOpcRelationshipEnumerator(IOpcRelationshipEnumerator *iface)
{
    return CONTAINING_RECORD(iface, struct opc_rel_enum, IOpcRelationshipEnumerator_iface);
}

167 168 169 170 171 172 173 174 175 176 177
static void opc_content_release(struct opc_content *content)
{
    ULONG refcount = InterlockedDecrement(&content->refcount);

    if (!refcount)
    {
        heap_free(content->data);
        heap_free(content);
    }
}

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 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 334 335 336 337 338
static HRESULT opc_part_enum_create(struct opc_part_set *part_set, IOpcPartEnumerator **out);

static HRESULT WINAPI opc_part_enum_QueryInterface(IOpcPartEnumerator *iface, REFIID iid, void **out)
{
    TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);

    if (IsEqualIID(&IID_IOpcPartEnumerator, iid) ||
            IsEqualIID(&IID_IUnknown, iid))
    {
        *out = iface;
        IOpcPartEnumerator_AddRef(iface);
        return S_OK;
    }

    *out = NULL;
    WARN("Unsupported interface %s.\n", debugstr_guid(iid));
    return E_NOINTERFACE;
}

static ULONG WINAPI opc_part_enum_AddRef(IOpcPartEnumerator *iface)
{
    struct opc_part_enum *part_enum = impl_from_IOpcPartEnumerator(iface);
    ULONG refcount = InterlockedIncrement(&part_enum->refcount);

    TRACE("%p increasing refcount to %u.\n", iface, refcount);

    return refcount;
}

static ULONG WINAPI opc_part_enum_Release(IOpcPartEnumerator *iface)
{
    struct opc_part_enum *part_enum = impl_from_IOpcPartEnumerator(iface);
    ULONG refcount = InterlockedDecrement(&part_enum->refcount);

    TRACE("%p decreasing refcount to %u.\n", iface, refcount);

    if (!refcount)
    {
        IOpcPartSet_Release(&part_enum->part_set->IOpcPartSet_iface);
        heap_free(part_enum);
    }

    return refcount;
}

static BOOL has_part_collection_changed(const struct opc_part_enum *part_enum)
{
    return !IsEqualGUID(&part_enum->id, &part_enum->part_set->id);
}

static HRESULT WINAPI opc_part_enum_MoveNext(IOpcPartEnumerator *iface, BOOL *has_next)
{
    struct opc_part_enum *part_enum = impl_from_IOpcPartEnumerator(iface);

    TRACE("iface %p, has_next %p.\n", iface, has_next);

    if (!has_next)
        return E_POINTER;

    if (has_part_collection_changed(part_enum))
        return OPC_E_ENUM_COLLECTION_CHANGED;

    if (part_enum->part_set->count && (part_enum->pos == ~(size_t)0 || part_enum->pos < part_enum->part_set->count))
        part_enum->pos++;

    *has_next = part_enum->pos < part_enum->part_set->count;

    return S_OK;
}

static HRESULT WINAPI opc_part_enum_MovePrevious(IOpcPartEnumerator *iface, BOOL *has_previous)
{
    struct opc_part_enum *part_enum = impl_from_IOpcPartEnumerator(iface);

    TRACE("iface %p, has_previous %p.\n", iface, has_previous);

    if (!has_previous)
        return E_POINTER;

    if (has_part_collection_changed(part_enum))
        return OPC_E_ENUM_COLLECTION_CHANGED;

    if (part_enum->pos != ~(size_t)0)
        part_enum->pos--;

    *has_previous = part_enum->pos != ~(size_t)0;

    return S_OK;
}

static HRESULT WINAPI opc_part_enum_GetCurrent(IOpcPartEnumerator *iface, IOpcPart **part)
{
    struct opc_part_enum *part_enum = impl_from_IOpcPartEnumerator(iface);

    TRACE("iface %p, part %p.\n", iface, part);

    if (!part)
        return E_POINTER;

    *part = NULL;

    if (has_part_collection_changed(part_enum))
        return OPC_E_ENUM_COLLECTION_CHANGED;

    if (part_enum->pos < part_enum->part_set->count)
    {
        *part = &part_enum->part_set->parts[part_enum->pos]->IOpcPart_iface;
        IOpcPart_AddRef(*part);
    }

    return *part ? S_OK : OPC_E_ENUM_INVALID_POSITION;
}

static HRESULT WINAPI opc_part_enum_Clone(IOpcPartEnumerator *iface, IOpcPartEnumerator **out)
{
    struct opc_part_enum *part_enum = impl_from_IOpcPartEnumerator(iface);

    TRACE("iface %p, out %p.\n", iface, out);

    if (!out)
        return E_POINTER;

    if (has_part_collection_changed(part_enum))
    {
        *out = NULL;
        return OPC_E_ENUM_COLLECTION_CHANGED;
    }

    return opc_part_enum_create(part_enum->part_set, out);
}

static const IOpcPartEnumeratorVtbl opc_part_enum_vtbl =
{
    opc_part_enum_QueryInterface,
    opc_part_enum_AddRef,
    opc_part_enum_Release,
    opc_part_enum_MoveNext,
    opc_part_enum_MovePrevious,
    opc_part_enum_GetCurrent,
    opc_part_enum_Clone,
};

static HRESULT opc_part_enum_create(struct opc_part_set *part_set, IOpcPartEnumerator **out)
{
    struct opc_part_enum *part_enum;

    if (!(part_enum = heap_alloc_zero(sizeof(*part_enum))))
        return E_OUTOFMEMORY;

    part_enum->IOpcPartEnumerator_iface.lpVtbl = &opc_part_enum_vtbl;
    part_enum->refcount = 1;
    part_enum->part_set = part_set;
    IOpcPartSet_AddRef(&part_set->IOpcPartSet_iface);
    part_enum->pos = ~(size_t)0;
    part_enum->id = part_set->id;

    *out = &part_enum->IOpcPartEnumerator_iface;
    TRACE("Created part enumerator %p.\n", *out);
    return S_OK;
}

339 340 341 342 343 344 345 346 347 348 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 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 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
static HRESULT opc_rel_enum_create(struct opc_relationship_set *rel_set, IOpcRelationshipEnumerator **out);

static HRESULT WINAPI opc_rel_enum_QueryInterface(IOpcRelationshipEnumerator *iface, REFIID iid, void **out)
{
    TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);

    if (IsEqualIID(&IID_IOpcRelationshipEnumerator, iid) ||
            IsEqualIID(&IID_IUnknown, iid))
    {
        *out = iface;
        IOpcRelationshipEnumerator_AddRef(iface);
        return S_OK;
    }

    *out = NULL;
    WARN("Unsupported interface %s.\n", debugstr_guid(iid));
    return E_NOINTERFACE;
}

static ULONG WINAPI opc_rel_enum_AddRef(IOpcRelationshipEnumerator *iface)
{
    struct opc_rel_enum *rel_enum = impl_from_IOpcRelationshipEnumerator(iface);
    ULONG refcount = InterlockedIncrement(&rel_enum->refcount);

    TRACE("%p increasing refcount to %u.\n", iface, refcount);

    return refcount;
}

static ULONG WINAPI opc_rel_enum_Release(IOpcRelationshipEnumerator *iface)
{
    struct opc_rel_enum *rel_enum = impl_from_IOpcRelationshipEnumerator(iface);
    ULONG refcount = InterlockedDecrement(&rel_enum->refcount);

    TRACE("%p decreasing refcount to %u.\n", iface, refcount);

    if (!refcount)
    {
        IOpcRelationshipSet_Release(&rel_enum->rel_set->IOpcRelationshipSet_iface);
        heap_free(rel_enum);
    }

    return refcount;
}

static BOOL has_rel_collection_changed(const struct opc_rel_enum *rel_enum)
{
    return !IsEqualGUID(&rel_enum->id, &rel_enum->rel_set->id);
}

static HRESULT WINAPI opc_rel_enum_MoveNext(IOpcRelationshipEnumerator *iface, BOOL *has_next)
{
    struct opc_rel_enum *rel_enum = impl_from_IOpcRelationshipEnumerator(iface);

    TRACE("iface %p, has_next %p.\n", iface, has_next);

    if (!has_next)
        return E_POINTER;

    if (has_rel_collection_changed(rel_enum))
        return OPC_E_ENUM_COLLECTION_CHANGED;

    if (rel_enum->rel_set->count && (rel_enum->pos == ~(size_t)0 || rel_enum->pos < rel_enum->rel_set->count))
        rel_enum->pos++;

    *has_next = rel_enum->pos < rel_enum->rel_set->count;

    return S_OK;
}

static HRESULT WINAPI opc_rel_enum_MovePrevious(IOpcRelationshipEnumerator *iface, BOOL *has_previous)
{
    struct opc_rel_enum *rel_enum = impl_from_IOpcRelationshipEnumerator(iface);

    TRACE("iface %p, has_previous %p.\n", iface, has_previous);

    if (!has_previous)
        return E_POINTER;

    if (has_rel_collection_changed(rel_enum))
        return OPC_E_ENUM_COLLECTION_CHANGED;

    if (rel_enum->pos != ~(size_t)0)
        rel_enum->pos--;

    *has_previous = rel_enum->pos != ~(size_t)0;

    return S_OK;
}

static HRESULT WINAPI opc_rel_enum_GetCurrent(IOpcRelationshipEnumerator *iface, IOpcRelationship **rel)
{
    struct opc_rel_enum *rel_enum = impl_from_IOpcRelationshipEnumerator(iface);

    TRACE("iface %p, rel %p.\n", iface, rel);

    if (!rel)
        return E_POINTER;

    *rel = NULL;

    if (has_rel_collection_changed(rel_enum))
        return OPC_E_ENUM_COLLECTION_CHANGED;

    if (rel_enum->pos < rel_enum->rel_set->count)
    {
        *rel = &rel_enum->rel_set->relationships[rel_enum->pos]->IOpcRelationship_iface;
        IOpcRelationship_AddRef(*rel);
    }

    return *rel ? S_OK : OPC_E_ENUM_INVALID_POSITION;
}

static HRESULT WINAPI opc_rel_enum_Clone(IOpcRelationshipEnumerator *iface, IOpcRelationshipEnumerator **out)
{
    struct opc_rel_enum *rel_enum = impl_from_IOpcRelationshipEnumerator(iface);

    TRACE("iface %p, out %p.\n", iface, out);

    if (!out)
        return E_POINTER;

    if (has_rel_collection_changed(rel_enum))
    {
        *out = NULL;
        return OPC_E_ENUM_COLLECTION_CHANGED;
    }

    return opc_rel_enum_create(rel_enum->rel_set, out);
}

static const IOpcRelationshipEnumeratorVtbl opc_rel_enum_vtbl =
{
    opc_rel_enum_QueryInterface,
    opc_rel_enum_AddRef,
    opc_rel_enum_Release,
    opc_rel_enum_MoveNext,
    opc_rel_enum_MovePrevious,
    opc_rel_enum_GetCurrent,
    opc_rel_enum_Clone,
};

static HRESULT opc_rel_enum_create(struct opc_relationship_set *rel_set, IOpcRelationshipEnumerator **out)
{
    struct opc_rel_enum *rel_enum;

    if (!(rel_enum = heap_alloc_zero(sizeof(*rel_enum))))
        return E_OUTOFMEMORY;

    rel_enum->IOpcRelationshipEnumerator_iface.lpVtbl = &opc_rel_enum_vtbl;
    rel_enum->refcount = 1;
    rel_enum->rel_set = rel_set;
    IOpcRelationshipSet_AddRef(&rel_set->IOpcRelationshipSet_iface);
    rel_enum->pos = ~(size_t)0;
    rel_enum->id = rel_set->id;

    *out = &rel_enum->IOpcRelationshipEnumerator_iface;
    TRACE("Created relationship enumerator %p.\n", *out);
    return S_OK;
}

500 501 502 503 504 505 506 507 508 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
static HRESULT WINAPI opc_content_stream_QueryInterface(IStream *iface, REFIID iid, void **out)
{
    TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);

    if (IsEqualIID(iid, &IID_IStream) ||
            IsEqualIID(iid, &IID_ISequentialStream) ||
            IsEqualIID(iid, &IID_IUnknown))
    {
        *out = iface;
        IStream_AddRef(iface);
        return S_OK;
    }

    *out = NULL;
    WARN("Unsupported interface %s.\n", debugstr_guid(iid));
    return E_NOINTERFACE;
}

static ULONG WINAPI opc_content_stream_AddRef(IStream *iface)
{
    struct opc_content_stream *stream = impl_from_IStream(iface);
    ULONG refcount = InterlockedIncrement(&stream->refcount);

    TRACE("%p increasing refcount to %u.\n", iface, refcount);

    return refcount;
}

static ULONG WINAPI opc_content_stream_Release(IStream *iface)
{
    struct opc_content_stream *stream = impl_from_IStream(iface);
    ULONG refcount = InterlockedDecrement(&stream->refcount);

    TRACE("%p decreasing refcount to %u.\n", iface, refcount);

    if (!refcount)
    {
        opc_content_release(stream->content);
        heap_free(stream);
    }

    return refcount;
}

static HRESULT WINAPI opc_content_stream_Read(IStream *iface, void *buff, ULONG size, ULONG *num_read)
{
    struct opc_content_stream *stream = impl_from_IStream(iface);
    DWORD read = 0;

    TRACE("iface %p, buff %p, size %u, num_read %p.\n", iface, buff, size, num_read);

    if (!num_read)
        num_read = &read;

    if (stream->content->size.QuadPart - stream->pos.QuadPart < size)
        *num_read = stream->content->size.QuadPart - stream->pos.QuadPart;
    else
        *num_read = size;

    if (*num_read)
        memcpy(buff, stream->content->data + stream->pos.QuadPart, *num_read);

562 563
    stream->pos.QuadPart += *num_read;

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 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
    return S_OK;
}

static HRESULT WINAPI opc_content_stream_Write(IStream *iface, const void *data, ULONG size, ULONG *num_written)
{
    struct opc_content_stream *stream = impl_from_IStream(iface);
    DWORD written = 0;

    TRACE("iface %p, data %p, size %u, num_written %p.\n", iface, data, size, num_written);

    if (!num_written)
        num_written = &written;

    *num_written = 0;

    if (size > stream->content->size.QuadPart - stream->pos.QuadPart)
    {
        void *ptr = heap_realloc(stream->content->data, stream->pos.QuadPart + size);
        if (!ptr)
            return E_OUTOFMEMORY;
        stream->content->data = ptr;
    }

    memcpy(stream->content->data + stream->pos.QuadPart, data, size);
    stream->pos.QuadPart += size;
    stream->content->size.QuadPart += size;
    *num_written = size;

    return S_OK;
}

static HRESULT WINAPI opc_content_stream_Seek(IStream *iface, LARGE_INTEGER move, DWORD origin, ULARGE_INTEGER *newpos)
{
    struct opc_content_stream *stream = impl_from_IStream(iface);
    ULARGE_INTEGER pos;

    TRACE("iface %p, move %s, origin %d, newpos %p.\n", iface, wine_dbgstr_longlong(move.QuadPart), origin, newpos);

    switch (origin)
    {
    case STREAM_SEEK_SET:
        pos.QuadPart = move.QuadPart;
        break;
    case STREAM_SEEK_CUR:
        pos.QuadPart = stream->pos.QuadPart + move.QuadPart;
        break;
    case STREAM_SEEK_END:
        pos.QuadPart = stream->content->size.QuadPart + move.QuadPart;
612
        break;
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 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
    default:
        WARN("Unknown origin mode %d.\n", origin);
        return E_INVALIDARG;
    }

    stream->pos = pos;

    if (newpos)
        *newpos = stream->pos;

    return S_OK;
}

static HRESULT WINAPI opc_content_stream_SetSize(IStream *iface, ULARGE_INTEGER size)
{
    FIXME("iface %p, size %s stub!\n", iface, wine_dbgstr_longlong(size.QuadPart));

    return E_NOTIMPL;
}

static HRESULT WINAPI opc_content_stream_CopyTo(IStream *iface, IStream *dest, ULARGE_INTEGER size,
        ULARGE_INTEGER *num_read, ULARGE_INTEGER *written)
{
    FIXME("iface %p, dest %p, size %s, num_read %p, written %p stub!\n", iface, dest,
            wine_dbgstr_longlong(size.QuadPart), num_read, written);

    return E_NOTIMPL;
}

static HRESULT WINAPI opc_content_stream_Commit(IStream *iface, DWORD flags)
{
    FIXME("iface %p, flags %#x stub!\n", iface, flags);

    return E_NOTIMPL;
}

static HRESULT WINAPI opc_content_stream_Revert(IStream *iface)
{
    FIXME("iface %p stub!\n", iface);

    return E_NOTIMPL;
}

static HRESULT WINAPI opc_content_stream_LockRegion(IStream *iface, ULARGE_INTEGER offset,
        ULARGE_INTEGER size, DWORD lock_type)
{
    FIXME("iface %p, offset %s, size %s, lock_type %d stub!\n", iface, wine_dbgstr_longlong(offset.QuadPart),
            wine_dbgstr_longlong(size.QuadPart), lock_type);

    return E_NOTIMPL;
}

static HRESULT WINAPI opc_content_stream_UnlockRegion(IStream *iface, ULARGE_INTEGER offset, ULARGE_INTEGER size,
        DWORD lock_type)
{
    FIXME("iface %p, offset %s, size %s, lock_type %d stub!\n", iface, wine_dbgstr_longlong(offset.QuadPart),
            wine_dbgstr_longlong(size.QuadPart), lock_type);

    return E_NOTIMPL;
}

static HRESULT WINAPI opc_content_stream_Stat(IStream *iface, STATSTG *statstg, DWORD flag)
{
    FIXME("iface %p, statstg %p, flag %d stub!\n", iface, statstg, flag);

    return E_NOTIMPL;
}

static HRESULT WINAPI opc_content_stream_Clone(IStream *iface, IStream **result)
{
    FIXME("iface %p, result %p stub!\n", iface, result);

    return E_NOTIMPL;
}

static const IStreamVtbl opc_content_stream_vtbl =
{
    opc_content_stream_QueryInterface,
    opc_content_stream_AddRef,
    opc_content_stream_Release,
    opc_content_stream_Read,
    opc_content_stream_Write,
    opc_content_stream_Seek,
    opc_content_stream_SetSize,
    opc_content_stream_CopyTo,
    opc_content_stream_Commit,
    opc_content_stream_Revert,
    opc_content_stream_LockRegion,
    opc_content_stream_UnlockRegion,
    opc_content_stream_Stat,
    opc_content_stream_Clone,
};

static HRESULT opc_content_stream_create(struct opc_content *content, IStream **out)
{
    struct opc_content_stream *stream;

    if (!(stream = heap_alloc_zero(sizeof(*stream))))
        return E_OUTOFMEMORY;

    stream->IStream_iface.lpVtbl = &opc_content_stream_vtbl;
    stream->refcount = 1;
    stream->content = content;
    InterlockedIncrement(&content->refcount);

    *out = &stream->IStream_iface;

    TRACE("Created content stream %p.\n", *out);
    return S_OK;
}

724
static HRESULT opc_relationship_set_create(IOpcUri *source_uri, IOpcRelationshipSet **relationship_set);
725

726 727 728 729 730 731 732 733
static WCHAR *opc_strdupW(const WCHAR *str)
{
    WCHAR *ret = NULL;

    if (str)
    {
        size_t size;

734
        size = (lstrlenW(str) + 1) * sizeof(WCHAR);
735 736 737 738 739 740 741 742
        ret = CoTaskMemAlloc(size);
        if (ret)
            memcpy(ret, str, size);
    }

    return ret;
}

743 744 745 746 747 748 749 750 751 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
static HRESULT WINAPI opc_part_QueryInterface(IOpcPart *iface, REFIID iid, void **out)
{
    TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);

    if (IsEqualIID(iid, &IID_IOpcPart) ||
            IsEqualIID(iid, &IID_IUnknown))
    {
        *out = iface;
        IOpcPart_AddRef(iface);
        return S_OK;
    }

    WARN("Unsupported interface %s.\n", debugstr_guid(iid));
    return E_NOINTERFACE;
}

static ULONG WINAPI opc_part_AddRef(IOpcPart *iface)
{
    struct opc_part *part = impl_from_IOpcPart(iface);
    ULONG refcount = InterlockedIncrement(&part->refcount);

    TRACE("%p increasing refcount to %u.\n", iface, refcount);

    return refcount;
}

static ULONG WINAPI opc_part_Release(IOpcPart *iface)
{
    struct opc_part *part = impl_from_IOpcPart(iface);
    ULONG refcount = InterlockedDecrement(&part->refcount);

    TRACE("%p decreasing refcount to %u.\n", iface, refcount);

    if (!refcount)
777
    {
778 779
        if (part->relationship_set)
            IOpcRelationshipSet_Release(part->relationship_set);
780 781
        IOpcPartUri_Release(part->name);
        CoTaskMemFree(part->content_type);
782
        opc_content_release(part->content);
783
        heap_free(part);
784
    }
785 786 787 788 789 790

    return refcount;
}

static HRESULT WINAPI opc_part_GetRelationshipSet(IOpcPart *iface, IOpcRelationshipSet **relationship_set)
{
791 792
    struct opc_part *part = impl_from_IOpcPart(iface);
    HRESULT hr;
793

794 795
    TRACE("iface %p, relationship_set %p.\n", iface, relationship_set);

796
    if (!part->relationship_set && FAILED(hr = opc_relationship_set_create((IOpcUri *)part->name, &part->relationship_set)))
797 798 799 800 801 802
        return hr;

    *relationship_set = part->relationship_set;
    IOpcRelationshipSet_AddRef(*relationship_set);

    return S_OK;
803 804 805 806
}

static HRESULT WINAPI opc_part_GetContentStream(IOpcPart *iface, IStream **stream)
{
807
    struct opc_part *part = impl_from_IOpcPart(iface);
808

809 810 811 812 813 814
    TRACE("iface %p, stream %p.\n", iface, stream);

    if (!stream)
        return E_POINTER;

    return opc_content_stream_create(part->content, stream);
815 816 817 818
}

static HRESULT WINAPI opc_part_GetName(IOpcPart *iface, IOpcPartUri **name)
{
819
    struct opc_part *part = impl_from_IOpcPart(iface);
820

821 822 823 824 825 826
    TRACE("iface %p, name %p.\n", iface, name);

    *name = part->name;
    IOpcPartUri_AddRef(*name);

    return S_OK;
827 828 829 830
}

static HRESULT WINAPI opc_part_GetContentType(IOpcPart *iface, LPWSTR *type)
{
831
    struct opc_part *part = impl_from_IOpcPart(iface);
832

833 834 835 836
    TRACE("iface %p, type %p.\n", iface, type);

    *type = opc_strdupW(part->content_type);
    return *type ? S_OK : E_OUTOFMEMORY;
837 838 839 840
}

static HRESULT WINAPI opc_part_GetCompressionOptions(IOpcPart *iface, OPC_COMPRESSION_OPTIONS *options)
{
841
    struct opc_part *part = impl_from_IOpcPart(iface);
842

843 844 845 846
    TRACE("iface %p, options %p.\n", iface, options);

    *options = part->compression_options;
    return S_OK;
847 848 849 850 851 852 853 854 855 856 857 858 859 860
}

static const IOpcPartVtbl opc_part_vtbl =
{
    opc_part_QueryInterface,
    opc_part_AddRef,
    opc_part_Release,
    opc_part_GetRelationshipSet,
    opc_part_GetContentStream,
    opc_part_GetName,
    opc_part_GetContentType,
    opc_part_GetCompressionOptions,
};

861
static HRESULT opc_part_create(struct opc_part_set *set, IOpcPartUri *name, const WCHAR *content_type,
862
        DWORD compression_options, IOpcPart **out)
863 864 865
{
    struct opc_part *part;

866 867 868
    if (!opc_array_reserve((void **)&set->parts, &set->size, set->count + 1, sizeof(*set->parts)))
        return E_OUTOFMEMORY;

869 870 871 872 873
    if (!(part = heap_alloc_zero(sizeof(*part))))
        return E_OUTOFMEMORY;

    part->IOpcPart_iface.lpVtbl = &opc_part_vtbl;
    part->refcount = 1;
874 875 876 877 878 879 880 881
    part->name = name;
    IOpcPartUri_AddRef(name);
    part->compression_options = compression_options;
    if (!(part->content_type = opc_strdupW(content_type)))
    {
        IOpcPart_Release(&part->IOpcPart_iface);
        return E_OUTOFMEMORY;
    }
882

883 884 885 886 887 888 889 890
    part->content = heap_alloc_zero(sizeof(*part->content));
    if (!part->content)
    {
        IOpcPart_Release(&part->IOpcPart_iface);
        return E_OUTOFMEMORY;
    }
    part->content->refcount = 1;

891 892
    set->parts[set->count++] = part;
    IOpcPart_AddRef(&part->IOpcPart_iface);
893
    CoCreateGuid(&set->id);
894

895 896 897 898 899
    *out = &part->IOpcPart_iface;
    TRACE("Created part %p.\n", *out);
    return S_OK;
}

900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
static struct opc_part *opc_part_set_get_part(const struct opc_part_set *part_set, IOpcPartUri *name)
{
    BOOL is_equal;
    size_t i;

    for (i = 0; i < part_set->count; ++i)
    {
        is_equal = FALSE;
        if (IOpcPartUri_IsEqual(part_set->parts[i]->name, (IUri *)name, &is_equal) == S_OK && is_equal)
            return part_set->parts[i];
    }

    return NULL;
}

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
static HRESULT WINAPI opc_part_set_QueryInterface(IOpcPartSet *iface, REFIID iid, void **out)
{
    TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);

    if (IsEqualIID(iid, &IID_IOpcPartSet) ||
            IsEqualIID(iid, &IID_IUnknown))
    {
        *out = iface;
        IOpcPartSet_AddRef(iface);
        return S_OK;
    }

    WARN("Unsupported interface %s.\n", debugstr_guid(iid));
    return E_NOINTERFACE;
}

static ULONG WINAPI opc_part_set_AddRef(IOpcPartSet *iface)
{
    struct opc_part_set *part_set = impl_from_IOpcPartSet(iface);
    ULONG refcount = InterlockedIncrement(&part_set->refcount);

    TRACE("%p increasing refcount to %u.\n", iface, refcount);

    return refcount;
}

static ULONG WINAPI opc_part_set_Release(IOpcPartSet *iface)
{
    struct opc_part_set *part_set = impl_from_IOpcPartSet(iface);
    ULONG refcount = InterlockedDecrement(&part_set->refcount);

    TRACE("%p decreasing refcount to %u.\n", iface, refcount);

    if (!refcount)
949 950 951 952 953 954
    {
        size_t i;

        for (i = 0; i < part_set->count; ++i)
            IOpcPart_Release(&part_set->parts[i]->IOpcPart_iface);
        heap_free(part_set->parts);
955
        heap_free(part_set);
956
    }
957 958 959 960

    return refcount;
}

961
static HRESULT WINAPI opc_part_set_GetPart(IOpcPartSet *iface, IOpcPartUri *name, IOpcPart **out)
962
{
963 964
    struct opc_part_set *part_set = impl_from_IOpcPartSet(iface);
    struct opc_part *part;
965

966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
    TRACE("iface %p, name %p, out %p.\n", iface, name, out);

    if (!out)
        return E_POINTER;

    *out = NULL;

    if (!name)
        return E_POINTER;

    if ((part = opc_part_set_get_part(part_set, name)))
    {
        *out = &part->IOpcPart_iface;
        IOpcPart_AddRef(*out);
    }

    return *out ? S_OK : OPC_E_NO_SUCH_PART;
983 984 985 986 987
}

static HRESULT WINAPI opc_part_set_CreatePart(IOpcPartSet *iface, IOpcPartUri *name, LPCWSTR content_type,
        OPC_COMPRESSION_OPTIONS compression_options, IOpcPart **part)
{
988 989
    struct opc_part_set *part_set = impl_from_IOpcPartSet(iface);

990
    TRACE("iface %p, name %p, content_type %s, compression_options %#x, part %p.\n", iface, name,
991 992
            debugstr_w(content_type), compression_options, part);

993 994 995 996 997 998 999 1000 1001 1002 1003
    if (!part)
        return E_POINTER;

    *part = NULL;

    if (!name)
        return E_POINTER;

    if (opc_part_set_get_part(part_set, name))
        return OPC_E_DUPLICATE_PART;

1004
    return opc_part_create(part_set, name, content_type, compression_options, part);
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
}

static HRESULT WINAPI opc_part_set_DeletePart(IOpcPartSet *iface, IOpcPartUri *name)
{
    FIXME("iface %p, name %p stub!\n", iface, name);

    return E_NOTIMPL;
}

static HRESULT WINAPI opc_part_set_PartExists(IOpcPartSet *iface, IOpcPartUri *name, BOOL *exists)
{
1016
    struct opc_part_set *part_set = impl_from_IOpcPartSet(iface);
1017

1018 1019 1020 1021 1022 1023 1024 1025
    TRACE("iface %p, name %p, exists %p.\n", iface, name, exists);

    if (!name || !exists)
        return E_POINTER;

    *exists = opc_part_set_get_part(part_set, name) != NULL;

    return S_OK;
1026 1027
}

1028
static HRESULT WINAPI opc_part_set_GetEnumerator(IOpcPartSet *iface, IOpcPartEnumerator **enumerator)
1029
{
1030
    struct opc_part_set *part_set = impl_from_IOpcPartSet(iface);
1031

1032 1033 1034 1035 1036 1037
    TRACE("iface %p, enumerator %p.\n", iface, enumerator);

    if (!enumerator)
        return E_POINTER;

    return opc_part_enum_create(part_set, enumerator);
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
}

static const IOpcPartSetVtbl opc_part_set_vtbl =
{
    opc_part_set_QueryInterface,
    opc_part_set_AddRef,
    opc_part_set_Release,
    opc_part_set_GetPart,
    opc_part_set_CreatePart,
    opc_part_set_DeletePart,
    opc_part_set_PartExists,
1049
    opc_part_set_GetEnumerator,
1050 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
static HRESULT WINAPI opc_relationship_QueryInterface(IOpcRelationship *iface, REFIID iid, void **out)
{
    TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);

    if (IsEqualIID(iid, &IID_IOpcRelationship) ||
            IsEqualIID(iid, &IID_IUnknown))
    {
        *out = iface;
        IOpcRelationship_AddRef(iface);
        return S_OK;
    }

    WARN("Unsupported interface %s.\n", debugstr_guid(iid));
    return E_NOINTERFACE;
}

static ULONG WINAPI opc_relationship_AddRef(IOpcRelationship *iface)
{
    struct opc_relationship *relationship = impl_from_IOpcRelationship(iface);
    ULONG refcount = InterlockedIncrement(&relationship->refcount);

    TRACE("%p increasing refcount to %u.\n", iface, refcount);

    return refcount;
}

static ULONG WINAPI opc_relationship_Release(IOpcRelationship *iface)
{
    struct opc_relationship *relationship = impl_from_IOpcRelationship(iface);
    ULONG refcount = InterlockedDecrement(&relationship->refcount);

    TRACE("%p decreasing refcount to %u.\n", iface, refcount);

    if (!refcount)
1086 1087
    {
        CoTaskMemFree(relationship->id);
1088
        CoTaskMemFree(relationship->type);
1089
        IOpcUri_Release(relationship->source_uri);
1090
        IUri_Release(relationship->target);
1091
        heap_free(relationship);
1092
    }
1093 1094 1095 1096 1097 1098

    return refcount;
}

static HRESULT WINAPI opc_relationship_GetId(IOpcRelationship *iface, WCHAR **id)
{
1099
    struct opc_relationship *relationship = impl_from_IOpcRelationship(iface);
1100

1101 1102 1103 1104
    TRACE("iface %p, id %p.\n", iface, id);

    *id = opc_strdupW(relationship->id);
    return *id ? S_OK : E_OUTOFMEMORY;
1105 1106 1107 1108
}

static HRESULT WINAPI opc_relationship_GetRelationshipType(IOpcRelationship *iface, WCHAR **type)
{
1109
    struct opc_relationship *relationship = impl_from_IOpcRelationship(iface);
1110

1111 1112 1113 1114
    TRACE("iface %p, type %p.\n", iface, type);

    *type = opc_strdupW(relationship->type);
    return *type ? S_OK : E_OUTOFMEMORY;
1115 1116 1117 1118
}

static HRESULT WINAPI opc_relationship_GetSourceUri(IOpcRelationship *iface, IOpcUri **uri)
{
1119
    struct opc_relationship *relationship = impl_from_IOpcRelationship(iface);
1120

1121 1122 1123 1124 1125 1126
    TRACE("iface %p, uri %p.\n", iface, uri);

    *uri = relationship->source_uri;
    IOpcUri_AddRef(*uri);

    return S_OK;
1127 1128 1129 1130
}

static HRESULT WINAPI opc_relationship_GetTargetUri(IOpcRelationship *iface, IUri **target)
{
1131
    struct opc_relationship *relationship = impl_from_IOpcRelationship(iface);
1132

1133 1134 1135 1136 1137 1138
    TRACE("iface %p, target %p.\n", iface, target);

    *target = relationship->target;
    IUri_AddRef(*target);

    return S_OK;
1139 1140 1141 1142
}

static HRESULT WINAPI opc_relationship_GetTargetMode(IOpcRelationship *iface, OPC_URI_TARGET_MODE *target_mode)
{
1143
    struct opc_relationship *relationship = impl_from_IOpcRelationship(iface);
1144

1145 1146 1147 1148 1149
    TRACE("iface %p, target_mode %p.\n", iface, target_mode);

    *target_mode = relationship->target_mode;

    return S_OK;
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
}

static const IOpcRelationshipVtbl opc_relationship_vtbl =
{
    opc_relationship_QueryInterface,
    opc_relationship_AddRef,
    opc_relationship_Release,
    opc_relationship_GetId,
    opc_relationship_GetRelationshipType,
    opc_relationship_GetSourceUri,
    opc_relationship_GetTargetUri,
    opc_relationship_GetTargetMode,
};

1164 1165 1166 1167 1168 1169 1170
static struct opc_relationship *opc_relationshipset_get_rel(struct opc_relationship_set *relationship_set,
        const WCHAR *id)
{
    size_t i;

    for (i = 0; i < relationship_set->count; i++)
    {
1171
        if (!wcscmp(id, relationship_set->relationships[i]->id))
1172 1173 1174 1175 1176 1177
            return relationship_set->relationships[i];
    }

    return NULL;
}

1178 1179
static HRESULT opc_relationship_create(struct opc_relationship_set *set, const WCHAR *id, const WCHAR *type,
        IUri *target_uri, OPC_URI_TARGET_MODE target_mode, IOpcRelationship **out)
1180 1181 1182
{
    struct opc_relationship *relationship;

1183 1184 1185
    if (!opc_array_reserve((void **)&set->relationships, &set->size, set->count + 1, sizeof(*set->relationships)))
        return E_OUTOFMEMORY;

1186 1187 1188 1189 1190 1191
    if (!(relationship = heap_alloc_zero(sizeof(*relationship))))
        return E_OUTOFMEMORY;

    relationship->IOpcRelationship_iface.lpVtbl = &opc_relationship_vtbl;
    relationship->refcount = 1;

1192 1193
    relationship->target = target_uri;
    IUri_AddRef(relationship->target);
1194 1195
    relationship->source_uri = set->source_uri;
    IOpcUri_AddRef(relationship->source_uri);
1196

1197 1198 1199 1200 1201 1202 1203 1204 1205
    if (id)
        relationship->id = opc_strdupW(id);
    else
    {
        relationship->id = CoTaskMemAlloc(10 * sizeof(WCHAR));
        if (relationship->id)
        {
            DWORD generated;

1206
            /* FIXME: test that generated id is unique */
1207
            RtlGenRandom(&generated, sizeof(generated));
1208
            swprintf(relationship->id, 10, L"R%08X", generated);
1209 1210 1211 1212 1213 1214 1215

            if (opc_relationshipset_get_rel(set, relationship->id))
            {
                WARN("Newly generated id %s already exists.\n", debugstr_w(relationship->id));
                IOpcRelationship_Release(&relationship->IOpcRelationship_iface);
                return E_FAIL;
            }
1216 1217 1218
        }
    }

1219 1220
    relationship->type = opc_strdupW(type);
    if (!relationship->id || !relationship->type)
1221
    {
1222
        IOpcRelationship_Release(&relationship->IOpcRelationship_iface);
1223 1224 1225
        return E_OUTOFMEMORY;
    }

1226 1227
    set->relationships[set->count++] = relationship;
    IOpcRelationship_AddRef(&relationship->IOpcRelationship_iface);
1228
    CoCreateGuid(&set->id);
1229

1230 1231 1232 1233 1234
    *out = &relationship->IOpcRelationship_iface;
    TRACE("Created relationship %p.\n", *out);
    return S_OK;
}

1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
static HRESULT WINAPI opc_relationship_set_QueryInterface(IOpcRelationshipSet *iface, REFIID iid, void **out)
{
    TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);

    if (IsEqualIID(iid, &IID_IOpcRelationshipSet) ||
            IsEqualIID(iid, &IID_IUnknown))
    {
        *out = iface;
        IOpcRelationshipSet_AddRef(iface);
        return S_OK;
    }

    WARN("Unsupported interface %s.\n", debugstr_guid(iid));
    return E_NOINTERFACE;
}

static ULONG WINAPI opc_relationship_set_AddRef(IOpcRelationshipSet *iface)
{
    struct opc_relationship_set *relationship_set = impl_from_IOpcRelationshipSet(iface);
    ULONG refcount = InterlockedIncrement(&relationship_set->refcount);

    TRACE("%p increasing refcount to %u.\n", iface, refcount);

    return refcount;
}

static ULONG WINAPI opc_relationship_set_Release(IOpcRelationshipSet *iface)
{
    struct opc_relationship_set *relationship_set = impl_from_IOpcRelationshipSet(iface);
    ULONG refcount = InterlockedDecrement(&relationship_set->refcount);

    TRACE("%p decreasing refcount to %u.\n", iface, refcount);

    if (!refcount)
1269 1270 1271 1272 1273
    {
        size_t i;

        for (i = 0; i < relationship_set->count; ++i)
            IOpcRelationship_Release(&relationship_set->relationships[i]->IOpcRelationship_iface);
1274
        IOpcUri_Release(relationship_set->source_uri);
1275
        heap_free(relationship_set->relationships);
1276
        heap_free(relationship_set);
1277
    }
1278 1279 1280 1281 1282 1283 1284

    return refcount;
}

static HRESULT WINAPI opc_relationship_set_GetRelationship(IOpcRelationshipSet *iface, const WCHAR *id,
        IOpcRelationship **relationship)
{
1285
    struct opc_relationship_set *relationship_set = impl_from_IOpcRelationshipSet(iface);
1286
    struct opc_relationship *ret;
1287

1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
    TRACE("iface %p, id %s, relationship %p.\n", iface, debugstr_w(id), relationship);

    if (!relationship)
        return E_POINTER;

    *relationship = NULL;

    if (!id)
        return E_POINTER;

1298
    if ((ret = opc_relationshipset_get_rel(relationship_set, id)))
1299
    {
1300 1301
        *relationship = &ret->IOpcRelationship_iface;
        IOpcRelationship_AddRef(*relationship);
1302 1303 1304
    }

    return *relationship ? S_OK : OPC_E_NO_SUCH_RELATIONSHIP;
1305 1306 1307 1308 1309
}

static HRESULT WINAPI opc_relationship_set_CreateRelationship(IOpcRelationshipSet *iface, const WCHAR *id,
        const WCHAR *type, IUri *target_uri, OPC_URI_TARGET_MODE target_mode, IOpcRelationship **relationship)
{
1310
    struct opc_relationship_set *relationship_set = impl_from_IOpcRelationshipSet(iface);
1311
    DWORD length;
1312

1313
    TRACE("iface %p, id %s, type %s, target_uri %p, target_mode %d, relationship %p.\n", iface, debugstr_w(id),
1314 1315
            debugstr_w(type), target_uri, target_mode, relationship);

1316 1317 1318 1319 1320
    if (!relationship)
        return E_POINTER;

    *relationship = NULL;

1321 1322 1323
    if (!type || !target_uri)
        return E_POINTER;

1324 1325 1326
    if (id && opc_relationshipset_get_rel(relationship_set, id))
        return OPC_E_DUPLICATE_RELATIONSHIP;

1327 1328 1329 1330
    if (IUri_GetPropertyLength(target_uri, Uri_PROPERTY_SCHEME_NAME, &length, 0) == S_OK && length != 0
            && target_mode == OPC_URI_TARGET_MODE_INTERNAL)
        return OPC_E_INVALID_RELATIONSHIP_TARGET;

1331
    return opc_relationship_create(relationship_set, id, type, target_uri, target_mode, relationship);
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
}

static HRESULT WINAPI opc_relationship_set_DeleteRelationship(IOpcRelationshipSet *iface, const WCHAR *id)
{
    FIXME("iface %p, id %s stub!\n", iface, debugstr_w(id));

    return E_NOTIMPL;
}

static HRESULT WINAPI opc_relationship_set_RelationshipExists(IOpcRelationshipSet *iface, const WCHAR *id, BOOL *exists)
{
1343
    struct opc_relationship_set *relationship_set = impl_from_IOpcRelationshipSet(iface);
1344

1345 1346 1347 1348 1349
    TRACE("iface %p, id %s, exists %p.\n", iface, debugstr_w(id), exists);

    if (!id || !exists)
        return E_POINTER;

1350
    *exists = opc_relationshipset_get_rel(relationship_set, id) != NULL;
1351 1352

    return S_OK;
1353 1354 1355 1356 1357
}

static HRESULT WINAPI opc_relationship_set_GetEnumerator(IOpcRelationshipSet *iface,
        IOpcRelationshipEnumerator **enumerator)
{
1358
    struct opc_relationship_set *relationship_set = impl_from_IOpcRelationshipSet(iface);
1359

1360 1361 1362 1363 1364 1365
    TRACE("iface %p, enumerator %p.\n", iface, enumerator);

    if (!enumerator)
        return E_POINTER;

    return opc_rel_enum_create(relationship_set, enumerator);
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
}

static HRESULT WINAPI opc_relationship_set_GetEnumeratorForType(IOpcRelationshipSet *iface, const WCHAR *type,
        IOpcRelationshipEnumerator **enumerator)
{
    FIXME("iface %p, type %s, enumerator %p stub!\n", iface, debugstr_w(type), enumerator);

    return E_NOTIMPL;
}

static HRESULT WINAPI opc_relationship_set_GetRelationshipsContentStream(IOpcRelationshipSet *iface, IStream **stream)
{
    FIXME("iface %p, stream %p stub!\n", iface, stream);

    return E_NOTIMPL;
}

static const IOpcRelationshipSetVtbl opc_relationship_set_vtbl =
{
    opc_relationship_set_QueryInterface,
    opc_relationship_set_AddRef,
    opc_relationship_set_Release,
    opc_relationship_set_GetRelationship,
    opc_relationship_set_CreateRelationship,
    opc_relationship_set_DeleteRelationship,
    opc_relationship_set_RelationshipExists,
    opc_relationship_set_GetEnumerator,
    opc_relationship_set_GetEnumeratorForType,
    opc_relationship_set_GetRelationshipsContentStream,
};

1397
static HRESULT opc_relationship_set_create(IOpcUri *source_uri, IOpcRelationshipSet **out)
1398 1399 1400 1401 1402 1403 1404 1405
{
    struct opc_relationship_set *relationship_set;

    if (!(relationship_set = heap_alloc_zero(sizeof(*relationship_set))))
        return E_OUTOFMEMORY;

    relationship_set->IOpcRelationshipSet_iface.lpVtbl = &opc_relationship_set_vtbl;
    relationship_set->refcount = 1;
1406 1407
    relationship_set->source_uri = source_uri;
    IOpcUri_AddRef(relationship_set->source_uri);
1408 1409 1410 1411 1412 1413

    *out = &relationship_set->IOpcRelationshipSet_iface;
    TRACE("Created relationship set %p.\n", *out);
    return S_OK;
}

1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
static HRESULT WINAPI opc_package_QueryInterface(IOpcPackage *iface, REFIID iid, void **out)
{
    TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);

    if (IsEqualIID(iid, &IID_IOpcPackage) ||
            IsEqualIID(iid, &IID_IUnknown))
    {
        *out = iface;
        IOpcPackage_AddRef(iface);
        return S_OK;
    }

    WARN("Unsupported interface %s.\n", debugstr_guid(iid));
    return E_NOINTERFACE;
}

static ULONG WINAPI opc_package_AddRef(IOpcPackage *iface)
{
    struct opc_package *package = impl_from_IOpcPackage(iface);
    ULONG refcount = InterlockedIncrement(&package->refcount);

    TRACE("%p increasing refcount to %u.\n", iface, refcount);

    return refcount;
}

static ULONG WINAPI opc_package_Release(IOpcPackage *iface)
{
    struct opc_package *package = impl_from_IOpcPackage(iface);
    ULONG refcount = InterlockedDecrement(&package->refcount);

    TRACE("%p decreasing refcount to %u.\n", iface, refcount);

    if (!refcount)
1448 1449 1450
    {
        if (package->part_set)
            IOpcPartSet_Release(package->part_set);
1451 1452
        if (package->relationship_set)
            IOpcRelationshipSet_Release(package->relationship_set);
1453 1454
        if (package->source_uri)
            IOpcUri_Release(package->source_uri);
1455
        heap_free(package);
1456
    }
1457 1458 1459 1460 1461 1462

    return refcount;
}

static HRESULT WINAPI opc_package_GetPartSet(IOpcPackage *iface, IOpcPartSet **part_set)
{
1463
    struct opc_package *package = impl_from_IOpcPackage(iface);
1464

1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
    TRACE("iface %p, part_set %p.\n", iface, part_set);

    if (!package->part_set)
    {
        struct opc_part_set *part_set = heap_alloc_zero(sizeof(*part_set));
        if (!part_set)
            return E_OUTOFMEMORY;

        part_set->IOpcPartSet_iface.lpVtbl = &opc_part_set_vtbl;
        part_set->refcount = 1;

        package->part_set = &part_set->IOpcPartSet_iface;
    }

    *part_set = package->part_set;
    IOpcPartSet_AddRef(*part_set);

    return S_OK;
1483 1484 1485 1486
}

static HRESULT WINAPI opc_package_GetRelationshipSet(IOpcPackage *iface, IOpcRelationshipSet **relationship_set)
{
1487 1488
    struct opc_package *package = impl_from_IOpcPackage(iface);
    HRESULT hr;
1489

1490 1491
    TRACE("iface %p, relationship_set %p.\n", iface, relationship_set);

1492 1493 1494
    if (!package->relationship_set)
    {
        if (FAILED(hr = opc_relationship_set_create(package->source_uri, &package->relationship_set)))
1495
            return hr;
1496
    }
1497 1498 1499 1500 1501

    *relationship_set = package->relationship_set;
    IOpcRelationshipSet_AddRef(*relationship_set);

    return S_OK;
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
}

static const IOpcPackageVtbl opc_package_vtbl =
{
    opc_package_QueryInterface,
    opc_package_AddRef,
    opc_package_Release,
    opc_package_GetPartSet,
    opc_package_GetRelationshipSet,
};

1513
HRESULT opc_package_create(IOpcFactory *factory, IOpcPackage **out)
1514 1515
{
    struct opc_package *package;
1516
    HRESULT hr;
1517

1518
    if (!(package = heap_alloc_zero(sizeof(*package))))
1519 1520 1521 1522 1523
        return E_OUTOFMEMORY;

    package->IOpcPackage_iface.lpVtbl = &opc_package_vtbl;
    package->refcount = 1;

1524 1525 1526 1527 1528 1529
    if (FAILED(hr = IOpcFactory_CreatePackageRootUri(factory, &package->source_uri)))
    {
        heap_free(package);
        return hr;
    }

1530 1531 1532 1533
    *out = &package->IOpcPackage_iface;
    TRACE("Created package %p.\n", *out);
    return S_OK;
}
1534

1535 1536 1537
struct content_types
{
    struct list types;
1538
    BOOL has_rels_part;
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
};

enum content_type_element
{
    CONTENT_TYPE_DEFAULT,
    CONTENT_TYPE_OVERRIDE,
};

struct content_type
{
    struct list entry;
    enum content_type_element element;
    union
    {
        struct default_type
        {
            WCHAR *ext;
            WCHAR *type;
        } def;
        struct override_type
        {
            IOpcPart *part;
        } override;
    } u;
};

static HRESULT opc_package_add_override_content_type(struct content_types *types, IOpcPart *part)
{
    struct content_type *type;

    if (!(type = heap_alloc(sizeof(*type))))
        return E_OUTOFMEMORY;

    type->element = CONTENT_TYPE_OVERRIDE;
    type->u.override.part = part;
    IOpcPart_AddRef(part);

    list_add_tail(&types->types, &type->entry);

    return S_OK;
}

static HRESULT opc_package_add_default_content_type(struct content_types *types,
        const WCHAR *ext, const WCHAR *content_type)
{
    struct content_type *type;

    if (!(type = heap_alloc(sizeof(*type))))
        return E_OUTOFMEMORY;

    type->element = CONTENT_TYPE_DEFAULT;
    type->u.def.ext = opc_strdupW(ext);
    type->u.def.type = opc_strdupW(content_type);
    if (!type->u.def.ext || !type->u.def.type)
    {
        CoTaskMemFree(type->u.def.ext);
        CoTaskMemFree(type->u.def.type);
        heap_free(type);
        return E_OUTOFMEMORY;
    }

    list_add_tail(&types->types, &type->entry);

    return S_OK;
}

static HRESULT opc_package_add_content_type(struct content_types *types, IOpcPart *part)
{
    struct content_type *cur;
    BSTR ext, content_type;
    BOOL added = FALSE;
    IOpcPartUri *name;
    HRESULT hr;

    if (FAILED(hr = IOpcPart_GetName(part, &name)))
        return hr;

    hr = IOpcPartUri_GetExtension(name, &ext);
    IOpcPartUri_Release(name);
    if (hr == S_FALSE)
    {
        hr = opc_package_add_override_content_type(types, part);
        SysFreeString(ext);
        return hr;
    }

    if (FAILED(hr))
        return hr;

    if (FAILED(hr = IOpcPart_GetContentType(part, &content_type)))
        return hr;

    LIST_FOR_EACH_ENTRY(cur, &types->types, struct content_type, entry)
    {
        if (cur->element == CONTENT_TYPE_OVERRIDE)
            continue;

1636
        if (!wcsicmp(cur->u.def.ext, ext))
1637 1638 1639
        {
            added = TRUE;

1640
            if (!wcscmp(cur->u.def.type, content_type))
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
                break;

            hr = opc_package_add_override_content_type(types, part);
            break;
        }
    }

    if (!added)
        hr = opc_package_add_default_content_type(types, ext, content_type);

    SysFreeString(ext);
    SysFreeString(content_type);

    return hr;
}

1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
static BOOL opc_package_has_rels_part(IOpcRelationshipSet *rel_set)
{
    IOpcRelationshipEnumerator *enumerator;
    BOOL has_next;
    HRESULT hr;

    if (FAILED(hr = IOpcRelationshipSet_GetEnumerator(rel_set, &enumerator)))
        return FALSE;

    has_next = FALSE;
    IOpcRelationshipEnumerator_MoveNext(enumerator, &has_next);
    IOpcRelationshipEnumerator_Release(enumerator);

    return has_next;
}

1673 1674 1675
static HRESULT opc_package_collect_content_types(IOpcPackage *package, struct content_types *types)
{
    IOpcPartEnumerator *enumerator;
1676
    IOpcRelationshipSet *rel_set;
1677 1678 1679 1680 1681 1682 1683
    IOpcPartSet *parts;
    BOOL has_next;
    HRESULT hr;

    if (FAILED(hr = IOpcPackage_GetPartSet(package, &parts)))
        return hr;

1684 1685 1686 1687 1688 1689 1690
    hr = IOpcPackage_GetRelationshipSet(package, &rel_set);
    if (SUCCEEDED(hr))
    {
        types->has_rels_part |= opc_package_has_rels_part(rel_set);
        IOpcRelationshipSet_Release(rel_set);
    }

1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
    hr = IOpcPartSet_GetEnumerator(parts, &enumerator);
    IOpcPartSet_Release(parts);
    if (FAILED(hr))
        return hr;

    if (FAILED(hr = IOpcPartEnumerator_MoveNext(enumerator, &has_next)) || !has_next)
    {
        IOpcPartEnumerator_Release(enumerator);
        return hr;
    }

    while (has_next)
    {
        IOpcPart *part;

        if (FAILED(hr = IOpcPartEnumerator_GetCurrent(enumerator, &part)))
            break;

1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
        if (!types->has_rels_part)
        {
            hr = IOpcPart_GetRelationshipSet(part, &rel_set);
            if (SUCCEEDED(hr))
            {
                types->has_rels_part |= opc_package_has_rels_part(rel_set);
                IOpcRelationshipSet_Release(rel_set);
            }
        }

1719 1720 1721 1722 1723
        hr = opc_package_add_content_type(types, part);
        IOpcPart_Release(part);
        if (FAILED(hr))
            break;

1724 1725
        if (FAILED(hr = IOpcPartEnumerator_MoveNext(enumerator, &has_next)))
            break;
1726 1727 1728 1729 1730 1731 1732
    }

    IOpcPartEnumerator_Release(enumerator);

    return hr;
}

1733 1734 1735 1736
static HRESULT opc_package_write_default_type(const WCHAR *ext, const WCHAR *type, IXmlWriter *writer)
{
    HRESULT hr;

1737
    hr = IXmlWriter_WriteStartElement(writer, NULL, L"Default", NULL);
1738
    if (SUCCEEDED(hr))
1739
        hr = IXmlWriter_WriteAttributeString(writer, NULL, L"Extension", NULL, ext);
1740
    if (SUCCEEDED(hr))
1741
        hr = IXmlWriter_WriteAttributeString(writer, NULL, L"ContentType", NULL, type);
1742 1743 1744
    return hr;
}

1745
static HRESULT opc_package_write_contenttypes(IOpcPackage *package, struct zip_archive *archive, IXmlWriter *writer)
1746
{
1747 1748 1749
    struct content_type *content_type, *content_type2;
    struct content_types types;
    IStream *content = NULL;
1750 1751
    HRESULT hr;

1752
    list_init(&types.types);
1753
    types.has_rels_part = FALSE;
1754

1755 1756 1757 1758 1759
    hr = CreateStreamOnHGlobal(NULL, TRUE, &content);
    if (SUCCEEDED(hr))
        hr = opc_package_collect_content_types(package, &types);
    if (SUCCEEDED(hr))
        hr = IXmlWriter_SetOutput(writer, (IUnknown *)content);
1760 1761
    if (SUCCEEDED(hr))
        hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Omit);
1762
    if (SUCCEEDED(hr))
1763 1764
        hr = IXmlWriter_WriteStartElement(writer, NULL, L"Types",
                L"http://schemas.openxmlformats.org/package/2006/content-types");
1765

1766 1767
    if (SUCCEEDED(hr) && types.has_rels_part)
    {
1768 1769
        hr = opc_package_write_default_type(L"rels",
                L"application/vnd.openxmlformats-package.relationships+xml", writer);
1770 1771 1772 1773
        if (SUCCEEDED(hr))
            hr = IXmlWriter_WriteEndElement(writer);
    }

1774 1775 1776 1777
    LIST_FOR_EACH_ENTRY_SAFE(content_type, content_type2, &types.types, struct content_type, entry)
    {
        if (content_type->element == CONTENT_TYPE_DEFAULT)
        {
1778
            hr = opc_package_write_default_type(content_type->u.def.ext + 1, content_type->u.def.type, writer);
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789

            CoTaskMemFree(content_type->u.def.ext);
            CoTaskMemFree(content_type->u.def.type);
        }
        else
        {
            IOpcPartUri *uri = NULL;
            WCHAR *type = NULL;
            BSTR name = NULL;

            if (SUCCEEDED(hr))
1790
                hr = IXmlWriter_WriteStartElement(writer, NULL, L"Override", NULL);
1791 1792 1793 1794 1795
            if (SUCCEEDED(hr))
                hr = IOpcPart_GetName(content_type->u.override.part, &uri);
            if (SUCCEEDED(hr))
                hr = IOpcPartUri_GetRawUri(uri, &name);
            if (SUCCEEDED(hr))
1796
                hr = IXmlWriter_WriteAttributeString(writer, NULL, L"PartName", NULL, name);
1797 1798 1799
            if (SUCCEEDED(hr))
                hr = IOpcPart_GetContentType(content_type->u.override.part, &type);
            if (SUCCEEDED(hr))
1800
                hr = IXmlWriter_WriteAttributeString(writer, NULL, L"ContentType", NULL, type);
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815

            if (uri)
                IOpcPartUri_Release(uri);
            SysFreeString(name);
            CoTaskMemFree(type);

            IOpcPart_Release(content_type->u.override.part);
        }
        if (SUCCEEDED(hr))
            hr = IXmlWriter_WriteEndElement(writer);

        list_remove(&content_type->entry);
        heap_free(content_type);
    }

1816 1817 1818 1819 1820 1821
    if (SUCCEEDED(hr))
        hr = IXmlWriter_WriteEndDocument(writer);
    if (SUCCEEDED(hr))
        hr = IXmlWriter_Flush(writer);

    if (SUCCEEDED(hr))
1822
        hr = compress_add_file(archive, L"[Content_Types].xml", content, OPC_COMPRESSION_NORMAL);
1823 1824 1825

    if (content)
        IStream_Release(content);
1826 1827 1828 1829

    return hr;
}

1830 1831 1832 1833 1834 1835 1836
static HRESULT opc_package_write_rel(IOpcRelationship *rel, IXmlWriter *writer)
{
    BSTR target_uri;
    HRESULT hr;
    WCHAR *str;
    IUri *uri;

1837
    if (FAILED(hr = IXmlWriter_WriteStartElement(writer, NULL, L"Relationship", NULL)))
1838 1839 1840 1841 1842 1843 1844 1845
        return hr;

    if (FAILED(hr = IOpcRelationship_GetTargetUri(rel, &uri)))
        return hr;

    IUri_GetRawUri(uri, &target_uri);
    IUri_Release(uri);

1846
    hr = IXmlWriter_WriteAttributeString(writer, NULL, L"Target", NULL, target_uri);
1847 1848 1849 1850 1851 1852 1853
    SysFreeString(target_uri);
    if (FAILED(hr))
        return hr;

    if (FAILED(hr = IOpcRelationship_GetId(rel, &str)))
        return hr;

1854
    hr = IXmlWriter_WriteAttributeString(writer, NULL, L"Id", NULL, str);
1855 1856 1857 1858 1859 1860 1861
    CoTaskMemFree(str);
    if (FAILED(hr))
        return hr;

    if (FAILED(hr = IOpcRelationship_GetRelationshipType(rel, &str)))
        return hr;

1862
    hr = IXmlWriter_WriteAttributeString(writer, NULL, L"Type", NULL, str);
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873
    CoTaskMemFree(str);
    if (FAILED(hr))
        return hr;

    return IXmlWriter_WriteEndElement(writer);
}

static HRESULT opc_package_write_rels(struct zip_archive *archive, IOpcRelationshipSet *rels,
        IOpcUri *uri, IXmlWriter *writer)
{
    IOpcRelationshipEnumerator *enumerator;
1874
    BSTR rels_part_uri = NULL;
1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895
    IOpcPartUri *rels_uri;
    IStream *content;
    BOOL has_next;
    HRESULT hr;

    if (FAILED(hr = IOpcRelationshipSet_GetEnumerator(rels, &enumerator)))
        return hr;

    hr = IOpcRelationshipEnumerator_MoveNext(enumerator, &has_next);
    if (!has_next)
    {
        IOpcRelationshipEnumerator_Release(enumerator);
        return hr;
    }

    if (FAILED(hr = CreateStreamOnHGlobal(NULL, TRUE, &content)))
    {
        IOpcRelationshipEnumerator_Release(enumerator);
        return hr;
    }

1896 1897 1898
    hr = IXmlWriter_SetOutput(writer, (IUnknown *)content);
    if (SUCCEEDED(hr))
        hr = IXmlWriter_WriteStartDocument(writer, XmlStandalone_Yes);
1899
    if (SUCCEEDED(hr))
1900 1901
        hr = IXmlWriter_WriteStartElement(writer, NULL, L"Relationships",
                L"http://schemas.openxmlformats.org/package/2006/relationships");
1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924

    while (has_next)
    {
        IOpcRelationship *rel;

        if (FAILED(hr = IOpcRelationshipEnumerator_GetCurrent(enumerator, &rel)))
            break;

        hr = opc_package_write_rel(rel, writer);
        IOpcRelationship_Release(rel);
        if (FAILED(hr))
            break;

        IOpcRelationshipEnumerator_MoveNext(enumerator, &has_next);
    }

    IOpcRelationshipEnumerator_Release(enumerator);

    if (SUCCEEDED(hr))
        hr = IXmlWriter_WriteEndDocument(writer);
    if (SUCCEEDED(hr))
        hr = IXmlWriter_Flush(writer);

1925 1926
    if (SUCCEEDED(hr))
        hr = IOpcUri_GetRelationshipsPartUri(uri, &rels_uri);
1927 1928 1929
    if (SUCCEEDED(hr))
        hr = IOpcPartUri_GetRawUri(rels_uri, &rels_part_uri);
    if (SUCCEEDED(hr))
1930 1931 1932 1933
    {
        /* Relationship part names always start with root '/', skip it. */
        hr = compress_add_file(archive, rels_part_uri + 1, content, OPC_COMPRESSION_NORMAL);
    }
1934 1935 1936 1937 1938 1939 1940 1941

    SysFreeString(rels_part_uri);
    IStream_Release(content);

    return hr;
}

static HRESULT opc_package_write_part(struct zip_archive *archive, IOpcPart *part, IXmlWriter *writer)
1942 1943
{
    OPC_COMPRESSION_OPTIONS options = OPC_COMPRESSION_NORMAL;
1944
    IOpcRelationshipSet *rels = NULL;
1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
    IStream *content = NULL;
    IOpcPartUri *name;
    BSTR uri = NULL;
    HRESULT hr;

    if (FAILED(hr = IOpcPart_GetName(part, &name)))
        return hr;

    hr = IOpcPartUri_GetRawUri(name, &uri);
    if (SUCCEEDED(hr))
        hr = IOpcPart_GetCompressionOptions(part, &options);
    if (SUCCEEDED(hr))
        hr = IOpcPart_GetContentStream(part, &content);
    if (SUCCEEDED(hr))
    {
        /* Part names always start with root '/', skip it. */
        hr = compress_add_file(archive, uri + 1, content, options);
    }
1963 1964 1965 1966
    if (SUCCEEDED(hr))
        hr = IOpcPart_GetRelationshipSet(part, &rels);
    if (SUCCEEDED(hr))
        hr = opc_package_write_rels(archive, rels, (IOpcUri *)name, writer);
1967

1968
    IOpcPartUri_Release(name);
1969 1970 1971
    SysFreeString(uri);
    if (content)
        IStream_Release(content);
1972 1973
    if (rels)
        IOpcRelationshipSet_Release(rels);
1974 1975 1976 1977

    return hr;
}

1978
static HRESULT opc_package_write_parts(struct zip_archive *archive, IOpcPackage *package, IXmlWriter *writer)
1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
{
    IOpcPartEnumerator *parts;
    IOpcPartSet *part_set;
    BOOL got_next;
    HRESULT hr;

    if (FAILED(hr = IOpcPackage_GetPartSet(package, &part_set)))
        return hr;

    hr = IOpcPartSet_GetEnumerator(part_set, &parts);
    IOpcPartSet_Release(part_set);
    if (FAILED(hr))
        return hr;

    while (IOpcPartEnumerator_MoveNext(parts, &got_next) == S_OK && got_next)
    {
        IOpcPart *part;

        if (FAILED(hr = IOpcPartEnumerator_GetCurrent(parts, &part)))
            break;

2000
        hr = opc_package_write_part(archive, part, writer);
2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011
        IOpcPart_Release(part);
        if (FAILED(hr))
            break;
    }

    IOpcPartEnumerator_Release(parts);

    return hr;
}

HRESULT opc_package_write(IOpcPackage *package, OPC_WRITE_FLAGS flags, IStream *stream)
2012
{
2013
    IOpcRelationshipSet *rels = NULL;
2014
    struct zip_archive *archive;
2015
    IOpcUri *uri = NULL;
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031
    IXmlWriter *writer;
    HRESULT hr;

    if (flags != OPC_WRITE_FORCE_ZIP32)
        FIXME("Unsupported write flags %#x.\n", flags);

    if (FAILED(hr = CreateXmlWriter(&IID_IXmlWriter, (void **)&writer, NULL)))
        return hr;

    if (FAILED(hr = compress_create_archive(stream, &archive)))
    {
        IXmlWriter_Release(writer);
        return hr;
    }

    /* [Content_Types].xml */
2032
    hr = opc_package_write_contenttypes(package, archive, writer);
2033 2034 2035 2036 2037 2038 2039 2040
    /* Package relationships. */
    if (SUCCEEDED(hr))
        hr = IOpcPackage_GetRelationshipSet(package, &rels);
    if (SUCCEEDED(hr))
        hr = opc_root_uri_create(&uri);
    if (SUCCEEDED(hr))
        hr = opc_package_write_rels(archive, rels, uri, writer);
    /* Parts. */
2041
    if (SUCCEEDED(hr))
2042
        hr = opc_package_write_parts(archive, package, writer);
2043

2044 2045 2046 2047 2048
    if (rels)
        IOpcRelationshipSet_Release(rels);
    if (uri)
        IOpcUri_Release(uri);

2049 2050 2051 2052 2053
    compress_finalize_archive(archive);
    IXmlWriter_Release(writer);

    return hr;
}