URL Unescaping in C
6
Another pull from my growing-towards-beta CGI library: sgcgi_url_unescape().
Note the use strcpy, which is faster than the equivalent memmove()ing. To ensure 64-bit safety, I plan to rename this function and then conditionally compile it to point to either strcpy or a 64-bit-safe memmove() implementation of strcpy.
However, even though copy order isn't guaranteed for strcpy, on 16-bit and 32-bit systems, all known implementations copy byte-by-byte from lower addresses to higher addresses. Some 64-bit optimized compilers may copy 8-byte chunks, making the assumption of full linearity unstable at best.
I know it sounds like I'm justifying use of nonstandard code for convenience . . . *blush* . . . it's just something that putting in a -DPEDANTIC type of preprocessor flag could fix if broken, and its SO much faster!
Note the use strcpy, which is faster than the equivalent memmove()ing. To ensure 64-bit safety, I plan to rename this function and then conditionally compile it to point to either strcpy or a 64-bit-safe memmove() implementation of strcpy.
However, even though copy order isn't guaranteed for strcpy, on 16-bit and 32-bit systems, all known implementations copy byte-by-byte from lower addresses to higher addresses. Some 64-bit optimized compilers may copy 8-byte chunks, making the assumption of full linearity unstable at best.
I know it sounds like I'm justifying use of nonstandard code for convenience . . . *blush* . . . it's just something that putting in a -DPEDANTIC type of preprocessor flag could fix if broken, and its SO much faster!
void sgcgi_url_unescape(char *string) {
int len = strlen(string);
char *ptr = string;
char ord[3]; // buffer for ordinal character value string
while(*ptr) {
if(*ptr == '%') {
strncpy(ord,ptr+1,2);
long ord_l = strtol(ord,(char **)NULL,16);
if( ord_l && ord_l > 0 && ord_l < 256 ) {
*ptr = (char)ord_l;
strcpy(ptr+1,ptr+3);
}
}
else if(*ptr == '+') {
*ptr = ' ';
}
ptr++;
}
return;
}
int len = strlen(string);
char *ptr = string;
char ord[3]; // buffer for ordinal character value string
while(*ptr) {
if(*ptr == '%') {
strncpy(ord,ptr+1,2);
long ord_l = strtol(ord,(char **)NULL,16);
if( ord_l && ord_l > 0 && ord_l < 256 ) {
*ptr = (char)ord_l;
strcpy(ptr+1,ptr+3);
}
}
else if(*ptr == '+') {
*ptr = ' ';
}
ptr++;
}
return;
}






There are currently no comments for this snippet.