Mercurial > ~mikael > mcabber > hg
comparison mcabber/libjabber/str.c @ 417:c3ae9251c197
Sync libjabber with upstream
Sync with jabberd-1.4.4.
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Thu, 01 Sep 2005 23:29:21 +0200 |
parents | 84222be1f440 |
children | 5f43b532cc37 |
comparison
equal
deleted
inserted
replaced
416:48e7808c4191 | 417:c3ae9251c197 |
---|---|
11 * | 11 * |
12 * You should have received a copy of the GNU General Public License | 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 | 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. | 14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
15 * | 15 * |
16 * Jabber | 16 * Copyrights |
17 * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/ | 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/. | |
21 * | |
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 */ | |
30 | |
31 /** | |
32 * @file str.c | |
33 * @brief utilities for string handling | |
34 * | |
35 * This file contains utility functions for string handling: | |
36 * - NULL pointer save versions of many functions in string.c | |
37 * - string spools | |
38 * - functions to (un)escape strings for XML usage | |
39 * | |
40 * String spools allow to create a string by concatenating several smaller strings | |
41 * and the spool implementation is allocating the neccessary memory using memory pools. | |
18 */ | 42 */ |
19 | 43 |
20 #include "libxode.h" | 44 #include "libxode.h" |
21 | 45 |
46 /** | |
47 * NULL pointer save version of strdup() | |
48 * | |
49 * @param str the string the should be duplicated | |
50 * @return the duplicated string | |
51 */ | |
22 char *j_strdup(const char *str) | 52 char *j_strdup(const char *str) |
23 { | 53 { |
24 if(str == NULL) | 54 if(str == NULL) |
25 return NULL; | 55 return NULL; |
26 else | 56 else |
27 return strdup(str); | 57 return strdup(str); |
28 } | 58 } |
29 | 59 |
60 /** | |
61 * NULL pointer save version of strcat() | |
62 * | |
63 * @note the return value of j_strcat() is not compatible with the return value of strcat() | |
64 * | |
65 * @todo check if the behaviour of the return value is intended | |
66 * | |
67 * @param dest where to append the string | |
68 * @param txt what to append | |
69 * @return dest if txt contains a NULL pointer, pointer to the terminating zero byte of the result else | |
70 */ | |
30 char *j_strcat(char *dest, char *txt) | 71 char *j_strcat(char *dest, char *txt) |
31 { | 72 { |
32 if(!txt) return(dest); | 73 if(!txt) return(dest); |
33 | 74 |
34 while(*txt) | 75 while(*txt) |
36 *dest = '\0'; | 77 *dest = '\0'; |
37 | 78 |
38 return(dest); | 79 return(dest); |
39 } | 80 } |
40 | 81 |
82 /** | |
83 * NULL pointer save version of strcmp | |
84 * | |
85 * If one of the parameters contains a NULL pointer, the string is considered to be unequal. | |
86 * | |
87 * @note the return value is not compatible with strcmp() | |
88 * | |
89 * @param a the one string | |
90 * @param b the other string | |
91 * @return 0 if the strings are equal, -1 if the strings are not equal | |
92 */ | |
41 int j_strcmp(const char *a, const char *b) | 93 int j_strcmp(const char *a, const char *b) |
42 { | 94 { |
43 if(a == NULL || b == NULL) | 95 if(a == NULL || b == NULL) |
44 return -1; | 96 return -1; |
45 else | 97 |
46 return strcmp(a, b); | 98 while(*a == *b && *a != '\0' && *b != '\0'){ a++; b++; } |
47 } | 99 |
48 | 100 if(*a == *b) return 0; |
101 | |
102 return -1; | |
103 } | |
104 | |
105 /** | |
106 * NULL pointer save version of strcasecmp() | |
107 * | |
108 * If one of the parameters contains a NULL pointer, the string is considered to be unequal | |
109 * | |
110 * @param a the one string | |
111 * @param b the other string | |
112 * @return 0 if the strings are equal, non zero else | |
113 */ | |
49 int j_strcasecmp(const char *a, const char *b) | 114 int j_strcasecmp(const char *a, const char *b) |
50 { | 115 { |
51 if(a == NULL || b == NULL) | 116 if(a == NULL || b == NULL) |
52 return -1; | 117 return -1; |
53 else | 118 else |
54 return strcasecmp(a, b); | 119 return strcasecmp(a, b); |
55 } | 120 } |
56 | 121 |
122 /** | |
123 * NULL pointer save version of strncmp() | |
124 * | |
125 * If one of the parameters contains a NULL pointer, the string is considered to be unequal | |
126 * | |
127 * @param a the first string | |
128 * @param b the second string | |
129 * @param i how many characters to compare at most | |
130 * @return 0 if the strings are equal (within the given length limitation), non zero else | |
131 */ | |
57 int j_strncmp(const char *a, const char *b, int i) | 132 int j_strncmp(const char *a, const char *b, int i) |
58 { | 133 { |
59 if(a == NULL || b == NULL) | 134 if(a == NULL || b == NULL) |
60 return -1; | 135 return -1; |
61 else | 136 else |
62 return strncmp(a, b, i); | 137 return strncmp(a, b, i); |
63 } | 138 } |
64 | 139 |
140 /** | |
141 * NULL pointer save version of strncasecmp() | |
142 * | |
143 * If one of the parameters contains a NULL pointer, the string is considered to be unequal | |
144 * | |
145 * @param a the first string | |
146 * @param b the second string | |
147 * @param i how many characters to compare at most | |
148 * @return 0 if the strings are equal (within the given length limitation), non zero else | |
149 */ | |
65 int j_strncasecmp(const char *a, const char *b, int i) | 150 int j_strncasecmp(const char *a, const char *b, int i) |
66 { | 151 { |
67 if(a == NULL || b == NULL) | 152 if(a == NULL || b == NULL) |
68 return -1; | 153 return -1; |
69 else | 154 else |
70 return strncasecmp(a, b, i); | 155 return strncasecmp(a, b, i); |
71 } | 156 } |
72 | 157 |
158 /** | |
159 * NULL pointer save version of strlen | |
160 * | |
161 * If the parameter contains a NULL pointer, 0 is returned | |
162 * | |
163 * @param a the string for which the length should be calculated | |
164 * @return 0 if a==NULL, length of the string else | |
165 */ | |
73 int j_strlen(const char *a) | 166 int j_strlen(const char *a) |
74 { | 167 { |
75 if(a == NULL) | 168 if(a == NULL) |
76 return 0; | 169 return 0; |
77 else | 170 else |
134 | 227 |
135 /* loop till we hit our end flag, the first arg */ | 228 /* loop till we hit our end flag, the first arg */ |
136 while(1) | 229 while(1) |
137 { | 230 { |
138 arg = va_arg(ap,char *); | 231 arg = va_arg(ap,char *); |
139 if((int)arg == (int)s) | 232 if((spool)arg == s) |
140 break; | 233 break; |
141 else | 234 else |
142 spool_add(s, arg); | 235 spool_add(s, arg); |
143 } | 236 } |
144 | 237 |
183 | 276 |
184 /* loop till we hit our end flag, the first arg */ | 277 /* loop till we hit our end flag, the first arg */ |
185 while(1) | 278 while(1) |
186 { | 279 { |
187 arg = va_arg(ap,char *); | 280 arg = va_arg(ap,char *); |
188 if((int)arg == (int)p) | 281 if((pool)arg == p) |
189 break; | 282 break; |
190 else | 283 else |
191 spool_add(s, arg); | 284 spool_add(s, arg); |
192 } | 285 } |
193 | 286 |
317 i = snprintf(buff,63,"%s:%d",file,line); | 410 i = snprintf(buff,63,"%s:%d",file,line); |
318 buff[i] = '\0'; | 411 buff[i] = '\0'; |
319 | 412 |
320 return buff; | 413 return buff; |
321 } | 414 } |
322 | |
323 void str_b64decode(char* str) | |
324 { | |
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 */ | |
345 }; | |
346 | |
347 dlast = 0; | |
348 phase = 0; | |
349 for (cur = str; *cur != '\0'; ++cur ) | |
350 { | |
351 d = table[(int)*cur]; | |
352 if(d != -1) | |
353 { | |
354 switch(phase) | |
355 { | |
356 case 0: | |
357 ++phase; | |
358 break; | |
359 case 1: | |
360 c = ((dlast << 2) | ((d & 0x30) >> 4)); | |
361 *str++ = c; | |
362 ++phase; | |
363 break; | |
364 case 2: | |
365 c = (((dlast & 0xf) << 4) | ((d & 0x3c) >> 2)); | |
366 *str++ = c; | |
367 ++phase; | |
368 break; | |
369 case 3: | |
370 c = (((dlast & 0x03 ) << 6) | d); | |
371 *str++ = c; | |
372 phase = 0; | |
373 break; | |
374 } | |
375 dlast = d; | |
376 } | |
377 } | |
378 *str = '\0'; | |
379 } |