From 63c86dce1e248aa03a99792f56077dc176bcd295 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Fri, 27 Apr 2012 15:15:40 +0200 Subject: piano: Move to JSON API (v5) In random order: Thanks to Chris Eby for his work on pithos, to an anonymous coward for sending me his Android client, to Alex Howard for sending me the webOS client, to ZigZagJoe for providing a temporary fix and thanks to everyone who sent Pandora a message. Although there are a few rough edges here and there this fixes #236. --- Makefile | 47 +-- src/libezxml/COPYING | 20 - src/libezxml/ezxml.c | 718 ------------------------------- src/libezxml/ezxml.h | 93 ---- src/libpiano/crypt.c | 205 +++------ src/libpiano/piano.c | 1146 ++++++++++++++++++++++++++++---------------------- src/libpiano/piano.h | 79 +++- src/libpiano/xml.c | 980 ------------------------------------------ src/libpiano/xml.h | 49 --- src/settings.c | 4 +- src/ui.c | 4 +- src/ui_act.c | 6 +- 12 files changed, 778 insertions(+), 2573 deletions(-) delete mode 100644 src/libezxml/COPYING delete mode 100644 src/libezxml/ezxml.c delete mode 100644 src/libezxml/ezxml.h delete mode 100644 src/libpiano/xml.c delete mode 100644 src/libpiano/xml.h diff --git a/Makefile b/Makefile index 3bfd7fe..b503e7f 100644 --- a/Makefile +++ b/Makefile @@ -39,12 +39,10 @@ PIANOBAR_OBJ=${PIANOBAR_SRC:.c=.o} LIBPIANO_DIR=src/libpiano LIBPIANO_SRC=\ ${LIBPIANO_DIR}/crypt.c \ - ${LIBPIANO_DIR}/piano.c \ - ${LIBPIANO_DIR}/xml.c + ${LIBPIANO_DIR}/piano.c LIBPIANO_HDR=\ ${LIBPIANO_DIR}/config.h \ ${LIBPIANO_DIR}/crypt_key_output.h \ - ${LIBPIANO_DIR}/xml.h \ ${LIBPIANO_DIR}/crypt.h \ ${LIBPIANO_DIR}/piano.h \ ${LIBPIANO_DIR}/crypt_key_input.h \ @@ -62,13 +60,6 @@ LIBWAITRESS_OBJ=${LIBWAITRESS_SRC:.c=.o} LIBWAITRESS_RELOBJ=${LIBWAITRESS_SRC:.c=.lo} LIBWAITRESS_INCLUDE=${LIBWAITRESS_DIR} -LIBEZXML_DIR=src/libezxml -LIBEZXML_SRC=${LIBEZXML_DIR}/ezxml.c -LIBEZXML_HDR=${LIBEZXML_DIR}/ezxml.h -LIBEZXML_OBJ=${LIBEZXML_SRC:.c=.o} -LIBEZXML_RELOBJ=${LIBEZXML_SRC:.c=.lo} -LIBEZXML_INCLUDE=${LIBEZXML_DIR} - ifeq (${DISABLE_FAAD}, 1) LIBFAAD_CFLAGS= LIBFAAD_LDFLAGS= @@ -88,51 +79,59 @@ endif LIBGNUTLS_CFLAGS= LIBGNUTLS_LDFLAGS=-lgnutls +LIBGCRYPT_CFLAGS= +LIBGCRYPT_LDFLAGS=-lgcrypt + +LIBJSONC_CFLAGS=$(shell pkg-config --cflags json) +LIBJSONC_LDFLAGS=$(shell pkg-config --libs json) + # build pianobar ifeq (${DYNLINK},1) pianobar: ${PIANOBAR_OBJ} ${PIANOBAR_HDR} libpiano.so.0 @echo " LINK $@" @${CC} -o $@ ${PIANOBAR_OBJ} ${LDFLAGS} -lao -lpthread -lm -L. -lpiano \ - ${LIBFAAD_LDFLAGS} ${LIBMAD_LDFLAGS} ${LIBGNUTLS_LDFLAGS} + ${LIBFAAD_LDFLAGS} ${LIBMAD_LDFLAGS} ${LIBGNUTLS_LDFLAGS} \ + ${LIBGCRYPT_LDFLAGS} ${LIBJSONC_LDFLAGS} else pianobar: ${PIANOBAR_OBJ} ${PIANOBAR_HDR} ${LIBPIANO_OBJ} ${LIBWAITRESS_OBJ} \ - ${LIBWAITRESS_HDR} ${LIBEZXML_OBJ} ${LIBEZXML_HDR} + ${LIBWAITRESS_HDR} @echo " LINK $@" @${CC} ${CFLAGS} ${LDFLAGS} ${PIANOBAR_OBJ} ${LIBPIANO_OBJ} \ - ${LIBWAITRESS_OBJ} ${LIBEZXML_OBJ} -lao -lpthread -lm \ - ${LIBFAAD_LDFLAGS} ${LIBMAD_LDFLAGS} ${LIBGNUTLS_LDFLAGS} -o $@ + ${LIBWAITRESS_OBJ} -lao -lpthread -lm \ + ${LIBFAAD_LDFLAGS} ${LIBMAD_LDFLAGS} ${LIBGNUTLS_LDFLAGS} \ + ${LIBGCRYPT_LDFLAGS} ${LIBJSONC_LDFLAGS} -o $@ endif # build shared and static libpiano libpiano.so.0: ${LIBPIANO_RELOBJ} ${LIBPIANO_HDR} ${LIBWAITRESS_RELOBJ} \ - ${LIBWAITRESS_HDR} ${LIBEZXML_RELOBJ} ${LIBEZXML_HDR} \ - ${LIBPIANO_OBJ} ${LIBWAITRESS_OBJ} ${LIBEZXML_OBJ} + ${LIBWAITRESS_HDR} ${LIBPIANO_OBJ} ${LIBWAITRESS_OBJ} @echo " LINK $@" - @${CC} -shared -Wl,-soname,libpiano.so.0 ${CFLAGS} ${LDFLAGS} ${LIBGNUTLS_LDFLAGS} \ + @${CC} -shared -Wl,-soname,libpiano.so.0 ${CFLAGS} ${LDFLAGS} \ + ${LIBGNUTLS_LDFLAGS} ${LIBGCRYPT_LDFLAGS} \ -o libpiano.so.0.0.0 ${LIBPIANO_RELOBJ} \ - ${LIBWAITRESS_RELOBJ} ${LIBEZXML_RELOBJ} + ${LIBWAITRESS_RELOBJ} @ln -s libpiano.so.0.0.0 libpiano.so.0 @ln -s libpiano.so.0 libpiano.so @echo " AR libpiano.a" - @${AR} rcs libpiano.a ${LIBPIANO_OBJ} ${LIBWAITRESS_OBJ} ${LIBEZXML_OBJ} + @${AR} rcs libpiano.a ${LIBPIANO_OBJ} ${LIBWAITRESS_OBJ} %.o: %.c @echo " CC $<" @${CC} ${CFLAGS} -I ${LIBPIANO_INCLUDE} -I ${LIBWAITRESS_INCLUDE} \ - -I ${LIBEZXML_INCLUDE} ${LIBFAAD_CFLAGS} \ - ${LIBMAD_CFLAGS} ${LIBGNUTLS_CFLAGS} -c -o $@ $< + ${LIBFAAD_CFLAGS} ${LIBMAD_CFLAGS} ${LIBGNUTLS_CFLAGS} \ + ${LIBJSONC_CFLAGS} -c -o $@ $< # create position independent code (for shared libraries) %.lo: %.c @echo " CC $< (PIC)" @${CC} ${CFLAGS} -I ${LIBPIANO_INCLUDE} -I ${LIBWAITRESS_INCLUDE} \ - -I ${LIBEZXML_INCLUDE} -c -fPIC -o $@ $< + -c -fPIC -o $@ $< clean: @echo " CLEAN" @${RM} ${PIANOBAR_OBJ} ${LIBPIANO_OBJ} ${LIBWAITRESS_OBJ} ${LIBWAITRESS_OBJ}/test.o \ - ${LIBEZXML_OBJ} ${LIBPIANO_RELOBJ} ${LIBWAITRESS_RELOBJ} \ - ${LIBEZXML_RELOBJ} pianobar libpiano.so* libpiano.a waitress-test + ${LIBPIANO_RELOBJ} ${LIBWAITRESS_RELOBJ} pianobar libpiano.so* \ + libpiano.a waitress-test all: pianobar diff --git a/src/libezxml/COPYING b/src/libezxml/COPYING deleted file mode 100644 index 80e4e88..0000000 --- a/src/libezxml/COPYING +++ /dev/null @@ -1,20 +0,0 @@ -Copyright 2004-2006 Aaron Voisine - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be included -in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/src/libezxml/ezxml.c b/src/libezxml/ezxml.c deleted file mode 100644 index 0e1dd5d..0000000 --- a/src/libezxml/ezxml.c +++ /dev/null @@ -1,718 +0,0 @@ -/* ezxml.c - * - * Copyright 2004-2006 Aaron Voisine - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#ifndef __FreeBSD__ -#define _BSD_SOURCE /* required by strdup() */ -#define _DARWIN_C_SOURCE /* required by strdup() on OS X */ -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include "ezxml.h" - -#define EZXML_WS "\t\r\n " // whitespace -#define EZXML_ERRL 128 // maximum error string length - -typedef struct ezxml_root *ezxml_root_t; -struct ezxml_root { // additional data for the root tag - struct ezxml xml; // is a super-struct built on top of ezxml struct - ezxml_t cur; // current xml tree insertion point - char *m; // original xml string - size_t len; // length of allocated memory for mmap, -1 for malloc - char *u; // UTF-8 conversion of string if original was UTF-16 - char *s; // start of work area - char *e; // end of work area - char **ent; // general entities (ampersand sequences) - char ***attr; // default attributes - char ***pi; // processing instructions - short standalone; // non-zero if - char err[EZXML_ERRL]; // error string -}; - -char *EZXML_NIL[] = { NULL }; // empty, null terminated array of strings - -// sets a flag for the given tag and returns the tag -static ezxml_t ezxml_set_flag(ezxml_t xml, short flag) { - if (xml) xml->flags |= flag; - return xml; -} - -// inserts an existing tag into an ezxml structure -static ezxml_t ezxml_insert(ezxml_t xml, ezxml_t dest, size_t off) -{ - ezxml_t cur, prev, head; - - xml->next = xml->sibling = xml->ordered = NULL; - xml->off = off; - xml->parent = dest; - - if ((head = dest->child)) { // already have sub tags - if (head->off <= off) { // not first subtag - for (cur = head; cur->ordered && cur->ordered->off <= off; - cur = cur->ordered); - xml->ordered = cur->ordered; - cur->ordered = xml; - } - else { // first subtag - xml->ordered = head; - dest->child = xml; - } - - for (cur = head, prev = NULL; cur && strcmp(cur->name, xml->name); - prev = cur, cur = cur->sibling); // find tag type - if (cur && cur->off <= off) { // not first of type - while (cur->next && cur->next->off <= off) cur = cur->next; - xml->next = cur->next; - cur->next = xml; - } - else { // first tag of this type - if (prev && cur) prev->sibling = cur->sibling; // remove old first - xml->next = cur; // old first tag is now next - for (cur = head, prev = NULL; cur && cur->off <= off; - prev = cur, cur = cur->sibling); // new sibling insert point - xml->sibling = cur; - if (prev) prev->sibling = xml; - } - } - else dest->child = xml; // only sub tag - - return xml; -} - -// Adds a child tag. off is the offset of the child tag relative to the start -// of the parent tag's character content. Returns the child tag. -static ezxml_t ezxml_add_child(ezxml_t xml, char *name, size_t off) -{ - ezxml_t child; - - if (! xml) return NULL; - child = (ezxml_t)memset(malloc(sizeof(struct ezxml)), '\0', - sizeof(struct ezxml)); - child->name = (char *)name; - child->attr = EZXML_NIL; - child->txt = ""; - - return ezxml_insert(child, xml, off); -} - -// returns the first child tag with the given name or NULL if not found -ezxml_t ezxml_child(ezxml_t xml, const char *name) -{ - xml = (xml) ? xml->child : NULL; - while (xml && strcmp(name, xml->name)) xml = xml->sibling; - return xml; -} - -// returns a new empty ezxml structure with the given root tag name -static ezxml_t ezxml_new(char *name) -{ - static char *ent[] = { "lt;", "<", "gt;", ">", "quot;", """, - "apos;", "'", "amp;", "&", NULL }; - ezxml_root_t root = (ezxml_root_t)memset(malloc(sizeof(struct ezxml_root)), - '\0', sizeof(struct ezxml_root)); - root->xml.name = (char *)name; - root->cur = &root->xml; - strcpy(root->err, root->xml.txt = ""); - root->ent = memcpy(malloc(sizeof(ent)), ent, sizeof(ent)); - root->attr = root->pi = (char ***)(root->xml.attr = EZXML_NIL); - return &root->xml; -} - -// returns the Nth tag with the same name in the same subsection or NULL if not -// found -ezxml_t ezxml_idx(ezxml_t xml, int idx) -{ - for (; xml && idx; idx--) xml = xml->next; - return xml; -} - -// returns the value of the requested tag attribute or NULL if not found -const char *ezxml_attr(ezxml_t xml, const char *attr) -{ - int i = 0, j = 1; - ezxml_root_t root = (ezxml_root_t)xml; - - if (! xml || ! xml->attr) return NULL; - while (xml->attr[i] && strcmp(attr, xml->attr[i])) i += 2; - if (xml->attr[i]) return xml->attr[i + 1]; // found attribute - - while (root->xml.parent) root = (ezxml_root_t)root->xml.parent; // root tag - for (i = 0; root->attr[i] && strcmp(xml->name, root->attr[i][0]); i++); - if (! root->attr[i]) return NULL; // no matching default attributes - while (root->attr[i][j] && strcmp(attr, root->attr[i][j])) j += 3; - return (root->attr[i][j]) ? root->attr[i][j + 1] : NULL; // found default -} - -// same as ezxml_get but takes an already initialized va_list -static ezxml_t ezxml_vget(ezxml_t xml, va_list ap) -{ - char *name = va_arg(ap, char *); - int idx = -1; - - if (name && *name) { - idx = va_arg(ap, int); - xml = ezxml_child(xml, name); - } - return (idx < 0) ? xml : ezxml_vget(ezxml_idx(xml, idx), ap); -} - -// Traverses the xml tree to retrieve a specific subtag. Takes a variable -// length list of tag names and indexes. The argument list must be terminated -// by either an index of -1 or an empty string tag name. Example: -// title = ezxml_get(library, "shelf", 0, "book", 2, "title", -1); -// This retrieves the title of the 3rd book on the 1st shelf of library. -// Returns NULL if not found. -ezxml_t ezxml_get(ezxml_t xml, ...) -{ - va_list ap; - ezxml_t r; - - va_start(ap, xml); - r = ezxml_vget(xml, ap); - va_end(ap); - return r; -} - -// set an error string and return root -static ezxml_t ezxml_err(ezxml_root_t root, char *s, const char *err, ...) -{ - va_list ap; - int line = 1; - char *t, fmt[EZXML_ERRL]; - - for (t = root->s; t < s; t++) if (*t == '\n') line++; - snprintf(fmt, EZXML_ERRL, "[error near line %d]: %s", line, err); - - va_start(ap, err); - vsnprintf(root->err, EZXML_ERRL, fmt, ap); - va_end(ap); - - return &root->xml; -} - -// Recursively decodes entity and character references and normalizes new lines -// ent is a null terminated array of alternating entity names and values. set t -// to '&' for general entity decoding, '%' for parameter entity decoding, 'c' -// for cdata sections, ' ' for attribute normalization, or '*' for non-cdata -// attribute normalization. Returns s, or if the decoded string is longer than -// s, returns a malloced string that must be freed. -static char *ezxml_decode(char *s, char **ent, char t) -{ - char *e, *r = s, *m = s; - long b, c, d, l; - - for (; *s; s++) { // normalize line endings - while (*s == '\r') { - *(s++) = '\n'; - if (*s == '\n') memmove(s, (s + 1), strlen(s)); - } - } - - for (s = r; ; ) { - while (*s && *s != '&' && (*s != '%' || t != '%') && !isspace(*s)) s++; - - if (! *s) break; - else if (t != 'c' && ! strncmp(s, "&#", 2)) { // character reference - if (s[2] == 'x') c = strtol(s + 3, &e, 16); // base 16 - else c = strtol(s + 2, &e, 10); // base 10 - if (! c || *e != ';') { s++; continue; } // not a character ref - - if (c < 0x80) *(s++) = c; // US-ASCII subset - else { // multi-byte UTF-8 sequence - for (b = 0, d = c; d; d /= 2) b++; // number of bits in c - b = (b - 2) / 5; // number of bytes in payload - *(s++) = (0xFF << (7 - b)) | (c >> (6 * b)); // head - while (b) *(s++) = 0x80 | ((c >> (6 * --b)) & 0x3F); // payload - } - - memmove(s, strchr(s, ';') + 1, strlen(strchr(s, ';'))); - } - else if ((*s == '&' && (t == '&' || t == ' ' || t == '*')) || - (*s == '%' && t == '%')) { // entity reference - for (b = 0; ent[b] && strncmp(s + 1, ent[b], strlen(ent[b])); - b += 2); // find entity in entity list - - if (ent[b++]) { // found a match - if ((c = strlen(ent[b])) - 1 > (e = strchr(s, ';')) - s) { - l = (d = (s - r)) + c + strlen(e); // new length - r = (r == m) ? strcpy(malloc(l), r) : realloc(r, l); - e = strchr((s = r + d), ';'); // fix up pointers - } - - memmove(s + c, e + 1, strlen(e)); // shift rest of string - strncpy(s, ent[b], c); // copy in replacement text - } - else s++; // not a known entity - } - else if ((t == ' ' || t == '*') && isspace(*s)) *(s++) = ' '; - else s++; // no decoding needed - } - - if (t == '*') { // normalize spaces for non-cdata attributes - for (s = r; *s; s++) { - if ((l = strspn(s, " "))) memmove(s, s + l, strlen(s + l) + 1); - while (*s && *s != ' ') s++; - } - if (--s >= r && *s == ' ') *s = '\0'; // trim any trailing space - } - return r; -} - -// called when parser finds start of new tag -static void ezxml_open_tag(ezxml_root_t root, char *name, char **attr) -{ - ezxml_t xml = root->cur; - - if (xml->name) xml = ezxml_add_child(xml, name, strlen(xml->txt)); - else xml->name = name; // first open tag - - xml->attr = attr; - root->cur = xml; // update tag insertion point -} - -// called when parser finds character content between open and closing tag -static void ezxml_char_content(ezxml_root_t root, char *s, size_t len, char t) -{ - ezxml_t xml = root->cur; - char *m = s; - size_t l; - - if (! xml || ! xml->name || ! len) return; // sanity check - - s[len] = '\0'; // null terminate text (calling functions anticipate this) - len = strlen(s = ezxml_decode(s, root->ent, t)) + 1; - - if (! *(xml->txt)) xml->txt = s; // initial character content - else { // allocate our own memory and make a copy - xml->txt = (xml->flags & EZXML_TXTM) // allocate some space - ? realloc(xml->txt, (l = strlen(xml->txt)) + len) - : strcpy(malloc((l = strlen(xml->txt)) + len), xml->txt); - strcpy(xml->txt + l, s); // add new char content - if (s != m) free(s); // free s if it was malloced by ezxml_decode() - } - - if (xml->txt != m) ezxml_set_flag(xml, EZXML_TXTM); -} - -// called when parser finds closing tag -static ezxml_t ezxml_close_tag(ezxml_root_t root, char *name, char *s) -{ - if (! root->cur || ! root->cur->name || strcmp(name, root->cur->name)) - return ezxml_err(root, s, "unexpected closing tag ", name); - - root->cur = root->cur->parent; - return NULL; -} - -// checks for circular entity references, returns non-zero if no circular -// references are found, zero otherwise -static int ezxml_ent_ok(char *name, char *s, char **ent) -{ - int i; - - for (; ; s++) { - while (*s && *s != '&') s++; // find next entity reference - if (! *s) return 1; - if (! strncmp(s + 1, name, strlen(name))) return 0; // circular ref. - for (i = 0; ent[i] && strncmp(ent[i], s + 1, strlen(ent[i])); i += 2); - if (ent[i] && ! ezxml_ent_ok(name, ent[i + 1], ent)) return 0; - } -} - -// called when the parser finds a processing instruction -static void ezxml_proc_inst(ezxml_root_t root, char *s, size_t len) -{ - int i = 0, j = 1; - char *target = s; - - s[len] = '\0'; // null terminate instruction - if (*(s += strcspn(s, EZXML_WS))) { - *s = '\0'; // null terminate target - s += strspn(s + 1, EZXML_WS) + 1; // skip whitespace after target - } - - if (! strcmp(target, "xml")) { // - if ((s = strstr(s, "standalone")) && ! strncmp(s + strspn(s + 10, - EZXML_WS "='\"") + 10, "yes", 3)) root->standalone = 1; - return; - } - - if (! root->pi[0]) *(root->pi = malloc(sizeof(char **))) = NULL; //first pi - - while (root->pi[i] && strcmp(target, root->pi[i][0])) i++; // find target - if (! root->pi[i]) { // new target - root->pi = realloc(root->pi, sizeof(char **) * (i + 2)); - root->pi[i] = malloc(sizeof(char *) * 3); - root->pi[i][0] = target; - root->pi[i][1] = (char *)(root->pi[i + 1] = NULL); // terminate pi list - root->pi[i][2] = strdup(""); // empty document position list - } - - while (root->pi[i][j]) j++; // find end of instruction list for this target - root->pi[i] = realloc(root->pi[i], sizeof(char *) * (j + 3)); - root->pi[i][j + 2] = realloc(root->pi[i][j + 1], j + 1); - strcpy(root->pi[i][j + 2] + j - 1, (root->xml.name) ? ">" : "<"); - root->pi[i][j + 1] = NULL; // null terminate pi list for this target - root->pi[i][j] = s; // set instruction -} - -// called when the parser finds an internal doctype subset -static short ezxml_internal_dtd(ezxml_root_t root, char *s, size_t len) -{ - char q, *c, *t, *n = NULL, *v, **ent, **pe; - int i, j; - - pe = memcpy(malloc(sizeof(EZXML_NIL)), EZXML_NIL, sizeof(EZXML_NIL)); - - for (s[len] = '\0'; s; ) { - while (*s && *s != '<' && *s != '%') s++; // find next declaration - - if (! *s) break; - else if (! strncmp(s, "'); - continue; - } - - for (i = 0, ent = (*c == '%') ? pe : root->ent; ent[i]; i++); - ent = realloc(ent, (i + 3) * sizeof(char *)); // space for next ent - if (*c == '%') pe = ent; - else root->ent = ent; - - *(++s) = '\0'; // null terminate name - if ((s = strchr(v, q))) *(s++) = '\0'; // null terminate value - ent[i + 1] = ezxml_decode(v, pe, '%'); // set value - ent[i + 2] = NULL; // null terminate entity list - if (! ezxml_ent_ok(n, ent[i + 1], ent)) { // circular reference - if (ent[i + 1] != v) free(ent[i + 1]); - ezxml_err(root, v, "circular entity declaration &%s", n); - break; - } - else ent[i] = n; // set entity name - } - else if (! strncmp(s, "")) == '>') continue; - else *s = '\0'; // null terminate tag name - for (i = 0; root->attr[i] && strcmp(n, root->attr[i][0]); i++); - - ++s; // ansi cpr - while (*(n = s + strspn(s, EZXML_WS)) && *n != '>') { - if (*(s = n + strcspn(n, EZXML_WS))) *s = '\0'; // attr name - else { ezxml_err(root, t, "malformed ") - 1; - if (*c == ' ') continue; // cdata is default, nothing to do - v = NULL; - } - else if ((*s == '"' || *s == '\'') && // default value - (s = strchr(v = s + 1, *s))) *s = '\0'; - else { ezxml_err(root, t, "malformed attr[i]) { // new tag name - root->attr = (! i) ? malloc(2 * sizeof(char **)) - : realloc(root->attr, - (i + 2) * sizeof(char **)); - root->attr[i] = malloc(2 * sizeof(char *)); - root->attr[i][0] = t; // set tag name - root->attr[i][1] = (char *)(root->attr[i + 1] = NULL); - } - - for (j = 1; root->attr[i][j]; j += 3); // find end of list - root->attr[i] = realloc(root->attr[i], - (j + 4) * sizeof(char *)); - - root->attr[i][j + 3] = NULL; // null terminate list - root->attr[i][j + 2] = c; // is it cdata? - root->attr[i][j + 1] = (v) ? ezxml_decode(v, root->ent, *c) - : NULL; - root->attr[i][j] = n; // attribute name - ++s; - } - } - else if (! strncmp(s, ""); // comments - else if (! strncmp(s, ""))) - ezxml_proc_inst(root, c, s++ - c); - } - else if (*s == '<') s = strchr(s, '>'); // skip other declarations - else if (*(s++) == '%' && ! root->standalone) break; - } - - free(pe); - return ! *root->err; -} - -// Converts a UTF-16 string to UTF-8. Returns a new string that must be freed -// or NULL if no conversion was needed. -static char *ezxml_str2utf8(char **s, size_t *len) -{ - char *u; - size_t l = 0, sl, max = *len; - long c, d; - int b, be = (**s == '\xFE') ? 1 : (**s == '\xFF') ? 0 : -1; - - if (be == -1) return NULL; // not UTF-16 - - u = malloc(max); - for (sl = 2; sl < *len - 1; sl += 2) { - c = (be) ? (((*s)[sl] & 0xFF) << 8) | ((*s)[sl + 1] & 0xFF) //UTF-16BE - : (((*s)[sl + 1] & 0xFF) << 8) | ((*s)[sl] & 0xFF); //UTF-16LE - if (c >= 0xD800 && c <= 0xDFFF && (sl += 2) < *len - 1) { // high-half - d = (be) ? (((*s)[sl] & 0xFF) << 8) | ((*s)[sl + 1] & 0xFF) - : (((*s)[sl + 1] & 0xFF) << 8) | ((*s)[sl] & 0xFF); - c = (((c & 0x3FF) << 10) | (d & 0x3FF)) + 0x10000; - } - - while (l + 6 > max) u = realloc(u, max += EZXML_BUFSIZE); - if (c < 0x80) u[l++] = c; // US-ASCII subset - else { // multi-byte UTF-8 sequence - for (b = 0, d = c; d; d /= 2) b++; // bits in c - b = (b - 2) / 5; // bytes in payload - u[l++] = (0xFF << (7 - b)) | (c >> (6 * b)); // head - while (b) u[l++] = 0x80 | ((c >> (6 * --b)) & 0x3F); // payload - } - } - return *s = realloc(u, *len = l); -} - -// frees a tag attribute list -static void ezxml_free_attr(char **attr) { - int i = 0; - char *m; - - if (! attr || attr == EZXML_NIL) return; // nothing to free - while (attr[i]) i += 2; // find end of attribute list - m = attr[i + 1]; // list of which names and values are malloced - for (i = 0; m[i]; i++) { - if (m[i] & EZXML_NAMEM) free(attr[i * 2]); - if (m[i] & EZXML_TXTM) free(attr[(i * 2) + 1]); - } - free(m); - free(attr); -} - -// parse the given xml string and return an ezxml structure -ezxml_t ezxml_parse_str(char *s, size_t len) -{ - ezxml_root_t root = (ezxml_root_t)ezxml_new(NULL); - char q, e, *d, **attr, **a = NULL; // initialize a to avoid compile warning - int l, i, j; - - root->m = s; - if (! len) return ezxml_err(root, NULL, "root tag missing"); - root->u = ezxml_str2utf8(&s, &len); // convert utf-16 to utf-8 - root->e = (root->s = s) + len; // record start and end of work area - - e = s[len - 1]; // save end char - s[len - 1] = '\0'; // turn end char into null terminator - - while (*s && *s != '<') s++; // find first tag - if (! *s) return ezxml_err(root, s, "root tag missing"); - - for (; ; ) { - attr = (char **)EZXML_NIL; - d = ++s; - - if (isalpha(*s) || *s == '_' || *s == ':' || *s < '\0') { // new tag - if (! root->cur) - return ezxml_err(root, d, "markup outside of root element"); - - s += strcspn(s, EZXML_WS "/>"); - while (isspace(*s)) *(s++) = '\0'; // null terminate tag name - - if (*s && *s != '/' && *s != '>') // find tag in default attr list - for (i = 0; (a = root->attr[i]) && strcmp(a[0], d); i++); - - for (l = 0; *s && *s != '/' && *s != '>'; l += 2) { // new attrib - attr = (l) ? realloc(attr, (l + 4) * sizeof(char *)) - : malloc(4 * sizeof(char *)); // allocate space - attr[l + 3] = (l) ? realloc(attr[l + 1], (l / 2) + 2) - : malloc(2); // mem for list of maloced vals - strcpy(attr[l + 3] + (l / 2), " "); // value is not malloced - attr[l + 2] = NULL; // null terminate list - attr[l + 1] = ""; // temporary attribute value - attr[l] = s; // set attribute name - - s += strcspn(s, EZXML_WS "=/>"); - if (*s == '=' || isspace(*s)) { - *(s++) = '\0'; // null terminate tag attribute name - q = *(s += strspn(s, EZXML_WS "=")); - if (q == '"' || q == '\'') { // attribute value - attr[l + 1] = ++s; - while (*s && *s != q) s++; - if (*s) *(s++) = '\0'; // null terminate attribute val - else { - ezxml_free_attr(attr); - return ezxml_err(root, d, "missing %c", q); - } - - for (j = 1; a && a[j] && strcmp(a[j], attr[l]); j +=3); - attr[l + 1] = ezxml_decode(attr[l + 1], root->ent, (a - && a[j]) ? *a[j + 2] : ' '); - if (attr[l + 1] < d || attr[l + 1] > s) - attr[l + 3][l / 2] = EZXML_TXTM; // value malloced - } - } - while (isspace(*s)) s++; - } - - if (*s == '/') { // self closing tag - *(s++) = '\0'; - if ((*s && *s != '>') || (! *s && e != '>')) { - if (l) ezxml_free_attr(attr); - return ezxml_err(root, d, "missing >"); - } - ezxml_open_tag(root, d, attr); - ezxml_close_tag(root, d, s); - } - else if ((q = *s) == '>' || (! *s && e == '>')) { // open tag - *s = '\0'; // temporarily null terminate tag name - ezxml_open_tag(root, d, attr); - *s = q; - } - else { - if (l) ezxml_free_attr(attr); - return ezxml_err(root, d, "missing >"); - } - } - else if (*s == '/') { // close tag - s += strcspn(d = s + 1, EZXML_WS ">") + 1; - if (! (q = *s) && e != '>') return ezxml_err(root, d, "missing >"); - *s = '\0'; // temporarily null terminate tag name - if (ezxml_close_tag(root, d, s)) return &root->xml; - if (isspace(*s = q)) s += strspn(s, EZXML_WS); - } - else if (! strncmp(s, "!--", 3)) { // xml comment - if (! (s = strstr(s + 3, "--")) || (*(s += 2) != '>' && *s) || - (! *s && e != '>')) return ezxml_err(root, d, "unclosed - * - * @param xml node named "struct" (or containing a similar structure) - * @param who wants to use this data? callback: content of as - * string, content of as xmlNode (may contain other nodes - * or text), additional data used by callback(); don't forget - * to *copy* data taken from or as they will be - * freed soon - * @param extra data for callback - */ -static void PianoXmlStructParser (const ezxml_t structRoot, - void (*callback) (const char *, const ezxml_t, void *), void *data) { - ezxml_t curNode, keyNode, valueNode; - char *key; - - /* get all nodes */ - for (curNode = ezxml_child (structRoot, "member"); curNode; curNode = curNode->next) { - /* reset variables */ - key = NULL; - valueNode = keyNode = NULL; - - keyNode = ezxml_child (curNode, "name"); - if (keyNode != NULL) { - key = ezxml_txt (keyNode); - } - - valueNode = ezxml_child (curNode, "value"); - /* this will ignore empty nodes, but well... */ - if (*key != '\0' && valueNode != NULL) { - (*callback) ((char *) key, valueNode, data); - } - } -} - -/* create xml parser from string - * @param xml document - * @param returns document pointer (needed to free memory later) - * @param returns document root - * @return _OK or error - */ -static PianoReturn_t PianoXmlInitDoc (char *xmlStr, ezxml_t *xmlDoc) { - PianoReturn_t ret; - - if ((*xmlDoc = ezxml_parse_str (xmlStr, strlen (xmlStr))) == NULL) { - return PIANO_RET_XML_INVALID; - } - - if ((ret = PianoXmlIsFault (*xmlDoc)) != PIANO_RET_OK) { - ezxml_free (*xmlDoc); - return ret; - } - - return PIANO_RET_OK; -} - -/* get text from nodes; some of them have , - * or subnodes, just ignore them - * @param xml node - */ -static char *PianoXmlGetNodeText (const ezxml_t node) { - char *retTxt = NULL; - - retTxt = ezxml_txt (node); - /* no text => empty string */ - if (*retTxt == '\0') { - retTxt = ezxml_txt (node->child); - } - return retTxt; -} - -/* structParser callback; writes userinfo to PianoUserInfo structure - * @param value identifier - * @param value node - * @param pointer to userinfo structure - * @return nothing - */ -static void PianoXmlParseUserinfoCb (const char *key, const ezxml_t value, - void *data) { - PianoUserInfo_t *user = data; - char *valueStr = PianoXmlGetNodeText (value); - - if (strcmp ("webAuthToken", key) == 0) { - user->webAuthToken = strdup (valueStr); - } else if (strcmp ("authToken", key) == 0) { - user->authToken = strdup (valueStr); - } else if (strcmp ("listenerId", key) == 0) { - user->listenerId = strdup (valueStr); - } -} - -static void PianoXmlParseStationsCb (const char *key, const ezxml_t value, - void *data) { - PianoStation_t *station = data; - char *valueStr = PianoXmlGetNodeText (value); - - if (strcmp ("stationName", key) == 0) { - station->name = strdup (valueStr); - } else if (strcmp ("stationId", key) == 0) { - station->id = strdup (valueStr); - } else if (strcmp ("isQuickMix", key) == 0) { - station->isQuickMix = (strcmp (valueStr, "1") == 0); - } else if (strcmp ("isCreator", key) == 0) { - station->isCreator = (strcmp (valueStr, "1") == 0); - } -} - -static void PianoXmlParsePlaylistCb (const char *key, const ezxml_t value, - void *data) { - PianoSong_t *song = data; - char *valueStr = PianoXmlGetNodeText (value); - - if (strcmp ("audioURL", key) == 0) { - /* last 48 chars of audioUrl are encrypted, but they put the key - * into the door's lock... */ - const char urlTailN = 48; - const size_t valueStrN = strlen (valueStr); - char *urlTail = NULL, - *urlTailCrypted = &valueStr[valueStrN - urlTailN]; - - /* don't try to decrypt if string is too short (=> invalid memory - * reads/writes) */ - if (valueStrN > urlTailN && - (urlTail = PianoDecryptString (urlTailCrypted, NULL)) != NULL) { - if ((song->audioUrl = calloc (valueStrN + 1, - sizeof (*song->audioUrl))) != NULL) { - memcpy (song->audioUrl, valueStr, valueStrN - urlTailN); - /* FIXME: the key seems to be broken... so ignore 8 x 0x08 - * postfix; urlTailN/2 because the encrypted hex string is now - * decoded */ - memcpy (&song->audioUrl[valueStrN - urlTailN], urlTail, - urlTailN/2 - 8); - } - free (urlTail); - } - } else if (strcmp ("artRadio", key) == 0) { - song->coverArt = strdup (valueStr); - } else if (strcmp ("artistSummary", key) == 0) { - song->artist = strdup (valueStr); - } else if (strcmp ("musicId", key) == 0) { - song->musicId = strdup (valueStr); - } else if (strcmp ("userSeed", key) == 0) { - song->userSeed = strdup (valueStr); - } else if (strcmp ("songTitle", key) == 0) { - song->title = strdup (valueStr); - } else if (strcmp ("rating", key) == 0) { - if (strcmp (valueStr, "1") == 0) { - song->rating = PIANO_RATE_LOVE; - } else { - song->rating = PIANO_RATE_NONE; - } - } else if (strcmp ("isPositive", key) == 0) { - if (strcmp (valueStr, "1") == 0) { - song->rating = PIANO_RATE_LOVE; - } else { - song->rating = PIANO_RATE_BAN; - } - } else if (strcmp ("stationId", key) == 0) { - song->stationId = strdup (valueStr); - } else if (strcmp ("albumTitle", key) == 0) { - song->album = strdup (valueStr); - } else if (strcmp ("fileGain", key) == 0) { - song->fileGain = atof (valueStr); - } else if (strcmp ("audioEncoding", key) == 0) { - if (strcmp (valueStr, "aacplus") == 0) { - song->audioFormat = PIANO_AF_AACPLUS; - } else if (strcmp (valueStr, "mp3") == 0) { - song->audioFormat = PIANO_AF_MP3; - } else if (strcmp (valueStr, "mp3-hifi") == 0) { - song->audioFormat = PIANO_AF_MP3_HI; - } - } else if (strcmp ("artistMusicId", key) == 0) { - song->artistMusicId = strdup (valueStr); - } else if (strcmp ("feedbackId", key) == 0) { - song->feedbackId = strdup (valueStr); - } else if (strcmp ("songDetailURL", key) == 0) { - song->detailUrl = strdup (valueStr); - } else if (strcmp ("trackToken", key) == 0) { - song->trackToken = strdup (valueStr); - } -} - -/* parses userinfos sent by pandora as login response - * @param piano handle - * @param utf-8 string - * @return _RET_OK or error - */ -PianoReturn_t PianoXmlParseUserinfo (PianoHandle_t *ph, char *xml) { - ezxml_t xmlDoc, structNode; - PianoReturn_t ret; - - if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { - return ret; - } - - /* */ - structNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", 0, "struct", -1); - PianoXmlStructParser (structNode, PianoXmlParseUserinfoCb, &ph->user); - - ezxml_free (xmlDoc); - - return PIANO_RET_OK; -} - -static void PianoXmlParseQuickMixStationsCb (const char *key, const ezxml_t value, - void *data) { - char ***retIds = data; - char **ids = NULL; - size_t idsN = 0; - ezxml_t curNode; - - if (strcmp ("quickMixStationIds", key) == 0) { - for (curNode = ezxml_child (ezxml_get (value, "array", 0, "data", -1), "value"); - curNode; curNode = curNode->next) { - idsN++; - if (ids == NULL) { - if ((ids = calloc (idsN, sizeof (*ids))) == NULL) { - *retIds = NULL; - return; - } - } else { - /* FIXME: memory leak (on failure) */ - if ((ids = realloc (ids, idsN * sizeof (*ids))) == NULL) { - *retIds = NULL; - return; - } - } - ids[idsN-1] = strdup (PianoXmlGetNodeText (curNode)); - } - /* append NULL: list ends here */ - idsN++; - /* FIXME: copy&waste */ - if (ids == NULL) { - if ((ids = calloc (idsN, sizeof (*ids))) == NULL) { - *retIds = NULL; - return; - } - } else { - if ((ids = realloc (ids, idsN * sizeof (*ids))) == NULL) { - *retIds = NULL; - return; - } - } - ids[idsN-1] = NULL; - - *retIds = ids; - } -} - -/* parse stations returned by pandora - * @param piano handle - * @param xml returned by pandora - * @return _RET_OK or error - */ -PianoReturn_t PianoXmlParseStations (PianoHandle_t *ph, char *xml) { - ezxml_t xmlDoc, dataNode; - PianoReturn_t ret; - char **quickMixIds = NULL, **curQuickMixId = NULL; - - if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { - return ret; - } - - dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", 0, "array", - 0, "data", -1); - - for (dataNode = ezxml_child (dataNode, "value"); dataNode; - dataNode = dataNode->next) { - PianoStation_t *tmpStation; - - if ((tmpStation = calloc (1, sizeof (*tmpStation))) == NULL) { - ezxml_free (xmlDoc); - return PIANO_RET_OUT_OF_MEMORY; - } - - PianoXmlStructParser (ezxml_child (dataNode, "struct"), - PianoXmlParseStationsCb, tmpStation); - - /* get stations selected for quickmix */ - if (tmpStation->isQuickMix) { - PianoXmlStructParser (ezxml_child (dataNode, "struct"), - PianoXmlParseQuickMixStationsCb, &quickMixIds); - } - /* start new linked list or append */ - if (ph->stations == NULL) { - ph->stations = tmpStation; - } else { - PianoStation_t *curStation = ph->stations; - while (curStation->next != NULL) { - curStation = curStation->next; - } - curStation->next = tmpStation; - } - } - /* set quickmix flags after all stations are read */ - if (quickMixIds != NULL) { - curQuickMixId = quickMixIds; - while (*curQuickMixId != NULL) { - PianoStation_t *curStation = PianoFindStationById (ph->stations, - *curQuickMixId); - if (curStation != NULL) { - curStation->useQuickMix = 1; - } - free (*curQuickMixId); - curQuickMixId++; - } - free (quickMixIds); - } - - ezxml_free (xmlDoc); - - return PIANO_RET_OK; -} - -/* parse "create station" answer (it returns a new station structure) - * @param piano handle - * @param xml document - * @return nothing yet - */ -PianoReturn_t PianoXmlParseCreateStation (PianoHandle_t *ph, char *xml) { - ezxml_t xmlDoc, dataNode; - PianoStation_t *tmpStation; - PianoReturn_t ret; - - if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { - return ret; - } - - dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", 0, "struct", -1); - - if ((tmpStation = calloc (1, sizeof (*tmpStation))) == NULL) { - ezxml_free (xmlDoc); - return PIANO_RET_OUT_OF_MEMORY; - } - PianoXmlStructParser (dataNode, PianoXmlParseStationsCb, tmpStation); - /* FIXME: copy & waste */ - /* start new linked list or append */ - if (ph->stations == NULL) { - ph->stations = tmpStation; - } else { - PianoStation_t *curStation = ph->stations; - while (curStation->next != NULL) { - curStation = curStation->next; - } - curStation->next = tmpStation; - } - - ezxml_free (xmlDoc); - - return PIANO_RET_OK; -} - -/* parse "add seed" answer, nearly the same as ParseCreateStation - * @param piano handle - * @param xml document - * @param update this station - */ -PianoReturn_t PianoXmlParseAddSeed (PianoHandle_t *ph, char *xml, - PianoStation_t *station) { - ezxml_t xmlDoc, dataNode; - PianoReturn_t ret; - - if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { - return ret; - } - - dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", 0, "struct", -1); - PianoDestroyStation (station); - PianoXmlStructParser (dataNode, PianoXmlParseStationsCb, station); - - ezxml_free (xmlDoc); - - return PIANO_RET_OK; -} - -static PianoReturn_t PianoXmlParsePlaylistStruct (ezxml_t xml, - PianoSong_t **retSong) { - PianoSong_t *playlist = *retSong, *tmpSong; - - if ((tmpSong = calloc (1, sizeof (*tmpSong))) == NULL) { - return PIANO_RET_OUT_OF_MEMORY; - } - - PianoXmlStructParser (ezxml_child (xml, "struct"), PianoXmlParsePlaylistCb, - tmpSong); - /* begin linked list or append */ - if (playlist == NULL) { - playlist = tmpSong; - } else { - PianoSong_t *curSong = playlist; - while (curSong->next != NULL) { - curSong = curSong->next; - } - curSong->next = tmpSong; - } - - *retSong = playlist; - - return PIANO_RET_OK; -} - -/* parses playlist; used when searching too - * @param piano handle - * @param xml document - * @param return: playlist - */ -PianoReturn_t PianoXmlParsePlaylist (PianoHandle_t *ph, char *xml, - PianoSong_t **retPlaylist) { - ezxml_t xmlDoc, dataNode; - PianoReturn_t ret = PIANO_RET_OK; - - if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { - return ret; - } - - dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", 0, "array", - 0, "data", -1); - - for (dataNode = ezxml_child (dataNode, "value"); dataNode; - dataNode = dataNode->next) { - if ((ret = PianoXmlParsePlaylistStruct (dataNode, retPlaylist)) != - PIANO_RET_OK) { - break; - } - } - - ezxml_free (xmlDoc); - - return ret; -} - -/* check for exception only - * @param xml string - * @return _OK or error - */ -PianoReturn_t PianoXmlParseSimple (char *xml) { - ezxml_t xmlDoc; - PianoReturn_t ret; - - if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { - return ret; - } - - ezxml_free (xmlDoc); - - return ret; -} - -/* xml struct parser callback, used in PianoXmlParseSearchCb - */ -static void PianoXmlParseSearchArtistCb (const char *key, const ezxml_t value, - void *data) { - PianoArtist_t *artist = data; - char *valueStr = PianoXmlGetNodeText (value); - - if (strcmp ("artistName", key) == 0) { - artist->name = strdup (valueStr); - } else if (strcmp ("musicId", key) == 0) { - artist->musicId = strdup (valueStr); - } -} - -/* callback for xml struct parser used in PianoXmlParseSearch, "switch" for - * PianoXmlParseSearchArtistCb and PianoXmlParsePlaylistCb - */ -static void PianoXmlParseSearchCb (const char *key, const ezxml_t value, - void *data) { - PianoSearchResult_t *searchResult = data; - ezxml_t curNode; - - if (strcmp ("artists", key) == 0) { - /* skip */ - for (curNode = ezxml_child (ezxml_get (value, "array", 0, "data", -1), "value"); - curNode; curNode = curNode->next) { - PianoArtist_t *artist; - - if ((artist = calloc (1, sizeof (*artist))) == NULL) { - /* fail silently */ - break; - } - - memset (artist, 0, sizeof (*artist)); - - PianoXmlStructParser (ezxml_child (curNode, "struct"), - PianoXmlParseSearchArtistCb, artist); - - /* add result to linked list */ - if (searchResult->artists == NULL) { - searchResult->artists = artist; - } else { - PianoArtist_t *curArtist = searchResult->artists; - while (curArtist->next != NULL) { - curArtist = curArtist->next; - } - curArtist->next = artist; - } - } - } else if (strcmp ("songs", key) == 0) { - for (curNode = ezxml_child (ezxml_get (value, "array", 0, "data", -1), "value"); - curNode; curNode = curNode->next) { - if (PianoXmlParsePlaylistStruct (curNode, &searchResult->songs) != - PIANO_RET_OK) { - break; - } - } - } -} - -/* parse search result; searchResult is nulled before use - * @param xml document - * @param returns search result - * @return nothing yet - */ -PianoReturn_t PianoXmlParseSearch (char *xml, - PianoSearchResult_t *searchResult) { - ezxml_t xmlDoc, dataNode; - PianoReturn_t ret; - - if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { - return ret; - } - - dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", 0, "struct", -1); - /* we need a "clean" search result (with null pointers) */ - memset (searchResult, 0, sizeof (*searchResult)); - PianoXmlStructParser (dataNode, PianoXmlParseSearchCb, searchResult); - - ezxml_free (xmlDoc); - - return PIANO_RET_OK; -} - -/* FIXME: copy&waste (PianoXmlParseSearch) - */ -PianoReturn_t PianoXmlParseSeedSuggestions (char *xml, - PianoSearchResult_t *searchResult) { - ezxml_t xmlDoc, dataNode; - PianoReturn_t ret; - - if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { - return ret; - } - - dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", -1); - /* we need a "clean" search result (with null pointers) */ - memset (searchResult, 0, sizeof (*searchResult)); - /* reuse seach result parser; structure is nearly the same */ - PianoXmlParseSearchCb ("artists", dataNode, searchResult); - - ezxml_free (xmlDoc); - - return PIANO_RET_OK; -} - -/* encode reserved xml chars - * TODO: remove and use ezxml_ampencode - * @param encode this - * @return encoded string or NULL - */ -char *PianoXmlEncodeString (const char *s) { - static const char *replacements[] = {"&&", "''", "\""", - "<<", ">>", NULL}; - const char **r; - char *sOut, *sOutCurr, found; - - if ((sOut = calloc (strlen (s) * 5 + 1, sizeof (*sOut))) == NULL) { - return NULL; - } - - sOutCurr = sOut; - - while (*s != '\0') { - r = replacements; - found = 0; - while (*r != NULL) { - if (*s == *r[0]) { - found = 1; - strcat (sOutCurr, (*r) + 1); - sOutCurr += strlen ((*r) + 1); - break; - } - r++; - } - if (!found) { - *sOutCurr = *s; - sOutCurr++; - } - s++; - } - return sOut; -} - -PianoReturn_t PianoXmlParseGenreExplorer (PianoHandle_t *ph, char *xml) { - ezxml_t xmlDoc, catNode; - PianoReturn_t ret; - - if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { - return ret; - } - - /* get all nodes */ - for (catNode = ezxml_child (xmlDoc, "category"); catNode; - catNode = catNode->next) { - PianoGenreCategory_t *tmpGenreCategory; - ezxml_t genreNode; - - if ((tmpGenreCategory = calloc (1, sizeof (*tmpGenreCategory))) == NULL) { - ezxml_free (xmlDoc); - return PIANO_RET_OUT_OF_MEMORY; - } - - tmpGenreCategory->name = strdup (ezxml_attr (catNode, "categoryName")); - - /* get genre subnodes */ - for (genreNode = ezxml_child (catNode, "genre"); genreNode; - genreNode = genreNode->next) { - PianoGenre_t *tmpGenre; - - if ((tmpGenre = calloc (1, sizeof (*tmpGenre))) == NULL) { - ezxml_free (xmlDoc); - return PIANO_RET_OUT_OF_MEMORY; - } - - /* get genre attributes */ - tmpGenre->name = strdup (ezxml_attr (genreNode, "name")); - tmpGenre->musicId = strdup (ezxml_attr (genreNode, "musicId")); - - /* append station */ - if (tmpGenreCategory->genres == NULL) { - tmpGenreCategory->genres = tmpGenre; - } else { - PianoGenre_t *curGenre = - tmpGenreCategory->genres; - while (curGenre->next != NULL) { - curGenre = curGenre->next; - } - curGenre->next = tmpGenre; - } - } - /* append category */ - if (ph->genreStations == NULL) { - ph->genreStations = tmpGenreCategory; - } else { - PianoGenreCategory_t *curCat = ph->genreStations; - while (curCat->next != NULL) { - curCat = curCat->next; - } - curCat->next = tmpGenreCategory; - } - } - - ezxml_free (xmlDoc); - - return PIANO_RET_OK; -} - -/* dummy function, only checks for errors - * @param xml doc - * @return _OK or error - */ -PianoReturn_t PianoXmlParseTranformStation (char *xml) { - ezxml_t xmlDoc; - PianoReturn_t ret; - - if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { - return ret; - } - - ezxml_free (xmlDoc); - - return PIANO_RET_OK; -} - -/* parses "why did you play ...?" answer - * @param xml - * @param returns the answer - * @return _OK or error - */ -PianoReturn_t PianoXmlParseNarrative (char *xml, char **retNarrative) { - ezxml_t xmlDoc, dataNode; - PianoReturn_t ret; - - if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { - return ret; - } - - /* $textnode */ - dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", -1); - *retNarrative = strdup (ezxml_txt (dataNode)); - - ezxml_free (xmlDoc); - - return ret; -} - -/* seed bag, required because seedId is not part of artist/song struct in - * pandora's xml response - */ -struct PianoXmlParseSeedBag { - char *seedId; - PianoSong_t *song; - PianoArtist_t *artist; - PianoStation_t *station; -}; - -/* parse seed struct - */ -static void PianoXmlParseSeedCb (const char *key, const ezxml_t value, - void *data) { - struct PianoXmlParseSeedBag *bag = data; - - assert (bag != NULL); - - if (strcmp ("song", key) == 0) { - assert (bag->song == NULL); - - if ((bag->song = calloc (1, sizeof (*bag->song))) == NULL) { - return; - } - - PianoXmlStructParser (ezxml_child (value, "struct"), - PianoXmlParsePlaylistCb, bag->song); - } else if (strcmp ("artist", key) == 0) { - assert (bag->artist == NULL); - - if ((bag->artist = calloc (1, sizeof (*bag->artist))) == NULL) { - return; - } - - PianoXmlStructParser (ezxml_child (value, "struct"), - PianoXmlParseSearchArtistCb, bag->artist); - } else if (strcmp ("nonGenomeStation", key) == 0) { - /* genre stations are "non genome" station seeds */ - assert (bag->station == NULL); - - if ((bag->station = calloc (1, sizeof (*bag->station))) == NULL) { - return; - } - - PianoXmlStructParser (ezxml_child (value, "struct"), - PianoXmlParseStationsCb, bag->station); - } else if (strcmp ("seedId", key) == 0) { - char *valueStr = PianoXmlGetNodeText (value); - bag->seedId = strdup (valueStr); - } -} - -/* parse getStation xml struct - */ -static void PianoXmlParseGetStationInfoCb (const char *key, const ezxml_t value, - void *data) { - PianoStationInfo_t *info = data; - - if (strcmp ("seeds", key) == 0) { - const ezxml_t dataNode = ezxml_get (value, "array", 0, "data", -1); - for (ezxml_t seedNode = ezxml_child (dataNode, "value"); seedNode; - seedNode = seedNode->next) { - struct PianoXmlParseSeedBag bag; - memset (&bag, 0, sizeof (bag)); - - PianoXmlStructParser (ezxml_child (seedNode, "struct"), - PianoXmlParseSeedCb, &bag); - - assert (bag.song != NULL || bag.artist != NULL || - bag.station != NULL); - - if (bag.seedId == NULL) { - /* seeds without id are useless */ - continue; - } - - /* FIXME: copy&waste */ - if (bag.song != NULL) { - bag.song->seedId = bag.seedId; - - if (info->songSeeds == NULL) { - info->songSeeds = bag.song; - } else { - PianoSong_t *curSong = info->songSeeds; - while (curSong->next != NULL) { - curSong = curSong->next; - } - curSong->next = bag.song; - } - } else if (bag.artist != NULL) { - bag.artist->seedId = bag.seedId; - - if (info->artistSeeds == NULL) { - info->artistSeeds = bag.artist; - } else { - PianoArtist_t *curSong = info->artistSeeds; - while (curSong->next != NULL) { - curSong = curSong->next; - } - curSong->next = bag.artist; - } - } else if (bag.station != NULL) { - bag.station->seedId = bag.seedId; - - if (info->stationSeeds == NULL) { - info->stationSeeds = bag.station; - } else { - PianoStation_t *curStation = info->stationSeeds; - while (curStation->next != NULL) { - curStation = curStation->next; - } - curStation->next = bag.station; - } - } else { - free (bag.seedId); - } - } - } else if (strcmp ("feedback", key) == 0) { - const ezxml_t dataNode = ezxml_get (value, "array", 0, "data", -1); - for (ezxml_t feedbackNode = ezxml_child (dataNode, "value"); feedbackNode; - feedbackNode = feedbackNode->next) { - if (PianoXmlParsePlaylistStruct (feedbackNode, &info->feedback) != - PIANO_RET_OK) { - break; - } - } - } -} - -/* parse getStation response - */ -PianoReturn_t PianoXmlParseGetStationInfo (char *xml, - PianoStationInfo_t *stationInfo) { - ezxml_t xmlDoc, dataNode; - PianoReturn_t ret; - - if ((ret = PianoXmlInitDoc (xml, &xmlDoc)) != PIANO_RET_OK) { - return ret; - } - - dataNode = ezxml_get (xmlDoc, "params", 0, "param", 0, "value", 0, "struct", -1); - PianoXmlStructParser (dataNode, PianoXmlParseGetStationInfoCb, stationInfo); - - ezxml_free (xmlDoc); - - return PIANO_RET_OK; -} - diff --git a/src/libpiano/xml.h b/src/libpiano/xml.h deleted file mode 100644 index 58ee28f..0000000 --- a/src/libpiano/xml.h +++ /dev/null @@ -1,49 +0,0 @@ -/* -Copyright (c) 2008-2011 - Lars-Dominik Braun - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -*/ - -#ifndef _XML_H -#define _XML_H - -#include "piano.h" - -PianoReturn_t PianoXmlParseUserinfo (PianoHandle_t *ph, char *xml); -PianoReturn_t PianoXmlParseStations (PianoHandle_t *ph, char *xml); -PianoReturn_t PianoXmlParsePlaylist (PianoHandle_t *ph, char *xml, - PianoSong_t **); -PianoReturn_t PianoXmlParseSearch (char *searchXml, - PianoSearchResult_t *searchResult); -PianoReturn_t PianoXmlParseSimple (char *xml); -PianoReturn_t PianoXmlParseCreateStation (PianoHandle_t *ph, - char *xml); -PianoReturn_t PianoXmlParseAddSeed (PianoHandle_t *ph, char *xml, - PianoStation_t *station); -PianoReturn_t PianoXmlParseGenreExplorer (PianoHandle_t *ph, - char *xmlContent); -PianoReturn_t PianoXmlParseTranformStation (char *searchXml); -PianoReturn_t PianoXmlParseNarrative (char *xml, char **retNarrative); -PianoReturn_t PianoXmlParseSeedSuggestions (char *, PianoSearchResult_t *); -PianoReturn_t PianoXmlParseGetStationInfo (char *, PianoStationInfo_t *); - -char *PianoXmlEncodeString (const char *s); - -#endif /* _XML_H */ diff --git a/src/settings.c b/src/settings.c index 51c2400..d1dcc7f 100644 --- a/src/settings.c +++ b/src/settings.c @@ -133,8 +133,8 @@ void BarSettingsRead (BarSettings_t *settings) { settings->listSongFormat = strdup ("%i) %a - %t%r"); settings->fifo = malloc (PATH_MAX * sizeof (*settings->fifo)); BarGetXdgConfigDir (PACKAGE "/ctl", settings->fifo, PATH_MAX); - memcpy (settings->tlsFingerprint, "\xD9\x98\x0B\xA2\xCC\x0F\x97\xBB" - "\x03\x82\x2C\x62\x11\xEA\xEA\x4A\x06\xEE\xF4\x27", + memcpy (settings->tlsFingerprint, "\xA2\xA0\xBE\x8A\x37\x92\x39\xAE" + "\x2B\x2E\x71\x4C\x56\xB3\x8B\xC1\x2A\x9B\x4B\x77", sizeof (settings->tlsFingerprint)); settings->msgFormat[MSG_NONE].prefix = NULL; diff --git a/src/ui.c b/src/ui.c index 4162dce..f13bed5 100644 --- a/src/ui.c +++ b/src/ui.c @@ -138,7 +138,7 @@ void BarUiMsg (const BarSettings_t *settings, const BarUiMsg_t type, */ static WaitressReturn_t BarPianoHttpRequest (WaitressHandle_t *waith, PianoRequest_t *req, bool forceTls) { - waith->extraHeaders = "Content-Type: text/xml\r\n"; + waith->extraHeaders = "Content-Type: text/plain\r\n"; waith->postData = req->postData; waith->method = WAITRESS_METHOD_POST; waith->url.path = req->urlPath; @@ -186,7 +186,7 @@ int BarUiPianoCall (BarApp_t * const app, PianoRequestType_t type, *pRet = PianoResponse (&app->ph, &req); if (*pRet != PIANO_RET_CONTINUE_REQUEST) { /* checking for request type avoids infinite loops */ - if (*pRet == PIANO_RET_AUTH_TOKEN_INVALID && + if (*pRet == PIANO_RET_P_INVALID_AUTH_TOKEN && type != PIANO_REQUEST_LOGIN) { /* reauthenticate */ PianoReturn_t authpRet; diff --git a/src/ui_act.c b/src/ui_act.c index 76b9411..93ba623 100644 --- a/src/ui_act.c +++ b/src/ui_act.c @@ -261,8 +261,7 @@ BarUiActCallback(BarUiActDebug) { "rating:\t%i\n" "stationId:\t%s\n" "title:\t%s\n" - "trackToken:\t%s\n" - "userSeed:\t%s\n", + "trackToken:\t%s\n", selSong->album, selSong->artist, selSong->audioFormat, @@ -274,8 +273,7 @@ BarUiActCallback(BarUiActDebug) { selSong->rating, selSong->stationId, selSong->title, - selSong->trackToken, - selSong->userSeed); + selSong->trackToken); } /* rate current song -- cgit v1.2.3