Fri, 11 Oct 2002 03:14:01 +0000
[gaim-migrate @ 3753]
Yeah this will probably break a lot of shit knowing my luck. But hey, I really don't care what people thnk.
| 3127 | 1 | /* -------------------------------------------------------------------------- |
| 2 | * | |
| 3 | * License | |
| 4 | * | |
| 5 | * The contents of this file are subject to the Jabber Open Source License | |
| 6 | * Version 1.0 (the "JOSL"). You may not copy or use this file, in either | |
| 7 | * source code or executable form, except in compliance with the JOSL. You | |
| 8 | * may obtain a copy of the JOSL at http://www.jabber.org/ or at | |
| 9 | * http://www.opensource.org/. | |
| 10 | * | |
| 11 | * Software distributed under the JOSL is distributed on an "AS IS" basis, | |
| 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the JOSL | |
| 13 | * for the specific language governing rights and limitations under the | |
| 14 | * JOSL. | |
| 15 | * | |
| 16 | * Copyrights | |
| 17 | * | |
| 18 | * Portions created by or assigned to Jabber.com, Inc. are | |
| 19 | * Copyright (c) 1999-2002 Jabber.com, Inc. All Rights Reserved. Contact | |
| 20 | * information for Jabber.com, Inc. is available at http://www.jabber.com/. | |
| 2086 | 21 | * |
| 3127 | 22 | * Portions Copyright (c) 1998-1999 Jeremie Miller. |
| 23 | * | |
| 24 | * Acknowledgements | |
| 25 | * | |
| 26 | * Special thanks to the Jabber Open Source Contributors for their | |
| 27 | * suggestions and support of Jabber. | |
| 28 | * | |
| 29 | * Alternatively, the contents of this file may be used under the terms of the | |
| 30 | * GNU General Public License Version 2 or later (the "GPL"), in which case | |
| 31 | * the provisions of the GPL are applicable instead of those above. If you | |
| 32 | * wish to allow use of your version of this file only under the terms of the | |
| 33 | * GPL and not to allow others to use your version of this file under the JOSL, | |
| 34 | * indicate your decision by deleting the provisions above and replace them | |
| 35 | * with the notice and other provisions required by the GPL. If you do not | |
| 36 | * delete the provisions above, a recipient may use your version of this file | |
| 37 | * under either the JOSL or the GPL. | |
| 38 | * | |
| 39 | * | |
| 40 | * --------------------------------------------------------------------------*/ | |
| 2086 | 41 | |
| 3127 | 42 | #include "lib.h" |
| 2086 | 43 | |
| 44 | char *j_strdup(const char *str) | |
| 45 | { | |
| 46 | if(str == NULL) | |
| 47 | return NULL; | |
| 48 | else | |
| 49 | return strdup(str); | |
| 50 | } | |
| 51 | ||
| 52 | char *j_strcat(char *dest, char *txt) | |
| 53 | { | |
| 54 | if(!txt) return(dest); | |
| 55 | ||
| 56 | while(*txt) | |
| 57 | *dest++ = *txt++; | |
| 58 | *dest = '\0'; | |
| 59 | ||
| 60 | return(dest); | |
| 61 | } | |
| 62 | ||
| 63 | int j_strcmp(const char *a, const char *b) | |
| 64 | { | |
| 65 | if(a == NULL || b == NULL) | |
| 66 | return -1; | |
| 3127 | 67 | |
| 68 | while(*a == *b && *a != '\0' && *b != '\0'){ a++; b++; } | |
| 69 | ||
| 70 | if(*a == *b) return 0; | |
| 71 | ||
| 72 | return -1; | |
| 2086 | 73 | } |
| 74 | ||
| 75 | int j_strcasecmp(const char *a, const char *b) | |
| 76 | { | |
| 77 | if(a == NULL || b == NULL) | |
| 78 | return -1; | |
| 79 | else | |
| 80 | return strcasecmp(a, b); | |
| 81 | } | |
| 82 | ||
| 83 | int j_strncmp(const char *a, const char *b, int i) | |
| 84 | { | |
| 85 | if(a == NULL || b == NULL) | |
| 86 | return -1; | |
| 87 | else | |
| 88 | return strncmp(a, b, i); | |
| 89 | } | |
| 90 | ||
| 91 | int j_strncasecmp(const char *a, const char *b, int i) | |
| 92 | { | |
| 93 | if(a == NULL || b == NULL) | |
| 94 | return -1; | |
| 95 | else | |
| 96 | return strncasecmp(a, b, i); | |
| 97 | } | |
| 98 | ||
| 99 | int j_strlen(const char *a) | |
| 100 | { | |
| 101 | if(a == NULL) | |
| 102 | return 0; | |
| 103 | else | |
| 104 | return strlen(a); | |
| 105 | } | |
| 106 | ||
| 107 | int j_atoi(const char *a, int def) | |
| 108 | { | |
| 109 | if(a == NULL) | |
| 110 | return def; | |
| 111 | else | |
| 112 | return atoi(a); | |
| 113 | } | |
| 114 | ||
| 115 | spool spool_new(pool p) | |
| 116 | { | |
| 117 | spool s; | |
| 118 | ||
| 119 | s = pmalloc(p, sizeof(struct spool_struct)); | |
| 120 | s->p = p; | |
| 121 | s->len = 0; | |
| 122 | s->last = NULL; | |
| 123 | s->first = NULL; | |
| 124 | return s; | |
| 125 | } | |
| 126 | ||
| 127 | void spool_add(spool s, char *str) | |
| 128 | { | |
| 129 | struct spool_node *sn; | |
| 130 | int len; | |
| 131 | ||
| 132 | if(str == NULL) | |
| 133 | return; | |
| 134 | ||
| 135 | len = strlen(str); | |
| 136 | if(len == 0) | |
| 137 | return; | |
| 138 | ||
| 139 | sn = pmalloc(s->p, sizeof(struct spool_node)); | |
| 140 | sn->c = pstrdup(s->p, str); | |
| 141 | sn->next = NULL; | |
| 142 | ||
| 143 | s->len += len; | |
| 144 | if(s->last != NULL) | |
| 145 | s->last->next = sn; | |
| 146 | s->last = sn; | |
| 147 | if(s->first == NULL) | |
| 148 | s->first = sn; | |
| 149 | } | |
| 150 | ||
| 151 | void spooler(spool s, ...) | |
| 152 | { | |
| 153 | va_list ap; | |
| 154 | char *arg = NULL; | |
| 155 | ||
| 156 | if(s == NULL) | |
| 157 | return; | |
| 158 | ||
| 159 | va_start(ap, s); | |
| 160 | ||
| 161 | /* loop till we hit our end flag, the first arg */ | |
| 162 | while(1) | |
| 163 | { | |
| 164 | arg = va_arg(ap,char *); | |
| 3127 | 165 | if((spool)arg == s) |
| 2086 | 166 | break; |
| 167 | else | |
| 168 | spool_add(s, arg); | |
| 169 | } | |
| 170 | ||
| 171 | va_end(ap); | |
| 172 | } | |
| 173 | ||
| 174 | char *spool_print(spool s) | |
| 175 | { | |
| 176 | char *ret,*tmp; | |
| 177 | struct spool_node *next; | |
| 178 | ||
| 179 | if(s == NULL || s->len == 0 || s->first == NULL) | |
| 180 | return NULL; | |
| 181 | ||
| 182 | ret = pmalloc(s->p, s->len + 1); | |
| 183 | *ret = '\0'; | |
| 184 | ||
| 185 | next = s->first; | |
| 186 | tmp = ret; | |
| 187 | while(next != NULL) | |
| 188 | { | |
| 189 | tmp = j_strcat(tmp,next->c); | |
| 190 | next = next->next; | |
| 191 | } | |
| 192 | ||
| 193 | return ret; | |
| 194 | } | |
| 195 | ||
| 196 | /* convenience :) */ | |
| 197 | char *spools(pool p, ...) | |
| 198 | { | |
| 199 | va_list ap; | |
| 200 | spool s; | |
| 201 | char *arg = NULL; | |
| 202 | ||
| 203 | if(p == NULL) | |
| 204 | return NULL; | |
| 205 | ||
| 206 | s = spool_new(p); | |
| 207 | ||
| 208 | va_start(ap, p); | |
| 209 | ||
| 210 | /* loop till we hit our end flag, the first arg */ | |
| 211 | while(1) | |
| 212 | { | |
| 213 | arg = va_arg(ap,char *); | |
| 3127 | 214 | if((pool)arg == p) |
| 2086 | 215 | break; |
| 216 | else | |
| 217 | spool_add(s, arg); | |
| 218 | } | |
| 219 | ||
| 220 | va_end(ap); | |
| 221 | ||
| 222 | return spool_print(s); | |
| 223 | } | |
| 224 | ||
| 225 | ||
| 226 | char *strunescape(pool p, char *buf) | |
| 227 | { | |
| 228 | int i,j=0; | |
| 229 | char *temp; | |
| 230 | ||
| 231 | if (p == NULL || buf == NULL) return(NULL); | |
| 232 | ||
| 233 | if (strchr(buf,'&') == NULL) return(buf); | |
| 234 | ||
| 235 | temp = pmalloc(p,strlen(buf)+1); | |
| 236 | ||
| 237 | if (temp == NULL) return(NULL); | |
| 238 | ||
| 239 | for(i=0;i<strlen(buf);i++) | |
| 240 | { | |
| 241 | if (buf[i]=='&') | |
| 242 | { | |
| 243 | if (strncmp(&buf[i],"&",5)==0) | |
| 244 | { | |
| 245 | temp[j] = '&'; | |
| 246 | i += 4; | |
| 247 | } else if (strncmp(&buf[i],""",6)==0) { | |
| 248 | temp[j] = '\"'; | |
| 249 | i += 5; | |
| 250 | } else if (strncmp(&buf[i],"'",6)==0) { | |
| 251 | temp[j] = '\''; | |
| 252 | i += 5; | |
| 253 | } else if (strncmp(&buf[i],"<",4)==0) { | |
| 254 | temp[j] = '<'; | |
| 255 | i += 3; | |
| 256 | } else if (strncmp(&buf[i],">",4)==0) { | |
| 257 | temp[j] = '>'; | |
| 258 | i += 3; | |
| 259 | } | |
| 260 | } else { | |
| 261 | temp[j]=buf[i]; | |
| 262 | } | |
| 263 | j++; | |
| 264 | } | |
| 265 | temp[j]='\0'; | |
| 266 | return(temp); | |
| 267 | } | |
| 268 | ||
| 269 | ||
| 270 | char *strescape(pool p, char *buf) | |
| 271 | { | |
| 272 | int i,j,oldlen,newlen; | |
| 273 | char *temp; | |
| 274 | ||
| 275 | if (p == NULL || buf == NULL) return(NULL); | |
| 276 | ||
| 277 | oldlen = newlen = strlen(buf); | |
| 278 | for(i=0;i<oldlen;i++) | |
| 279 | { | |
| 280 | switch(buf[i]) | |
| 281 | { | |
| 282 | case '&': | |
| 283 | newlen+=5; | |
| 284 | break; | |
| 285 | case '\'': | |
| 286 | newlen+=6; | |
| 287 | break; | |
| 288 | case '\"': | |
| 289 | newlen+=6; | |
| 290 | break; | |
| 291 | case '<': | |
| 292 | newlen+=4; | |
| 293 | break; | |
| 294 | case '>': | |
| 295 | newlen+=4; | |
| 296 | break; | |
| 297 | } | |
| 298 | } | |
| 299 | ||
| 300 | if(oldlen == newlen) return buf; | |
| 301 | ||
| 302 | temp = pmalloc(p,newlen+1); | |
| 303 | ||
| 304 | if (temp==NULL) return(NULL); | |
| 305 | ||
| 306 | for(i=j=0;i<oldlen;i++) | |
| 307 | { | |
| 308 | switch(buf[i]) | |
| 309 | { | |
| 310 | case '&': | |
| 311 | memcpy(&temp[j],"&",5); | |
| 312 | j += 5; | |
| 313 | break; | |
| 314 | case '\'': | |
| 315 | memcpy(&temp[j],"'",6); | |
| 316 | j += 6; | |
| 317 | break; | |
| 318 | case '\"': | |
| 319 | memcpy(&temp[j],""",6); | |
| 320 | j += 6; | |
| 321 | break; | |
| 322 | case '<': | |
| 323 | memcpy(&temp[j],"<",4); | |
| 324 | j += 4; | |
| 325 | break; | |
| 326 | case '>': | |
| 327 | memcpy(&temp[j],">",4); | |
| 328 | j += 4; | |
| 329 | break; | |
| 330 | default: | |
| 331 | temp[j++] = buf[i]; | |
| 332 | } | |
| 333 | } | |
| 334 | temp[j] = '\0'; | |
| 335 | return temp; | |
| 336 | } | |
| 337 | ||
| 338 | char *zonestr(char *file, int line) | |
| 339 | { | |
| 340 | static char buff[64]; | |
| 341 | int i; | |
| 342 | ||
| 343 | i = snprintf(buff,63,"%s:%d",file,line); | |
| 344 | buff[i] = '\0'; | |
| 345 | ||
| 346 | return buff; | |
| 347 | } | |
| 348 | ||
| 349 | void str_b64decode(char* str) | |
| 350 | { | |
| 351 | char *cur; | |
| 352 | int d, dlast, phase; | |
| 353 | unsigned char c; | |
| 354 | static int table[256] = { | |
| 355 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */ | |
| 356 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */ | |
| 357 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */ | |
| 358 | 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */ | |
| 359 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */ | |
| 360 | 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */ | |
| 361 | -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */ | |
| 362 | 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */ | |
| 363 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */ | |
| 364 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */ | |
| 365 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */ | |
| 366 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */ | |
| 367 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */ | |
| 368 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */ | |
| 369 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */ | |
| 370 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */ | |
| 371 | }; | |
| 372 | ||
| 3127 | 373 | d = dlast = phase = 0; |
| 2086 | 374 | for (cur = str; *cur != '\0'; ++cur ) |
| 375 | { | |
| 376 | d = table[(int)*cur]; | |
| 377 | if(d != -1) | |
| 378 | { | |
| 379 | switch(phase) | |
| 380 | { | |
| 381 | case 0: | |
| 382 | ++phase; | |
| 383 | break; | |
| 384 | case 1: | |
| 385 | c = ((dlast << 2) | ((d & 0x30) >> 4)); | |
| 386 | *str++ = c; | |
| 387 | ++phase; | |
| 388 | break; | |
| 389 | case 2: | |
| 390 | c = (((dlast & 0xf) << 4) | ((d & 0x3c) >> 2)); | |
| 391 | *str++ = c; | |
| 392 | ++phase; | |
| 393 | break; | |
| 394 | case 3: | |
| 395 | c = (((dlast & 0x03 ) << 6) | d); | |
| 396 | *str++ = c; | |
| 397 | phase = 0; | |
| 398 | break; | |
| 399 | } | |
| 400 | dlast = d; | |
| 401 | } | |
| 402 | } | |
| 403 | *str = '\0'; | |
| 404 | } |