Created
May 20, 2015 15:25
-
-
Save TzuYangLin/9e3c97867c18cad93c44 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
int main(int argc, const char * argv[]) | |
{ | |
int left, offset=0; | |
char *target = "daniel&vivid<daniel>,\"vivid\'"; | |
char c, *dest; | |
const char *inp; | |
inp = target; | |
left = 32*5; | |
dest = (char *)calloc(left+1, 1); | |
while ((c = (char)*inp++) && (left >= 6)) | |
{ | |
printf("%c,", c); | |
switch (c) | |
{ | |
case '>': | |
dest[offset++] = '&'; | |
dest[offset++] = 'g'; | |
dest[offset++] = 't'; | |
dest[offset++] = ';'; | |
left -= 4; | |
break; | |
case '<': | |
dest[offset++] = '&'; | |
dest[offset++] = 'l'; | |
dest[offset++] = 't'; | |
dest[offset++] = ';'; | |
left -= 4; | |
break; | |
case '&': | |
dest[offset++] = '&'; | |
dest[offset++] = 'a'; | |
dest[offset++] = 'm'; | |
dest[offset++] = 'p'; | |
dest[offset++] = ';'; | |
left -= 5; | |
break; | |
case '\"': | |
dest[offset++] = '&'; | |
dest[offset++] = 'q'; | |
dest[offset++] = 'u'; | |
dest[offset++] = 'o'; | |
dest[offset++] = 't'; | |
dest[offset++] = ';'; | |
left -= 6; | |
break; | |
case '\'': | |
dest[offset++] = '&'; | |
dest[offset++] = 'a'; | |
dest[offset++] = 'p'; | |
dest[offset++] = 'o'; | |
dest[offset++] = 's'; | |
dest[offset++] = ';'; | |
left -= 6; | |
break; | |
default: | |
dest[offset++] = c; | |
break; | |
} | |
} | |
printf("\n"); | |
printf("dest=%s, left=%d\n", dest, left); | |
if (dest) free(dest); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment