jabberd2
2.2.16
|
00001 /* 00002 * jabberd - Jabber Open Source Server 00003 * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney, 00004 * Ryan Eatmon, Robert Norris 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA 00019 */ 00020 00021 #include "pool.h" 00022 #include "util.h" 00023 00024 char *j_strdup(const char *str) 00025 { 00026 if(str == NULL) 00027 return NULL; 00028 else 00029 return strdup(str); 00030 } 00031 00032 char *j_strcat(char *dest, char *txt) 00033 { 00034 if(!txt) return(dest); 00035 00036 while(*txt) 00037 *dest++ = *txt++; 00038 *dest = '\0'; 00039 00040 return(dest); 00041 } 00042 00043 int j_strcmp(const char *a, const char *b) 00044 { 00045 if(a == NULL || b == NULL) 00046 return -1; 00047 00048 while(*a == *b && *a != '\0' && *b != '\0'){ a++; b++; } 00049 00050 if(*a == *b) return 0; 00051 00052 return -1; 00053 } 00054 00055 int j_strcasecmp(const char *a, const char *b) 00056 { 00057 if(a == NULL || b == NULL) 00058 return -1; 00059 else 00060 return strcasecmp(a, b); 00061 } 00062 00063 int j_strncmp(const char *a, const char *b, int i) 00064 { 00065 if(a == NULL || b == NULL) 00066 return -1; 00067 else 00068 return strncmp(a, b, i); 00069 } 00070 00071 int j_strncasecmp(const char *a, const char *b, int i) 00072 { 00073 if(a == NULL || b == NULL) 00074 return -1; 00075 else 00076 return strncasecmp(a, b, i); 00077 } 00078 00079 int j_strlen(const char *a) 00080 { 00081 if(a == NULL) 00082 return 0; 00083 else 00084 return strlen(a); 00085 } 00086 00087 int j_atoi(const char *a, int def) 00088 { 00089 if(a == NULL) 00090 return def; 00091 else 00092 return atoi(a); 00093 } 00094 00095 char *j_attr(const char** atts, const char *attr) 00096 { 00097 int i = 0; 00098 00099 while(atts[i] != '\0') 00100 { 00101 if(j_strcmp(atts[i],attr) == 0) return (char*)atts[i+1]; 00102 i += 2; 00103 } 00104 00105 return NULL; 00106 } 00107 00109 char *j_strnchr(const char *s, int c, int n) { 00110 int count; 00111 00112 for(count = 0; count < n; count++) 00113 if(s[count] == (char) c) 00114 return &((char *)s)[count]; 00115 00116 return NULL; 00117 } 00118 00119 spool spool_new(pool_t p) 00120 { 00121 spool s; 00122 00123 s = pmalloc(p, sizeof(struct spool_struct)); 00124 s->p = p; 00125 s->len = 0; 00126 s->last = NULL; 00127 s->first = NULL; 00128 return s; 00129 } 00130 00131 static void _spool_add(spool s, char *goodstr) 00132 { 00133 struct spool_node *sn; 00134 00135 sn = pmalloc(s->p, sizeof(struct spool_node)); 00136 sn->c = goodstr; 00137 sn->next = NULL; 00138 00139 s->len += strlen(goodstr); 00140 if(s->last != NULL) 00141 s->last->next = sn; 00142 s->last = sn; 00143 if(s->first == NULL) 00144 s->first = sn; 00145 } 00146 00147 void spool_add(spool s, char *str) 00148 { 00149 if(str == NULL || strlen(str) == 0) 00150 return; 00151 00152 _spool_add(s, pstrdup(s->p, str)); 00153 } 00154 00155 void spool_escape(spool s, char *raw, int len) 00156 { 00157 if(raw == NULL || len <= 0) 00158 return; 00159 00160 _spool_add(s, strescape(s->p, raw, len)); 00161 } 00162 00163 void spooler(spool s, ...) 00164 { 00165 va_list ap; 00166 char *arg = NULL; 00167 00168 if(s == NULL) 00169 return; 00170 00171 va_start(ap, s); 00172 00173 /* loop till we hit our end flag, the first arg */ 00174 while(1) 00175 { 00176 arg = va_arg(ap,char *); 00177 if((spool)arg == s) 00178 break; 00179 else 00180 spool_add(s, arg); 00181 } 00182 00183 va_end(ap); 00184 } 00185 00186 char *spool_print(spool s) 00187 { 00188 char *ret,*tmp; 00189 struct spool_node *next; 00190 00191 if(s == NULL || s->len == 0 || s->first == NULL) 00192 return NULL; 00193 00194 ret = pmalloc(s->p, s->len + 1); 00195 *ret = '\0'; 00196 00197 next = s->first; 00198 tmp = ret; 00199 while(next != NULL) 00200 { 00201 tmp = j_strcat(tmp,next->c); 00202 next = next->next; 00203 } 00204 00205 return ret; 00206 } 00207 00209 char *spools(pool_t p, ...) 00210 { 00211 va_list ap; 00212 spool s; 00213 char *arg = NULL; 00214 00215 if(p == NULL) 00216 return NULL; 00217 00218 s = spool_new(p); 00219 00220 va_start(ap, p); 00221 00222 /* loop till we hit our end flag, the first arg */ 00223 while(1) 00224 { 00225 arg = va_arg(ap,char *); 00226 if((pool_t)arg == p) 00227 break; 00228 else 00229 spool_add(s, arg); 00230 } 00231 00232 va_end(ap); 00233 00234 return spool_print(s); 00235 } 00236 00237 00238 char *strunescape(pool_t p, char *buf) 00239 { 00240 int i,j=0; 00241 char *temp; 00242 00243 if (buf == NULL) return(NULL); 00244 00245 if (strchr(buf,'&') == NULL) return(buf); 00246 00247 if(p != NULL) 00248 temp = pmalloc(p,strlen(buf)+1); 00249 else 00250 temp = malloc(strlen(buf)+1); 00251 00252 if (temp == NULL) return(NULL); 00253 00254 for(i=0;i<strlen(buf);i++) 00255 { 00256 if (buf[i]=='&') 00257 { 00258 if (strncmp(&buf[i],"&",5)==0) 00259 { 00260 temp[j] = '&'; 00261 i += 4; 00262 } else if (strncmp(&buf[i],""",6)==0) { 00263 temp[j] = '\"'; 00264 i += 5; 00265 } else if (strncmp(&buf[i],"'",6)==0) { 00266 temp[j] = '\''; 00267 i += 5; 00268 } else if (strncmp(&buf[i],"<",4)==0) { 00269 temp[j] = '<'; 00270 i += 3; 00271 } else if (strncmp(&buf[i],">",4)==0) { 00272 temp[j] = '>'; 00273 i += 3; 00274 } 00275 } else { 00276 temp[j]=buf[i]; 00277 } 00278 j++; 00279 } 00280 temp[j]='\0'; 00281 return(temp); 00282 } 00283 00284 00285 char *strescape(pool_t p, char *buf, int len) 00286 { 00287 int i,j,newlen = len; 00288 char *temp; 00289 00290 if (buf == NULL || len < 0) return NULL; 00291 00292 for(i=0;i<len;i++) 00293 { 00294 switch(buf[i]) 00295 { 00296 case '&': 00297 newlen+=5; 00298 break; 00299 case '\'': 00300 newlen+=6; 00301 break; 00302 case '\"': 00303 newlen+=6; 00304 break; 00305 case '<': 00306 newlen+=4; 00307 break; 00308 case '>': 00309 newlen+=4; 00310 break; 00311 } 00312 } 00313 00314 if(p != NULL) 00315 temp = pmalloc(p,newlen+1); 00316 else 00317 temp = malloc(newlen+1); 00318 if(newlen == len) 00319 { 00320 memcpy(temp,buf,len); 00321 temp[len] = '\0'; 00322 return temp; 00323 } 00324 00325 for(i=j=0;i<len;i++) 00326 { 00327 switch(buf[i]) 00328 { 00329 case '&': 00330 memcpy(&temp[j],"&",5); 00331 j += 5; 00332 break; 00333 case '\'': 00334 memcpy(&temp[j],"'",6); 00335 j += 6; 00336 break; 00337 case '\"': 00338 memcpy(&temp[j],""",6); 00339 j += 6; 00340 break; 00341 case '<': 00342 memcpy(&temp[j],"<",4); 00343 j += 4; 00344 break; 00345 case '>': 00346 memcpy(&temp[j],">",4); 00347 j += 4; 00348 break; 00349 default: 00350 temp[j++] = buf[i]; 00351 } 00352 } 00353 temp[j] = '\0'; 00354 return temp; 00355 } 00356 00358 void shahash_r(const char* str, char hashbuf[41]) { 00359 unsigned char hashval[20]; 00360 00361 shahash_raw(str, hashval); 00362 hex_from_raw(hashval, 20, hashbuf); 00363 } 00364 void shahash_raw(const char* str, unsigned char hashval[20]) { 00365 #ifdef HAVE_SSL 00366 /* use OpenSSL functions when available */ 00367 # include <openssl/sha.h> 00368 SHA1((unsigned char *)str, strlen(str), hashval); 00369 #else 00370 sha1_hash((unsigned char *)str, strlen(str), hashval); 00371 #endif 00372 }