/thirdparty/libportfwd/third-party/miniupnpc-1.6/upnpreplyparse.c

http://github.com/tomahawk-player/tomahawk · C · 152 lines · 129 code · 10 blank · 13 comment · 22 complexity · bbdcb4a614e0b9f3a21150608917be77 MD5 · raw file

  1. /* $Id: upnpreplyparse.c,v 1.11 2011/02/07 16:17:06 nanard Exp $ */
  2. /* MiniUPnP project
  3. * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
  4. * (c) 2006-2011 Thomas Bernard
  5. * This software is subject to the conditions detailed
  6. * in the LICENCE file provided within the distribution */
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include "upnpreplyparse.h"
  11. #include "minixml.h"
  12. static void
  13. NameValueParserStartElt(void * d, const char * name, int l)
  14. {
  15. struct NameValueParserData * data = (struct NameValueParserData *)d;
  16. if(l>63)
  17. l = 63;
  18. memcpy(data->curelt, name, l);
  19. data->curelt[l] = '\0';
  20. }
  21. static void
  22. NameValueParserGetData(void * d, const char * datas, int l)
  23. {
  24. struct NameValueParserData * data = (struct NameValueParserData *)d;
  25. struct NameValue * nv;
  26. if(strcmp(data->curelt, "NewPortListing") == 0)
  27. {
  28. /* specific case for NewPortListing which is a XML Document */
  29. data->portListing = malloc(l + 1);
  30. if(!data->portListing)
  31. {
  32. /* malloc error */
  33. return;
  34. }
  35. memcpy(data->portListing, datas, l);
  36. data->portListing[l] = '\0';
  37. data->portListingLength = l;
  38. }
  39. else
  40. {
  41. /* standard case. Limited to 63 chars strings */
  42. nv = malloc(sizeof(struct NameValue));
  43. if(l>63)
  44. l = 63;
  45. strncpy(nv->name, data->curelt, 64);
  46. nv->name[63] = '\0';
  47. memcpy(nv->value, datas, l);
  48. nv->value[l] = '\0';
  49. LIST_INSERT_HEAD( &(data->head), nv, entries);
  50. }
  51. }
  52. void
  53. ParseNameValue(const char * buffer, int bufsize,
  54. struct NameValueParserData * data)
  55. {
  56. struct xmlparser parser;
  57. LIST_INIT(&(data->head));
  58. data->portListing = NULL;
  59. data->portListingLength = 0;
  60. /* init xmlparser object */
  61. parser.xmlstart = buffer;
  62. parser.xmlsize = bufsize;
  63. parser.data = data;
  64. parser.starteltfunc = NameValueParserStartElt;
  65. parser.endeltfunc = 0;
  66. parser.datafunc = NameValueParserGetData;
  67. parser.attfunc = 0;
  68. parsexml(&parser);
  69. }
  70. void
  71. ClearNameValueList(struct NameValueParserData * pdata)
  72. {
  73. struct NameValue * nv;
  74. if(pdata->portListing)
  75. {
  76. free(pdata->portListing);
  77. pdata->portListing = NULL;
  78. pdata->portListingLength = 0;
  79. }
  80. while((nv = pdata->head.lh_first) != NULL)
  81. {
  82. LIST_REMOVE(nv, entries);
  83. free(nv);
  84. }
  85. }
  86. char *
  87. GetValueFromNameValueList(struct NameValueParserData * pdata,
  88. const char * Name)
  89. {
  90. struct NameValue * nv;
  91. char * p = NULL;
  92. for(nv = pdata->head.lh_first;
  93. (nv != NULL) && (p == NULL);
  94. nv = nv->entries.le_next)
  95. {
  96. if(strcmp(nv->name, Name) == 0)
  97. p = nv->value;
  98. }
  99. return p;
  100. }
  101. #if 0
  102. /* useless now that minixml ignores namespaces by itself */
  103. char *
  104. GetValueFromNameValueListIgnoreNS(struct NameValueParserData * pdata,
  105. const char * Name)
  106. {
  107. struct NameValue * nv;
  108. char * p = NULL;
  109. char * pname;
  110. for(nv = pdata->head.lh_first;
  111. (nv != NULL) && (p == NULL);
  112. nv = nv->entries.le_next)
  113. {
  114. pname = strrchr(nv->name, ':');
  115. if(pname)
  116. pname++;
  117. else
  118. pname = nv->name;
  119. if(strcmp(pname, Name)==0)
  120. p = nv->value;
  121. }
  122. return p;
  123. }
  124. #endif
  125. /* debug all-in-one function
  126. * do parsing then display to stdout */
  127. #ifdef DEBUG
  128. void
  129. DisplayNameValueList(char * buffer, int bufsize)
  130. {
  131. struct NameValueParserData pdata;
  132. struct NameValue * nv;
  133. ParseNameValue(buffer, bufsize, &pdata);
  134. for(nv = pdata.head.lh_first;
  135. nv != NULL;
  136. nv = nv->entries.le_next)
  137. {
  138. printf("%s = %s\n", nv->name, nv->value);
  139. }
  140. ClearNameValueList(&pdata);
  141. }
  142. #endif