wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / wsdapi / xml.c
blobca47583da6401b83d25d0f80c87f372973ee718c
1 /*
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
21 #include <stdarg.h>
23 #define COBJMACROS
25 #include "wsdapi_internal.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(wsdapi);
30 LPWSTR duplicate_string(void *parentMemoryBlock, LPCWSTR value)
32 int valueLen;
33 LPWSTR dup;
35 valueLen = lstrlenW(value) + 1;
37 dup = WSDAllocateLinkedMemory(parentMemoryBlock, valueLen * sizeof(WCHAR));
39 if (dup) memcpy(dup, value, valueLen * sizeof(WCHAR));
40 return dup;
43 static WSDXML_NAMESPACE *duplicate_namespace(void *parentMemoryBlock, WSDXML_NAMESPACE *ns)
45 WSDXML_NAMESPACE *newNs;
47 newNs = WSDAllocateLinkedMemory(parentMemoryBlock, sizeof(WSDXML_NAMESPACE));
49 if (newNs == NULL)
51 return NULL;
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;
58 newNs->Names = NULL;
60 newNs->PreferredPrefix = duplicate_string(newNs, ns->PreferredPrefix);
61 newNs->Uri = duplicate_string(newNs, ns->Uri);
63 return newNs;
66 WSDXML_NAME *duplicate_name(void *parentMemoryBlock, WSDXML_NAME *name)
68 WSDXML_NAME *dup;
70 dup = WSDAllocateLinkedMemory(parentMemoryBlock, sizeof(WSDXML_NAME));
72 if (dup == NULL)
74 return NULL;
77 dup->Space = duplicate_namespace(dup, name->Space);
78 dup->LocalName = duplicate_string(dup, name->LocalName);
80 if (dup->LocalName == NULL)
82 WSDFreeLinkedMemory(dup);
83 return NULL;
86 return 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))
97 return E_INVALIDARG;
100 /* See if the parent already has a child */
101 currentNode = pParent->FirstChild;
103 if (currentNode == NULL)
105 pParent->FirstChild = (WSDXML_NODE *)pChild;
107 else
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);
118 return S_OK;
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))
129 return E_INVALIDARG;
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;
139 else
141 /* Find the last sibling node and make the second element the next sibling */
142 while (1)
144 if (currentNode->Next == NULL)
146 currentNode->Next = (WSDXML_NODE *)pSecond;
147 break;
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);
160 return S_OK;
163 HRESULT WINAPI WSDXMLBuildAnyForSingleElement(WSDXML_NAME *pElementName, LPCWSTR pszText, WSDXML_ELEMENT **ppAny)
165 WSDXML_TEXT *child;
167 TRACE("(%p, %s, %p)\n", pElementName, debugstr_w(pszText), ppAny);
169 if ((pElementName == NULL) || ((pszText != NULL) && (lstrlenW(pszText) > WSD_MAX_TEXT_LENGTH)))
171 return E_INVALIDARG;
174 if (ppAny == NULL)
176 return E_POINTER;
179 *ppAny = WSDAllocateLinkedMemory(NULL, sizeof(WSDXML_ELEMENT));
181 if (*ppAny == NULL)
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;
196 if (pszText != NULL)
198 child = WSDAllocateLinkedMemory(*ppAny, sizeof(WSDXML_TEXT));
200 if (child == NULL)
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;
220 return S_OK;
223 HRESULT WINAPI WSDXMLCleanupElement(WSDXML_ELEMENT *pAny)
225 TRACE("(%p)\n", pAny);
227 if (pAny == NULL)
229 return E_INVALIDARG;
232 WSDFreeLinkedMemory(pAny);
233 return S_OK;
236 HRESULT WINAPI WSDXMLGetValueFromAny(const WCHAR *pszNamespace, const WCHAR *pszName, WSDXML_ELEMENT *pAny, LPCWSTR *ppszValue)
238 WSDXML_ELEMENT *element;
239 WSDXML_TEXT *text;
241 if (pAny == NULL)
242 return E_INVALIDARG;
244 if (ppszValue == NULL)
245 return E_POINTER;
247 if ((pszNamespace == NULL) || (pszName == NULL) || (lstrlenW(pszNamespace) > WSD_MAX_TEXT_LENGTH) || (lstrlenW(pszName) > WSD_MAX_TEXT_LENGTH))
248 return E_INVALIDARG;
250 element = pAny;
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))
260 return E_FAIL;
263 text = (WSDXML_TEXT *) element->FirstChild;
264 *ppszValue = (LPCWSTR) text->Text;
266 return S_OK;
270 element = (WSDXML_ELEMENT *) element->Node.Next;
273 return E_FAIL;
276 /* IWSDXMLContext implementation */
278 struct xmlNamespace
280 struct list entry;
281 WSDXML_NAMESPACE *namespace;
284 typedef struct IWSDXMLContextImpl
286 IWSDXMLContext IWSDXMLContext_iface;
287 LONG ref;
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;
305 return NULL;
308 static WSDXML_NAME *find_name(WSDXML_NAMESPACE *ns, LPCWSTR name)
310 int i;
312 for (i = 0; i < ns->NamesCount; i++)
314 if (lstrcmpW(ns->Names[i].LocalName, name) == 0)
316 return &ns->Names[i];
320 return NULL;
323 static WSDXML_NAME *add_name(WSDXML_NAMESPACE *ns, LPCWSTR name)
325 WSDXML_NAME *names;
326 WSDXML_NAME *newName;
327 int i;
329 names = WSDAllocateLinkedMemory(ns, sizeof(WSDXML_NAME) * (ns->NamesCount + 1));
331 if (names == NULL)
333 return NULL;
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);
350 ns->Names = names;
352 newName = &names[ns->NamesCount];
354 newName->LocalName = duplicate_string(names, name);
355 newName->Space = ns;
357 if (newName->LocalName == NULL)
359 return NULL;
362 ns->NamesCount++;
363 return newName;
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)
374 return FALSE;
378 return TRUE;
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);
397 return NULL;
400 static WSDXML_NAMESPACE *add_namespace(struct list *namespaces, LPCWSTR uri)
402 struct xmlNamespace *ns = WSDAllocateLinkedMemory(namespaces, sizeof(struct xmlNamespace));
404 if (ns == NULL)
406 return NULL;
409 ns->namespace = WSDAllocateLinkedMemory(ns, sizeof(WSDXML_NAMESPACE));
411 if (ns->namespace == NULL)
413 WSDFreeLinkedMemory(ns);
414 return NULL;
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);
423 return NULL;
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);
441 if (!ppv)
443 WARN("Invalid parameter\n");
444 return E_INVALIDARG;
447 *ppv = NULL;
449 if (IsEqualIID(riid, &IID_IUnknown) ||
450 IsEqualIID(riid, &IID_IWSDXMLContext))
452 *ppv = &This->IWSDXMLContext_iface;
454 else
456 WARN("Unknown IID %s\n", debugstr_guid(riid));
457 return E_NOINTERFACE;
460 IUnknown_AddRef((IUnknown*)*ppv);
461 return S_OK;
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);
470 return 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);
480 if (ref == 0)
482 WSDFreeLinkedMemory(This);
485 return ref;
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;
493 BOOL setNewPrefix;
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))
500 return E_INVALIDARG;
503 ns = find_namespace(This->namespaces, pszUri);
505 if (ns == NULL)
507 ns = add_namespace(This->namespaces, pszUri);
509 if (ns == NULL)
511 return E_OUTOFMEMORY;
515 setNewPrefix = (ns->PreferredPrefix == NULL);
517 if ((ns->PreferredPrefix == NULL) || (lstrcmpW(ns->PreferredPrefix, pszSuggestedPrefix) != 0))
519 newPrefix = pszSuggestedPrefix;
520 setNewPrefix = TRUE;
523 if (setNewPrefix)
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);
532 else
534 ns->PreferredPrefix = generate_namespace_prefix(This, ns, pszUri);
535 if (ns->PreferredPrefix == NULL)
537 return E_FAIL;
542 if (ppNamespace != NULL)
544 *ppNamespace = duplicate_namespace(NULL, ns);
546 if (*ppNamespace == NULL)
548 return E_OUTOFMEMORY;
552 return S_OK;
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;
559 WSDXML_NAME *name;
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))
565 return E_INVALIDARG;
568 ns = find_namespace(This->namespaces, pszUri);
570 if (ns == NULL)
572 /* The namespace doesn't exist, add it */
573 ns = add_namespace(This->namespaces, pszUri);
575 if (ns == NULL)
577 return E_OUTOFMEMORY;
580 ns->PreferredPrefix = generate_namespace_prefix(This, ns, pszUri);
581 if (ns->PreferredPrefix == NULL)
583 return E_FAIL;
587 name = find_name(ns, pszName);
589 if (name == NULL)
591 name = add_name(ns, pszName);
593 if (name == NULL)
595 return E_OUTOFMEMORY;
599 if (ppName != NULL)
601 *ppName = duplicate_name(NULL, name);
603 if (*ppName == NULL)
605 return E_OUTOFMEMORY;
609 return S_OK;
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);
615 return E_NOTIMPL;
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);
621 return E_NOTIMPL;
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");
644 return E_POINTER;
647 *ppContext = NULL;
649 obj = WSDAllocateLinkedMemory(NULL, sizeof(*obj));
651 if (!obj)
653 return E_OUTOFMEMORY;
656 obj->IWSDXMLContext_iface.lpVtbl = &xmlcontext_vtbl;
657 obj->ref = 1;
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);
672 return S_OK;
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;
688 return NULL;