gaim/gaim

e99fd2dc0168
Parents 6a67538a18a0
Children b7d00e60f82c
Fixed URL encoding (and filename encoding) to work with UTF-8 strings
  • +39 -19
    src/util.c
  • --- a/src/util.c Thu Mar 17 22:01:41 2005 -0500
    +++ b/src/util.c Fri Mar 18 14:52:34 2005 -0500
    @@ -2904,19 +2904,29 @@
    const char *
    gaim_url_encode(const char *str)
    {
    + const char *iter;
    static char buf[BUF_LEN];
    + char utf_char[6];
    guint i, j = 0;
    g_return_val_if_fail(str != NULL, NULL);
    -
    - for (i = 0; i < strlen(str) && j < (BUF_LEN - 1); i++) {
    - if (isalnum(str[i]))
    - buf[j++] = str[i];
    - else {
    - if (j > (BUF_LEN - 4))
    - break;
    - sprintf(buf + j, "%%%02x", (unsigned char)str[i]);
    - j += 3;
    + g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
    +
    + iter = str;
    + for (; *iter && j < (BUF_LEN - 1) ; iter = g_utf8_next_char(iter)) {
    + gunichar c = g_utf8_get_char(iter);
    + /* If the character is an ASCII character and is alphanumeric,
    + * or one of the specified values, no need to escape */
    + if (c < 256 && isalnum(c)) {
    + buf[j++] = c;
    + } else {
    + int bytes = g_unichar_to_utf8(c, utf_char);
    + for (i = 0; i < bytes; i++) {
    + if (j > (BUF_LEN - 4))
    + break;
    + sprintf(buf + j, "%%%02x", utf_char[i] & 0xff);
    + j += 3;
    + }
    }
    }
    @@ -3269,20 +3279,30 @@
    const char *
    gaim_escape_filename(const char *str)
    {
    + const char *iter;
    static char buf[BUF_LEN];
    + char utf_char[6];
    guint i, j = 0;
    g_return_val_if_fail(str != NULL, NULL);
    -
    - for (i = 0; i < strlen(str) && j < (BUF_LEN - 1); i++) {
    - if (isalnum(str[i]) || str[i] == '@' || str[i] == '-' ||
    - str[i] == '_' || str[i] == '.' || str[i] == '#')
    - buf[j++] = str[i];
    - else {
    - if (j > (BUF_LEN - 4))
    - break;
    - sprintf(buf + j, "%%%02x", (unsigned char)str[i]);
    - j += 3;
    + g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
    +
    + iter = str;
    + for (; *iter && j < (BUF_LEN - 1) ; iter = g_utf8_next_char(iter)) {
    + gunichar c = g_utf8_get_char(iter);
    + /* If the character is an ASCII character and is alphanumeric,
    + * or one of the specified values, no need to escape */
    + if (c < 256 && (isalnum(c) || c == '@' || c == '-' ||
    + c == '_' || c == '.' || c == '#')) {
    + buf[j++] = c;
    + } else {
    + int bytes = g_unichar_to_utf8(c, utf_char);
    + for (i = 0; i < bytes; i++) {
    + if (j > (BUF_LEN - 4))
    + break;
    + sprintf(buf + j, "%%%02x", utf_char[i] & 0xff);
    + j += 3;
    + }
    }
    }