1/* MiniUPnP project 2 * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ 3 * 4 * Copyright (c) 2006, Thomas Bernard 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are met: 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * * The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29#include <stdlib.h> 30#include <string.h> 31#include <stdio.h> 32 33#include "upnpreplyparse.h" 34#include "minixml.h" 35 36static void 37NameValueParserStartElt(void * d, const char * name, int l) 38{ 39 struct NameValueParserData * data = (struct NameValueParserData *)d; 40 if(l>63) 41 l = 63; 42 memcpy(data->curelt, name, l); 43 data->curelt[l] = '\0'; 44 45 /* store root element */ 46 if(!data->head.lh_first) 47 { 48 struct NameValue * nv; 49 nv = malloc(sizeof(struct NameValue)+l+1); 50 strcpy(nv->name, "rootElement"); 51 memcpy(nv->value, name, l); 52 nv->value[l] = '\0'; 53 LIST_INSERT_HEAD(&(data->head), nv, entries); 54 } 55} 56 57static void 58NameValueParserGetData(void * d, const char * datas, int l) 59{ 60 struct NameValueParserData * data = (struct NameValueParserData *)d; 61 struct NameValue * nv; 62 if(l>1975) 63 l = 1975; 64 nv = malloc(sizeof(struct NameValue)+l+1); 65 strncpy(nv->name, data->curelt, 64); 66 nv->name[63] = '\0'; 67 memcpy(nv->value, datas, l); 68 nv->value[l] = '\0'; 69 LIST_INSERT_HEAD(&(data->head), nv, entries); 70} 71 72void 73ParseNameValue(const char * buffer, int bufsize, 74 struct NameValueParserData * data) 75{ 76 struct xmlparser parser; 77 LIST_INIT(&(data->head)); 78 /* init xmlparser object */ 79 parser.xmlstart = buffer; 80 parser.xmlsize = bufsize; 81 parser.data = data; 82 parser.starteltfunc = NameValueParserStartElt; 83 parser.endeltfunc = 0; 84 parser.datafunc = NameValueParserGetData; 85 parser.attfunc = 0; 86 parsexml(&parser); 87} 88 89void 90ClearNameValueList(struct NameValueParserData * pdata) 91{ 92 struct NameValue * nv; 93 while((nv = pdata->head.lh_first) != NULL) 94 { 95 LIST_REMOVE(nv, entries); 96 free(nv); 97 } 98} 99 100char * 101GetValueFromNameValueList(struct NameValueParserData * pdata, 102 const char * Name) 103{ 104 struct NameValue * nv; 105 char * p = NULL; 106 for(nv = pdata->head.lh_first; 107 (nv != NULL) && (p == NULL); 108 nv = nv->entries.le_next) 109 { 110 if(strcmp(nv->name, Name) == 0) 111 p = nv->value; 112 } 113 return p; 114} 115 116/* debug all-in-one function 117 * do parsing then display to stdout */ 118#ifdef DEBUG 119void 120DisplayNameValueList(char * buffer, int bufsize) 121{ 122 struct NameValueParserData pdata; 123 struct NameValue * nv; 124 ParseNameValue(buffer, bufsize, &pdata); 125 for(nv = pdata.head.lh_first; 126 nv != NULL; 127 nv = nv->entries.le_next) 128 { 129 printf("%s = %s\n", nv->name, nv->value); 130 } 131 ClearNameValueList(&pdata); 132} 133#endif 134 135