2 * Configuration file parsing
4 * Copyright 2010 Vincent Povirk
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
35 #include "wine/list.h"
36 #include "mscoree_private.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL( mscoree
);
44 STATE_ASSEMBLY_BINDING
,
53 typedef struct ConfigFileHandler
55 ISAXContentHandler ISAXContentHandler_iface
;
56 ISAXErrorHandler ISAXErrorHandler_iface
;
58 enum parse_state states
[16];
60 parsed_config_file
*result
;
65 IStream IStream_iface
;
70 static inline ConfigStream
*impl_from_IStream(IStream
*iface
)
72 return CONTAINING_RECORD(iface
, ConfigStream
, IStream_iface
);
75 static HRESULT WINAPI
ConfigStream_QueryInterface(IStream
*iface
, REFIID riid
, void **ppv
)
77 ConfigStream
*This
= impl_from_IStream(iface
);
79 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
81 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IStream
))
82 *ppv
= &This
->IStream_iface
;
85 WARN("Not supported iface %s\n", debugstr_guid(riid
));
90 IUnknown_AddRef((IUnknown
*)*ppv
);
94 static ULONG WINAPI
ConfigStream_AddRef(IStream
*iface
)
96 ConfigStream
*This
= impl_from_IStream(iface
);
97 ULONG ref
= InterlockedIncrement(&This
->ref
);
99 TRACE("(%p) ref=%lu\n", This
, ref
);
104 static ULONG WINAPI
ConfigStream_Release(IStream
*iface
)
106 ConfigStream
*This
= impl_from_IStream(iface
);
107 ULONG ref
= InterlockedDecrement(&This
->ref
);
109 TRACE("(%p) ref=%lu\n",This
, ref
);
113 CloseHandle(This
->file
);
120 static HRESULT WINAPI
ConfigStream_Read(IStream
*iface
, void *buf
, ULONG size
, ULONG
*ret_read
)
122 ConfigStream
*This
= impl_from_IStream(iface
);
125 TRACE("(%p)->(%p %lu %p)\n", This
, buf
, size
, ret_read
);
127 if (!ReadFile(This
->file
, buf
, size
, &read
, NULL
))
129 WARN("error %ld reading file\n", GetLastError());
130 return HRESULT_FROM_WIN32(GetLastError());
133 if (ret_read
) *ret_read
= read
;
137 static HRESULT WINAPI
ConfigStream_Write(IStream
*iface
, const void *buf
, ULONG size
, ULONG
*written
)
139 ConfigStream
*This
= impl_from_IStream(iface
);
140 TRACE("(%p)->(%p %lu %p)\n", This
, buf
, size
, written
);
144 static HRESULT WINAPI
ConfigStream_Seek(IStream
*iface
, LARGE_INTEGER dlibMove
,
145 DWORD dwOrigin
, ULARGE_INTEGER
*pNewPos
)
147 ConfigStream
*This
= impl_from_IStream(iface
);
148 TRACE("(%p)->(%ld %ld %p)\n", This
, dlibMove
.u
.LowPart
, dwOrigin
, pNewPos
);
152 static HRESULT WINAPI
ConfigStream_SetSize(IStream
*iface
, ULARGE_INTEGER libNewSize
)
154 ConfigStream
*This
= impl_from_IStream(iface
);
155 TRACE("(%p)->(%ld)\n", This
, libNewSize
.u
.LowPart
);
159 static HRESULT WINAPI
ConfigStream_CopyTo(IStream
*iface
, IStream
*stream
, ULARGE_INTEGER size
,
160 ULARGE_INTEGER
*read
, ULARGE_INTEGER
*written
)
162 ConfigStream
*This
= impl_from_IStream(iface
);
163 FIXME("(%p)->(%p %ld %p %p)\n", This
, stream
, size
.u
.LowPart
, read
, written
);
167 static HRESULT WINAPI
ConfigStream_Commit(IStream
*iface
, DWORD flags
)
169 ConfigStream
*This
= impl_from_IStream(iface
);
170 FIXME("(%p,%ld)\n", This
, flags
);
174 static HRESULT WINAPI
ConfigStream_Revert(IStream
*iface
)
176 ConfigStream
*This
= impl_from_IStream(iface
);
177 TRACE("(%p)\n", This
);
181 static HRESULT WINAPI
ConfigStream_LockUnlockRegion(IStream
*iface
, ULARGE_INTEGER libOffset
,
182 ULARGE_INTEGER cb
, DWORD dwLockType
)
184 ConfigStream
*This
= impl_from_IStream(iface
);
185 TRACE("(%p,%ld,%ld,%ld)\n", This
, libOffset
.u
.LowPart
, cb
.u
.LowPart
, dwLockType
);
189 static HRESULT WINAPI
ConfigStream_Stat(IStream
*iface
, STATSTG
*lpStat
, DWORD grfStatFlag
)
191 ConfigStream
*This
= impl_from_IStream(iface
);
192 FIXME("(%p,%p,%ld)\n", This
, lpStat
, grfStatFlag
);
196 static HRESULT WINAPI
ConfigStream_Clone(IStream
*iface
, IStream
**ppstm
)
198 ConfigStream
*This
= impl_from_IStream(iface
);
199 TRACE("(%p)\n",This
);
203 static const IStreamVtbl ConfigStreamVtbl
= {
204 ConfigStream_QueryInterface
,
206 ConfigStream_Release
,
210 ConfigStream_SetSize
,
214 ConfigStream_LockUnlockRegion
,
215 ConfigStream_LockUnlockRegion
,
220 HRESULT WINAPI
CreateConfigStream(const WCHAR
*filename
, IStream
**stream
)
222 ConfigStream
*config_stream
;
225 TRACE("(%s, %p)\n", debugstr_w(filename
), stream
);
228 return COR_E_NULLREFERENCE
;
230 file
= CreateFileW(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
231 if (file
== INVALID_HANDLE_VALUE
)
232 return GetLastError() == ERROR_FILE_NOT_FOUND
? COR_E_FILENOTFOUND
: E_FAIL
;
234 config_stream
= malloc(sizeof(*config_stream
));
238 return E_OUTOFMEMORY
;
241 config_stream
->IStream_iface
.lpVtbl
= &ConfigStreamVtbl
;
242 config_stream
->ref
= 1;
243 config_stream
->file
= file
;
245 *stream
= &config_stream
->IStream_iface
;
249 static inline ConfigFileHandler
*impl_from_ISAXContentHandler(ISAXContentHandler
*iface
)
251 return CONTAINING_RECORD(iface
, ConfigFileHandler
, ISAXContentHandler_iface
);
254 static inline ConfigFileHandler
*impl_from_ISAXErrorHandler(ISAXErrorHandler
*iface
)
256 return CONTAINING_RECORD(iface
, ConfigFileHandler
, ISAXErrorHandler_iface
);
259 static HRESULT WINAPI
ConfigFileHandler_QueryInterface(ISAXContentHandler
*iface
,
260 REFIID riid
, void **ppvObject
)
262 ConfigFileHandler
*This
= impl_from_ISAXContentHandler(iface
);
264 if (IsEqualGUID(riid
, &IID_ISAXContentHandler
) || IsEqualGUID(riid
, &IID_IUnknown
))
265 *ppvObject
= &This
->ISAXContentHandler_iface
;
266 else if (IsEqualGUID(riid
, &IID_ISAXErrorHandler
))
267 *ppvObject
= &This
->ISAXErrorHandler_iface
;
270 WARN("Unsupported interface %s\n", debugstr_guid(riid
));
271 return E_NOINTERFACE
;
274 IUnknown_AddRef((IUnknown
*)*ppvObject
);
279 static ULONG WINAPI
ConfigFileHandler_AddRef(ISAXContentHandler
*iface
)
281 ConfigFileHandler
*This
= impl_from_ISAXContentHandler(iface
);
282 return InterlockedIncrement(&This
->ref
);
285 static ULONG WINAPI
ConfigFileHandler_Release(ISAXContentHandler
*iface
)
287 ConfigFileHandler
*This
= impl_from_ISAXContentHandler(iface
);
288 ULONG ref
= InterlockedDecrement(&This
->ref
);
296 static HRESULT WINAPI
ConfigFileHandler_putDocumentLocator(ISAXContentHandler
*iface
,
297 ISAXLocator
*pLocator
)
302 static HRESULT WINAPI
ConfigFileHandler_startDocument(ISAXContentHandler
*iface
)
307 static HRESULT WINAPI
ConfigFileHandler_endDocument(ISAXContentHandler
*iface
)
312 static HRESULT WINAPI
ConfigFileHandler_startPrefixMapping(ISAXContentHandler
*iface
,
313 const WCHAR
*pPrefix
, int nPrefix
, const WCHAR
*pUri
, int nUri
)
318 static HRESULT WINAPI
ConfigFileHandler_endPrefixMapping(ISAXContentHandler
*iface
,
319 const WCHAR
*pPrefix
, int nPrefix
)
324 static HRESULT
parse_startup(ConfigFileHandler
*This
, ISAXAttributes
*pAttr
)
326 static const WCHAR legacy
[] = {'u','s','e','L','e','g','a','c','y','V','2','R','u','n','t','i','m','e','A','c','t','i','v','a','t','i','o','n','P','o','l','i','c','y',0};
327 static const WCHAR empty
[] = {0};
332 hr
= ISAXAttributes_getValueFromName(pAttr
, empty
, 0, legacy
, lstrlenW(legacy
), &value
, &value_size
);
334 FIXME("useLegacyV2RuntimeActivationPolicy=%s not implemented\n", debugstr_wn(value
, value_size
));
340 static HRESULT
parse_probing(ConfigFileHandler
*This
, ISAXAttributes
*pAttr
)
342 static const WCHAR privatePath
[] = {'p','r','i','v','a','t','e','P','a','t','h',0};
343 static const WCHAR empty
[] = {0};
348 hr
= ISAXAttributes_getValueFromName(pAttr
, empty
, 0, privatePath
, lstrlenW(privatePath
), &value
, &value_size
);
351 TRACE("%s\n", debugstr_wn(value
, value_size
));
353 This
->result
->private_path
= wcsdup(value
);
354 if (!This
->result
->private_path
)
362 static HRESULT
parse_supported_runtime(ConfigFileHandler
*This
, ISAXAttributes
*pAttr
)
364 static const WCHAR version
[] = {'v','e','r','s','i','o','n',0};
365 static const WCHAR sku
[] = {'s','k','u',0};
366 static const WCHAR empty
[] = {0};
370 supported_runtime
*entry
;
372 hr
= ISAXAttributes_getValueFromName(pAttr
, empty
, 0, version
, lstrlenW(version
), &value
, &value_size
);
375 TRACE("%s\n", debugstr_wn(value
, value_size
));
376 entry
= malloc(sizeof(supported_runtime
));
379 entry
->version
= wcsdup(value
);
382 list_add_tail(&This
->result
->supported_runtimes
, &entry
->entry
);
394 WARN("Missing version attribute\n");
398 hr
= ISAXAttributes_getValueFromName(pAttr
, empty
, 0, sku
, lstrlenW(sku
), &value
, &value_size
);
400 FIXME("sku=%s not implemented\n", debugstr_wn(value
, value_size
));
407 static HRESULT WINAPI
ConfigFileHandler_startElement(ISAXContentHandler
*iface
,
408 const WCHAR
*pNamespaceUri
, int nNamespaceUri
, const WCHAR
*pLocalName
,
409 int nLocalName
, const WCHAR
*pQName
, int nQName
, ISAXAttributes
*pAttr
)
411 ConfigFileHandler
*This
= impl_from_ISAXContentHandler(iface
);
412 static const WCHAR configuration
[] = {'c','o','n','f','i','g','u','r','a','t','i','o','n',0};
413 static const WCHAR assemblyBinding
[] = {'a','s','s','e','m','b','l','y','B','i','n','d','i','n','g',0};
414 static const WCHAR probing
[] = {'p','r','o','b','i','n','g',0};
415 static const WCHAR runtime
[] = {'r','u','n','t','i','m','e',0};
416 static const WCHAR startup
[] = {'s','t','a','r','t','u','p',0};
417 static const WCHAR supportedRuntime
[] = {'s','u','p','p','o','r','t','e','d','R','u','n','t','i','m','e',0};
421 TRACE("%s %s %s\n", debugstr_wn(pNamespaceUri
,nNamespaceUri
),
422 debugstr_wn(pLocalName
,nLocalName
), debugstr_wn(pQName
,nQName
));
424 if (This
->statenum
== ARRAY_SIZE(This
->states
) - 1)
426 ERR("file has too much nesting\n");
430 switch (This
->states
[This
->statenum
])
433 if (nLocalName
== ARRAY_SIZE(configuration
) - 1 && wcscmp(pLocalName
, configuration
) == 0)
435 This
->states
[++This
->statenum
] = STATE_CONFIGURATION
;
440 case STATE_CONFIGURATION
:
441 if (nLocalName
== ARRAY_SIZE(startup
) - 1 && wcscmp(pLocalName
, startup
) == 0)
443 hr
= parse_startup(This
, pAttr
);
444 This
->states
[++This
->statenum
] = STATE_STARTUP
;
447 else if (nLocalName
== ARRAY_SIZE(runtime
) - 1 && wcscmp(pLocalName
, runtime
) == 0)
449 This
->states
[++This
->statenum
] = STATE_RUNTIME
;
455 if (nLocalName
== ARRAY_SIZE(assemblyBinding
) - 1 &&
456 wcscmp(pLocalName
, assemblyBinding
) == 0)
458 This
->states
[++This
->statenum
] = STATE_ASSEMBLY_BINDING
;
463 case STATE_ASSEMBLY_BINDING
:
464 if (nLocalName
== ARRAY_SIZE(probing
) - 1 && wcscmp(pLocalName
, probing
) == 0)
466 hr
= parse_probing(This
, pAttr
);
467 This
->states
[++This
->statenum
] = STATE_PROBING
;
473 if (nLocalName
== ARRAY_SIZE(supportedRuntime
) - 1 &&
474 wcscmp(pLocalName
, supportedRuntime
) == 0)
476 hr
= parse_supported_runtime(This
, pAttr
);
477 This
->states
[++This
->statenum
] = STATE_UNKNOWN
;
489 TRACE("Unknown element %s in state %u\n", debugstr_wn(pLocalName
,nLocalName
),
490 This
->states
[This
->statenum
]);
492 This
->states
[++This
->statenum
] = STATE_UNKNOWN
;
497 static HRESULT WINAPI
ConfigFileHandler_endElement(ISAXContentHandler
*iface
,
498 const WCHAR
*pNamespaceUri
, int nNamespaceUri
, const WCHAR
*pLocalName
,
499 int nLocalName
, const WCHAR
*pQName
, int nQName
)
501 ConfigFileHandler
*This
= impl_from_ISAXContentHandler(iface
);
503 TRACE("%s %s %s\n", debugstr_wn(pNamespaceUri
,nNamespaceUri
),
504 debugstr_wn(pLocalName
,nLocalName
), debugstr_wn(pQName
,nQName
));
506 if (This
->statenum
> 0)
512 ERR("element end does not match a start\n");
519 static HRESULT WINAPI
ConfigFileHandler_characters(ISAXContentHandler
*iface
,
520 const WCHAR
*pChars
, int nChars
)
522 TRACE("%s\n", debugstr_wn(pChars
,nChars
));
527 static HRESULT WINAPI
ConfigFileHandler_ignorableWhitespace(ISAXContentHandler
*iface
,
528 const WCHAR
*pChars
, int nChars
)
533 static HRESULT WINAPI
ConfigFileHandler_processingInstruction(ISAXContentHandler
*iface
,
534 const WCHAR
*pTarget
, int nTarget
, const WCHAR
*pData
, int nData
)
539 static HRESULT WINAPI
ConfigFileHandler_skippedEntity(ISAXContentHandler
*iface
,
540 const WCHAR
* pName
, int nName
)
542 TRACE("%s\n", debugstr_wn(pName
,nName
));
546 static const struct ISAXContentHandlerVtbl ConfigFileHandlerVtbl
=
548 ConfigFileHandler_QueryInterface
,
549 ConfigFileHandler_AddRef
,
550 ConfigFileHandler_Release
,
551 ConfigFileHandler_putDocumentLocator
,
552 ConfigFileHandler_startDocument
,
553 ConfigFileHandler_endDocument
,
554 ConfigFileHandler_startPrefixMapping
,
555 ConfigFileHandler_endPrefixMapping
,
556 ConfigFileHandler_startElement
,
557 ConfigFileHandler_endElement
,
558 ConfigFileHandler_characters
,
559 ConfigFileHandler_ignorableWhitespace
,
560 ConfigFileHandler_processingInstruction
,
561 ConfigFileHandler_skippedEntity
564 static HRESULT WINAPI
ConfigFileHandler_Error_QueryInterface(ISAXErrorHandler
*iface
,
565 REFIID riid
, void **ppvObject
)
567 ConfigFileHandler
*This
= impl_from_ISAXErrorHandler(iface
);
568 return ISAXContentHandler_QueryInterface(&This
->ISAXContentHandler_iface
, riid
, ppvObject
);
571 static ULONG WINAPI
ConfigFileHandler_Error_AddRef(ISAXErrorHandler
*iface
)
573 ConfigFileHandler
*This
= impl_from_ISAXErrorHandler(iface
);
574 return ISAXContentHandler_AddRef(&This
->ISAXContentHandler_iface
);
577 static ULONG WINAPI
ConfigFileHandler_Error_Release(ISAXErrorHandler
*iface
)
579 ConfigFileHandler
*This
= impl_from_ISAXErrorHandler(iface
);
580 return ISAXContentHandler_Release(&This
->ISAXContentHandler_iface
);
583 static HRESULT WINAPI
ConfigFileHandler_error(ISAXErrorHandler
*iface
,
584 ISAXLocator
* pLocator
, const WCHAR
* pErrorMessage
, HRESULT hrErrorCode
)
586 WARN("%s,%lx\n", debugstr_w(pErrorMessage
), hrErrorCode
);
590 static HRESULT WINAPI
ConfigFileHandler_fatalError(ISAXErrorHandler
*iface
,
591 ISAXLocator
* pLocator
, const WCHAR
* pErrorMessage
, HRESULT hrErrorCode
)
593 WARN("%s,%lx\n", debugstr_w(pErrorMessage
), hrErrorCode
);
597 static HRESULT WINAPI
ConfigFileHandler_ignorableWarning(ISAXErrorHandler
*iface
,
598 ISAXLocator
* pLocator
, const WCHAR
* pErrorMessage
, HRESULT hrErrorCode
)
600 WARN("%s,%lx\n", debugstr_w(pErrorMessage
), hrErrorCode
);
604 static const struct ISAXErrorHandlerVtbl ConfigFileHandlerErrorVtbl
=
606 ConfigFileHandler_Error_QueryInterface
,
607 ConfigFileHandler_Error_AddRef
,
608 ConfigFileHandler_Error_Release
,
609 ConfigFileHandler_error
,
610 ConfigFileHandler_fatalError
,
611 ConfigFileHandler_ignorableWarning
614 static void init_config(parsed_config_file
*config
)
616 list_init(&config
->supported_runtimes
);
617 config
->private_path
= NULL
;
620 static HRESULT
parse_config(VARIANT input
, parsed_config_file
*result
)
622 ISAXXMLReader
*reader
;
623 ConfigFileHandler
*handler
;
626 handler
= malloc(sizeof(ConfigFileHandler
));
628 return E_OUTOFMEMORY
;
630 handler
->ISAXContentHandler_iface
.lpVtbl
= &ConfigFileHandlerVtbl
;
631 handler
->ISAXErrorHandler_iface
.lpVtbl
= &ConfigFileHandlerErrorVtbl
;
633 handler
->states
[0] = STATE_ROOT
;
634 handler
->statenum
= 0;
635 handler
->result
= result
;
637 hr
= CoCreateInstance(&CLSID_SAXXMLReader
, NULL
, CLSCTX_INPROC_SERVER
,
638 &IID_ISAXXMLReader
, (LPVOID
*)&reader
);
642 hr
= ISAXXMLReader_putContentHandler(reader
, &handler
->ISAXContentHandler_iface
);
645 hr
= ISAXXMLReader_putErrorHandler(reader
, &handler
->ISAXErrorHandler_iface
);
648 hr
= ISAXXMLReader_parse(reader
, input
);
650 ISAXXMLReader_Release(reader
);
653 ISAXContentHandler_Release(&handler
->ISAXContentHandler_iface
);
658 HRESULT
parse_config_stream(IStream
*stream
, parsed_config_file
*result
)
666 initresult
= CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
667 V_VT(&var
) = VT_UNKNOWN
;
668 V_UNKNOWN(&var
) = (IUnknown
*)stream
;
670 hr
= parse_config(var
, result
);
672 if (SUCCEEDED(initresult
))
678 HRESULT
parse_config_file(LPCWSTR filename
, parsed_config_file
*result
)
685 hr
= CreateConfigStream(filename
, &stream
);
689 hr
= parse_config_stream(stream
, result
);
691 IStream_Release(stream
);
696 void free_parsed_config_file(parsed_config_file
*file
)
698 supported_runtime
*cursor
, *cursor2
;
700 LIST_FOR_EACH_ENTRY_SAFE(cursor
, cursor2
, &file
->supported_runtimes
, supported_runtime
, entry
)
702 free(cursor
->version
);
703 list_remove(&cursor
->entry
);
707 free(file
->private_path
);