| 3095 } |
3095 } |
| 3096 |
3096 |
| 3097 const char * |
3097 const char * |
| 3098 gaim_url_encode(const char *str) |
3098 gaim_url_encode(const char *str) |
| 3099 { |
3099 { |
| |
3100 const char *iter; |
| 3100 static char buf[BUF_LEN]; |
3101 static char buf[BUF_LEN]; |
| |
3102 char utf_char[6]; |
| 3101 guint i, j = 0; |
3103 guint i, j = 0; |
| 3102 |
3104 |
| 3103 g_return_val_if_fail(str != NULL, NULL); |
3105 g_return_val_if_fail(str != NULL, NULL); |
| 3104 |
3106 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL); |
| 3105 for (i = 0; i < strlen(str) && j < (BUF_LEN - 1); i++) { |
3107 |
| 3106 if (isalnum(str[i])) |
3108 iter = str; |
| 3107 buf[j++] = str[i]; |
3109 for (; *iter && j < (BUF_LEN - 1) ; iter = g_utf8_next_char(iter)) { |
| 3108 else { |
3110 gunichar c = g_utf8_get_char(iter); |
| 3109 if (j > (BUF_LEN - 4)) |
3111 /* If the character is an ASCII character and is alphanumeric, |
| 3110 break; |
3112 * or one of the specified values, no need to escape */ |
| 3111 sprintf(buf + j, "%%%02x", (unsigned char)str[i]); |
3113 if (c < 256 && isalnum(c)) { |
| 3112 j += 3; |
3114 buf[j++] = c; |
| |
3115 } else { |
| |
3116 int bytes = g_unichar_to_utf8(c, utf_char); |
| |
3117 for (i = 0; i < bytes; i++) { |
| |
3118 if (j > (BUF_LEN - 4)) |
| |
3119 break; |
| |
3120 sprintf(buf + j, "%%%02x", utf_char[i] & 0xff); |
| |
3121 j += 3; |
| |
3122 } |
| 3113 } |
3123 } |
| 3114 } |
3124 } |
| 3115 |
3125 |
| 3116 buf[j] = '\0'; |
3126 buf[j] = '\0'; |
| 3117 |
3127 |
| 3460 * being used above), but we want to keep certain characters unescaped |
3470 * being used above), but we want to keep certain characters unescaped |
| 3461 * for compat reasons */ |
3471 * for compat reasons */ |
| 3462 const char * |
3472 const char * |
| 3463 gaim_escape_filename(const char *str) |
3473 gaim_escape_filename(const char *str) |
| 3464 { |
3474 { |
| |
3475 const char *iter; |
| 3465 static char buf[BUF_LEN]; |
3476 static char buf[BUF_LEN]; |
| |
3477 char utf_char[6]; |
| 3466 guint i, j = 0; |
3478 guint i, j = 0; |
| 3467 |
3479 |
| 3468 g_return_val_if_fail(str != NULL, NULL); |
3480 g_return_val_if_fail(str != NULL, NULL); |
| 3469 |
3481 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL); |
| 3470 for (i = 0; i < strlen(str) && j < (BUF_LEN - 1); i++) { |
3482 |
| 3471 if (isalnum(str[i]) || str[i] == '@' || str[i] == '-' || |
3483 iter = str; |
| 3472 str[i] == '_' || str[i] == '.' || str[i] == '#') |
3484 for (; *iter && j < (BUF_LEN - 1) ; iter = g_utf8_next_char(iter)) { |
| 3473 buf[j++] = str[i]; |
3485 gunichar c = g_utf8_get_char(iter); |
| 3474 else { |
3486 /* If the character is an ASCII character and is alphanumeric, |
| 3475 if (j > (BUF_LEN - 4)) |
3487 * or one of the specified values, no need to escape */ |
| 3476 break; |
3488 if (c < 256 && (isalnum(c) || c == '@' || c == '-' || |
| 3477 sprintf(buf + j, "%%%02x", (unsigned char)str[i]); |
3489 c == '_' || c == '.' || c == '#')) { |
| 3478 j += 3; |
3490 buf[j++] = c; |
| |
3491 } else { |
| |
3492 int bytes = g_unichar_to_utf8(c, utf_char); |
| |
3493 for (i = 0; i < bytes; i++) { |
| |
3494 if (j > (BUF_LEN - 4)) |
| |
3495 break; |
| |
3496 sprintf(buf + j, "%%%02x", utf_char[i] & 0xff); |
| |
3497 j += 3; |
| |
3498 } |
| 3479 } |
3499 } |
| 3480 } |
3500 } |
| 3481 |
3501 |
| 3482 buf[j] = '\0'; |
3502 buf[j] = '\0'; |
| 3483 |
3503 |