Mercurial > pidgin.yaz
comparison libpurple/util.c @ 29751:55515a760e87
propagate from branch 'im.pidgin.pidgin' (head 0085c32abf29d034d30feef1ffb1d483e316a9a8)
to branch 'im.pidgin.pidgin.next.minor' (head 2bbe60cf7bc2495f5e36d078796c57690c1e402b)
author | Paul Aurich <paul@darkrain42.org> |
---|---|
date | Wed, 10 Feb 2010 03:43:55 +0000 |
parents | 422889fb57e0 |
children | 05d727f76ca9 |
comparison
equal
deleted
inserted
replaced
29398:e1c01f236674 | 29751:55515a760e87 |
---|---|
218 "0123456789abcdef"; | 218 "0123456789abcdef"; |
219 | 219 |
220 gchar * | 220 gchar * |
221 purple_base64_encode(const guchar *data, gsize len) | 221 purple_base64_encode(const guchar *data, gsize len) |
222 { | 222 { |
223 #if GLIB_CHECK_VERSION(2,12,0) | |
224 return g_base64_encode(data, len); | 223 return g_base64_encode(data, len); |
225 #else | |
226 char *out, *rv; | |
227 | |
228 g_return_val_if_fail(data != NULL, NULL); | |
229 g_return_val_if_fail(len > 0, NULL); | |
230 | |
231 rv = out = g_malloc(((len/3)+1)*4 + 1); | |
232 | |
233 for (; len >= 3; len -= 3) | |
234 { | |
235 *out++ = alphabet[data[0] >> 2]; | |
236 *out++ = alphabet[((data[0] << 4) & 0x30) | (data[1] >> 4)]; | |
237 *out++ = alphabet[((data[1] << 2) & 0x3c) | (data[2] >> 6)]; | |
238 *out++ = alphabet[data[2] & 0x3f]; | |
239 data += 3; | |
240 } | |
241 | |
242 if (len > 0) | |
243 { | |
244 unsigned char fragment; | |
245 | |
246 *out++ = alphabet[data[0] >> 2]; | |
247 fragment = (data[0] << 4) & 0x30; | |
248 | |
249 if (len > 1) | |
250 fragment |= data[1] >> 4; | |
251 | |
252 *out++ = alphabet[fragment]; | |
253 *out++ = (len < 2) ? '=' : alphabet[(data[1] << 2) & 0x3c]; | |
254 *out++ = '='; | |
255 } | |
256 | |
257 *out = '\0'; | |
258 | |
259 return rv; | |
260 #endif /* GLIB < 2.12.0 */ | |
261 } | 224 } |
262 | 225 |
263 guchar * | 226 guchar * |
264 purple_base64_decode(const char *str, gsize *ret_len) | 227 purple_base64_decode(const char *str, gsize *ret_len) |
265 { | 228 { |
266 #if GLIB_CHECK_VERSION(2,12,0) | |
267 /* | 229 /* |
268 * We want to allow ret_len to be NULL for backward compatibility, | 230 * We want to allow ret_len to be NULL for backward compatibility, |
269 * but g_base64_decode() requires a valid length variable. So if | 231 * but g_base64_decode() requires a valid length variable. So if |
270 * ret_len is NULL then pass in a dummy variable. | 232 * ret_len is NULL then pass in a dummy variable. |
271 */ | 233 */ |
272 gsize unused; | 234 gsize unused; |
273 return g_base64_decode(str, ret_len != NULL ? ret_len : &unused); | 235 return g_base64_decode(str, ret_len != NULL ? ret_len : &unused); |
274 #else | |
275 guchar *out = NULL; | |
276 char tmp = 0; | |
277 const char *c; | |
278 gint32 tmp2 = 0; | |
279 int len = 0, n = 0; | |
280 | |
281 g_return_val_if_fail(str != NULL, NULL); | |
282 | |
283 c = str; | |
284 | |
285 while (*c) { | |
286 if (*c >= 'A' && *c <= 'Z') { | |
287 tmp = *c - 'A'; | |
288 } else if (*c >= 'a' && *c <= 'z') { | |
289 tmp = 26 + (*c - 'a'); | |
290 } else if (*c >= '0' && *c <= 57) { | |
291 tmp = 52 + (*c - '0'); | |
292 } else if (*c == '+') { | |
293 tmp = 62; | |
294 } else if (*c == '/') { | |
295 tmp = 63; | |
296 } else if (*c == '\r' || *c == '\n') { | |
297 c++; | |
298 continue; | |
299 } else if (*c == '=') { | |
300 if (n == 3) { | |
301 out = g_realloc(out, len + 2); | |
302 out[len] = (guchar)(tmp2 >> 10) & 0xff; | |
303 len++; | |
304 out[len] = (guchar)(tmp2 >> 2) & 0xff; | |
305 len++; | |
306 } else if (n == 2) { | |
307 out = g_realloc(out, len + 1); | |
308 out[len] = (guchar)(tmp2 >> 4) & 0xff; | |
309 len++; | |
310 } | |
311 break; | |
312 } | |
313 tmp2 = ((tmp2 << 6) | (tmp & 0xff)); | |
314 n++; | |
315 if (n == 4) { | |
316 out = g_realloc(out, len + 3); | |
317 out[len] = (guchar)((tmp2 >> 16) & 0xff); | |
318 len++; | |
319 out[len] = (guchar)((tmp2 >> 8) & 0xff); | |
320 len++; | |
321 out[len] = (guchar)(tmp2 & 0xff); | |
322 len++; | |
323 tmp2 = 0; | |
324 n = 0; | |
325 } | |
326 c++; | |
327 } | |
328 | |
329 out = g_realloc(out, len + 1); | |
330 out[len] = 0; | |
331 | |
332 if (ret_len != NULL) | |
333 *ret_len = len; | |
334 | |
335 return out; | |
336 #endif /* GLIB < 2.12.0 */ | |
337 } | 236 } |
338 | 237 |
339 /************************************************************************** | 238 /************************************************************************** |
340 * Quoted Printable Functions (see RFC 2045). | 239 * Quoted Printable Functions (see RFC 2045). |
341 **************************************************************************/ | 240 **************************************************************************/ |
2654 custom_user_dir = NULL; | 2553 custom_user_dir = NULL; |
2655 } | 2554 } |
2656 | 2555 |
2657 int purple_build_dir (const char *path, int mode) | 2556 int purple_build_dir (const char *path, int mode) |
2658 { | 2557 { |
2659 #if GLIB_CHECK_VERSION(2,8,0) | |
2660 return g_mkdir_with_parents(path, mode); | 2558 return g_mkdir_with_parents(path, mode); |
2661 #else | |
2662 char *dir, **components, delim[] = { G_DIR_SEPARATOR, '\0' }; | |
2663 int cur, len; | |
2664 | |
2665 g_return_val_if_fail(path != NULL, -1); | |
2666 | |
2667 dir = g_new0(char, strlen(path) + 1); | |
2668 components = g_strsplit(path, delim, -1); | |
2669 len = 0; | |
2670 for (cur = 0; components[cur] != NULL; cur++) { | |
2671 /* If you don't know what you're doing on both | |
2672 * win32 and *NIX, stay the hell away from this code */ | |
2673 if(cur > 1) | |
2674 dir[len++] = G_DIR_SEPARATOR; | |
2675 strcpy(dir + len, components[cur]); | |
2676 len += strlen(components[cur]); | |
2677 if(cur == 0) | |
2678 dir[len++] = G_DIR_SEPARATOR; | |
2679 | |
2680 if(g_file_test(dir, G_FILE_TEST_IS_DIR)) { | |
2681 continue; | |
2682 #ifdef _WIN32 | |
2683 /* allow us to create subdirs on UNC paths | |
2684 * (\\machinename\path\to\blah) | |
2685 * g_file_test() doesn't work on "\\machinename" */ | |
2686 } else if (cur == 2 && dir[0] == '\\' && dir[1] == '\\' | |
2687 && components[cur + 1] != NULL) { | |
2688 continue; | |
2689 #endif | |
2690 } else if(g_file_test(dir, G_FILE_TEST_EXISTS)) { | |
2691 purple_debug_warning("build_dir", "bad path: %s\n", path); | |
2692 g_strfreev(components); | |
2693 g_free(dir); | |
2694 return -1; | |
2695 } | |
2696 | |
2697 if (g_mkdir(dir, mode) < 0) { | |
2698 purple_debug_warning("build_dir", "mkdir: %s\n", g_strerror(errno)); | |
2699 g_strfreev(components); | |
2700 g_free(dir); | |
2701 return -1; | |
2702 } | |
2703 } | |
2704 | |
2705 g_strfreev(components); | |
2706 g_free(dir); | |
2707 return 0; | |
2708 #endif | |
2709 } | 2559 } |
2710 | 2560 |
2711 /* | 2561 /* |
2712 * This function is long and beautiful, like my--um, yeah. Anyway, | 2562 * This function is long and beautiful, like my--um, yeah. Anyway, |
2713 * it includes lots of error checking so as we don't overwrite | 2563 * it includes lots of error checking so as we don't overwrite |
3216 } | 3066 } |
3217 | 3067 |
3218 gboolean | 3068 gboolean |
3219 purple_str_has_prefix(const char *s, const char *p) | 3069 purple_str_has_prefix(const char *s, const char *p) |
3220 { | 3070 { |
3221 #if GLIB_CHECK_VERSION(2,2,0) | |
3222 return g_str_has_prefix(s, p); | 3071 return g_str_has_prefix(s, p); |
3223 #else | |
3224 g_return_val_if_fail(s != NULL, FALSE); | |
3225 g_return_val_if_fail(p != NULL, FALSE); | |
3226 | |
3227 return (!strncmp(s, p, strlen(p))); | |
3228 #endif | |
3229 } | 3072 } |
3230 | 3073 |
3231 gboolean | 3074 gboolean |
3232 purple_str_has_suffix(const char *s, const char *x) | 3075 purple_str_has_suffix(const char *s, const char *x) |
3233 { | 3076 { |
3234 #if GLIB_CHECK_VERSION(2,2,0) | |
3235 return g_str_has_suffix(s, x); | 3077 return g_str_has_suffix(s, x); |
3236 #else | |
3237 int off; | |
3238 | |
3239 g_return_val_if_fail(s != NULL, FALSE); | |
3240 g_return_val_if_fail(x != NULL, FALSE); | |
3241 | |
3242 off = strlen(s) - strlen(x); | |
3243 return (off >= 0 && purple_strequal(s + off, x)); | |
3244 #endif | |
3245 } | 3078 } |
3246 | 3079 |
3247 char * | 3080 char * |
3248 purple_str_add_cr(const char *text) | 3081 purple_str_add_cr(const char *text) |
3249 { | 3082 { |
5136 } | 4969 } |
5137 | 4970 |
5138 const gchar * | 4971 const gchar * |
5139 purple_get_host_name(void) | 4972 purple_get_host_name(void) |
5140 { | 4973 { |
5141 #if GLIB_CHECK_VERSION(2,8,0) | |
5142 return g_get_host_name(); | 4974 return g_get_host_name(); |
5143 #else | 4975 } |
5144 static char hostname[256]; | 4976 |
5145 int ret = gethostname(hostname, sizeof(hostname)); | 4977 gchar * |
5146 hostname[sizeof(hostname) - 1] = '\0'; | 4978 purple_uuid_random(void) |
5147 | 4979 { |
5148 if (ret == -1 || hostname[0] == '\0') { | 4980 guint32 tmp, a, b; |
5149 purple_debug_info("purple_get_host_name: ", "could not find host name"); | 4981 |
5150 return "localhost"; | 4982 tmp = g_random_int(); |
5151 } else { | 4983 a = 0x4000 | (tmp & 0xFFF); /* 0x4000 to 0x4FFF */ |
5152 return hostname; | 4984 tmp >>= 12; |
5153 } | 4985 b = ((1 << 3) << 12) | (tmp & 0x3FFF); /* 0x8000 to 0xBFFF */ |
5154 #endif | 4986 |
5155 } | 4987 tmp = g_random_int(); |
4988 | |
4989 return g_strdup_printf("%08x-%04x-%04x-%04x-%04x%08x", | |
4990 g_random_int(), | |
4991 tmp & 0xFFFF, | |
4992 a, | |
4993 b, | |
4994 (tmp >> 16) & 0xFFFF, g_random_int()); | |
4995 } |