[gaim-migrate @ 3063]
[pidgin-git.git] / src / protocols / jabber / str.c
blobd2f79d6c78081cd044cc4c2538cd8857389fa485
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 * Jabber
17 * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
20 #include "libxode.h"
22 char *j_strdup(const char *str)
24 if(str == NULL)
25 return NULL;
26 else
27 return strdup(str);
30 char *j_strcat(char *dest, char *txt)
32 if(!txt) return(dest);
34 while(*txt)
35 *dest++ = *txt++;
36 *dest = '\0';
38 return(dest);
41 int j_strcmp(const char *a, const char *b)
43 if(a == NULL || b == NULL)
44 return -1;
45 else
46 return strcmp(a, b);
49 int j_strcasecmp(const char *a, const char *b)
51 if(a == NULL || b == NULL)
52 return -1;
53 else
54 return strcasecmp(a, b);
57 int j_strncmp(const char *a, const char *b, int i)
59 if(a == NULL || b == NULL)
60 return -1;
61 else
62 return strncmp(a, b, i);
65 int j_strncasecmp(const char *a, const char *b, int i)
67 if(a == NULL || b == NULL)
68 return -1;
69 else
70 return strncasecmp(a, b, i);
73 int j_strlen(const char *a)
75 if(a == NULL)
76 return 0;
77 else
78 return strlen(a);
81 int j_atoi(const char *a, int def)
83 if(a == NULL)
84 return def;
85 else
86 return atoi(a);
89 spool spool_new(pool p)
91 spool s;
93 s = pmalloc(p, sizeof(struct spool_struct));
94 s->p = p;
95 s->len = 0;
96 s->last = NULL;
97 s->first = NULL;
98 return s;
101 void spool_add(spool s, char *str)
103 struct spool_node *sn;
104 int len;
106 if(str == NULL)
107 return;
109 len = strlen(str);
110 if(len == 0)
111 return;
113 sn = pmalloc(s->p, sizeof(struct spool_node));
114 sn->c = pstrdup(s->p, str);
115 sn->next = NULL;
117 s->len += len;
118 if(s->last != NULL)
119 s->last->next = sn;
120 s->last = sn;
121 if(s->first == NULL)
122 s->first = sn;
125 void spooler(spool s, ...)
127 va_list ap;
128 char *arg = NULL;
130 if(s == NULL)
131 return;
133 va_start(ap, s);
135 /* loop till we hit our end flag, the first arg */
136 while(1)
138 arg = va_arg(ap,char *);
139 if((int)arg == (int)s)
140 break;
141 else
142 spool_add(s, arg);
145 va_end(ap);
148 char *spool_print(spool s)
150 char *ret,*tmp;
151 struct spool_node *next;
153 if(s == NULL || s->len == 0 || s->first == NULL)
154 return NULL;
156 ret = pmalloc(s->p, s->len + 1);
157 *ret = '\0';
159 next = s->first;
160 tmp = ret;
161 while(next != NULL)
163 tmp = j_strcat(tmp,next->c);
164 next = next->next;
167 return ret;
170 /* convenience :) */
171 char *spools(pool p, ...)
173 va_list ap;
174 spool s;
175 char *arg = NULL;
177 if(p == NULL)
178 return NULL;
180 s = spool_new(p);
182 va_start(ap, p);
184 /* loop till we hit our end flag, the first arg */
185 while(1)
187 arg = va_arg(ap,char *);
188 if((int)arg == (int)p)
189 break;
190 else
191 spool_add(s, arg);
194 va_end(ap);
196 return spool_print(s);
200 char *strunescape(pool p, char *buf)
202 int i,j=0;
203 char *temp;
205 if (p == NULL || buf == NULL) return(NULL);
207 if (strchr(buf,'&') == NULL) return(buf);
209 temp = pmalloc(p,strlen(buf)+1);
211 if (temp == NULL) return(NULL);
213 for(i=0;i<strlen(buf);i++)
215 if (buf[i]=='&')
217 if (strncmp(&buf[i],"&amp;",5)==0)
219 temp[j] = '&';
220 i += 4;
221 } else if (strncmp(&buf[i],"&quot;",6)==0) {
222 temp[j] = '\"';
223 i += 5;
224 } else if (strncmp(&buf[i],"&apos;",6)==0) {
225 temp[j] = '\'';
226 i += 5;
227 } else if (strncmp(&buf[i],"&lt;",4)==0) {
228 temp[j] = '<';
229 i += 3;
230 } else if (strncmp(&buf[i],"&gt;",4)==0) {
231 temp[j] = '>';
232 i += 3;
234 } else {
235 temp[j]=buf[i];
237 j++;
239 temp[j]='\0';
240 return(temp);
244 char *strescape(pool p, char *buf)
246 int i,j,oldlen,newlen;
247 char *temp;
249 if (p == NULL || buf == NULL) return(NULL);
251 oldlen = newlen = strlen(buf);
252 for(i=0;i<oldlen;i++)
254 switch(buf[i])
256 case '&':
257 newlen+=5;
258 break;
259 case '\'':
260 newlen+=6;
261 break;
262 case '\"':
263 newlen+=6;
264 break;
265 case '<':
266 newlen+=4;
267 break;
268 case '>':
269 newlen+=4;
270 break;
274 if(oldlen == newlen) return buf;
276 temp = pmalloc(p,newlen+1);
278 if (temp==NULL) return(NULL);
280 for(i=j=0;i<oldlen;i++)
282 switch(buf[i])
284 case '&':
285 memcpy(&temp[j],"&amp;",5);
286 j += 5;
287 break;
288 case '\'':
289 memcpy(&temp[j],"&apos;",6);
290 j += 6;
291 break;
292 case '\"':
293 memcpy(&temp[j],"&quot;",6);
294 j += 6;
295 break;
296 case '<':
297 memcpy(&temp[j],"&lt;",4);
298 j += 4;
299 break;
300 case '>':
301 memcpy(&temp[j],"&gt;",4);
302 j += 4;
303 break;
304 default:
305 temp[j++] = buf[i];
308 temp[j] = '\0';
309 return temp;
312 char *zonestr(char *file, int line)
314 static char buff[64];
315 int i;
317 i = snprintf(buff,63,"%s:%d",file,line);
318 buff[i] = '\0';
320 return buff;
323 void str_b64decode(char* str)
325 char *cur;
326 int d, dlast, phase;
327 unsigned char c;
328 static int table[256] = {
329 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
330 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
331 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
332 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
333 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
334 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
335 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
336 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
337 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
338 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
339 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
340 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
341 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
342 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
343 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
344 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
347 phase = 0;
348 for (cur = str; *cur != '\0'; ++cur )
350 d = table[(int)*cur];
351 if(d != -1)
353 switch(phase)
355 case 0:
356 ++phase;
357 break;
358 case 1:
359 c = ((dlast << 2) | ((d & 0x30) >> 4));
360 *str++ = c;
361 ++phase;
362 break;
363 case 2:
364 c = (((dlast & 0xf) << 4) | ((d & 0x3c) >> 2));
365 *str++ = c;
366 ++phase;
367 break;
368 case 3:
369 c = (((dlast & 0x03 ) << 6) | d);
370 *str++ = c;
371 phase = 0;
372 break;
374 dlast = d;
377 *str = '\0';