123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- #include "includes.h"
- #include "common.h"
- #include "base64.h"
- #include "http.h"
- #include "upnp_xml.h"
- static int xml_next_tag(const char *in, const char **out,
- const char **out_tagname, const char **end)
- {
- while (*in && *in != '<')
- in++;
- if (*in != '<')
- return 1;
- *out = ++in;
- if (*in == '/')
- in++;
- *out_tagname = in;
- while (isalnum(*in) || *in == '-')
- in++;
- if (*in == ':')
- *out_tagname = ++in;
- while (*in && *in != '>')
- in++;
- if (*in != '>')
- return 1;
- *end = ++in;
- return 0;
- }
- void xml_data_encode(struct wpabuf *buf, const char *data, int len)
- {
- int i;
- for (i = 0; i < len; i++) {
- u8 c = ((u8 *) data)[i];
- if (c == '<') {
- wpabuf_put_str(buf, "<");
- continue;
- }
- if (c == '>') {
- wpabuf_put_str(buf, ">");
- continue;
- }
- if (c == '&') {
- wpabuf_put_str(buf, "&");
- continue;
- }
- if (c == '\'') {
- wpabuf_put_str(buf, "'");
- continue;
- }
- if (c == '"') {
- wpabuf_put_str(buf, """);
- continue;
- }
-
- wpabuf_put_u8(buf, c);
- }
- }
- void xml_add_tagged_data(struct wpabuf *buf, const char *tag, const char *data)
- {
- wpabuf_printf(buf, "<%s>", tag);
- xml_data_encode(buf, data, os_strlen(data));
- wpabuf_printf(buf, "</%s>\n", tag);
- }
- char * xml_get_first_item(const char *doc, const char *item)
- {
- const char *match = item;
- int match_len = os_strlen(item);
- const char *tag, *tagname, *end;
- char *value;
-
- for (;;) {
- if (xml_next_tag(doc, &tag, &tagname, &end))
- return NULL;
- doc = end;
- if (!os_strncasecmp(tagname, match, match_len) &&
- *tag != '/' &&
- (tagname[match_len] == '>' ||
- !isgraph(tagname[match_len]))) {
- break;
- }
- }
- end = doc;
- while (*end && *end != '<')
- end++;
- value = os_zalloc(1 + (end - doc));
- if (value == NULL)
- return NULL;
- os_memcpy(value, doc, end - doc);
- return value;
- }
- struct wpabuf * xml_get_base64_item(const char *data, const char *name,
- enum http_reply_code *ret)
- {
- char *msg;
- struct wpabuf *buf;
- unsigned char *decoded;
- size_t len;
- msg = xml_get_first_item(data, name);
- if (msg == NULL) {
- *ret = UPNP_ARG_VALUE_INVALID;
- return NULL;
- }
- decoded = base64_decode((unsigned char *) msg, os_strlen(msg), &len);
- os_free(msg);
- if (decoded == NULL) {
- *ret = UPNP_OUT_OF_MEMORY;
- return NULL;
- }
- buf = wpabuf_alloc_ext_data(decoded, len);
- if (buf == NULL) {
- os_free(decoded);
- *ret = UPNP_OUT_OF_MEMORY;
- return NULL;
- }
- return buf;
- }
|