Skip to content

Instantly share code, notes, and snippets.

@co3k
Created August 9, 2013 12:14
Show Gist options
  • Save co3k/6193148 to your computer and use it in GitHub Desktop.
Save co3k/6193148 to your computer and use it in GitHub Desktop.
diff -u urlencode rawurlencode
--- urlencode 2013-08-09 20:57:57.000000000 +0900
+++ rawurlencode 2013-08-09 21:11:45.000000000 +0900
@@ -1,43 +1,30 @@
-PHPAPI char *php_url_encode(char const *s, int len, int *new_length)
+PHPAPI char *php_raw_url_encode(char const *s, int len, int *new_length)
{
- register unsigned char c;
- unsigned char *to, *start;
- unsigned char const *from, *end;
-
- from = (unsigned char *)s;
- end = (unsigned char *)s + len;
- start = to = (unsigned char *) safe_emalloc(3, len, 1);
+ register int x, y;
+ unsigned char *str;
- while (from < end) {
- c = *from++;
-
- if (c == ' ') {
- *to++ = '+';
+ str = (unsigned char *) safe_emalloc(3, len, 1);
+ for (x = 0, y = 0; len--; x++, y++) {
+ str[y] = (unsigned char) s[x];
#ifndef CHARSET_EBCDIC
- } else if ((c < '0' && c != '-' && c != '.') ||
- (c < 'A' && c > '9') ||
- (c > 'Z' && c < 'a' && c != '_') ||
- (c > 'z')) {
- to[0] = '%';
- to[1] = hexchars[c >> 4];
- to[2] = hexchars[c & 15];
- to += 3;
+ if ((str[y] < '0' && str[y] != '-' && str[y] != '.') ||
+ (str[y] < 'A' && str[y] > '9') ||
+ (str[y] > 'Z' && str[y] < 'a' && str[y] != '_') ||
+ (str[y] > 'z' && str[y] != '~')) {
+ str[y++] = '%';
+ str[y++] = hexchars[(unsigned char) s[x] >> 4];
+ str[y] = hexchars[(unsigned char) s[x] & 15];
#else /*CHARSET_EBCDIC*/
- } else if (!isalnum(c) && strchr("_-.", c) == NULL) {
- /* Allow only alphanumeric chars and '_', '-', '.'; escape the rest */
- to[0] = '%';
- to[1] = hexchars[os_toascii[c] >> 4];
- to[2] = hexchars[os_toascii[c] & 15];
- to += 3;
+ if (!isalnum(str[y]) && strchr("_-.~", str[y]) != NULL) {
+ str[y++] = '%';
+ str[y++] = hexchars[os_toascii[(unsigned char) s[x]] >> 4];
+ str[y] = hexchars[os_toascii[(unsigned char) s[x]] & 15];
#endif /*CHARSET_EBCDIC*/
- } else {
- *to++ = c;
}
}
- *to = 0;
+ str[y] = '\0';
if (new_length) {
- *new_length = to - start;
+ *new_length = y;
}
- return (char *) start;
+ return ((char *) str);
}
-/* }}} */
@co3k
Copy link
Author

co3k commented Aug 9, 2013

at 2c9b4e87a0bf3890a3f12d9929123f4e49d0b1f5 (5.5.1)

@co3k
Copy link
Author

co3k commented Aug 9, 2013

The first commit is php/php-src@257de2b but it is just copying many files from PHP3's anywhere (?) to PHP 4 so we can't understand why these functions are separated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment