#include #include #include #include #include /* *-._ はそのまま、スペースは +、あとはエンコード */ static char enctbl[256] = { 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', '1', '2', '3', '4', '5', '6', '7', '8', '9', 0, 0, 0, 0, 0, 0, 0, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 0, 0, 0, 0, '_', 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; static char codetbl[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; URLencode(char *in, char **out) { int len; /* data length */ char *str = in; /* in data pointer */ unsigned char hi, lo; unsigned char cstr; char *outp; int cnt = 0; /* in param nodata */ if (!in || (len = strlen(in)) == 0) return -1; /* output area */ outp = (char *)malloc(sizeof(char) * ((len * 3) + 1)); memset( outp, 0, (len * 3) + 1); /**/ while( *str ) { cstr = *str; if ( enctbl[cstr] > 0) { outp[cnt++] = enctbl[cstr]; } else { hi = cstr >> 4; lo = cstr << 4; lo = lo >> 4; outp[cnt++] = '%'; outp[cnt++] = codetbl[hi]; outp[cnt++] = codetbl[lo]; } str++; } /* data output area malloc */ if ((*out = (char *)malloc(sizeof(char) * cnt + 1)) == NULL) { free(outp); return(-1); } /**/ strcpy(*out, outp); free(outp); return (cnt + 1); } main() { char *ecbuf; unsigned char a = 0xa3; char b; char c; URLencode("abZ ()'~!あ*-._\nあああ", &ecbuf ); printf("%s\n", ecbuf); }