try new method for sending find peer requests
[oweals/gnunet.git] / src / upnp / upnp_util.c
1 /*
2  * @file upnp_util.cUtility Functions
3  * @ingroup core
4  *
5  * Gaim is the legal property of its developers, whose names are too numerous
6  * to list here.  Please refer to the COPYRIGHT file distributed with this
7  * source distribution.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "platform.h"
25 #include "util.h"
26 #include "gnunet_util.h"
27
28 /* Returns a NULL-terminated string after unescaping an entity
29  * (eg. &, < &#38 etc.) starting at s. Returns NULL on failure.*/
30 static char *
31 detect_entity (const char *text, int *length)
32 {
33   const char *pln;
34   int len;
35   int pound;
36   char b[7];
37   char *buf;
38
39   if (!text || *text != '&')
40     return NULL;
41
42 #define IS_ENTITY(s)  (!strncasecmp(text, s, (len = sizeof(s) - 1)))
43
44   if (IS_ENTITY ("&"))
45     pln = "&";
46   else if (IS_ENTITY ("<"))
47     pln = "<";
48   else if (IS_ENTITY ("&gt;"))
49     pln = ">";
50   else if (IS_ENTITY ("&nbsp;"))
51     pln = " ";
52   else if (IS_ENTITY ("&copy;"))
53     pln = "\302\251";           /* or use g_unichar_to_utf8(0xa9); */
54   else if (IS_ENTITY ("&quot;"))
55     pln = "\"";
56   else if (IS_ENTITY ("&reg;"))
57     pln = "\302\256";           /* or use g_unichar_to_utf8(0xae); */
58   else if (IS_ENTITY ("&apos;"))
59     pln = "\'";
60   else if (*(text + 1) == '#' && (sscanf (text, "&#%u;", &pound) == 1) &&
61            pound != 0 && *(text + 3 + (int) log10 (pound)) == ';')
62     {
63       buf = GNUNET_convert_string_to_utf8 (NULL,
64                                            (const char *) &pound,
65                                            2, "UNICODE");
66       if (strlen (buf) > 6)
67         buf[6] = '\0';
68       strcpy (b, buf);
69       pln = b;
70       GNUNET_free (buf);
71       len = 2;
72       while (isdigit ((int) text[len]))
73         len++;
74       if (text[len] == ';')
75         len++;
76     }
77   else
78     return NULL;
79
80   if (length)
81     *length = len;
82   return GNUNET_strdup (pln);
83 }
84
85 char *
86 g_strdup_printf (const char *fmt, ...)
87 {
88   size_t size;
89   char *buf;
90   va_list va;
91
92   va_start (va, fmt);
93   size = VSNPRINTF (NULL, 0, fmt, va) + 1;
94   va_end (va);
95   buf = GNUNET_malloc (size);
96   va_start (va, fmt);
97   VSNPRINTF (buf, size, fmt, va);
98   va_end (va);
99   return buf;
100 }
101
102 char *
103 gaim_unescape_html (const char *html)
104 {
105   if (html != NULL)
106     {
107       const char *c = html;
108       char *ret = GNUNET_strdup ("");
109       char *app;
110       while (*c)
111         {
112           int len;
113           char *ent;
114
115           if ((ent = detect_entity (c, &len)) != NULL)
116             {
117               app = g_strdup_printf ("%s%s", ret, ent);
118               GNUNET_free (ret);
119               ret = app;
120               c += len;
121               GNUNET_free (ent);
122             }
123           else if (!strncmp (c, "<br>", 4))
124             {
125               app = g_strdup_printf ("%s%s", ret, "\n");
126               GNUNET_free (ret);
127               ret = app;
128               c += 4;
129             }
130           else
131             {
132               app = g_strdup_printf ("%s%c", ret, *c);
133               GNUNET_free (ret);
134               ret = app;
135               c++;
136             }
137         }
138       return ret;
139     }
140   return NULL;
141 }
142
143
144 int
145 gaim_str_has_prefix (const char *s, const char *p)
146 {
147   if ((s == NULL) || (p == NULL))
148     return 0;
149   return !strncmp (s, p, strlen (p));
150 }
151
152 /* end of util.c */