2 * Copyright 2015 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "webservices.h"
25 #include "wine/debug.h"
26 #include "webservices_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(webservices
);
37 { sizeof(ULONG
), FALSE
}, /* WS_XML_WRITER_PROPERTY_MAX_DEPTH */
38 { sizeof(BOOL
), FALSE
}, /* WS_XML_WRITER_PROPERTY_ALLOW_FRAGMENT */
39 { sizeof(ULONG
), FALSE
}, /* WS_XML_READER_PROPERTY_MAX_ATTRIBUTES */
40 { sizeof(BOOL
), FALSE
}, /* WS_XML_WRITER_PROPERTY_WRITE_DECLARATION */
41 { sizeof(ULONG
), FALSE
}, /* WS_XML_WRITER_PROPERTY_INDENT */
42 { sizeof(ULONG
), FALSE
}, /* WS_XML_WRITER_PROPERTY_BUFFER_TRIM_SIZE */
43 { sizeof(WS_CHARSET
), FALSE
}, /* WS_XML_WRITER_PROPERTY_CHARSET */
44 { sizeof(WS_BUFFERS
), FALSE
}, /* WS_XML_WRITER_PROPERTY_BUFFERS */
45 { sizeof(ULONG
), FALSE
}, /* WS_XML_WRITER_PROPERTY_BUFFER_MAX_SIZE */
46 { sizeof(WS_BYTES
), FALSE
}, /* WS_XML_WRITER_PROPERTY_BYTES */
47 { sizeof(BOOL
), TRUE
}, /* WS_XML_WRITER_PROPERTY_IN_ATTRIBUTE */
48 { sizeof(ULONG
), FALSE
}, /* WS_XML_WRITER_PROPERTY_MAX_MIME_PARTS_BUFFER_SIZE */
49 { sizeof(WS_BYTES
), FALSE
}, /* WS_XML_WRITER_PROPERTY_INITIAL_BUFFER */
50 { sizeof(BOOL
), FALSE
}, /* WS_XML_WRITER_PROPERTY_ALLOW_INVALID_CHARACTER_REFERENCES */
51 { sizeof(ULONG
), FALSE
}, /* WS_XML_WRITER_PROPERTY_MAX_NAMESPACES */
52 { sizeof(ULONG
), TRUE
}, /* WS_XML_WRITER_PROPERTY_BYTES_WRITTEN */
53 { sizeof(ULONG
), TRUE
}, /* WS_XML_WRITER_PROPERTY_BYTES_TO_CLOSE */
54 { sizeof(BOOL
), FALSE
}, /* WS_XML_WRITER_PROPERTY_COMPRESS_EMPTY_ELEMENTS */
55 { sizeof(BOOL
), FALSE
} /* WS_XML_WRITER_PROPERTY_EMIT_UNCOMPRESSED_EMPTY_ELEMENTS */
62 WS_XML_WRITER_OUTPUT_TYPE output_type
;
63 struct xmlbuf
*output_buf
;
66 WS_XML_WRITER_PROPERTY prop
[sizeof(writer_props
)/sizeof(writer_props
[0])];
69 static struct writer
*alloc_writer(void)
71 static const ULONG count
= sizeof(writer_props
)/sizeof(writer_props
[0]);
73 ULONG i
, size
= sizeof(*ret
) + count
* sizeof(WS_XML_WRITER_PROPERTY
);
76 for (i
= 0; i
< count
; i
++) size
+= writer_props
[i
].size
;
77 if (!(ret
= heap_alloc_zero( size
))) return NULL
;
79 ptr
= (char *)&ret
->prop
[count
];
80 for (i
= 0; i
< count
; i
++)
82 ret
->prop
[i
].value
= ptr
;
83 ret
->prop
[i
].valueSize
= writer_props
[i
].size
;
84 ptr
+= ret
->prop
[i
].valueSize
;
86 ret
->prop_count
= count
;
90 static HRESULT
set_writer_prop( struct writer
*writer
, WS_XML_WRITER_PROPERTY_ID id
, const void *value
,
93 if (id
>= writer
->prop_count
|| size
!= writer_props
[id
].size
|| writer_props
[id
].readonly
)
96 memcpy( writer
->prop
[id
].value
, value
, size
);
100 static HRESULT
get_writer_prop( struct writer
*writer
, WS_XML_WRITER_PROPERTY_ID id
, void *buf
, ULONG size
)
102 if (id
>= writer
->prop_count
|| size
!= writer_props
[id
].size
)
105 memcpy( buf
, writer
->prop
[id
].value
, writer
->prop
[id
].valueSize
);
109 static void free_writer( struct writer
*writer
)
111 WsFreeHeap( writer
->output_heap
);
115 /**************************************************************************
116 * WsCreateWriter [webservices.@]
118 HRESULT WINAPI
WsCreateWriter( const WS_XML_WRITER_PROPERTY
*properties
, ULONG count
,
119 WS_XML_WRITER
**handle
, WS_ERROR
*error
)
121 struct writer
*writer
;
122 ULONG i
, max_depth
= 32, max_attrs
= 128, trim_size
= 4096, max_size
= 65536, max_ns
= 32;
123 WS_CHARSET charset
= WS_CHARSET_UTF8
;
126 TRACE( "%p %u %p %p\n", properties
, count
, handle
, error
);
127 if (error
) FIXME( "ignoring error parameter\n" );
129 if (!handle
) return E_INVALIDARG
;
130 if (!(writer
= alloc_writer())) return E_OUTOFMEMORY
;
132 set_writer_prop( writer
, WS_XML_WRITER_PROPERTY_MAX_DEPTH
, &max_depth
, sizeof(max_depth
) );
133 set_writer_prop( writer
, WS_XML_WRITER_PROPERTY_MAX_ATTRIBUTES
, &max_attrs
, sizeof(max_attrs
) );
134 set_writer_prop( writer
, WS_XML_WRITER_PROPERTY_BUFFER_TRIM_SIZE
, &trim_size
, sizeof(trim_size
) );
135 set_writer_prop( writer
, WS_XML_WRITER_PROPERTY_CHARSET
, &charset
, sizeof(charset
) );
136 set_writer_prop( writer
, WS_XML_WRITER_PROPERTY_BUFFER_MAX_SIZE
, &max_size
, sizeof(max_size
) );
137 set_writer_prop( writer
, WS_XML_WRITER_PROPERTY_MAX_MIME_PARTS_BUFFER_SIZE
, &max_size
, sizeof(max_size
) );
138 set_writer_prop( writer
, WS_XML_WRITER_PROPERTY_MAX_NAMESPACES
, &max_ns
, sizeof(max_ns
) );
140 for (i
= 0; i
< count
; i
++)
142 hr
= set_writer_prop( writer
, properties
[i
].id
, properties
[i
].value
, properties
[i
].valueSize
);
145 free_writer( writer
);
150 hr
= get_writer_prop( writer
, WS_XML_WRITER_PROPERTY_BUFFER_MAX_SIZE
, &max_size
, sizeof(max_size
) );
153 free_writer( writer
);
157 hr
= WsCreateHeap( max_size
, 0, NULL
, 0, &writer
->output_heap
, NULL
);
160 free_writer( writer
);
164 *handle
= (WS_XML_WRITER
*)writer
;
168 /**************************************************************************
169 * WsFreeWriter [webservices.@]
171 void WINAPI
WsFreeWriter( WS_XML_WRITER
*handle
)
173 struct writer
*writer
= (struct writer
*)handle
;
175 TRACE( "%p\n", handle
);
176 free_writer( writer
);
179 #define XML_BUFFER_INITIAL_ALLOCATED_SIZE 256
180 static struct xmlbuf
*alloc_xmlbuf( WS_HEAP
*heap
)
184 if (!(ret
= ws_alloc( heap
, sizeof(*ret
) ))) return NULL
;
185 if (!(ret
->ptr
= ws_alloc( heap
, XML_BUFFER_INITIAL_ALLOCATED_SIZE
)))
187 ws_free( heap
, ret
);
191 ret
->size_allocated
= XML_BUFFER_INITIAL_ALLOCATED_SIZE
;
196 static void free_xmlbuf( struct xmlbuf
*xmlbuf
)
199 ws_free( xmlbuf
->heap
, xmlbuf
->ptr
);
200 ws_free( xmlbuf
->heap
, xmlbuf
);
203 /**************************************************************************
204 * WsCreateXmlBuffer [webservices.@]
206 HRESULT WINAPI
WsCreateXmlBuffer( WS_HEAP
*heap
, const WS_XML_BUFFER_PROPERTY
*properties
,
207 ULONG count
, WS_XML_BUFFER
**handle
, WS_ERROR
*error
)
209 struct xmlbuf
*xmlbuf
;
211 if (!heap
|| !handle
) return E_INVALIDARG
;
212 if (count
) FIXME( "properties not implemented\n" );
214 if (!(xmlbuf
= alloc_xmlbuf( heap
))) return E_OUTOFMEMORY
;
216 *handle
= (WS_XML_BUFFER
*)xmlbuf
;
220 /**************************************************************************
221 * WsGetWriterProperty [webservices.@]
223 HRESULT WINAPI
WsGetWriterProperty( WS_XML_WRITER
*handle
, WS_XML_WRITER_PROPERTY_ID id
,
224 void *buf
, ULONG size
, WS_ERROR
*error
)
226 struct writer
*writer
= (struct writer
*)handle
;
228 TRACE( "%p %u %p %u %p\n", handle
, id
, buf
, size
, error
);
229 if (error
) FIXME( "ignoring error parameter\n" );
231 if (!writer
->output_type
) return WS_E_INVALID_OPERATION
;
232 return get_writer_prop( writer
, id
, buf
, size
);
235 static void set_output_buffer( struct writer
*writer
, struct xmlbuf
*xmlbuf
)
237 /* free current buffer if it's ours */
238 if (writer
->output_buf
&& writer
->output_buf
->heap
== writer
->output_heap
)
240 free_xmlbuf( writer
->output_buf
);
242 writer
->output_buf
= xmlbuf
;
243 writer
->output_type
= WS_XML_WRITER_OUTPUT_TYPE_BUFFER
;
244 writer
->write_bufptr
= xmlbuf
->ptr
;
245 writer
->write_pos
= 0;
248 /**************************************************************************
249 * WsSetOutput [webservices.@]
251 HRESULT WINAPI
WsSetOutput( WS_XML_WRITER
*handle
, const WS_XML_WRITER_ENCODING
*encoding
,
252 const WS_XML_WRITER_OUTPUT
*output
, const WS_XML_WRITER_PROPERTY
*properties
,
253 ULONG count
, WS_ERROR
*error
)
255 struct writer
*writer
= (struct writer
*)handle
;
259 TRACE( "%p %p %p %p %u %p\n", handle
, encoding
, output
, properties
, count
, error
);
260 if (error
) FIXME( "ignoring error parameter\n" );
262 if (!writer
) return E_INVALIDARG
;
264 for (i
= 0; i
< count
; i
++)
266 hr
= set_writer_prop( writer
, properties
[i
].id
, properties
[i
].value
, properties
[i
].valueSize
);
267 if (hr
!= S_OK
) return hr
;
270 switch (encoding
->encodingType
)
272 case WS_XML_WRITER_ENCODING_TYPE_TEXT
:
274 WS_XML_WRITER_TEXT_ENCODING
*text
= (WS_XML_WRITER_TEXT_ENCODING
*)encoding
;
275 if (text
->charSet
!= WS_CHARSET_UTF8
)
277 FIXME( "charset %u not supported\n", text
->charSet
);
283 FIXME( "encoding type %u not supported\n", encoding
->encodingType
);
286 switch (output
->outputType
)
288 case WS_XML_WRITER_OUTPUT_TYPE_BUFFER
:
290 struct xmlbuf
*xmlbuf
;
292 if (!(xmlbuf
= alloc_xmlbuf( writer
->output_heap
))) return E_OUTOFMEMORY
;
293 set_output_buffer( writer
, xmlbuf
);
297 FIXME( "output type %u not supported\n", output
->outputType
);
304 /**************************************************************************
305 * WsSetOutputToBuffer [webservices.@]
307 HRESULT WINAPI
WsSetOutputToBuffer( WS_XML_WRITER
*handle
, WS_XML_BUFFER
*buffer
,
308 const WS_XML_WRITER_PROPERTY
*properties
, ULONG count
,
311 struct writer
*writer
= (struct writer
*)handle
;
312 struct xmlbuf
*xmlbuf
= (struct xmlbuf
*)buffer
;
316 TRACE( "%p %p %p %u %p\n", handle
, buffer
, properties
, count
, error
);
317 if (error
) FIXME( "ignoring error parameter\n" );
319 if (!writer
|| !xmlbuf
) return E_INVALIDARG
;
321 for (i
= 0; i
< count
; i
++)
323 hr
= set_writer_prop( writer
, properties
[i
].id
, properties
[i
].value
, properties
[i
].valueSize
);
324 if (hr
!= S_OK
) return hr
;
327 set_output_buffer( writer
, xmlbuf
);