2 * Web Services on Devices
4 * Copyright 2017 Owen Rudge for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wsdapi_internal.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(wsdapi
);
30 LPWSTR
duplicate_string(void *parentMemoryBlock
, LPCWSTR value
)
35 valueLen
= lstrlenW(value
) + 1;
37 dup
= WSDAllocateLinkedMemory(parentMemoryBlock
, valueLen
* sizeof(WCHAR
));
39 if (dup
) memcpy(dup
, value
, valueLen
* sizeof(WCHAR
));
43 static WSDXML_NAMESPACE
*duplicate_namespace(void *parentMemoryBlock
, WSDXML_NAMESPACE
*ns
)
45 WSDXML_NAMESPACE
*newNs
;
47 newNs
= WSDAllocateLinkedMemory(parentMemoryBlock
, sizeof(WSDXML_NAMESPACE
));
54 newNs
->Encoding
= ns
->Encoding
;
56 /* On Windows, both Names and NamesCount are set to null even if there are names present */
57 newNs
->NamesCount
= 0;
60 newNs
->PreferredPrefix
= duplicate_string(newNs
, ns
->PreferredPrefix
);
61 newNs
->Uri
= duplicate_string(newNs
, ns
->Uri
);
66 WSDXML_NAME
*duplicate_name(void *parentMemoryBlock
, WSDXML_NAME
*name
)
70 dup
= WSDAllocateLinkedMemory(parentMemoryBlock
, sizeof(WSDXML_NAME
));
77 dup
->Space
= duplicate_namespace(dup
, name
->Space
);
78 dup
->LocalName
= duplicate_string(dup
, name
->LocalName
);
80 if (dup
->LocalName
== NULL
)
82 WSDFreeLinkedMemory(dup
);
89 HRESULT WINAPI
WSDXMLAddChild(WSDXML_ELEMENT
*pParent
, WSDXML_ELEMENT
*pChild
)
91 WSDXML_NODE
*currentNode
;
93 TRACE("(%p, %p)\n", pParent
, pChild
);
95 if ((pParent
== NULL
) || (pChild
== NULL
) || (pChild
->Node
.Parent
!= NULL
))
100 /* See if the parent already has a child */
101 currentNode
= pParent
->FirstChild
;
103 if (currentNode
== NULL
)
105 pParent
->FirstChild
= (WSDXML_NODE
*)pChild
;
109 /* Find the last sibling node and make this child the next sibling */
110 WSDXMLAddSibling((WSDXML_ELEMENT
*)currentNode
, pChild
);
113 pChild
->Node
.Parent
= pParent
;
115 /* Link the memory allocations */
116 WSDAttachLinkedMemory(pParent
, pChild
);
121 HRESULT WINAPI
WSDXMLAddSibling(WSDXML_ELEMENT
*pFirst
, WSDXML_ELEMENT
*pSecond
)
123 WSDXML_NODE
*currentNode
;
125 TRACE("(%p, %p)\n", pFirst
, pSecond
);
127 if ((pFirst
== NULL
) || (pSecond
== NULL
))
132 /* See if the first node already has a sibling */
133 currentNode
= pFirst
->Node
.Next
;
135 if (currentNode
== NULL
)
137 pFirst
->Node
.Next
= (WSDXML_NODE
*)pSecond
;
141 /* Find the last sibling node and make the second element the next sibling */
144 if (currentNode
->Next
== NULL
)
146 currentNode
->Next
= (WSDXML_NODE
*)pSecond
;
150 currentNode
= currentNode
->Next
;
154 /* Reparent the second node under the first */
155 pSecond
->Node
.Parent
= pFirst
->Node
.Parent
;
157 /* Link the memory allocations */
158 WSDAttachLinkedMemory(pFirst
->Node
.Parent
, pSecond
);
163 HRESULT WINAPI
WSDXMLBuildAnyForSingleElement(WSDXML_NAME
*pElementName
, LPCWSTR pszText
, WSDXML_ELEMENT
**ppAny
)
167 TRACE("(%p, %s, %p)\n", pElementName
, debugstr_w(pszText
), ppAny
);
169 if ((pElementName
== NULL
) || ((pszText
!= NULL
) && (lstrlenW(pszText
) > WSD_MAX_TEXT_LENGTH
)))
179 *ppAny
= WSDAllocateLinkedMemory(NULL
, sizeof(WSDXML_ELEMENT
));
183 return E_OUTOFMEMORY
;
186 ZeroMemory(*ppAny
, sizeof(WSDXML_ELEMENT
));
188 (*ppAny
)->Name
= duplicate_name(*ppAny
, pElementName
);
190 if ((*ppAny
)->Name
== NULL
)
192 WSDFreeLinkedMemory(*ppAny
);
193 return E_OUTOFMEMORY
;
198 child
= WSDAllocateLinkedMemory(*ppAny
, sizeof(WSDXML_TEXT
));
202 WSDFreeLinkedMemory(*ppAny
);
203 return E_OUTOFMEMORY
;
206 child
->Node
.Parent
= *ppAny
;
207 child
->Node
.Next
= NULL
;
208 child
->Node
.Type
= TextType
;
209 child
->Text
= duplicate_string(child
, pszText
);
211 if (child
->Text
== NULL
)
213 WSDFreeLinkedMemory(*ppAny
);
214 return E_OUTOFMEMORY
;
217 (*ppAny
)->FirstChild
= (WSDXML_NODE
*)child
;
223 HRESULT WINAPI
WSDXMLCleanupElement(WSDXML_ELEMENT
*pAny
)
225 TRACE("(%p)\n", pAny
);
232 WSDFreeLinkedMemory(pAny
);
236 HRESULT WINAPI
WSDXMLGetValueFromAny(const WCHAR
*pszNamespace
, const WCHAR
*pszName
, WSDXML_ELEMENT
*pAny
, LPCWSTR
*ppszValue
)
238 WSDXML_ELEMENT
*element
;
244 if (ppszValue
== NULL
)
247 if ((pszNamespace
== NULL
) || (pszName
== NULL
) || (lstrlenW(pszNamespace
) > WSD_MAX_TEXT_LENGTH
) || (lstrlenW(pszName
) > WSD_MAX_TEXT_LENGTH
))
252 while (element
!= NULL
)
254 if (element
->Node
.Type
== ElementType
)
256 if ((lstrcmpW(element
->Name
->LocalName
, pszName
) == 0) && (lstrcmpW(element
->Name
->Space
->Uri
, pszNamespace
) == 0))
258 if ((element
->FirstChild
== NULL
) || (element
->FirstChild
->Type
!= TextType
))
263 text
= (WSDXML_TEXT
*) element
->FirstChild
;
264 *ppszValue
= (LPCWSTR
) text
->Text
;
270 element
= (WSDXML_ELEMENT
*) element
->Node
.Next
;
276 /* IWSDXMLContext implementation */
281 WSDXML_NAMESPACE
*namespace;
284 typedef struct IWSDXMLContextImpl
286 IWSDXMLContext IWSDXMLContext_iface
;
289 struct list
*namespaces
;
290 int nextUnknownPrefix
;
291 } IWSDXMLContextImpl
;
293 static WSDXML_NAMESPACE
*find_namespace(struct list
*namespaces
, LPCWSTR uri
)
295 struct xmlNamespace
*ns
;
297 LIST_FOR_EACH_ENTRY(ns
, namespaces
, struct xmlNamespace
, entry
)
299 if (lstrcmpW(ns
->namespace->Uri
, uri
) == 0)
301 return ns
->namespace;
308 static WSDXML_NAME
*find_name(WSDXML_NAMESPACE
*ns
, LPCWSTR name
)
312 for (i
= 0; i
< ns
->NamesCount
; i
++)
314 if (lstrcmpW(ns
->Names
[i
].LocalName
, name
) == 0)
316 return &ns
->Names
[i
];
323 static WSDXML_NAME
*add_name(WSDXML_NAMESPACE
*ns
, LPCWSTR name
)
326 WSDXML_NAME
*newName
;
329 names
= WSDAllocateLinkedMemory(ns
, sizeof(WSDXML_NAME
) * (ns
->NamesCount
+ 1));
336 if (ns
->NamesCount
> 0)
338 /* Copy the existing names array over to the new allocation */
339 memcpy(names
, ns
->Names
, sizeof(WSDXML_NAME
) * ns
->NamesCount
);
341 for (i
= 0; i
< ns
->NamesCount
; i
++)
343 /* Attach the local name memory to the new names allocation */
344 WSDAttachLinkedMemory(names
, names
[i
].LocalName
);
347 WSDFreeLinkedMemory(ns
->Names
);
352 newName
= &names
[ns
->NamesCount
];
354 newName
->LocalName
= duplicate_string(names
, name
);
357 if (newName
->LocalName
== NULL
)
366 static BOOL
is_prefix_unique(struct list
*namespaces
, LPCWSTR prefix
)
368 struct xmlNamespace
*ns
;
370 LIST_FOR_EACH_ENTRY(ns
, namespaces
, struct xmlNamespace
, entry
)
372 if (lstrcmpW(ns
->namespace->PreferredPrefix
, prefix
) == 0)
381 static LPWSTR
generate_namespace_prefix(IWSDXMLContextImpl
*impl
, void *parentMemoryBlock
, LPCWSTR uri
)
383 WCHAR suggestedPrefix
[7];
385 /* Find a unique prefix */
386 while (impl
->nextUnknownPrefix
< 1000)
388 wsprintfW(suggestedPrefix
, L
"un%d", impl
->nextUnknownPrefix
++);
390 /* For the unlikely event where somebody has explicitly created a prefix called 'unX', check it is unique */
391 if (is_prefix_unique(impl
->namespaces
, suggestedPrefix
))
393 return duplicate_string(parentMemoryBlock
, suggestedPrefix
);
400 static WSDXML_NAMESPACE
*add_namespace(struct list
*namespaces
, LPCWSTR uri
)
402 struct xmlNamespace
*ns
= WSDAllocateLinkedMemory(namespaces
, sizeof(struct xmlNamespace
));
409 ns
->namespace = WSDAllocateLinkedMemory(ns
, sizeof(WSDXML_NAMESPACE
));
411 if (ns
->namespace == NULL
)
413 WSDFreeLinkedMemory(ns
);
417 ZeroMemory(ns
->namespace, sizeof(WSDXML_NAMESPACE
));
418 ns
->namespace->Uri
= duplicate_string(ns
->namespace, uri
);
420 if (ns
->namespace->Uri
== NULL
)
422 WSDFreeLinkedMemory(ns
);
426 list_add_tail(namespaces
, &ns
->entry
);
427 return ns
->namespace;
430 static inline IWSDXMLContextImpl
*impl_from_IWSDXMLContext(IWSDXMLContext
*iface
)
432 return CONTAINING_RECORD(iface
, IWSDXMLContextImpl
, IWSDXMLContext_iface
);
435 static HRESULT WINAPI
IWSDXMLContextImpl_QueryInterface(IWSDXMLContext
*iface
, REFIID riid
, void **ppv
)
437 IWSDXMLContextImpl
*This
= impl_from_IWSDXMLContext(iface
);
439 TRACE("(%p, %s, %p)\n", This
, debugstr_guid(riid
), ppv
);
443 WARN("Invalid parameter\n");
449 if (IsEqualIID(riid
, &IID_IUnknown
) ||
450 IsEqualIID(riid
, &IID_IWSDXMLContext
))
452 *ppv
= &This
->IWSDXMLContext_iface
;
456 WARN("Unknown IID %s\n", debugstr_guid(riid
));
457 return E_NOINTERFACE
;
460 IUnknown_AddRef((IUnknown
*)*ppv
);
464 static ULONG WINAPI
IWSDXMLContextImpl_AddRef(IWSDXMLContext
*iface
)
466 IWSDXMLContextImpl
*This
= impl_from_IWSDXMLContext(iface
);
467 ULONG ref
= InterlockedIncrement(&This
->ref
);
469 TRACE("(%p) ref=%d\n", This
, ref
);
473 static ULONG WINAPI
IWSDXMLContextImpl_Release(IWSDXMLContext
*iface
)
475 IWSDXMLContextImpl
*This
= impl_from_IWSDXMLContext(iface
);
476 ULONG ref
= InterlockedDecrement(&This
->ref
);
478 TRACE("(%p) ref=%d\n", This
, ref
);
482 WSDFreeLinkedMemory(This
);
488 static HRESULT WINAPI
IWSDXMLContextImpl_AddNamespace(IWSDXMLContext
*iface
, LPCWSTR pszUri
, LPCWSTR pszSuggestedPrefix
, WSDXML_NAMESPACE
**ppNamespace
)
490 IWSDXMLContextImpl
*This
= impl_from_IWSDXMLContext(iface
);
491 WSDXML_NAMESPACE
*ns
;
492 LPCWSTR newPrefix
= NULL
;
495 TRACE("(%p, %s, %s, %p)\n", This
, debugstr_w(pszUri
), debugstr_w(pszSuggestedPrefix
), ppNamespace
);
497 if ((pszUri
== NULL
) || (pszSuggestedPrefix
== NULL
) || (lstrlenW(pszUri
) > WSD_MAX_TEXT_LENGTH
) ||
498 (lstrlenW(pszSuggestedPrefix
) > WSD_MAX_TEXT_LENGTH
))
503 ns
= find_namespace(This
->namespaces
, pszUri
);
507 ns
= add_namespace(This
->namespaces
, pszUri
);
511 return E_OUTOFMEMORY
;
515 setNewPrefix
= (ns
->PreferredPrefix
== NULL
);
517 if ((ns
->PreferredPrefix
== NULL
) || (lstrcmpW(ns
->PreferredPrefix
, pszSuggestedPrefix
) != 0))
519 newPrefix
= pszSuggestedPrefix
;
525 WSDFreeLinkedMemory((void *)ns
->PreferredPrefix
);
526 ns
->PreferredPrefix
= NULL
;
528 if ((newPrefix
!= NULL
) && (is_prefix_unique(This
->namespaces
, newPrefix
)))
530 ns
->PreferredPrefix
= duplicate_string(ns
, newPrefix
);
534 ns
->PreferredPrefix
= generate_namespace_prefix(This
, ns
, pszUri
);
535 if (ns
->PreferredPrefix
== NULL
)
542 if (ppNamespace
!= NULL
)
544 *ppNamespace
= duplicate_namespace(NULL
, ns
);
546 if (*ppNamespace
== NULL
)
548 return E_OUTOFMEMORY
;
555 static HRESULT WINAPI
IWSDXMLContextImpl_AddNameToNamespace(IWSDXMLContext
*iface
, LPCWSTR pszUri
, LPCWSTR pszName
, WSDXML_NAME
**ppName
)
557 IWSDXMLContextImpl
*This
= impl_from_IWSDXMLContext(iface
);
558 WSDXML_NAMESPACE
*ns
;
561 TRACE("(%p, %s, %s, %p)\n", This
, debugstr_w(pszUri
), debugstr_w(pszName
), ppName
);
563 if ((pszUri
== NULL
) || (pszName
== NULL
) || (lstrlenW(pszUri
) > WSD_MAX_TEXT_LENGTH
) || (lstrlenW(pszName
) > WSD_MAX_TEXT_LENGTH
))
568 ns
= find_namespace(This
->namespaces
, pszUri
);
572 /* The namespace doesn't exist, add it */
573 ns
= add_namespace(This
->namespaces
, pszUri
);
577 return E_OUTOFMEMORY
;
580 ns
->PreferredPrefix
= generate_namespace_prefix(This
, ns
, pszUri
);
581 if (ns
->PreferredPrefix
== NULL
)
587 name
= find_name(ns
, pszName
);
591 name
= add_name(ns
, pszName
);
595 return E_OUTOFMEMORY
;
601 *ppName
= duplicate_name(NULL
, name
);
605 return E_OUTOFMEMORY
;
612 static HRESULT WINAPI
IWSDXMLContextImpl_SetNamespaces(IWSDXMLContext
*iface
, const PCWSDXML_NAMESPACE
*pNamespaces
, WORD wNamespacesCount
, BYTE bLayerNumber
)
614 FIXME("(%p, %p, %d, %d)\n", iface
, pNamespaces
, wNamespacesCount
, bLayerNumber
);
618 static HRESULT WINAPI
IWSDXMLContextImpl_SetTypes(IWSDXMLContext
*iface
, const PCWSDXML_TYPE
*pTypes
, DWORD dwTypesCount
, BYTE bLayerNumber
)
620 FIXME("(%p, %p, %d, %d)\n", iface
, pTypes
, dwTypesCount
, bLayerNumber
);
624 static const IWSDXMLContextVtbl xmlcontext_vtbl
=
626 IWSDXMLContextImpl_QueryInterface
,
627 IWSDXMLContextImpl_AddRef
,
628 IWSDXMLContextImpl_Release
,
629 IWSDXMLContextImpl_AddNamespace
,
630 IWSDXMLContextImpl_AddNameToNamespace
,
631 IWSDXMLContextImpl_SetNamespaces
,
632 IWSDXMLContextImpl_SetTypes
635 HRESULT WINAPI
WSDXMLCreateContext(IWSDXMLContext
**ppContext
)
637 IWSDXMLContextImpl
*obj
;
639 TRACE("(%p)\n", ppContext
);
641 if (ppContext
== NULL
)
643 WARN("Invalid parameter: ppContext == NULL\n");
649 obj
= WSDAllocateLinkedMemory(NULL
, sizeof(*obj
));
653 return E_OUTOFMEMORY
;
656 obj
->IWSDXMLContext_iface
.lpVtbl
= &xmlcontext_vtbl
;
658 obj
->namespaces
= WSDAllocateLinkedMemory(obj
, sizeof(struct list
));
659 obj
->nextUnknownPrefix
= 0;
661 if (obj
->namespaces
== NULL
)
663 WSDFreeLinkedMemory(obj
);
664 return E_OUTOFMEMORY
;
667 list_init(obj
->namespaces
);
669 *ppContext
= &obj
->IWSDXMLContext_iface
;
670 TRACE("Returning iface %p\n", *ppContext
);
675 WSDXML_NAMESPACE
*xml_context_find_namespace_by_prefix(IWSDXMLContext
*context
, LPCWSTR prefix
)
677 IWSDXMLContextImpl
*impl
= impl_from_IWSDXMLContext(context
);
678 struct xmlNamespace
*ns
;
680 if (prefix
== NULL
) return NULL
;
682 LIST_FOR_EACH_ENTRY(ns
, impl
->namespaces
, struct xmlNamespace
, entry
)
684 if (lstrcmpW(ns
->namespace->PreferredPrefix
, prefix
) == 0)
685 return ns
->namespace;