summaryrefslogtreecommitdiff
path: root/src/libs/dutil/WixToolset.DUtil/rssutil.cpp
blob: 8f994dfc393b10b7a7296e92719484579a84c495 (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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
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
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
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
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
637
638
639
640
641
642
643
644
645
646
647
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

#include "precomp.h"


// Exit macros
#define RssExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_RSSUTIL, x, s, __VA_ARGS__)
#define RssExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_RSSUTIL, x, s, __VA_ARGS__)
#define RssExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_RSSUTIL, x, s, __VA_ARGS__)
#define RssExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_RSSUTIL, x, s, __VA_ARGS__)
#define RssExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_RSSUTIL, x, s, __VA_ARGS__)
#define RssExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_RSSUTIL, x, s, __VA_ARGS__)
#define RssExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_RSSUTIL, p, x, e, s, __VA_ARGS__)
#define RssExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_RSSUTIL, p, x, s, __VA_ARGS__)
#define RssExitOnNullDebugTrace(p, x, e, s, ...)  ExitOnNullDebugTraceSource(DUTIL_SOURCE_RSSUTIL, p, x, e, s, __VA_ARGS__)
#define RssExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_RSSUTIL, p, x, s, __VA_ARGS__)
#define RssExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_RSSUTIL, e, x, s, __VA_ARGS__)
#define RssExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_RSSUTIL, g, x, s, __VA_ARGS__)

static HRESULT ParseRssDocument(
    __in IXMLDOMDocument *pixd,
    __out RSS_CHANNEL **ppChannel
    );
static HRESULT ParseRssChannel(
    __in IXMLDOMNode *pixnChannel,
    __out RSS_CHANNEL **ppChannel
    );
static HRESULT ParseRssItem(
    __in IXMLDOMNode *pixnItem,
    __in DWORD cItem,
    __in_xcount(pChannel->cItems) RSS_CHANNEL *pChannel
    );
static HRESULT ParseRssUnknownElement(
    __in IXMLDOMNode *pNode,
    __inout RSS_UNKNOWN_ELEMENT** ppUnknownElement
    );
static HRESULT ParseRssUnknownAttribute(
    __in IXMLDOMNode *pNode,
    __inout RSS_UNKNOWN_ATTRIBUTE** ppUnknownAttribute
    );
static void FreeRssUnknownElementList(
    __in_opt RSS_UNKNOWN_ELEMENT* pUnknownElement
    );
static void FreeRssUnknownAttributeList(
    __in_opt RSS_UNKNOWN_ATTRIBUTE* pUnknownAttribute
    );


/********************************************************************
 RssInitialize - Initialize RSS utilities.

*********************************************************************/
extern "C" HRESULT DAPI RssInitialize()
{
    return XmlInitialize();
}


/********************************************************************
 RssUninitialize - Uninitialize RSS utilities.

*********************************************************************/
extern "C" void DAPI RssUninitialize()
{
    XmlUninitialize();
}


/********************************************************************
 RssParseFromString - parses out an RSS channel from a string.

*********************************************************************/
extern "C" HRESULT DAPI RssParseFromString(
    __in_z LPCWSTR wzRssString,
    __out RSS_CHANNEL **ppChannel
    )
{
    Assert(wzRssString);
    Assert(ppChannel);

    HRESULT hr = S_OK;
    RSS_CHANNEL *pNewChannel = NULL;
    IXMLDOMDocument *pixdRss = NULL;

    hr = XmlLoadDocument(wzRssString, &pixdRss);
    RssExitOnFailure(hr, "Failed to load RSS string as XML document.");

    hr = ParseRssDocument(pixdRss, &pNewChannel);
    RssExitOnFailure(hr, "Failed to parse RSS document.");

    *ppChannel = pNewChannel;
    pNewChannel = NULL;

LExit:
    ReleaseObject(pixdRss);

    ReleaseRssChannel(pNewChannel);

    return hr;
}


/********************************************************************
 RssParseFromFile - parses out an RSS channel from a file path.

*********************************************************************/
extern "C" HRESULT DAPI RssParseFromFile(
    __in_z LPCWSTR wzRssFile,
    __out RSS_CHANNEL **ppChannel
    )
{
    Assert(wzRssFile);
    Assert(ppChannel);

    HRESULT hr = S_OK;
    RSS_CHANNEL *pNewChannel = NULL;
    IXMLDOMDocument *pixdRss = NULL;

    hr = XmlLoadDocumentFromFile(wzRssFile, &pixdRss);
    RssExitOnFailure(hr, "Failed to load RSS string as XML document.");

    hr = ParseRssDocument(pixdRss, &pNewChannel);
    RssExitOnFailure(hr, "Failed to parse RSS document.");

    *ppChannel = pNewChannel;
    pNewChannel = NULL;

LExit:
    ReleaseObject(pixdRss);

    ReleaseRssChannel(pNewChannel);

    return hr;
}


/********************************************************************
 RssFreeChannel - parses out an RSS channel from a string.

*********************************************************************/
extern "C" void DAPI RssFreeChannel(
    __in_xcount(pChannel->cItems) RSS_CHANNEL *pChannel
    )
{
    if (pChannel)
    {
        for (DWORD i = 0; i < pChannel->cItems; ++i)
        {
            ReleaseStr(pChannel->rgItems[i].wzTitle);
            ReleaseStr(pChannel->rgItems[i].wzLink);
            ReleaseStr(pChannel->rgItems[i].wzDescription);
            ReleaseStr(pChannel->rgItems[i].wzGuid);
            ReleaseStr(pChannel->rgItems[i].wzEnclosureUrl);
            ReleaseStr(pChannel->rgItems[i].wzEnclosureType);

            FreeRssUnknownElementList(pChannel->rgItems[i].pUnknownElements);
        }

        ReleaseStr(pChannel->wzTitle);
        ReleaseStr(pChannel->wzLink);
        ReleaseStr(pChannel->wzDescription);
        FreeRssUnknownElementList(pChannel->pUnknownElements);

        MemFree(pChannel);
    }
}


/********************************************************************
 ParseRssDocument - parses out an RSS channel from a loaded XML DOM document.

*********************************************************************/
static HRESULT ParseRssDocument(
    __in IXMLDOMDocument *pixd,
    __out RSS_CHANNEL **ppChannel
    )
{
    Assert(pixd);
    Assert(ppChannel);

    HRESULT hr = S_OK;
    IXMLDOMElement *pRssElement = NULL;
    IXMLDOMNodeList *pChannelNodes = NULL;
    IXMLDOMNode *pNode = NULL;
    BSTR bstrNodeName = NULL;

    RSS_CHANNEL *pNewChannel = NULL;

    //
    // Get the document element and start processing channels.
    //
    hr = pixd ->get_documentElement(&pRssElement);
    RssExitOnFailure(hr, "failed get_documentElement in ParseRssDocument");

    hr = pRssElement->get_childNodes(&pChannelNodes);
    RssExitOnFailure(hr, "Failed to get child nodes of Rss Document element.");

    while (S_OK == (hr = XmlNextElement(pChannelNodes, &pNode, &bstrNodeName)))
    {
        if (0 == lstrcmpW(bstrNodeName, L"channel"))
        {
            hr = ParseRssChannel(pNode, &pNewChannel);
            RssExitOnFailure(hr, "Failed to parse RSS channel.");
        }
        else if (0 == lstrcmpW(bstrNodeName, L"link"))
        {
        }

        ReleaseNullBSTR(bstrNodeName);
        ReleaseNullObject(pNode);
    }

    if (S_FALSE == hr)
    {
        hr = S_OK;
    }

    *ppChannel = pNewChannel;
    pNewChannel = NULL;

LExit:
    ReleaseBSTR(bstrNodeName);
    ReleaseObject(pNode);
    ReleaseObject(pChannelNodes);
    ReleaseObject(pRssElement);

    ReleaseRssChannel(pNewChannel);

    return hr;
}


/********************************************************************
 ParseRssChannel - parses out an RSS channel from a loaded XML DOM element.

*********************************************************************/
static HRESULT ParseRssChannel(
    __in IXMLDOMNode *pixnChannel,
    __out RSS_CHANNEL **ppChannel
    )
{
    Assert(pixnChannel);
    Assert(ppChannel);

    HRESULT hr = S_OK;
    IXMLDOMNodeList *pNodeList = NULL;

    RSS_CHANNEL *pNewChannel = NULL;
    long cItems = 0;

    IXMLDOMNode *pNode = NULL;
    BSTR bstrNodeName = NULL;
    BSTR bstrNodeValue = NULL;

    //
    // First, calculate how many RSS items we're going to have and allocate
    // the RSS_CHANNEL structure
    //
    hr = XmlSelectNodes(pixnChannel, L"item", &pNodeList);
    RssExitOnFailure(hr, "Failed to select all RSS items in an RSS channel.");

    hr = pNodeList->get_length(&cItems);
    RssExitOnFailure(hr, "Failed to count the number of RSS items in RSS channel.");

    pNewChannel = static_cast<RSS_CHANNEL*>(MemAlloc(sizeof(RSS_CHANNEL) + sizeof(RSS_ITEM) * cItems, TRUE));
    RssExitOnNull(pNewChannel, hr, E_OUTOFMEMORY, "Failed to allocate RSS channel structure.");

    pNewChannel->cItems = cItems;

    //
    // Process the elements under a channel now.
    //
    hr = pixnChannel->get_childNodes(&pNodeList);
    RssExitOnFailure(hr, "Failed to get child nodes of RSS channel element.");

    cItems = 0; // reset the counter and use this to walk through the channel items
    while (S_OK == (hr = XmlNextElement(pNodeList, &pNode, &bstrNodeName)))
    {
        if (0 == lstrcmpW(bstrNodeName, L"title"))
        {
            hr = XmlGetText(pNode, &bstrNodeValue);
            RssExitOnFailure(hr, "Failed to get RSS channel title.");

            hr = StrAllocString(&pNewChannel->wzTitle, bstrNodeValue, 0);
            RssExitOnFailure(hr, "Failed to allocate RSS channel title.");
        }
        else if (0 == lstrcmpW(bstrNodeName, L"link"))
        {
            hr = XmlGetText(pNode, &bstrNodeValue);
            RssExitOnFailure(hr, "Failed to get RSS channel link.");

            hr = StrAllocString(&pNewChannel->wzLink, bstrNodeValue, 0);
            RssExitOnFailure(hr, "Failed to allocate RSS channel link.");
        }
        else if (0 == lstrcmpW(bstrNodeName, L"description"))
        {
            hr = XmlGetText(pNode, &bstrNodeValue);
            RssExitOnFailure(hr, "Failed to get RSS channel description.");

            hr = StrAllocString(&pNewChannel->wzDescription, bstrNodeValue, 0);
            RssExitOnFailure(hr, "Failed to allocate RSS channel description.");
        }
        else if (0 == lstrcmpW(bstrNodeName, L"ttl"))
        {
            hr = XmlGetText(pNode, &bstrNodeValue);
            RssExitOnFailure(hr, "Failed to get RSS channel description.");

            pNewChannel->dwTimeToLive = (DWORD)wcstoul(bstrNodeValue, NULL, 10);
        }
        else if (0 == lstrcmpW(bstrNodeName, L"item"))
        {
            hr = ParseRssItem(pNode, cItems, pNewChannel);
            RssExitOnFailure(hr, "Failed to parse RSS item.");

            ++cItems;
        }
        else
        {
            hr = ParseRssUnknownElement(pNode, &pNewChannel->pUnknownElements);
            RssExitOnFailure(hr, "Failed to parse unknown RSS channel element: %ls", bstrNodeName);
        }

        ReleaseNullBSTR(bstrNodeValue);
        ReleaseNullBSTR(bstrNodeName);
        ReleaseNullObject(pNode);
    }

    *ppChannel = pNewChannel;
    pNewChannel = NULL;

LExit:
    ReleaseBSTR(bstrNodeName);
    ReleaseObject(pNode);
    ReleaseObject(pNodeList);

    ReleaseRssChannel(pNewChannel);

    return hr;
}


/********************************************************************
 ParseRssItem - parses out an RSS item from a loaded XML DOM node.

*********************************************************************/
static HRESULT ParseRssItem(
    __in IXMLDOMNode *pixnItem,
    __in DWORD cItem,
    __in_xcount(pChannel->cItems) RSS_CHANNEL *pChannel
    )
{
    HRESULT hr = S_OK;

    RSS_ITEM *pItem = NULL;
    IXMLDOMNodeList *pNodeList = NULL;

    IXMLDOMNode *pNode = NULL;
    BSTR bstrNodeName = NULL;
    BSTR bstrNodeValue = NULL;

    //
    // First make sure we're dealing with a valid item.
    //
    if (pChannel->cItems <= cItem)
    {
        hr = E_UNEXPECTED;
        RssExitOnFailure(hr, "Unexpected number of items parsed.");
    }

    pItem = pChannel->rgItems + cItem;

    //
    // Process the elements under an item now.
    //
    hr = pixnItem->get_childNodes(&pNodeList);
    RssExitOnFailure(hr, "Failed to get child nodes of RSS item element.");
    while (S_OK == (hr = XmlNextElement(pNodeList, &pNode, &bstrNodeName)))
    {
        if (0 == lstrcmpW(bstrNodeName, L"title"))
        {
            hr = XmlGetText(pNode, &bstrNodeValue);
            RssExitOnFailure(hr, "Failed to get RSS channel title.");

            hr = StrAllocString(&pItem->wzTitle, bstrNodeValue, 0);
            RssExitOnFailure(hr, "Failed to allocate RSS item title.");
        }
        else if (0 == lstrcmpW(bstrNodeName, L"link"))
        {
            hr = XmlGetText(pNode, &bstrNodeValue);
            RssExitOnFailure(hr, "Failed to get RSS channel link.");

            hr = StrAllocString(&pItem->wzLink, bstrNodeValue, 0);
            RssExitOnFailure(hr, "Failed to allocate RSS item link.");
        }
        else if (0 == lstrcmpW(bstrNodeName, L"description"))
        {
            hr = XmlGetText(pNode, &bstrNodeValue);
            RssExitOnFailure(hr, "Failed to get RSS item description.");

            hr = StrAllocString(&pItem->wzDescription, bstrNodeValue, 0);
            RssExitOnFailure(hr, "Failed to allocate RSS item description.");
        }
        else if (0 == lstrcmpW(bstrNodeName, L"guid"))
        {
            hr = XmlGetText(pNode, &bstrNodeValue);
            RssExitOnFailure(hr, "Failed to get RSS item guid.");

            hr = StrAllocString(&pItem->wzGuid, bstrNodeValue, 0);
            RssExitOnFailure(hr, "Failed to allocate RSS item guid.");
        }
        else if (0 == lstrcmpW(bstrNodeName, L"pubDate"))
        {
            hr = XmlGetText(pNode, &bstrNodeValue);
            RssExitOnFailure(hr, "Failed to get RSS item guid.");

            hr = TimeFromString(bstrNodeValue, &pItem->ftPublished);
            RssExitOnFailure(hr, "Failed to convert RSS item time.");
        }
        else if (0 == lstrcmpW(bstrNodeName, L"enclosure"))
        {
            hr = XmlGetAttribute(pNode, L"url", &bstrNodeValue);
            RssExitOnFailure(hr, "Failed to get RSS item enclosure url.");

            hr = StrAllocString(&pItem->wzEnclosureUrl, bstrNodeValue, 0);
            RssExitOnFailure(hr, "Failed to allocate RSS item enclosure url.");
            ReleaseNullBSTR(bstrNodeValue);

            hr = XmlGetAttributeNumber(pNode, L"length", &pItem->dwEnclosureSize);
            RssExitOnFailure(hr, "Failed to get RSS item enclosure length.");

            hr = XmlGetAttribute(pNode, L"type", &bstrNodeValue);
            RssExitOnFailure(hr, "Failed to get RSS item enclosure type.");

            hr = StrAllocString(&pItem->wzEnclosureType, bstrNodeValue, 0);
            RssExitOnFailure(hr, "Failed to allocate RSS item enclosure type.");
        }
        else
        {
            hr = ParseRssUnknownElement(pNode, &pItem->pUnknownElements);
            RssExitOnFailure(hr, "Failed to parse unknown RSS item element: %ls", bstrNodeName);
        }

        ReleaseNullBSTR(bstrNodeValue);
        ReleaseNullBSTR(bstrNodeName);
        ReleaseNullObject(pNode);
    }

LExit:
    ReleaseBSTR(bstrNodeValue);
    ReleaseBSTR(bstrNodeName);
    ReleaseObject(pNode);
    ReleaseObject(pNodeList);

    return hr;
}


/********************************************************************
 ParseRssUnknownElement - parses out an unknown item from the RSS feed from a loaded XML DOM node.

*********************************************************************/
static HRESULT ParseRssUnknownElement(
    __in IXMLDOMNode *pNode,
    __inout RSS_UNKNOWN_ELEMENT** ppUnknownElement
    )
{
    Assert(ppUnknownElement);

    HRESULT hr = S_OK;
    BSTR bstrNodeNamespace = NULL;
    BSTR bstrNodeName = NULL;
    BSTR bstrNodeValue = NULL;
    IXMLDOMNamedNodeMap* pixnnmAttributes = NULL;
    IXMLDOMNode* pixnAttribute = NULL;
    RSS_UNKNOWN_ELEMENT* pNewUnknownElement;

    pNewUnknownElement = static_cast<RSS_UNKNOWN_ELEMENT*>(MemAlloc(sizeof(RSS_UNKNOWN_ELEMENT), TRUE));
    RssExitOnNull(pNewUnknownElement, hr, E_OUTOFMEMORY, "Failed to allocate unknown element.");

    hr = pNode->get_namespaceURI(&bstrNodeNamespace);
    if (S_OK == hr)
    {
        hr = StrAllocString(&pNewUnknownElement->wzNamespace, bstrNodeNamespace, 0);
        RssExitOnFailure(hr, "Failed to allocate RSS unknown element namespace.");
    }
    else if (S_FALSE == hr)
    {
        hr = S_OK;
    }
    RssExitOnFailure(hr, "Failed to get unknown element namespace.");

    hr = pNode->get_baseName(&bstrNodeName);
    RssExitOnFailure(hr, "Failed to get unknown element name.");

    hr = StrAllocString(&pNewUnknownElement->wzElement, bstrNodeName, 0);
    RssExitOnFailure(hr, "Failed to allocate RSS unknown element name.");

    hr = XmlGetText(pNode, &bstrNodeValue);
    RssExitOnFailure(hr, "Failed to get unknown element value.");

    hr = StrAllocString(&pNewUnknownElement->wzValue, bstrNodeValue, 0);
    RssExitOnFailure(hr, "Failed to allocate RSS unknown element value.");

    hr = pNode->get_attributes(&pixnnmAttributes);
    RssExitOnFailure(hr, "Failed get attributes on RSS unknown element.");

    while (S_OK == (hr = pixnnmAttributes->nextNode(&pixnAttribute)))
    {
        hr = ParseRssUnknownAttribute(pixnAttribute, &pNewUnknownElement->pAttributes);
        RssExitOnFailure(hr, "Failed to parse attribute on RSS unknown element.");

        ReleaseNullObject(pixnAttribute);
    }

    if (S_FALSE == hr)
    {
        hr = S_OK;
    }
    RssExitOnFailure(hr, "Failed to enumerate all attributes on RSS unknown element.");

    RSS_UNKNOWN_ELEMENT** ppTail = ppUnknownElement;
    while (*ppTail)
    {
        ppTail = &(*ppTail)->pNext;
    }

    *ppTail = pNewUnknownElement;
    pNewUnknownElement = NULL;

LExit:
    FreeRssUnknownElementList(pNewUnknownElement);

    ReleaseBSTR(bstrNodeNamespace);
    ReleaseBSTR(bstrNodeName);
    ReleaseBSTR(bstrNodeValue);
    ReleaseObject(pixnnmAttributes);
    ReleaseObject(pixnAttribute);

    return hr;
}


/********************************************************************
 ParseRssUnknownAttribute - parses out attribute from an unknown element

*********************************************************************/
static HRESULT ParseRssUnknownAttribute(
    __in IXMLDOMNode *pNode,
    __inout RSS_UNKNOWN_ATTRIBUTE** ppUnknownAttribute
    )
{
    Assert(ppUnknownAttribute);

    HRESULT hr = S_OK;
    BSTR bstrNodeNamespace = NULL;
    BSTR bstrNodeName = NULL;
    BSTR bstrNodeValue = NULL;
    RSS_UNKNOWN_ATTRIBUTE* pNewUnknownAttribute;

    pNewUnknownAttribute = static_cast<RSS_UNKNOWN_ATTRIBUTE*>(MemAlloc(sizeof(RSS_UNKNOWN_ATTRIBUTE), TRUE));
    RssExitOnNull(pNewUnknownAttribute, hr, E_OUTOFMEMORY, "Failed to allocate unknown attribute.");

    hr = pNode->get_namespaceURI(&bstrNodeNamespace);
    if (S_OK == hr)
    {
        hr = StrAllocString(&pNewUnknownAttribute->wzNamespace, bstrNodeNamespace, 0);
        RssExitOnFailure(hr, "Failed to allocate RSS unknown attribute namespace.");
    }
    else if (S_FALSE == hr)
    {
        hr = S_OK;
    }
    RssExitOnFailure(hr, "Failed to get unknown attribute namespace.");

    hr = pNode->get_baseName(&bstrNodeName);
    RssExitOnFailure(hr, "Failed to get unknown attribute name.");

    hr = StrAllocString(&pNewUnknownAttribute->wzAttribute, bstrNodeName, 0);
    RssExitOnFailure(hr, "Failed to allocate RSS unknown attribute name.");

    hr = XmlGetText(pNode, &bstrNodeValue);
    RssExitOnFailure(hr, "Failed to get unknown attribute value.");

    hr = StrAllocString(&pNewUnknownAttribute->wzValue, bstrNodeValue, 0);
    RssExitOnFailure(hr, "Failed to allocate RSS unknown attribute value.");

    RSS_UNKNOWN_ATTRIBUTE** ppTail = ppUnknownAttribute;
    while (*ppTail)
    {
        ppTail = &(*ppTail)->pNext;
    }

    *ppTail = pNewUnknownAttribute;
    pNewUnknownAttribute = NULL;

LExit:
    FreeRssUnknownAttributeList(pNewUnknownAttribute);

    ReleaseBSTR(bstrNodeNamespace);
    ReleaseBSTR(bstrNodeName);
    ReleaseBSTR(bstrNodeValue);

    return hr;
}


/********************************************************************
 FreeRssUnknownElement - releases all of the memory used by a list of unknown elements

*********************************************************************/
static void FreeRssUnknownElementList(
    __in_opt RSS_UNKNOWN_ELEMENT* pUnknownElement
    )
{
    while (pUnknownElement)
    {
        RSS_UNKNOWN_ELEMENT* pFree = pUnknownElement;
        pUnknownElement = pUnknownElement->pNext;

        FreeRssUnknownAttributeList(pFree->pAttributes);
        ReleaseStr(pFree->wzNamespace);
        ReleaseStr(pFree->wzElement);
        ReleaseStr(pFree->wzValue);
        MemFree(pFree);
    }
}


/********************************************************************
 FreeRssUnknownAttribute - releases all of the memory used by a list of unknown attributes

*********************************************************************/
static void FreeRssUnknownAttributeList(
    __in_opt RSS_UNKNOWN_ATTRIBUTE* pUnknownAttribute
    )
{
    while (pUnknownAttribute)
    {
        RSS_UNKNOWN_ATTRIBUTE* pFree = pUnknownAttribute;
        pUnknownAttribute = pUnknownAttribute->pNext;

        ReleaseStr(pFree->wzNamespace);
        ReleaseStr(pFree->wzAttribute);
        ReleaseStr(pFree->wzValue);
        MemFree(pFree);
    }
}