Skip to content

Instantly share code, notes, and snippets.

@TzuYangLin
Created May 20, 2015 15:25
Show Gist options
  • Save TzuYangLin/9e3c97867c18cad93c44 to your computer and use it in GitHub Desktop.
Save TzuYangLin/9e3c97867c18cad93c44 to your computer and use it in GitHub Desktop.
#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