jabberd2  2.3.6
str.c
Go to the documentation of this file.
1 /*
2  * jabberd - Jabber Open Source Server
3  * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
4  * Ryan Eatmon, Robert Norris
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
19  */
20 
21 #include "pool.h"
22 #include "util.h"
23 
24 char *j_strdup(const char *str)
25 {
26  if(str == NULL)
27  return NULL;
28  else
29  return strdup(str);
30 }
31 
32 char *j_strcat(char *dest, const char *txt)
33 {
34  if(!txt) return(dest);
35 
36  while(*txt)
37  *dest++ = *txt++;
38  *dest = '\0';
39 
40  return(dest);
41 }
42 
43 int j_strcmp(const char *a, const char *b)
44 {
45  if(a == NULL || b == NULL)
46  return -1;
47 
48  while(*a == *b && *a != '\0' && *b != '\0'){ a++; b++; }
49 
50  if(*a == *b) return 0;
51 
52  return -1;
53 }
54 
55 int j_strcasecmp(const char *a, const char *b)
56 {
57  if(a == NULL || b == NULL)
58  return -1;
59  else
60  return strcasecmp(a, b);
61 }
62 
63 int j_strncmp(const char *a, const char *b, int i)
64 {
65  if(a == NULL || b == NULL)
66  return -1;
67  else
68  return strncmp(a, b, i);
69 }
70 
71 int j_strncasecmp(const char *a, const char *b, int i)
72 {
73  if(a == NULL || b == NULL)
74  return -1;
75  else
76  return strncasecmp(a, b, i);
77 }
78 
79 int j_strlen(const char *a)
80 {
81  if(a == NULL)
82  return 0;
83  else
84  return strlen(a);
85 }
86 
87 int j_atoi(const char *a, int def)
88 {
89  if(a == NULL)
90  return def;
91  else
92  return atoi(a);
93 }
94 
95 char *j_attr(const char** atts, const char *attr)
96 {
97  int i = 0;
98 
99  while(atts[i] != '\0')
100  {
101  if(j_strcmp(atts[i],attr) == 0) return (char*)atts[i+1];
102  i += 2;
103  }
104 
105  return NULL;
106 }
107 
109 char *j_strnchr(const char *s, int c, int n) {
110  int count;
111 
112  for(count = 0; count < n; count++)
113  if(s[count] == (char) c)
114  return &((char *)s)[count];
115 
116  return NULL;
117 }
118 
120 {
121  spool s;
122 
123  s = pmalloc(p, sizeof(struct spool_struct));
124  s->p = p;
125  s->len = 0;
126  s->last = NULL;
127  s->first = NULL;
128  return s;
129 }
130 
131 static void _spool_add(spool s, const char *goodstr)
132 {
133  struct spool_node *sn;
134 
135  sn = pmalloc(s->p, sizeof(struct spool_node));
136  sn->c = goodstr;
137  sn->next = NULL;
138 
139  s->len += strlen(goodstr);
140  if(s->last != NULL)
141  s->last->next = sn;
142  s->last = sn;
143  if(s->first == NULL)
144  s->first = sn;
145 }
146 
147 void spool_add(spool s, const char *str)
148 {
149  if(str == NULL || strlen(str) == 0)
150  return;
151 
152  _spool_add(s, pstrdup(s->p, str));
153 }
154 
155 void spool_escape(spool s, const char *raw, int len)
156 {
157  if(raw == NULL || len <= 0)
158  return;
159 
160  _spool_add(s, strescape(s->p, raw, len));
161 }
162 
163 void spooler(spool s, ...)
164 {
165  va_list ap;
166  char *arg = NULL;
167 
168  if(s == NULL)
169  return;
170 
171  va_start(ap, s);
172 
173  /* loop till we hit our end flag, the first arg */
174  while(1)
175  {
176  arg = va_arg(ap,char *);
177  if((spool)arg == s)
178  break;
179  else
180  spool_add(s, arg);
181  }
182 
183  va_end(ap);
184 }
185 
186 const char *spool_print(spool s)
187 {
188  char *ret,*tmp;
189  struct spool_node *next;
190 
191  if(s == NULL || s->len == 0 || s->first == NULL)
192  return NULL;
193 
194  ret = pmalloc(s->p, s->len + 1);
195  *ret = '\0';
196 
197  next = s->first;
198  tmp = ret;
199  while(next != NULL)
200  {
201  tmp = j_strcat(tmp,next->c);
202  next = next->next;
203  }
204 
205  return ret;
206 }
207 
209 const char *spools(pool_t p, ...)
210 {
211  va_list ap;
212  spool s;
213  char *arg = NULL;
214 
215  if(p == NULL)
216  return NULL;
217 
218  s = spool_new(p);
219 
220  va_start(ap, p);
221 
222  /* loop till we hit our end flag, the first arg */
223  while(1)
224  {
225  arg = va_arg(ap,char *);
226  if((pool_t)arg == p)
227  break;
228  else
229  spool_add(s, arg);
230  }
231 
232  va_end(ap);
233 
234  return spool_print(s);
235 }
236 
237 
238 char *strunescape(pool_t p, char *buf)
239 {
240  int i,j=0;
241  char *temp;
242 
243  if (buf == NULL) return(NULL);
244 
245  if (strchr(buf,'&') == NULL) return(buf);
246 
247  if(p != NULL)
248  temp = pmalloc(p,strlen(buf)+1);
249  else
250  temp = malloc(strlen(buf)+1);
251 
252  if (temp == NULL) return(NULL);
253 
254  for(i=0;i<strlen(buf);i++)
255  {
256  if (buf[i]=='&')
257  {
258  if (strncmp(&buf[i],"&amp;",5)==0)
259  {
260  temp[j] = '&';
261  i += 4;
262  } else if (strncmp(&buf[i],"&quot;",6)==0) {
263  temp[j] = '\"';
264  i += 5;
265  } else if (strncmp(&buf[i],"&apos;",6)==0) {
266  temp[j] = '\'';
267  i += 5;
268  } else if (strncmp(&buf[i],"&lt;",4)==0) {
269  temp[j] = '<';
270  i += 3;
271  } else if (strncmp(&buf[i],"&gt;",4)==0) {
272  temp[j] = '>';
273  i += 3;
274  }
275  } else {
276  temp[j]=buf[i];
277  }
278  j++;
279  }
280  temp[j]='\0';
281  return(temp);
282 }
283 
284 
285 char *strescape(pool_t p, const char *buf, int len)
286 {
287  int i,j,newlen = len;
288  char *temp;
289 
290  if (buf == NULL || len < 0) return NULL;
291 
292  for(i=0;i<len;i++)
293  {
294  switch(buf[i])
295  {
296  case '&':
297  newlen+=5;
298  break;
299  case '\'':
300  newlen+=6;
301  break;
302  case '\"':
303  newlen+=6;
304  break;
305  case '<':
306  newlen+=4;
307  break;
308  case '>':
309  newlen+=4;
310  break;
311  }
312  }
313 
314  if(p != NULL)
315  temp = pmalloc(p,newlen+1);
316  else
317  temp = malloc(newlen+1);
318  if(newlen == len)
319  {
320  memcpy(temp,buf,len);
321  temp[len] = '\0';
322  return temp;
323  }
324 
325  for(i=j=0;i<len;i++)
326  {
327  switch(buf[i])
328  {
329  case '&':
330  memcpy(&temp[j],"&amp;",5);
331  j += 5;
332  break;
333  case '\'':
334  memcpy(&temp[j],"&apos;",6);
335  j += 6;
336  break;
337  case '\"':
338  memcpy(&temp[j],"&quot;",6);
339  j += 6;
340  break;
341  case '<':
342  memcpy(&temp[j],"&lt;",4);
343  j += 4;
344  break;
345  case '>':
346  memcpy(&temp[j],"&gt;",4);
347  j += 4;
348  break;
349  default:
350  temp[j++] = buf[i];
351  }
352  }
353  temp[j] = '\0';
354  return temp;
355 }
356 
358 void shahash_r(const char* str, char hashbuf[41]) {
359  unsigned char hashval[20];
360 
361  shahash_raw(str, hashval);
362  hex_from_raw(hashval, 20, hashbuf);
363 }
364 void shahash_raw(const char* str, unsigned char hashval[20]) {
365 #ifdef HAVE_SSL
366  /* use OpenSSL functions when available */
367 # include <openssl/sha.h>
368  SHA1((unsigned char *)str, strlen(str), hashval);
369 #else
370  sha1_hash((unsigned char *)str, strlen(str), hashval);
371 #endif
372 }
const char * spools(pool_t p,...)
convenience :)
Definition: str.c:209
struct spool_node * next
Definition: util.h:146
char * j_strnchr(const char *s, int c, int n)
like strchr, but only searches n chars
Definition: str.c:109
void * pmalloc(pool_t p, int size)
Definition: pool.c:141
pool_t p
Definition: util.h:151
char * j_strdup(const char *str)
Definition: str.c:24
int j_strcasecmp(const char *a, const char *b)
Definition: str.c:55
void shahash_r(const char *str, char hashbuf[41])
convenience (originally by Thomas Muldowney)
Definition: str.c:358
int j_atoi(const char *a, int def)
Definition: str.c:87
void shahash_raw(const char *str, unsigned char hashval[20])
Definition: str.c:364
struct spool_node * last
Definition: util.h:153
char * strunescape(pool_t p, char *buf)
Definition: str.c:238
int j_strcmp(const char *a, const char *b)
Definition: str.c:43
void spool_add(spool s, const char *str)
Definition: str.c:147
const char * spool_print(spool s)
Definition: str.c:186
void hex_from_raw(const unsigned char *in, int inlen, char *out)
turn raw into hex - out must be (inlen*2)+1
Definition: hex.c:26
int j_strncmp(const char *a, const char *b, int i)
Definition: str.c:63
struct spool_node * first
Definition: util.h:154
void spooler(spool s,...)
Definition: str.c:163
const char * c
Definition: util.h:145
void sha1_hash(const unsigned char *dataIn, int len, unsigned char hashout[20])
Definition: sha1.c:105
int len
Definition: util.h:152
char * pstrdup(pool_t p, const char *src)
XXX efficient: move this to const char * and then loop throug the existing heaps to see if src is wit...
Definition: pool.c:191
char * j_attr(const char **atts, const char *attr)
Definition: str.c:95
int j_strlen(const char *a)
Definition: str.c:79
int j_strncasecmp(const char *a, const char *b, int i)
Definition: str.c:71
static void _spool_add(spool s, const char *goodstr)
Definition: str.c:131
void spool_escape(spool s, const char *raw, int len)
Definition: str.c:155
pool - base node for a pool.
Definition: pool.h:80
spool spool_new(pool_t p)
Definition: str.c:119
char * j_strcat(char *dest, const char *txt)
Definition: str.c:32
char * strescape(pool_t p, const char *buf, int len)
Definition: str.c:285