Wed, 16 Oct 2002 19:57:03 +0000
[gaim-migrate @ 3850]
Warning fixes and WIN32 ifdef removals
| 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 | |
|
3717
2fc0789e04e8
[gaim-migrate @ 3850]
Herman Bloggs <herman@bluedigits.com>
parents:
3127
diff
changeset
|
44 | #ifdef _WIN32 |
|
2fc0789e04e8
[gaim-migrate @ 3850]
Herman Bloggs <herman@bluedigits.com>
parents:
3127
diff
changeset
|
45 | #include "win32dep.h" |
|
2fc0789e04e8
[gaim-migrate @ 3850]
Herman Bloggs <herman@bluedigits.com>
parents:
3127
diff
changeset
|
46 | #endif |
|
2fc0789e04e8
[gaim-migrate @ 3850]
Herman Bloggs <herman@bluedigits.com>
parents:
3127
diff
changeset
|
47 | |
| 2086 | 48 | char *j_strdup(const char *str) |
| 49 | { | |
| 50 | if(str == NULL) | |
| 51 | return NULL; | |
| 52 | else | |
| 53 | return strdup(str); | |
| 54 | } | |
| 55 | ||
| 56 | char *j_strcat(char *dest, char *txt) | |
| 57 | { | |
| 58 | if(!txt) return(dest); | |
| 59 | ||
| 60 | while(*txt) | |
| 61 | *dest++ = *txt++; | |
| 62 | *dest = '\0'; | |
| 63 | ||
| 64 | return(dest); | |
| 65 | } | |
| 66 | ||
| 67 | int j_strcmp(const char *a, const char *b) | |
| 68 | { | |
| 69 | if(a == NULL || b == NULL) | |
| 70 | return -1; | |
| 3127 | 71 | |
| 72 | while(*a == *b && *a != '\0' && *b != '\0'){ a++; b++; } | |
| 73 | ||
| 74 | if(*a == *b) return 0; | |
| 75 | ||
| 76 | return -1; | |
| 2086 | 77 | } |
| 78 | ||
| 79 | int j_strcasecmp(const char *a, const char *b) | |
| 80 | { | |
| 81 | if(a == NULL || b == NULL) | |
| 82 | return -1; | |
| 83 | else | |
| 84 | return strcasecmp(a, b); | |
| 85 | } | |
| 86 | ||
| 87 | int j_strncmp(const char *a, const char *b, int i) | |
| 88 | { | |
| 89 | if(a == NULL || b == NULL) | |
| 90 | return -1; | |
| 91 | else | |
| 92 | return strncmp(a, b, i); | |
| 93 | } | |
| 94 | ||
| 95 | int j_strncasecmp(const char *a, const char *b, int i) | |
| 96 | { | |
| 97 | if(a == NULL || b == NULL) | |
| 98 | return -1; | |
| 99 | else | |
| 100 | return strncasecmp(a, b, i); | |
| 101 | } | |
| 102 | ||
| 103 | int j_strlen(const char *a) | |
| 104 | { | |
| 105 | if(a == NULL) | |
| 106 | return 0; | |
| 107 | else | |
| 108 | return strlen(a); | |
| 109 | } | |
| 110 | ||
| 111 | int j_atoi(const char *a, int def) | |
| 112 | { | |
| 113 | if(a == NULL) | |
| 114 | return def; | |
| 115 | else | |
| 116 | return atoi(a); | |
| 117 | } | |
| 118 | ||
| 119 | spool spool_new(pool p) | |
| 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 | void spool_add(spool s, char *str) | |
| 132 | { | |
| 133 | struct spool_node *sn; | |
| 134 | int len; | |
| 135 | ||
| 136 | if(str == NULL) | |
| 137 | return; | |
| 138 | ||
| 139 | len = strlen(str); | |
| 140 | if(len == 0) | |
| 141 | return; | |
| 142 | ||
| 143 | sn = pmalloc(s->p, sizeof(struct spool_node)); | |
| 144 | sn->c = pstrdup(s->p, str); | |
| 145 | sn->next = NULL; | |
| 146 | ||
| 147 | s->len += len; | |
| 148 | if(s->last != NULL) | |
| 149 | s->last->next = sn; | |
| 150 | s->last = sn; | |
| 151 | if(s->first == NULL) | |
| 152 | s->first = sn; | |
| 153 | } | |
| 154 | ||
| 155 | void spooler(spool s, ...) | |
| 156 | { | |
| 157 | va_list ap; | |
| 158 | char *arg = NULL; | |
| 159 | ||
| 160 | if(s == NULL) | |
| 161 | return; | |
| 162 | ||
| 163 | va_start(ap, s); | |
| 164 | ||
| 165 | /* loop till we hit our end flag, the first arg */ | |
| 166 | while(1) | |
| 167 | { | |
| 168 | arg = va_arg(ap,char *); | |
| 3127 | 169 | if((spool)arg == s) |
| 2086 | 170 | break; |
| 171 | else | |
| 172 | spool_add(s, arg); | |
| 173 | } | |
| 174 | ||
| 175 | va_end(ap); | |
| 176 | } | |
| 177 | ||
| 178 | char *spool_print(spool s) | |
| 179 | { | |
| 180 | char *ret,*tmp; | |
| 181 | struct spool_node *next; | |
| 182 | ||
| 183 | if(s == NULL || s->len == 0 || s->first == NULL) | |
| 184 | return NULL; | |
| 185 | ||
| 186 | ret = pmalloc(s->p, s->len + 1); | |
| 187 | *ret = '\0'; | |
| 188 | ||
| 189 | next = s->first; | |
| 190 | tmp = ret; | |
| 191 | while(next != NULL) | |
| 192 | { | |
| 193 | tmp = j_strcat(tmp,next->c); | |
| 194 | next = next->next; | |
| 195 | } | |
| 196 | ||
| 197 | return ret; | |
| 198 | } | |
| 199 | ||
| 200 | /* convenience :) */ | |
| 201 | char *spools(pool p, ...) | |
| 202 | { | |
| 203 | va_list ap; | |
| 204 | spool s; | |
| 205 | char *arg = NULL; | |
| 206 | ||
| 207 | if(p == NULL) | |
| 208 | return NULL; | |
| 209 | ||
| 210 | s = spool_new(p); | |
| 211 | ||
| 212 | va_start(ap, p); | |
| 213 | ||
| 214 | /* loop till we hit our end flag, the first arg */ | |
| 215 | while(1) | |
| 216 | { | |
| 217 | arg = va_arg(ap,char *); | |
| 3127 | 218 | if((pool)arg == p) |
| 2086 | 219 | break; |
| 220 | else | |
| 221 | spool_add(s, arg); | |
| 222 | } | |
| 223 | ||
| 224 | va_end(ap); | |
| 225 | ||
| 226 | return spool_print(s); | |
| 227 | } | |
| 228 | ||
| 229 | ||
| 230 | char *strunescape(pool p, char *buf) | |
| 231 | { | |
| 232 | int i,j=0; | |
| 233 | char *temp; | |
| 234 | ||
| 235 | if (p == NULL || buf == NULL) return(NULL); | |
| 236 | ||
| 237 | if (strchr(buf,'&') == NULL) return(buf); | |
| 238 | ||
| 239 | temp = pmalloc(p,strlen(buf)+1); | |
| 240 | ||
| 241 | if (temp == NULL) return(NULL); | |
| 242 | ||
| 243 | for(i=0;i<strlen(buf);i++) | |
| 244 | { | |
| 245 | if (buf[i]=='&') | |
| 246 | { | |
| 247 | if (strncmp(&buf[i],"&",5)==0) | |
| 248 | { | |
| 249 | temp[j] = '&'; | |
| 250 | i += 4; | |
| 251 | } else if (strncmp(&buf[i],""",6)==0) { | |
| 252 | temp[j] = '\"'; | |
| 253 | i += 5; | |
| 254 | } else if (strncmp(&buf[i],"'",6)==0) { | |
| 255 | temp[j] = '\''; | |
| 256 | i += 5; | |
| 257 | } else if (strncmp(&buf[i],"<",4)==0) { | |
| 258 | temp[j] = '<'; | |
| 259 | i += 3; | |
| 260 | } else if (strncmp(&buf[i],">",4)==0) { | |
| 261 | temp[j] = '>'; | |
| 262 | i += 3; | |
| 263 | } | |
| 264 | } else { | |
| 265 | temp[j]=buf[i]; | |
| 266 | } | |
| 267 | j++; | |
| 268 | } | |
| 269 | temp[j]='\0'; | |
| 270 | return(temp); | |
| 271 | } | |
| 272 | ||
| 273 | ||
| 274 | char *strescape(pool p, char *buf) | |
| 275 | { | |
| 276 | int i,j,oldlen,newlen; | |
| 277 | char *temp; | |
| 278 | ||
| 279 | if (p == NULL || buf == NULL) return(NULL); | |
| 280 | ||
| 281 | oldlen = newlen = strlen(buf); | |
| 282 | for(i=0;i<oldlen;i++) | |
| 283 | { | |
| 284 | switch(buf[i]) | |
| 285 | { | |
| 286 | case '&': | |
| 287 | newlen+=5; | |
| 288 | break; | |
| 289 | case '\'': | |
| 290 | newlen+=6; | |
| 291 | break; | |
| 292 | case '\"': | |
| 293 | newlen+=6; | |
| 294 | break; | |
| 295 | case '<': | |
| 296 | newlen+=4; | |
| 297 | break; | |
| 298 | case '>': | |
| 299 | newlen+=4; | |
| 300 | break; | |
| 301 | } | |
| 302 | } | |
| 303 | ||
| 304 | if(oldlen == newlen) return buf; | |
| 305 | ||
| 306 | temp = pmalloc(p,newlen+1); | |
| 307 | ||
| 308 | if (temp==NULL) return(NULL); | |
| 309 | ||
| 310 | for(i=j=0;i<oldlen;i++) | |
| 311 | { | |
| 312 | switch(buf[i]) | |
| 313 | { | |
| 314 | case '&': | |
| 315 | memcpy(&temp[j],"&",5); | |
| 316 | j += 5; | |
| 317 | break; | |
| 318 | case '\'': | |
| 319 | memcpy(&temp[j],"'",6); | |
| 320 | j += 6; | |
| 321 | break; | |
| 322 | case '\"': | |
| 323 | memcpy(&temp[j],""",6); | |
| 324 | j += 6; | |
| 325 | break; | |
| 326 | case '<': | |
| 327 | memcpy(&temp[j],"<",4); | |
| 328 | j += 4; | |
| 329 | break; | |
| 330 | case '>': | |
| 331 | memcpy(&temp[j],">",4); | |
| 332 | j += 4; | |
| 333 | break; | |
| 334 | default: | |
| 335 | temp[j++] = buf[i]; | |
| 336 | } | |
| 337 | } | |
| 338 | temp[j] = '\0'; | |
| 339 | return temp; | |
| 340 | } | |
| 341 | ||
| 342 | char *zonestr(char *file, int line) | |
| 343 | { | |
| 344 | static char buff[64]; | |
| 345 | int i; | |
| 346 | ||
| 347 | i = snprintf(buff,63,"%s:%d",file,line); | |
| 348 | buff[i] = '\0'; | |
| 349 | ||
| 350 | return buff; | |
| 351 | } | |
| 352 | ||
| 353 | void str_b64decode(char* str) | |
| 354 | { | |
| 355 | char *cur; | |
| 356 | int d, dlast, phase; | |
| 357 | unsigned char c; | |
| 358 | static int table[256] = { | |
| 359 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */ | |
| 360 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */ | |
| 361 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */ | |
| 362 | 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */ | |
| 363 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */ | |
| 364 | 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */ | |
| 365 | -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */ | |
| 366 | 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */ | |
| 367 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */ | |
| 368 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */ | |
| 369 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */ | |
| 370 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */ | |
| 371 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */ | |
| 372 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */ | |
| 373 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */ | |
| 374 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */ | |
| 375 | }; | |
| 376 | ||
| 3127 | 377 | d = dlast = phase = 0; |
| 2086 | 378 | for (cur = str; *cur != '\0'; ++cur ) |
| 379 | { | |
| 380 | d = table[(int)*cur]; | |
| 381 | if(d != -1) | |
| 382 | { | |
| 383 | switch(phase) | |
| 384 | { | |
| 385 | case 0: | |
| 386 | ++phase; | |
| 387 | break; | |
| 388 | case 1: | |
| 389 | c = ((dlast << 2) | ((d & 0x30) >> 4)); | |
| 390 | *str++ = c; | |
| 391 | ++phase; | |
| 392 | break; | |
| 393 | case 2: | |
| 394 | c = (((dlast & 0xf) << 4) | ((d & 0x3c) >> 2)); | |
| 395 | *str++ = c; | |
| 396 | ++phase; | |
| 397 | break; | |
| 398 | case 3: | |
| 399 | c = (((dlast & 0x03 ) << 6) | d); | |
| 400 | *str++ = c; | |
| 401 | phase = 0; | |
| 402 | break; | |
| 403 | } | |
| 404 | dlast = d; | |
| 405 | } | |
| 406 | } | |
| 407 | *str = '\0'; | |
| 408 | } |