
Cphil51
|
Bon. Voila le travail... Normalement c'est TRES rapide maintenant :
#include <stdio.h> #include <stdlib.h> #include <string.h>
char * url_encode(const unsigned char * str) { unsigned char * s = str; unsigned char * t = NULL; unsigned char * ret; unsigned char isValidChar[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //16 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //32 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, //48 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, //64 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //80 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, //96 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //112 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, //128 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // >128 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
int lenght = 0;
// calcul de la taille de la chaine urlEncodée do{ if(!isValidChar[*s]) lenght+=3; // %xx : 3 caractères else lenght++; // sinon un seul }while(*++s); // avance d'un cran dans la chaine. Si on est pas à la fin, on continue...
s = str; t = (char *)malloc(sizeof(unsigned char) * (lenght + 1)); // Allocation à la bonne taille if(!t) exit(EXIT_FAILURE); ret = t;
//encodage do{ if(!isValidChar[*s]) sprintf(t, "%%%2X", *s), t+=3; else sprintf(t, "%c", *s), t++; }while(*++s);
*t = 0; // 0 final return ret; }
int main() { unsigned char * urlEncoded = url_encode("C:\\Program Files\\Documents web\\index.html"); printf("%s", urlEncoded); free(urlEncoded); }
@+
|